Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!perl
use strict;
use warnings;
use Config::Pit;
use Growl::Any;
use AnyEvent::Twitter::Stream;
use Encode;
use Web::Scraper;
use URI;
my $done = AE::cv;
my $config = pit_get("twitter.com", require => {
"consumer_key" => "consumer_key on twitter.com",
"consumer_secret" => "consumer_secret on twitter.com",
"access_token" => "access_token on twitter.com",
"access_token_secret" => "access_token_secret on twitter.com"
});
my $growl = Growl::Any->new;
$growl->register("my app", ["tweet"]);
my $streamer; $streamer = AnyEvent::Twitter::Stream->new(
consumer_key => $config->{consumer_key},
consumer_secret => $config->{consumer_secret},
token => $config->{access_token},
token_secret => $config->{access_token_secret},
method => 'userstream',
on_tweet => sub {
my $tweet = shift;
if( defined($tweet->{text}) && $tweet->{text} =~ /(http:\/\/instagr.am\/(.+))/ ) {
my $s = scraper {
process ".photo", link => '@src';
};
my $res;
eval { $res = $s->scrape(URI->new($1)) };
# Error処理
if( $@ ) {
print "Exception: $@";
}
else {
print "$tweet->{user}{screen_name}: $res->{link}\n";
$growl->notify(
"tweet",
encode_utf8($tweet->{user}{screen_name}),
encode_utf8($tweet->{text}),
$res->{link}
);
}
}
},
on_error => sub {
my $error = shift;
warn "ERROR: $error";
$done->send;
},
on_eof => sub {
$done->send;
},
);
$done->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment