Skip to content

Instantly share code, notes, and snippets.

@makotoworld
Created August 7, 2009 01:00
Show Gist options
  • Save makotoworld/163655 to your computer and use it in GitHub Desktop.
Save makotoworld/163655 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Perl6::Say;
use Data::Dumper;
use DateTime;
use LWP::UserAgent;
use XML::Simple;
use Encode;
use Net::Twitter;
my %num_of_mon = (
jan => 1, feb => 2, mar => 3, apr => 4, may => 5, jun => 6,
jul => 7, aug => 8, sep => 9, oct => 10, nov => 11, dec => 12,
);
srand;
my @message = ("これが最格安ですよ! 【",
"求めているものはこれですね! 【",
"これがお薦めですよ! 【",
"あなたはお目が高い! 【");
my $user = "xxxx";
my $password = "xxxx";
my (@comment, @word, $time, @replies_name, $da);
my (%dates,$text);
my $json = JSON::Any->new;
my $nt = Net::Twitter->new(
traits => [qw/API::REST/],
username => $user,
password => $password
);
#ランダムメッセージ
sub message{
my ($message) = @_;
my $index = rand @message;
return $message[$index];
}
#短縮URL
sub bit_ly{
my ($item_url, $ua) = @_;
my $bit_ly = 'http://api.bit.ly/shorten?version=2.0.1&longUrl='.
$item_url.'&login=makotoworld&apiKey=xxxx';
my $res = $ua->get($bit_ly);
my $obj = $json->jsonToObj($res->content);
foreach my $data1(keys %{$obj->{'results'}}){
return $obj->{'results'}{$data1}{'shortUrl'};
}
}
my $dt = DateTime->now;
#CRON設定に合わせて修正
$dt = $dt->subtract(days => 7);
#$dt = $dt->subtract(minutes => 6);
#$dt = $dt->subtract(months => 2);
eval {
my $res = $nt->mentions({ count => '10'});
for my $status ( @$res ) {
my ($week, $month, $day, $hour, $minute, $second, $year) =
$status->{created_at} =~ m/^(\w{3})\s(\w{3})\s(\d{2})\s(\d{1,2})\:(\d{2})\:(\d{2})\s\+\d{4}\s(\d{4})/;
$month = $num_of_mon{lc ($month)};
$da = DateTime->new(
year => $year,
month => $month,
day => $day,
hour => $hour,
minute => $minute,
second => $second,
);
if( $dt->epoch <= $da->epoch ){
# push(@comment, $status->{text});
# push(@replies_name ,$status->{user}->{screen_name});
# my ($sta_text) = $status->{text} =~ m/\@.*\s(.*)/;
$dates{$status->{id}}{'screen_name'} = $status->{user}->{screen_name};
($dates{$status->{id}}{'comment'}) = $status->{text} =~ m/\@.*\s(.*)/;
}
}
};
foreach(@comment){
push(@word, $_ =~ m/\@web_makoto\s(.*)/);
}
#say Dumper(%dates);
foreach(keys %dates){
my $keyword = $dates{$_}{'comment'};
# say $dates{$_}{'screen_name'};
# foreach(@word){
# my $keyword = $_;
my $kakaku = "http://api.kakaku.com/WebAPI/ItemSearch/Ver1.0/".
"ItemSearch.aspx?resultset=mini&ApiKey=".
"xxxx&SortOrder=pricerank&Keyword=".$keyword;
my $ua = LWP::UserAgent->new();
my $res = $ua->get($kakaku);
$res->is_success or die 'Can\'t connect to api.kakaku.com';
my $xml_parser = XML::Simple->new();
$XML::Simple::PREFERRED_PARSER = 'XML::Parser';
my $xml = $res->content;
my $data = XMLin($xml);
my $key = "";
my $datas;
unless($data->{Message}){
if( $data->{Item} =~ m/HASH/ ){
foreach ( $data->{Item} ){
unless (ref $_->{'LowestPrice'}){
if($key ne "end"){
$datas = "\@".$dates{$_}{'screen_name'};
$datas .= message(@message);
$datas .= Encode::encode("UTF-8",$_->{'ProductName'});
$datas .= "】¥".$_->{'LowestPrice'}. " ";
$datas .= bit_ly($_->{'ItemPageUrl'}, $ua);
say $datas;
# $nt->update($datas, $dates{$_}{'screen_name'});
}
$key = "end";
}
}
}elsif($data->{Item} =~ m/ARRAY/){
foreach ( @{$data->{Item}} ){
unless (ref $_->{'LowestPrice'}){
if($key ne "end"){
$datas .= message(@message);
$datas .= Encode::encode("UTF-8",$_->{'ProductName'});
$datas .= "】¥".$_->{'LowestPrice'}. " ";
$datas .= bit_ly($_->{'ItemPageUrl'}, $ua);
say $datas;
# $nt->update($datas, $dates{$_}{'screen_name'});
}
$key = "end";
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment