Skip to content

Instantly share code, notes, and snippets.

@zaphar
Created September 11, 2009 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaphar/185104 to your computer and use it in GitHub Desktop.
Save zaphar/185104 to your computer and use it in GitHub Desktop.
## 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