Last active
October 9, 2015 21:17
-
-
Save ichigotake/3578219 to your computer and use it in GitHub Desktop.
[旧] 特定ワードが含まれるツイートをyanchaへ投げるスクリプト。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use utf8; | |
| use Encode qw/encode_utf8/; | |
| use URL::Encode qw/url_encode_utf8/; | |
| use JSON qw/encode_json decode_json/; | |
| use HTTP::Request::Common qw/POST/; | |
| use LWP::UserAgent; | |
| use feature qw/say/; | |
| use AnyEvent; | |
| use lib qw/lib/; | |
| use Yancha::Client; | |
| #サーバーURL | |
| my $yancha_url = 'http://yancha.hachiojipm.org:443'; | |
| #検索ワード | |
| my $query = 'hachiojipm'; | |
| my $cv = AnyEvent->condvar; | |
| my $io; $io = AnyEvent->io( | |
| fh => \*STDIN, | |
| poll => 'r', | |
| cb => sub { | |
| chomp(my $input = <STDIN>); | |
| undef $io; | |
| $cv->send($input); | |
| warn 'end'; | |
| }, | |
| ); | |
| #クライアント生成とログイン | |
| my $client = Yancha::Client->new(); | |
| my $token = yancha_get_token(); | |
| $client->run(sub { | |
| my ( $self, $socket ) = @_; | |
| $socket->emit('token login', $self->token); | |
| }); | |
| my $id; | |
| my $cv_timer = AnyEvent->condvar; | |
| my $timer; $timer = AnyEvent->timer( | |
| after => 0, | |
| interval => 30, | |
| cb => sub { | |
| my $content = twitter_search( $query ); | |
| if ( $id && $content ) { | |
| for my$res( @{ $content->{ results } } ) { | |
| #同じ発言IDのものや過去のツイートは無視 | |
| last if ($id >= $res->{ id }); | |
| my $text = "@" . $res->{ from_user } . ": " . $res->{ text } . " #PUBLIC"; | |
| yancha_post($text, $token); | |
| $id = $res->{ id }; | |
| } | |
| } | |
| else { | |
| $id = $content->{ results }[0]->{ id }; | |
| } | |
| $cv_timer->send; | |
| }, | |
| ); | |
| $cv_timer->recv; | |
| if (defined(my $input = $cv->recv)) { | |
| print "got: [$input]\n"; | |
| } | |
| sub twitter_search { | |
| my ($word) = @_; | |
| my $search_url = 'http://search.twitter.com/search.json?q='; | |
| $search_url .= url_encode_utf8($word); | |
| my $ua = LWP::UserAgent->new(); | |
| my $res = $ua->get($search_url); | |
| my $data; | |
| $data = eval { | |
| decode_json( $res->content ); | |
| }; | |
| if ( $@ ) { | |
| return {}; | |
| } | |
| return $data; | |
| } | |
| sub yancha_post { | |
| my ($text, $token) = @_; | |
| my $endpoint = $yancha_url . '/api/post'; | |
| my $param = { | |
| text => encode_utf8($text), | |
| token => $token, | |
| }; | |
| my $req = POST($endpoint, $param); | |
| my $ua = LWP::UserAgent->new; | |
| my $res = $ua->request($req); | |
| } | |
| sub yancha_get_token { | |
| my $res = $client->login( | |
| $yancha_url, => 'login', { nick => 'ついとりちゃん@bot' } | |
| ); | |
| $res = $client->connect; | |
| my $token = $client->token; | |
| return $token; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment