Advertisement
rplantiko

My last 7 tweets - with API 1.1

Aug 30th, 2013
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.50 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # New Version of "Get my latest 7 tweets"
  4. # Switched to new Twitter API v. 1.1 (2013)
  5.  
  6. # This replaces my former version http://pastebin.com/8U3MuTcX
  7. # Since the XML output format has been erased by Twitter,
  8. # everything is done with JSON now.
  9. # The execution time of the script is extremely slow: 1 second
  10. # => Serverside buffering will be provided (not contained in this code).
  11.  
  12. use strict;
  13. use warnings;
  14. use Net::Twitter;
  15. use JSON;
  16.  
  17. use CGI::Carp qw(fatalsToBrowser);
  18. use Encode;
  19.  
  20. my $MAX_AGE_IN_SECS  = 300;
  21. my $NUMBER_OF_TWEETS = 7;
  22.  
  23. # --- Output result
  24. print "Content-Type: application/json; charset=utf-8\n";
  25. print "Cache-Control: max-age=$MAX_AGE_IN_SECS\n";
  26. print "\n";
  27.  
  28. my $nt = Net::Twitter->new(
  29.   traits              =>   [ qw/API::RESTv1_1/ ],
  30.   consumer_key        => '<CONSUMER_KEY>',
  31.   consumer_secret     => '<CONSUMER_SECRET>',
  32.   access_token        =>  '<ACCESS_TOKEN>',
  33.   access_token_secret => '<ACCESS_TOKEN_SECRET>'
  34.   );
  35.  
  36. my $t = $nt->user_timeline({
  37.   count => $NUMBER_OF_TWEETS,
  38.   include_entities => 0,
  39.   contributor_details => 0,
  40.   include_rts => 1,
  41.   truncated => 0,
  42.   });
  43.  
  44. my (@tweets, $rt, $text);
  45.  
  46. for my $status (@$t) {
  47.   $rt = $status->{retweeted_status};
  48.  
  49.   if ($rt) { $text = 'RT @'.$rt->{user}->{screen_name}.' '.$rt->{text};  }
  50.   else { $text = $status->{text};  }
  51.  
  52.   push( @tweets,
  53.         [ "".$status->{id},
  54.           $status->{created_at},
  55.           $text
  56.           ] );
  57.   }
  58.  
  59. print encode_json( \@tweets ) . "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement