Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Created January 7, 2012 01:53
Show Gist options
  • Save Shinpeim/1573460 to your computer and use it in GitHub Desktop.
Save Shinpeim/1573460 to your computer and use it in GitHub Desktop.
#!/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