miyagawa (owner)

Revisions

gist: 208907 Download_button fork
public
Public Clone URL: git://gist.github.com/208907.git
Embed All Files: show embed
eg/dot-psgi/echo-coro.psgi #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use AnyEvent::Twitter::Stream;
use Encode;
use Coro;
use Coro::Channel;
use Coro::AnyEvent;
use Coro::Timer;
use IO::Handle::Util qw(io_from_getline);
 
my $app = sub {
    my $env = shift;
 
    my $keyword = $env->{PATH_INFO};
    $keyword =~ s!^/!!;
 
    my $queue = Coro::Channel->new;
 
    # track keywords
    my $guard = AnyEvent::Twitter::Stream->new(
        username => $ENV{TWITTER_USERNAME},
        password => $ENV{TWITTER_PASSWORD},
        method => "filter",
        track => $keyword || "twitter",
        on_tweet => sub { $queue->put(@_) },
    );
 
    my $count;
    my $body = io_from_getline sub {
        my $tweet = $queue->get;
        return Encode::encode_utf8($tweet->{text}) . "\n";
    };
 
    return [ 200, ['X-Foo' => 'bar'], $body ];
};