Today's Astronomy Picture of the Day has an excellent time-lapse photo showing Mars (and, coincidentally, Uranus) in retrograde motion. Neat!
use strict;
use Sys::Hostname;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use File::Copy;
my $DEBUG = 1;
my $backgroundLocation = "/usr/share/pixmaps/backgrounds/dailyAstro.jpg";
my $publicId = "AstroFind/v1.0";
my $baseUrl = "http://antwrp.gsfc.nasa.gov";
my $url = "$baseUrl/apod/astropix.html";
## get the stable HTML page
print "astro: fetching $url\n" if $DEBUG;
my $ua = LWP::UserAgent->new();
$ua->agent($publicId);
my $req = HTTP::Request->new(GET => $url);
$req->referer(Sys::Hostname::hostname());
my $response = $ua->request($req);
## get the image location
my $astroHTML = $response->content();
my ($imgTag) = $astroHTML =~/<a href="(image.*)">?/mi;
my $imageLocation = "$baseUrl/$imgTag";
## add on jpg if it doesn't have an extension
if ($imageLocation !~/\.....?$/) { $imageLocation .= '.jpg'; }
print STDERR "astro: fetching $imageLocation\n" if $DEBUG;
## fetch the image
$req = HTTP::Request->new(GET => $imageLocation);
my $response = $ua->request($req);
my $astroImage = $response->content();
## write the file!
open(IMG,">$backgroundLocation");
print IMG $astroImage;
close(IMG);
if ($response->is_success()) {
print "astro: got image and wrote to $backgroundLocation\n" if $DEBUG;
}
else {
print "astro: failed to get image\n";
}