Created
November 9, 2011 05:43
-
-
Save hayajo/1350531 to your computer and use it in GitHub Desktop.
最近CPANにアップデートした人の顔写真をターミナルに表示
This file contains hidden or 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
use strict; | |
use warnings; | |
use Image::Term256Color; | |
use Term::ReadKey; | |
use POSIX qw/ceil/; | |
use Web::Scraper; | |
use URI; | |
use Furl; | |
use File::Temp; | |
main(); | |
sub main { | |
local $| = 1; | |
print "get recent authors ... "; | |
my $recents = get_recent_cpan_author(); | |
for (@$recents) { | |
print "\x1bc"; # ESC c - reset terminal | |
print Image::Term256Color::convert( $_->{image}, | |
{ scale_x => get_scale( $_->{module} ) } ) | |
. "\n"; | |
print $_->{module}; | |
sleep 1; | |
} | |
} | |
sub get_scale { | |
my ( $wchar, $hchar ) = GetTerminalSize; | |
my $w = ( $hchar > $wchar / 2 ) ? $wchar / 2 : $hchar; | |
$w - ceil( length( $_[0] ) / $wchar ); | |
} | |
sub get_recent_cpan_author { | |
my $recents = scraper { | |
process '.module', "modules[]" => scraper { | |
process 'h3 > a', module => 'TEXT'; | |
process 'div.author > a > div > img', image => '@src'; | |
}; | |
}; | |
my $authors = $recents->scrape( URI->new('http://frepan.org/') ); | |
my $index = 1; | |
my $furl = Furl->new; | |
my $tmpdir = File::Temp->newdir; | |
for my $author ( @{ $authors->{modules} } ) { | |
my $img_url = $author->{image}->as_string; | |
my $res = $furl->get($img_url); | |
unless ( $res->is_success ) { | |
warn "Can't download $img_url\n"; | |
next; | |
} | |
my $file = File::Spec->catfile( $tmpdir, $index++ . ".png" ); | |
open my $fh, '>', $file or die "Can't open $file"; | |
print {$fh} $res->content; | |
close $fh; | |
$author->{image} = $file; | |
} | |
no strict 'refs'; | |
${"main\::__tmpdir__"} = $tmpdir; | |
$authors->{modules}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment