Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created September 3, 2009 16:45
Show Gist options
  • Save sharifulin/180393 to your computer and use it in GitHub Desktop.
Save sharifulin/180393 to your computer and use it in GitHub Desktop.
Twitter Stream API + HTTPD
use strict;
use utf8;
use JSON;
use Encode ();
use AnyEvent::HTTPD;
use AnyEvent::Twitter::Stream;
use constant L => 30;
my $done = AnyEvent->condvar;
my $httpd = AnyEvent::HTTPD->new(port => 9090);
my $T;
AnyEvent::Twitter::Stream->new(
username => '******',
password => '******',
method => 'filter',
follow => join(',', map { $_->{'twitter_id'} } @{ $DB->select('select * from user_account where type="twitter"') }),
on_tweet => sub {
my $tweet = shift;
warn "$tweet->{user}->{screen_name}: $tweet->{text}\n";
unshift @$T, $tweet;
},
on_error => sub {
my $error = shift;
warn "ERROR: $error";
$done->send;
},
);
$httpd->reg_cb (
'/' => sub {
my($httpd, $req) = @_;
$req->respond({ content => ['text/plain', encode_json([ grep { $_ } @$T ])] });
splice @$T, L+1 if @$T > L;
},
);
$httpd->run;
$done->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment