Created
January 7, 2012 01:53
-
-
Save Shinpeim/1573460 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 IO::File; | |
use LWP::Simple; | |
use AnyEvent; | |
use AnyEvent::HTTP; | |
use Digest::SHA1 qw/sha1_hex/; | |
my $content = get(shift); | |
my @urls = $content =~ m{<a.*?href=["']?(https?://[^"'>]+?\.jpg)["']?}gi; | |
die "no images" unless @urls; | |
my $cv = AnyEvent->condvar; | |
for my $url (@urls) { | |
$cv->begin; | |
warn "send request to $url"; | |
http_get $url, sub { | |
my ($body, $header) = @_; | |
warn "receive response from $url"; | |
if ($header->{Status} !~ m{^2}) { | |
$cv->end; | |
return; | |
} | |
my $filename = sha1_hex($body).".jpg"; | |
warn "save as $filename"; | |
my $fh = IO::File->new($filename, "w") or die $!; | |
$fh->print($body); | |
$cv->end; | |
$fh->close; | |
}; | |
} | |
$cv->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment