Local copypaste @ orl.sumy.ua
File: stream_twitter.perl (perl syntax), download it
  1. ## TWITTER STREAM DAEMON ##
  2. our $VERSION = '1.2';
  3. our $T_USER  = 'middlesizetit';
  4. our $T_PASS = '**********';
  5.  
  6. use warnings;
  7. use strict;
  8. use LWP::UserAgent;
  9. use DBI;
  10.  
  11. my $mysql = DBI->connect("DBI:mysql:tc:localhost","twitter");
  12.  
  13. while (1) {
  14.         my $req = HTTP::Request->new ('GET',"http://$T_USER:$T_PASS\@stream.twitter.com/1/statuses/sample.json");
  15.         LWP::UserAgent->new->request ($req,sub {my($chunk,$res )=@_;\&gettw($chunk,$res);});
  16.  
  17.         sub gettw {
  18.                 my $tweet = shift;
  19.                 my $clurl = '';
  20.                 my $clname = '';
  21.                 if ($tweet=~/"source":"(.*?)(",)/gi) {
  22.                         my $ahref = $1;
  23.                         if ($ahref=~/<a href=\\"(.*?)\\" rel=\\"nofollow\\">(.*?)<\/a>/gi) {
  24.                                 $clurl = $1;
  25.                                 $clname = $2;
  26.                         } else { $clurl = 'http://twitter.com'; $clname = 'web'; }
  27.                         my $query = $mysql->prepare("INSERT INTO source (name,url,count) VALUES (?,?,1) ON DUPLICATE KEY UPDATE count = count + 1");
  28.                         $query->bind_param(1,$clname);
  29.                         $query->bind_param(2,$clurl);
  30.                         $query->execute;
  31.                 }      
  32.         }
  33. }
Filesize: 966 bytes; Generation time: 0.0625 sec; Memory usage: 1.98MB; GeSHi version: 1.0.8.1;