jump to navigation

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);

About these ads

Comments»

1. Kamen - September 10, 2011

My suggestion – make matching case insensitive:
- $content =~ m/<IMG SRC="(.*)"/g;
+ $content =~ m/<IMG SRC="(.*)"/ig;

In case they change markup some day.

2. Gabor Szabo - September 11, 2011

Maybe using WWW::Mechanize and the “images” method would eliminate the need for that regex finding images?

claudio - September 11, 2011

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.

3. Chad A. Davis (@vorcidel) - September 14, 2011

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”);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 135 other followers

%d bloggers like this: