Skip to content

Instantly share code, notes, and snippets.

@sekimura
Forked from miyagawa/tweet_displayer.pl
Created March 21, 2010 01:52
Show Gist options
  • Save sekimura/339019 to your computer and use it in GitHub Desktop.
Save sekimura/339019 to your computer and use it in GitHub Desktop.
fixed name of this script itself and added "feature" pragma
#!/usr/bin/perl
# TWITTER_USERNAME=xxx TWITTER_PASSWORD=xxx plackup -s Twiggy twitter_displayer.pl
use AnyEvent::Twitter::Stream;
use Markapl;
use autobox::Core;
use autobox::Encode;
use feature qw(state);
my $buf = [];
# NOTE: $stream should live outside this PSGI file scope
# Change state to our if you're using perl 5.8
state $stream = AnyEvent::Twitter::Stream->new(
username => $ENV{TWITTER_USERNAME},
password => $ENV{TWITTER_PASSWORD},
method => 'sample',
on_tweet => sub {
my $tweet = shift;
return unless $tweet->{user};
$buf->unshift($tweet);
$buf = $buf->slice(0..9) if $buf->size >= 10;
},
);
sub {
my $mp = html {
body {
ul { $buf->map(sub {
li {
b{ $_->{user}->{screen_name} };
': ' . $_->{text};
}
})}
}
};
return [
200,
[ 'Content-Type', 'text/html; charset=utf-8' ],
[ $mp->()->encode('utf8') ],
];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment