Skip to content

Instantly share code, notes, and snippets.

@blmarket
Last active December 19, 2015 19:18
Show Gist options
  • Save blmarket/6004602 to your computer and use it in GitHub Desktop.
Save blmarket/6004602 to your computer and use it in GitHub Desktop.
Twitter Image-only Reader.
#!/usr/bin/env perl
use utf8;
use strict;
binmode STDOUT, ':utf8';
use Tk;
use Tk::Photo;
use Tk::PNG;
use Tk::JPEG;
use Tk::After;
use LWP::UserAgent;
use MIME::Base64;
use Net::Twitter;
my $ua = LWP::UserAgent->new;
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => 'EKz64eVIJwAyQrEQH0AHw',
consumer_secret => 'm4ht8LXDiTZtA32wanhqI5kpiRyQOXjH23nRQXeeqA',
access_token => '',
access_token_secret => '',
);
my $mw = new MainWindow();
my $fr = $mw->Frame()->pack;
$mw->bind("<Escape>" => sub { $mw->destroy(); });
func();
$mw->after(60000, \&func);
MainLoop;
sub func {
$fr->destroy();
$fr = $mw->Frame()->pack;
my $line = $nt->home_timeline({ count => 20 });
for my $status (@$line) {
print "<$status->{user}{screen_name}> $status->{text}\n";
if ($_ = fetch_image($status)) {
print "Got URL : $_\n";
my $res = $ua->get($_);
my $img = $mw->Photo(-data => encode_base64($res->decoded_content));
$fr->Label(-image => $img)->pack;
}
}
$mw->after(60000, \&func);
}
sub fetch_image {
my ($status) = shift;
if (!$status->{entities}{media}) {
return undef;
}
my $url = $status->{entities}{media}->[0]{media_url};
return $url;
}
#!/usr/bin/env perl
use utf8;
use strict;
binmode STDOUT, ':utf8';
use Net::Twitter;
use Data::Dumper;
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => 'EKz64eVIJwAyQrEQH0AHw',
consumer_secret => 'm4ht8LXDiTZtA32wanhqI5kpiRyQOXjH23nRQXeeqA',
access_token => '',
access_token_secret => '',
);
my $line = $nt->home_timeline({ count => 30 });
for my $status (@$line) {
print "$status->{created_at} <$status->{user}{screen_name}> $status->{text}\n";
if ($_ = fetch_image($status)) {
print "Got URL : $_\n";
}
}
# my $status = $nt->show_status({ id => '356929197786157056' });
# if($_ = fetch_image($status)) {
# print "Got URL : $_\n";
# }
sub fetch_image {
my ($status) = shift;
if (!$status->{entities}{media}) {
return undef;
}
my $url = $status->{entities}{media}->[0]{media_url};
return $url;
}
# print Dumper($line->[12]->{entities});
# print $line->[12]->{id}."\n";
# print Dumper($line->[12]);
# print Dumper($line);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment