Created
February 19, 2012 02:13
-
-
Save rightgo09/1861622 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use URI; | |
use LWP::Simple (); | |
use XML::Simple; | |
use Web::Scraper; | |
use Encode qw/ encode_utf8 /; | |
my $url = URI->new('http://gihyo.jp/book/feed/rss2'); | |
my $rss = LWP::Simple::get($url); | |
die "FAIL!!" unless $rss; | |
my $scraper = scraper { | |
process '#publishedDetail div.cover img', 'source' => '@src'; | |
}; | |
my $xml = XML::Simple->new->XMLin($rss); | |
for my $item (@{ $xml->{channel}->{item} }) { | |
warn encode_utf8($item->{title}); | |
warn encode_utf8($item->{link}); | |
warn encode_utf8($item->{author}); | |
warn encode_utf8($item->{pubDate}); | |
warn encode_utf8($item->{description}); | |
if ($item->{link}) { | |
my $result = eval { $scraper->scrape(URI->new($item->{link})) }; | |
warn($@) && next if $@; | |
if ($result->{source}) { | |
warn $result->{source}; | |
my $thumbnail = LWP::Simple::get(URI->new($result->{source})); | |
warn "FAIL!! GETTING THUMBNAIL!!" unless $thumbnail; | |
(my $file = $result->{source}) =~ s{^.*/([^/]+)$}{$1}; | |
warn $file; | |
open my $fh, '>', $file or warn($@) && next; | |
print $fh $thumbnail; | |
close $fh; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment