Created
April 6, 2012 02:00
-
-
Save walkure/2315980 to your computer and use it in GitHub Desktop.
automatic niseho(にせほー) poster(implement handshake ver.)
This file contains 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 Encode; | |
use OAuth::Lite::Consumer; | |
use IO::Socket::SSL; | |
use JSON; | |
use Date::Parse; | |
use Net::Twitter::Lite; | |
use URI; | |
#target account | |
my %account = ( | |
consumer_key => '<COMSUMER KEY>', | |
consumer_secret => '<CONSUMER SECRET>', | |
token => '<ACCESS TOKEN>', | |
secret => '<ACCESS SECRET>', | |
); | |
#charset and message | |
my $charset = 'euc-jp'; | |
my $niseho = 'にせほー'; | |
#nisehorns | |
my @nisehorn_id = ( | |
96348838, #nisehorn | |
96560355, #nisehorrn | |
229752118,#nisehorrrn | |
229754624,#nisehorrrrn | |
295206903,#nisehorrrrrn | |
295216106,#nisehorrrrrrn | |
295218654,#nisehorrrrrrrn | |
); | |
my $c = OAuth::Lite::Consumer->new(%account); | |
my $t = OAuth::Lite::Token->new(%account); | |
my $p = Net::Twitter::Lite->new(%account); | |
$p->access_token($account{token}); | |
$p->access_token_secret($account{secret}); | |
print "++Begin Loop\n"; | |
$| = 1; | |
my $wait = 10; | |
my $method = 'POST'; | |
my $ep = URI->new('https://stream.twitter.com/1/statuses/filter.json'); | |
while(1) | |
{ | |
print "++Generate Request\n"; | |
my $request = $c->gen_oauth_request( | |
method => $method, | |
url => $ep, | |
token => $t, | |
params => {follow=>join(',',@nisehorn_id)}, | |
); | |
$request->header(Host => $ep->host); | |
$request->header(UserAgent => 'nisehotter'); | |
my $client = new IO::Socket::SSL($ep->host_port); | |
unless(defined $client){ | |
die "--ERROR:",&IO::Socket::SSL::errstr(); | |
} | |
print "++Send Request\n"; | |
print $client $method.' '.$ep->path." HTTP/1.0\r\n"; | |
print $client $request->headers->as_string; | |
print $client "\r\n"; | |
print $client $request->content(); | |
print "++Get Response Header\n"; | |
my $res; | |
while(<$client>){ | |
chomp; | |
$_ =~ s/\r//g; | |
last unless length $_; | |
unless(defined $res){ | |
$res = HTTP::Response->parse($_); | |
}else{ | |
my($name,$body) = split(/:/,$_); | |
$res->header($name => $body); | |
} | |
} | |
#want only 200 response | |
if($res->code eq '200'){ | |
print "++Successfully connected to server and collect repsponse...\n"; | |
$wait = 10; | |
}else{ | |
print '--Connecting error['.$res->code()."]. Waiting for $wait seconds.\n"; | |
sleep($wait); | |
$wait *= 2; | |
next; | |
} | |
#body | |
print "++Get Response Body\n"; | |
while(<$client>){ | |
chomp; | |
$_ =~ s/\r//g; | |
if(length $_){ | |
parse_json($_); | |
}else{ | |
#heartbeat | |
print '.'; | |
} | |
} | |
#error clse | |
close $client; | |
print "\n--Close Connection...\n"; | |
sleep(10); | |
} | |
exit; | |
sub parse_json | |
{ | |
my $json = shift; | |
my $tweet = decode_json($json); | |
return unless defined $tweet->{text}; | |
foreach my $id(@nisehorn_id){ | |
next if($id ne $tweet->{user}{id_str}); | |
process_tweet($tweet); | |
last; | |
} | |
} | |
sub process_tweet | |
{ | |
my $tweet = shift; | |
my $text = Encode::encode($charset,$tweet->{text}); | |
my $talker = Encode::encode($charset,$tweet->{user}{name}); | |
my $t = str2time($tweet->{created_at}); | |
my $time = (defined $t) ? scalar localtime($t) : $tweet->{created_at}; | |
if($text =~ /^@/){ | |
print '@'; | |
return; | |
} | |
print "\n[($tweet->{user}{screen_name})($tweet->{user}{id_str})$talker]<$time>[$text]\n"; | |
if($text =~ /^$niseho/){ | |
my $niseho_msg = Encode::decode($charset,"\@$tweet->{user}{screen_name} $niseho"); | |
if(fork()){ | |
return; | |
}else{ | |
sleep(10); | |
# eval{ | |
$p->update({status => $niseho_msg , in_reply_to_status_id => $tweet->{id_str}}); | |
# }; | |
if($@){ | |
print "--Cannot Niseho[$@]\n"; | |
}else{ | |
print "++Send Niseho\n"; | |
} | |
exit; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use AnyEvent;
https://gist.github.com/2315992