Skip to content

Instantly share code, notes, and snippets.

@n13i
Last active July 23, 2016 15:15
Show Gist options
  • Save n13i/5421671 to your computer and use it in GitHub Desktop.
Save n13i/5421671 to your computer and use it in GitHub Desktop.
#moyasearch
#!/usr/bin/perl
use warnings;
use strict;
use utf8;
use FindBin qw($Bin);
use Encode;
use YAML;
use Net::Twitter::Lite::WithAPIv1_1;
use DateTime;
use Schedule::At;
use Web::Scraper;
use LWP::UserAgent;
use URI::Escape;
my $MyName = $Bin . '/post.pl';
binmode STDOUT, ':encoding(utf8)';
#exit if($#ARGV < 0);
my $conffile = $Bin . '/conf/config.yaml';
my $conf = YAML::LoadFile($conffile) or die('load config failed');
my $twit_r = Net::Twitter::Lite::WithAPIv1_1->new(
consumer_key => $conf->{twitter}->{consumer_key},
consumer_secret => $conf->{twitter}->{consumer_secret},
legacy_lists_api => 0,
ssl => 1,
);
$twit_r->access_token($conf->{twitter}->{read}->{access_token});
$twit_r->access_token_secret($conf->{twitter}->{read}->{access_token_secret});
print "Requesting rate limit status\n";
print Dump($twit_r->rate_limit_status());
my $dt_now = DateTime->now(time_zone => $conf->{timezone});
printf "Now: %s\n", $dt_now->strftime('%Y/%m/%d %H:%M:%S');
my $last_ids = &load_last_postid;
print Dump($last_ids);
print "Requesting mentions\n";
my $dms = $twit_r->direct_messages({ since_id => $last_ids->{dm} });
#my $mentions = $twit_r->mentions({ since_id => $last_ids->{mentions} });
my $mentions = [];
my $max_ids = $last_ids;
my @queries = ();
foreach my $item (@{$dms})
{
print Dump($item);
next if($item->{sender}->{id_str} ne $conf->{accept_sender_id});
if($item->{id} > $max_ids->{dm})
{
$max_ids->{dm} = $item->{id};
}
push(@queries, $item);
}
foreach my $item (@{$mentions})
{
print Dump($item);
next if($item->{user}->{id_str} ne $conf->{accept_sender_id});
if($item->{id} > $max_ids->{mentions})
{
$max_ids->{mentions} = $item->{id};
}
push(@queries, $item);
}
foreach my $query (@queries)
{
my $text = $query->{text};
$text =~ s/^\@n13i_g\s+//g;
printf "%s--> %s\n", $query->{id}, $text;
#next;
my $keyword = '"' . $text . '"';
my $result = &google_it($keyword);
my $post = undef;
if(defined($result->{error2}))
{
$post = $result->{error2};
}
elsif(defined($result->{error}))
{
$post = $result->{error};
$post =~ s/検索ツールをリセット//g;
}
elsif(defined($result->{count}))
{
$post = $keyword . ' ' . $result->{count};
}
$post .= ' #moyasearch';
&post_it($post);
}
&save_last_postid($max_ids);
exit;
sub post_it
{
my $post = shift || die;
my $twit_w = Net::Twitter::Lite::WithAPIv1_1->new(
consumer_key => $conf->{twitter}->{consumer_key},
consumer_secret => $conf->{twitter}->{consumer_secret},
ssl => 1,
);
$twit_w->access_token($conf->{twitter}->{write}->{access_token});
$twit_w->access_token_secret($conf->{twitter}->{write}->{access_token_secret});
if(defined($post))
{
#$post .= ' [auto]';
printf "post: %s\n", $post;
my $status = undef;
eval {
$status = $twit_w->update($post);
};
if($@)
{
warn "$@\n";
}
}
}
sub google_it
{
my $query = shift || die;
my $ua = LWP::UserAgent->new(agent => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16');
#my $res = $ua->get('http://www.google.co.jp/search?q=' . uri_escape_utf8($query) . '&nfpr=1&tbs=li:1&safe=off');
my $res = $ua->get('http://www.google.co.jp/search?q=' . uri_escape_utf8($query) . '&nfpr=1&safe=off');
my $google_html = decode('utf8', $res->content);
#print $google_html;
my $scraper = scraper {
process 'div#resultStats', 'count' => 'TEXT';
process 'div#topstuff div.med', 'error' => 'TEXT';
#process 'div#topstuff div.med p:nth-child(2)', 'error2' => 'TEXT';
process 'div#topstuff div.med p:nth-child(2)', 'error2' => 'TEXT';
};
my $r = $scraper->scrape($google_html);
#print Dump($r);
return $r;
}
sub save_last_postid
{
my $postid = shift || return;
YAML::DumpFile($Bin . '/lastid', $postid) or die;
#open FH, '>', $Bin . '/lastid' or die;
#print FH $postid . "\n";
#close FH;
}
sub load_last_postid
{
my $lastid;
eval {
$lastid = YAML::LoadFile($Bin . '/lastid');
};
if($@)
{
$lastid = { dm => 722471195827544063, mentions => 607471195827544063 };
}
#open FH, '<', $Bin . '/lastid' or return 0;
#my $lastid = <FH>;
#close FH;
return $lastid;
}
# vim: noexpandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment