| 1 | package App::Horris::CLI::Command::twitter_stream; | ||
| 2 | # ABSTRACT: anyevent twitter streaming script | ||
| 3 | |||
| 4 | |||
| 5 | use Moose; | ||
| 6 | use Config::General qw/ParseConfig/; | ||
| 7 | use DBI; | ||
| 8 | use AnyEvent::Twitter::Stream; | ||
| 9 | use namespace::autoclean; | ||
| 10 | extends 'MooseX::App::Cmd::Command'; | ||
| 11 | |||
| 12 | has database => ( | ||
| 13 | is => 'ro', | ||
| 14 | isa => 'Str', | ||
| 15 | traits => ['Getopt'], | ||
| 16 | required => 1, | ||
| 17 | documentation => "sqlite3 database file", | ||
| 18 | ); | ||
| 19 | |||
| 20 | has key_config => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Str', | ||
| 23 | traits => ['Getopt'], | ||
| 24 | default => "$ENV{HOME}/.twitter_key", | ||
| 25 | cmd_aliases => 'k', | ||
| 26 | documentation => | ||
| 27 | "twitter api key file. default using $ENV{HOME}/.twitter_key", | ||
| 28 | ); | ||
| 29 | |||
| 30 | has track => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Str', | ||
| 33 | traits => ['Getopt'], | ||
| 34 | default => 'perl,anyevent,catalyst,dancer,plack,psgi', | ||
| 35 | cmd_aliases => 't', | ||
| 36 | documentation => "tracking keywords", | ||
| 37 | ); | ||
| 38 | |||
| 39 | has key => ( | ||
| 40 | is => 'rw', | ||
| 41 | isa => 'HashRef', | ||
| 42 | traits => ['NoGetopt'], | ||
| 43 | lazy_build => 1 | ||
| 44 | ); | ||
| 45 | |||
| 46 | sub _build_key { | ||
| 47 | my $self = shift; | ||
| 48 | return { ParseConfig($self->key_config) }; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub execute { | ||
| 52 | my ( $self, $opt, $args ) = @_; | ||
| 53 | |||
| 54 | my $consumer_key = $self->key->{consumer_key}; | ||
| 55 | my $consumer_secret = $self->key->{consumer_secret}; | ||
| 56 | my $access_token = $self->key->{access_token}; | ||
| 57 | my $access_token_secret = $self->key->{access_token_secret}; | ||
| 58 | |||
| 59 | my $dbh = DBI->connect( "dbi:SQLite:dbname=" . $self->database, "", "" ); | ||
| 60 | my $sth_insert = $dbh->prepare("insert into messages values (?, ?, 0, ?)"); | ||
| 61 | |||
| 62 | my $done = AE::cv; | ||
| 63 | |||
| 64 | # to use OAuth authentication | ||
| 65 | my $listener = AnyEvent::Twitter::Stream->new( | ||
| 66 | consumer_key => $consumer_key, | ||
| 67 | consumer_secret => $consumer_secret, | ||
| 68 | token => $access_token, | ||
| 69 | token_secret => $access_token_secret, | ||
| 70 | method => "filter", | ||
| 71 | track => $self->track, | ||
| 72 | on_tweet => sub { | ||
| 73 | my $tweet = shift; | ||
| 74 | |||
| 75 | my $text = $tweet->{text}; | ||
| 76 | my @chars = split //, $text; | ||
| 77 | |||
| 78 | my $cnt = 0; | ||
| 79 | map { $cnt++ if /[\p{Hangul}\p{Hiragana}\p{Katakana}\p{Latin}\p{Common}]/; } @chars; | ||
| 80 | if($cnt / scalar @chars > 0.5 and $tweet->{user}{screen_name} !~ m/perlism/i) { | ||
| 81 | $sth_insert->execute( 'twitter_stream', scalar time, | ||
| 82 | "$tweet->{user}{screen_name}: $tweet->{text}" ); | ||
| 83 | } | ||
| 84 | }, | ||
| 85 | on_keepalive => sub { | ||
| 86 | warn "ping\n"; | ||
| 87 | }, | ||
| 88 | on_error => sub { | ||
| 89 | my $error = shift; | ||
| 90 | warn "Error : $error\n"; | ||
| 91 | $done->send; | ||
| 92 | }, | ||
| 93 | on_eof => sub { | ||
| 94 | $done->send; | ||
| 95 | }, | ||
| 96 | timeout => 60, | ||
| 97 | ); | ||
| 98 | |||
| 99 | $done->recv; | ||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ |