Created
September 11, 2009 05:26
-
-
Save zaphar/185104 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
## script to use the hubble json api to download images to your harddrive. | |
## modify to suit. | |
use JSON; | |
use IO::Pipe; | |
use IO::File; | |
my $hubble_data; | |
my $pipe = IO::Pipe->new(); | |
$pipe->reader(qw{curl -d action=all-images http://www.hubblesite.org/gallery/album/show/}); | |
while (<$pipe>) { | |
$hubble_data .= $_; | |
} | |
my $image_url_prefix = 'http://imgsrc.hubblesite.org/hu/db/images/hs-'; | |
my $image_re = qr/(^\d{4})\/(\d{1,2})\/image\/(.+)$/; | |
my $data_parser = sub { | |
my $row = shift; | |
my ($y, $n, $alpha) = $row->{i} =~ $image_re; | |
my $url = $image_url_prefix . "$y-$n-$alpha-xlarge_web.jpg"; | |
$row->{url} = $url; | |
$row->{title} = $row->{t}; | |
delete $row->{t}; | |
return $row; | |
}; | |
my @list = map { $data_parser->($_); } @{from_json($hubble_data)}; | |
for my $img (@list) { | |
my $file; | |
eval { | |
warn "downloading: " . $img->{url}; | |
$file = $img->{title} . ".jpg"; | |
$file =~ s/\s+/_/g; | |
my $pipe = IO::Pipe->new(); | |
$pipe->reader(curl => $img->{url}); | |
my $fh = IO::File->new("> $file"); | |
while (<$pipe>) { | |
print $fh $_; | |
} | |
$fh->close(); | |
}; | |
if ($@) { | |
warn "Failed to download: ". $img->{url}; | |
unlink $file; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment