Skip to content

Instantly share code, notes, and snippets.

@nmelnick
Last active March 14, 2018 02:55
Show Gist options
  • Save nmelnick/4ccc0a8e3d30f32cf771aeeb7b19cbfa to your computer and use it in GitHub Desktop.
Save nmelnick/4ccc0a8e3d30f32cf771aeeb7b19cbfa to your computer and use it in GitHub Desktop.
This is a quick and dirty script to change the elementary os wallpaper to the Bing image of the day
#!/usr/bin/env perl
# Sets the current elementary os wallpaper to the Bing image of the day. This
# is similar to Bing wallpaper daemons for Windows, GNOME, and the like. Instead
# of running as a daemon, just add to your crontab. For instance, to run every
# 3 hours, execute `crontab -e` from the command line, and insert:
# 0 */3 * * * /path/to/elementary-bing-wallpaper.pl
#
# This may require the installation of libjson-xs-perl.
# sudo apt install libjson-xs-perl
#
# One may also go back in time by executing with a start index, but only if that
# wallpaper hasn't already been downloaded. For example:
# /path/to/elementary-bing-wallpaper.pl 3
#
use 5.14.0;
use strict;
use warnings;
use JSON::XS qw(decode_json);
use LWP::Simple qw(get);
my $start_index = (shift(@ARGV) or '0');
my $set_wallpaper = '/usr/libexec/switchboard-plug-pantheon-shell/set-wallpaper';
my $bing_uri = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=' . $start_index . '&n=1';
my $paper_home = $ENV{HOME} . '/Pictures/Bing';
my $o = decode_json(get($bing_uri));
my $paper = 'http://www.bing.com' . $o->{images}->[0]->{url};
if ($paper) {
unless ( -e $paper_home ) {
say 'Creating directory ' . $paper_home;
system('mkdir -p ' . $paper_home);
}
my $base_name = ( split(/\//, $paper) )[-1];
my $image_path = $paper_home . '/' . $base_name;
unless ( -e $image_path ) {
say 'Retrieving new wallpaper from ' . $paper;
my $img = get($paper);
if ($img) {
open( my $ofh, '>', $image_path ) or die $!;
binmode($ofh);
print $ofh $img;
close($ofh);
system($set_wallpaper, $image_path);
} else {
say 'Unable to retrieve ' . $paper;
}
} else {
say 'Wallpaper already exists';
}
} else {
say 'No result from Bing found';
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment