æè¿çºè¡¨ããã¦ãä¸é¨ã§è©±é¡ãå¼ãã§ããTwitterã®æ°ããStreaming APIãChirpUserStreamsãã
ã¾ã developerã¸ã®previewãªãªã¼ã¹ã¨ãããã¨ã§ãããã¯ãã«ä½¿ããããªãã¨ã¯ã§ããªããã©ã
http://apiwiki.twitter.com/ChirpUserStreams
ä»ã¾ã§ã®Streaming APIã§ã¯åºæ¬çã«
- public timelineãããæå®ãããã¼ã¯ã¼ããå«ãçºè¨ãæ¾ã
- æå®ããã¦ã¼ã¶ã¼ã®çºè¨ãæ¾ã
ã¨ãããã®ã ã£ã(ã¯ã)ãæ¤ç´¢ç³»ã¨ãã¦ã¯ä¾¿å©ã ãã©ãhome_timelineã®ããã«èªåããã©ãã¼ãã¦ããã¦ã¼ã¶ã¼ã®çºè¨ãStreamingã§å¾ããã¨ããã¨å
¨idãç¹ãããã®ããªã¯ã¨ã¹ããã©ã¡ã¼ã¿ã¨ãã¦éããªãã¨ãããªãã£ããã§ããã¾ãã«ãã³ãããªãå½¢ã«ãªã£ã¦ãã¾ã£ã¦ããã
ã¨ãããä»å使ããããã«ãªã£ããChirpUserStreamsãã¯ãèªåããã©ãã¼ãã¦ããã¦ã¼ã¶ã¼ã®ã¢ã¯ãã£ããã£ãStreamingã§åå¾ã§ããããããç´ æ´ãããï¼
ãã£ãããããªã®ãæ¸ãã¦ã¿ãã
#!/usr/bin/perl use strict; use warnings; use AnyEvent::HTTP 'http_request'; use Config::Pit 'pit_get'; use Encode 'encode_utf8'; use MIME::Base64 'encode_base64'; my $config = pit_get( "twitter.com", require => { "username" => "your username on Twitter", "password" => "your password on Twitter", } ); my $cv = AE::cv; http_request( 'GET', 'http://chirpstream.twitter.com/2b/user.json', headers => { Authorization => "Basic " . encode_base64(join ':', @$config{qw/username password/}), }, want_body_handle => 1, sub { my $hdl = shift; my $r = sub { my (undef, $json) = @_; if (my $text = $json->{text}) { print encode_utf8 "$json->{user}{screen_name}: $text\n"; } }; $hdl->on_read(sub { $hdl->push_read( json => $r ); }); } ); $cv->recv;
ãã£ãããã ãã®ã³ã¼ãã§ãhome_timelineã®ããã«èªåã®friendsã®çºè¨ãStreamingã§åå¾ã§ããï¼
AnyEvent::HTTPã§BASICèªè¨¼ã§ã¢ã¯ã»ã¹ãã¦ãwant_body_handleã使ç¨ãã¦AnyEvent::Handleãªãã¸ã§ã¯ããåå¾ãããã使ã£ã¦JSONãã¼ã¿ãåå¾ãç¶ããããã«ãã¦ã¿ãã
statuses/home_timeline REST APIã¨ã¯éããèªåããã©ãã¼ãã¦ããã¦ã¼ã¶ã¼ããèªåããã©ãã¼ãã¦ããªãã¦ã¼ã¶ã¼ã¸ã®ãªãã©ã¤ãªãããããã«ãã¹ã¦æµãã¦ãããã¨ã«ãªãããããè¦ãå¿ è¦ããªãå ´åã¯ä¸çªæåã«friendsã®idä¸è¦§ãåå¾ã§ããã®ã§ããããå ã«ãã£ã«ã¿ãªã³ã°ããã®ãè¯ãããã
ããã¦ãçºè¨ã ãã§ãªãèªåã®friendsããRetweetããããfavoriteããããfollowãããã¨ãã£ãæ
å ±ãstreamingã§ãªã¢ã«ã¿ã¤ã ã«åå¾ã§ãããããã¯ãã¸ã§ãããï¼
ã誰ã誰ãfollowãããã¨ãã誰ã誰ã®ã©ã®çºè¨ãfavoriteããããªãã¦ã®ã¯ä»ã¾ã§ã»ã¨ãã©åããã¨ãã§ããªãã£ãã¯ããunfollowãããã¨ãã¯æ®å¿µãªããåããªãããã ãããã¯ããã追å ãããã®ããªâ¦ï¼
ããã使ãã°ãã¡ã¼ã«ç£è¦ãªã©ãããªãã¦ãAPIçµç±ã§ç¬éãã©ãã¼è¿ããè¡ããã¨ãã§ããã¯ãã
Twitter でイチイチ follow するのが面倒くさい - にぽたん研究所
ã¨ããããã§ããããªã®ãæ¸ãã¦ã¿ããã
#!/usr/bin/perl use strict; use warnings; use AnyEvent::HTTP 'http_request'; use Config::Pit 'pit_get'; use Encode 'encode_utf8'; use JSON::XS 'decode_json'; use LWP::UserAgent; use MIME::Base64 'encode_base64'; my $config = pit_get( "twitter.com", require => { username => "your username on Twitter", password => "your password on Twitter", } ); # BASICèªè¨¼ã«ä½¿ç¨ãããããæ å ± my $auth_header = HTTP::Headers->new( Authorization => "Basic " . encode_base64(join ':', @$config{qw/username password/}), ); # èªåèªèº«ã®ã¦ã¼ã¶ã¼IDãåå¾ my $res = LWP::UserAgent->new( default_headers => $auth_header, )->get('http://twitter.com/account/verify_credentials.json'); my $self_id = decode_json($res->content)->{id}; # Streamingãããã¼ã¿ãå¾ãéã®æå my $callback = sub { my (undef, $json) = @_; my $event = $json->{event} or return; if ($event eq 'follow' && $json->{target}{id} == $self_id) { reply_with($json->{source}{id}, '@%s followãããã¨ããããã¾ãï¼'); } if ($event eq 'favorite' && $json->{target}{id} == $self_id) { reply_with($json->{source}{id}, '@%s favãããã¨ããããã¾ãï¼'); } if ($event eq 'retweet' && $json->{target}{id} == $self_id) { reply_with($json->{source}{id}, '@%s RTãããã¨ããããã¾ãï¼'); } }; # Streaming APIã«æ¥ç¶ãã my $cv = AE::cv; http_request( GET => 'http://chirpstream.twitter.com/2b/user.json', headers => $auth_header, want_body_handle => 1, sub { shift->on_read(sub { shift->push_read( json => $callback ); }); } ); $cv->recv; # idããç¸æã®screen_nameãåå¾ãã¦replyãã sub reply_with { my ($id, $message) = @_; http_request( GET => "http://twitter.com/users/show/$id.json", headers => $auth_header, sub { my $data = shift; my $screen_name = decode_json($data)->{screen_name}; my $status = sprintf $message, $screen_name; my $uri = URI->new('http://twitter.com/statuses/update.json'); $uri->query_form( status => $status ), http_request( POST => $uri->as_string, headers => $auth_header, sub {}, ); } ) }
followãfavoriteãRTãããç¬éã«æ¯åã礼ãè¨ãã¾ãã
ã¾ã æ£å¼ãªãªã¼ã¹ã§ããªãã®ã§ä»æ§å¤æ´ãªã©ãããããç¥ããªããã©ã大ãã«æå¾ ã§ãã便å©ãªAPIã§ããï¼