Advertisement
rplantiko

My last 7 tweets

Oct 17th, 2011
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.89 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Get the latest N tweets, using well-tried technologies, as are Perl and
  4. # James Clark's "expat" XML parser, back from the golden 90es
  5. # No CGI modules needed, hand-written output with "print"
  6. # Except CGI::Carp for exception handling
  7. # Idea:
  8. #   Use a hash to keep track of which elements are currently open
  9. #   Collect the text content of open elements
  10. # Output as HTML fragment: unordered list
  11. # Buffer on client and server with max-age 300 sec. = 5 min.
  12.  
  13. use strict;
  14. use warnings;
  15. use CGI::Carp qw(fatalsToBrowser);
  16. use Encode;
  17.  
  18. use LWP::Simple;
  19. use XML::Parser;
  20. use File::stat;
  21.  
  22. my $MAX_AGE_IN_SECS  = 300;
  23. my $NUMBER_OF_TWEETS = 7;
  24.  
  25. # --- Get data from Twitter, buffer it on server file
  26. my $xml;
  27. get_tweets_cached(
  28.   \$xml,
  29.   $MAX_AGE_IN_SECS,
  30.   "tweets_cache.xml",
  31.   { count         => $NUMBER_OF_TWEETS,
  32.     screen_name   => "rplantiko" }
  33.   );
  34.  
  35. # --- Output result
  36. print "Content-Type: text/html; charset=utf-8\n";
  37. print "Cache-Control: max-age=$MAX_AGE_IN_SECS\n";
  38. print "\n";
  39.  
  40. print "<ul>\n";
  41. if ($xml =~ /<text>/) {  # If we have a chance
  42.   my $parser = new XML::Parser(Handlers => { Init  => \&handle_init,
  43.                                              Start => \&handle_start,
  44.                                              End   => \&handle_end,
  45.                                              Char  => \&handle_text } );
  46.   $parser->parse($xml);
  47.   }
  48. print "</ul>\n";
  49.  
  50.  
  51. { # <<<--- Begin of XML Parser Block
  52.  
  53. my (%watched, $current);
  54.  
  55. sub handle_init {
  56.   %watched = (
  57.     retweeted_status => { active => 0, content => "" },
  58.     id               => { active => 0, content => "" },
  59.     text             => { active => 0, content => "" },
  60.     created_at       => { active => 0, content => "" },
  61.     );
  62.   $current = "";
  63.   }
  64.  
  65. sub handle_start {
  66.   my ($p, $el, %atts) = @_;
  67.   my $status;
  68.   if ($status = $watched{$el}) {
  69.     $status->{active} = 1;
  70.     $status->{content} = "";
  71.     }
  72.   $current = $el;
  73.   }
  74.  
  75. sub handle_end {
  76.   my ($p, $el) = @_;
  77.   my $status;
  78.   if ($status = $watched{$el} and $status->{active}) {
  79.   $status->{active} = 0;
  80.     if ($el eq "text" and not $watched{retweeted_status}->{active}) {
  81.       my $text = $status->{content};
  82.       $text =~ s/(http:\/\/\S+)/<a href="$1">$1<\/a>/g;
  83.       $text =~ s/@(\w+)/<a href="http:\/\/twitter.com\/#!\/$1">\@$1<\/a>/g;
  84.       $text =~ s/#(\w+)/<a href="http:\/\/twitter.com\/#!\/search?q=%23$1">\#$1<\/a>/g;
  85.       my $created_at = $watched{created_at}->{content};
  86.       $created_at =~ s/\s*\+\d{4}.*//;
  87.       my $id = $watched{id}->{content};
  88.       my $item =
  89.         qq(<li><a href="http://twitter.com/#!/rplantiko/status/$id">$created_at</a> $text</li>\n);
  90. # explicit encode is necessary here, since the text content comes from twitter in Latin-1:
  91.       print encode( 'utf-8', $item );  
  92.       }
  93.     }
  94.   }
  95.  
  96. sub handle_text {
  97.   my ($p, $s) = @_;
  98.   my $status;
  99.   if ($status = $watched{$current} and $status->{active}) {
  100.     $status->{content} .= $s;
  101.     }
  102.   }  
  103. } # --->>> End of XML Parser Block
  104.  
  105.  
  106. # Use buffer file if still valid
  107. sub get_tweets_cached {
  108.  
  109.   my ($xmlref,$max_age,$filename,$params) = @_;
  110.  
  111.   local $/=undef;
  112.   open my $file, "<$filename";  # for stats
  113.   if (not eof $file and (time()-stat($file)->[9] < $max_age)) {
  114.     $$xmlref = <$file>;
  115.     }
  116.   else {
  117.     open $file, ">$filename";
  118.     $$xmlref = get twitter_api_url( $params );
  119.     print $file $$xmlref;
  120.     }
  121.   close $file;
  122.  
  123.   }  
  124.  
  125. # Build Twitter API URL
  126. sub twitter_api_url {
  127.  
  128.   my %param = (
  129.     %{$_[0]},
  130. # add some more obscure parameters
  131.     include_entities    => "false",
  132.     contributor_details => "false",
  133.     include_rts         => "true" );
  134.  
  135.   my $url = "http://api.twitter.com/1/statuses/user_timeline.xml";
  136.  
  137.   my $query = "";
  138.   while (my ($key,$value) = each %param ) {
  139.     $query .= "&" if $query;
  140.     $query .= "$key=$value";
  141.     }
  142.  
  143.   return "$url?$query";
  144.  
  145.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement