Re: APOD to Desktop Wallpaper September 9, 2011
Posted by claudio in Uncategorized.trackback
I saw markam’s post on the Planet Perl Iron Man –where we are both syndicated– where he posts a short script to use the Astronomy Picture of the Day as a wallpaper… on MS Windows.
Being a GNU/Linux + Gnome user myself, I post a quick and dirty “translation” of the script (e.g. to use with cron).
#!/usr/bin/env perl
use strict;
use warnings;
use LWP::Simple;
require Carp;
my $apodbase = 'http://apod.nasa.gov/';
my $dlbase = '/home/mmeyer/Downloads/';
chdir $dlbase or die "Couldnt chdir to $dlbase: $!";
my $content = get($apodbase) or die "Couldn't download image: $!";
Carp::croak 'Content doesn\'t match' unless $content =~ m/<IMG SRC="(.*)"/g;
my $urlend = $1;
Carp::croak 'No url found' unless $urlend =~ m|([^/]+)$|;
my $targ = $1;
my $status = getstore($apodbase . $urlend, $targ);
Carp::croak "Couldn't store image: $status" unless is_success($status);
my @shell_cmd = ('/usr/bin/gconftool-2', '-t', 'str', '--set', '/desktop/gnome/background/picture_filename', $dlbase . $targ);
exec(@shell_cmd);
My suggestion – make matching case insensitive:
- $content =~ m/<IMG SRC="(.*)"/g;
+ $content =~ m/<IMG SRC="(.*)"/ig;
In case they change markup some day.
Maybe using WWW::Mechanize and the “images” method would eliminate the need for that regex finding images?
If I would do a little more than adapting the original script to Gnome, I would:
- use %ENV for home dir and user (no ediiting necessary).
- overwrite the daily image instead of using the original name.
- use Mechanize to download the big image and resize it for the screen size (a parameter?).
However, it should run out of the box (no cpan setup for novice users).
C.
I’d also point out that you don’t have to keep running gconf-tool2 once you’ve given it a jpg, you can just overwrite the same file. And if you’ve set it to ‘Stretch’, that will also apply to new images. So, you only need this daily:
perl -MLWP::Simple -e ‘
$url=”http://apod.nasa.gov/apod”;
($img)=get($url)=~m|img src=\”(image/.*?\.jpg)\”|i;
mirror(“$url/$img”,”$ENV{HOME}/Downloads/background.jpg”);
‘