Skip to content

Instantly share code, notes, and snippets.

@nihen
Created September 26, 2009 13:32
Show Gist options
  • Save nihen/194227 to your computer and use it in GitHub Desktop.
Save nihen/194227 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Data::Dumper;
use Devel::Peek;
use feature qw( say );
use Encode;
use AnyEvent::Twitter::Stream;
use Continuity;
use Coro::Event;
my $user = 'nihen';
my $password = '';
my $done = AnyEvent->condvar;
my @tweets;
my $new_tweet_id;
my $streamer = AnyEvent::Twitter::Stream->new(
username => $user,
password => $password,
method => 'filter',
track => 'http',
on_tweet => sub {
my $tweet = shift;
if( $tweet->{text} =~ /[\p{InHiragana}\p{InKatakana}\p{InHalfwidthAndFullwidthForms}\p{InCjkUnifiedIdeographs}\p{InCjkUnifiedIdeographsExtensionA}\p{InCjkUnifiedIdeographsExtensionB}\p{InEnclosedCjkLettersAndMonths}]/ ) {
shift @tweets if( $#tweets > 20 );
push( @tweets, encode( 'utf8', "$tweet->{user}{screen_name}: $tweet->{text}\n" ) );
$new_tweet_id = $tweet->{id};
}
},
on_error => sub {
my $error = shift;
warn "ERROR: $error";
$done->send;
},
on_eof => sub {
$done->send;
},
);
my $server = Continuity->new(
port => 16001,
path_session => 1,
cookie_session => 'sid',
staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html)$/ },
);
$server->loop;
$done->recv;
sub main {
my ($req) = @_;
my $path = $req->request->url->path;
print STDERR "Path: '$path'\n";
pushstream($req) if $path =~ /pushstream/;
}
sub pushstream {
my ($req) = @_;
my $w = Coro::Event->var(var => \$new_tweet_id, poll => 'w');
while (1) {
my $log = join "<br />", @tweets;
$req->print($log);
$req->next;
$w->next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment