banner
Previous Page
PCLinuxOS Magazine
PCLinuxOS
Article List
Disclaimer
Next Page

Get A New HD NatGeo Wallpaper Image Every Day

by Paul Arnote (parnote)

Back in July, agmg made a post in the PCLinuxOS forum seeking sites to download new wallpaper images. As usual, the PCLinuxOS community responded with numerous suggestions of sites from which to download wallpaper images. The PCLinuxOS Magazine even ran an article listing some popular wallpaper sites in the past.

Then, I came across this custom script that downloads the National Geographic Picture Of The Day. If you're unfamiliar with these images, follow the previous link and have a look. The original script is here. A modified version of the same script is here. Below is a version of the script that I've further modified. True to the reputation of National Geographic, the images are stunning and of very high quality.



The original version of the script automatically sets the newly downloaded image as your wallpaper -- provided that you are running the GNOME desktop. However, each different desktop environment has a unique method for setting the desktop wallpaper from the command line. One thing that is quite problematic is determining which desktop environment is currently running, via the command line. I've yet been able to find a test that reliably returns the currently running desktop from the command line or a bash file.

Here is the script, with my modifications (which I'll explain below). You can copy and paste it into your favorite plain text editor (no, LibreOffice does not qualify). You can also download a copy from The PCLinuxOS Magazine website. If you download a copy of the script, be sure to remove the .txt file extension. In either case, be sure to save it to a location that is in your system's path, and be sure to set the file to be executable (e.g., chmod +x ~/path/to/script/NatGeo-POD.sh).


#!/bin/bash  

 # Copyright (c) 2011 Josh Schreuder  

 # http://www.postteenageliving.com  

 #  

 # Permission is hereby granted, free of charge, to any person obtaining a copy  

 # of this software and associated documentation files (the "Software"), to deal  

 # in the Software without restriction, including without limitation the rights  

 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  

 # copies of the Software, and to permit persons to whom the Software is  

 # furnished to do so, subject to the following conditions:  

 #  

 # The above copyright notice and this permission notice shall be included in  

 # all copies or substantial portions of the Software.  

 #  

 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  

 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  

 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  

 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  

 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  

 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  

 # THE SOFTWARE.  

 # ********************************  

 # *** OPTIONS  

 # ********************************  

 # Set this to 'yes' to save a description (to ~/description.txt) from ngeo page  

 #

 # I can't see where this line is used anywhere in the script, so let's comment it out

 # GET_DESCRIPTION="yes"  

 #

 # Set this to the directory you want pictures saved  

 PICTURES_DIR=~/Wallpaper/NatGeo

 if [ ! -d $PICTURES_DIR ]; then

   mkdir -p $PICTURES_DIR

 fi

 sleep 1

 # ********************************  

 # *** FUNCTIONS  

 # ********************************  

 function get_page {  

   echo "Downloading page to find image"  

   wget http://photography.nationalgeographic.com/photography/photo-of-the-day/ --quiet -O- 2> /dev/null |  

   grep -m 1 http://images.nationalgeographic.com/.*.jpg -o > /tmp/pic_url

   wget http://photography.nationalgeographic.com/photography/photo-of-the-day/ --quiet -O- 2> /dev/null |  

   grep -m 1 http://images.nationalgeographic.com/.*1600x1200.*.jpg -o > /tmp/pic_url2

 }  

 function clean_up {  

   # Clean up  

   echo "Cleaning up temporary files"  

   if [ -e "/tmp/pic_url" ]; then  

     rm /tmp/pic_url  

   fi  

   if [ -e "/tmp/pic_url2" ]; then  

     rm /tmp/pic_url2

   fi

   if [ -f "~/tmp/NatGeo.edc" ]; then

     rm -f ~/tmp/NatGeo.edc

   fi

}

  function make_js {

    js=$(mktemp)

    cat > $js <<_EOF

      var wallpaper = "$PICTURES_DIR/${TODAY}_ngeo.jpg";

      var activity = activities()[0];

      activity.currentConfigGroup = new Array("Wallpaper", "image");

      activity.writeConfig("wallpaper", wallpaper);

      activity.writeConfig("userswallpaper", wallpaper);

      activity.reloadConfig();

_EOF

}

 function kde_wallpaper {

   make_js

   qdbus org.kde.plasma-desktop /MainApplication loadScriptInInteractiveConsole $js > /dev/null

   # sleep 2

   # You will need to install xdotool from Synaptic

   xdotool search --name "Desktop Shell Scripting Console -- Plasma Desktop Shell" windowactivate key ctrl+e key ctrl+w

   rm -f "$js"

   dbus-send --dest=org.kde.plasma-desktop /MainApplication org.kde.plasma-desktop.reparseConfiguration

   dbus-send --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.ReloadConfig

   dbus-send --dest=org.kde.kwin /KWin org.kde.KWin.reloadConfig

   # kbuildsycoca4 2>/dev/null && kquitapp plasma-desktop 2>/dev/null ; kstart plasma-desktop > /dev/null 2>&1

 }

 function xfce_wallpaper {

   xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s $PICTURES_DIR/${TODAY}_ngeo.jpg

 }

 function lxde_wallpaper {

   pcmanfm -w "$PICTURES_DIR/${TODAY}_ngeo.jpg"

 }

 function mate_wallpaper {

   gsettings set org.mate.background picture-filename $PICTURES_DIR/${TODAY}_ngeo.jpg

 }

 function e17_wallpaper {

   OUTPUT_DIR=~/.e/e/backgrounds

   FileName=$PICTURES_DIR/${TODAY}_ngeo.jpg

   edcFile=~/tmp/NatGeo.edc



   echo 'images { image: "'$FileName'" LOSSY 90; }' > $edcFile

   echo 'collections {' >> $edcFile

   echo 'group { name: "e/desktop/background";' >> $edcFile

   echo 'data { item: "style" "4"; }' >> $edcFile

   echo 'data.item: "noanimation" "1";' >> $edcFile

   echo 'max: 990 742;' >> $edcFile

   echo 'parts {' >> $edcFile

   echo 'part { name: "bg"; mouse_events: 0;' >> $edcFile

   echo 'description { state: "default" 0.0;' >> $edcFile

   echo 'aspect: 1.334231806 1.334231806; aspect_preference: NONE;' >> $edcFile

   echo 'image { normal: "'$FileName'";  scale_hint: STATIC; }' >> $edcFile

   echo '} } } } }' >> $edcFile

   edje_cc -nothreads ~/tmp/NatGeo.edc -o $OUTPUT_DIR/NatGeo.edj

   sleep 2 && rm -f ~/tmp/NatGeo.edc

   echo 'Enlightenment e17 NatGeo.edj file created'

   enlightenment_remote -desktop-bg-del 0 0 -1 -1

   enlightenment_remote -desktop-bg-add 0 0 -1 -1 $OUTPUT_DIR/NatGeo.edj;

 }

 function usage {

   printf "%s\n%s\n\n%s\n%s\n\n%s\n\n%s" \

   "NatGeo-POD will download the National Geographic Picture Of The Day,"\

   "and (optionally) set that picture as the new wallpaper."\

   "Written and drawn from several sources by Paul Arnote for PCLinuxOS."\

   "Originally published in The PCLinuxOS Magazine (http://pclosmag.com), Sept. 2013 issue."\

   "Works for KDE4, Xfce, LXDE, Mate and e17 desktops."\

   "Usage: $0 [arguments]"\



   printf "\n %s\t%s" \

   "-h, --help" "This help text"

   printf "\n %s\t\t%s" \

   "-d" "Download pictures ONLY"

   printf "\n %s\t\tSetup for the %s" \

   "--xfce"    "XFCE4 Desktop"\

   "--mate"    "Mate Desktop"\

   "--lxde"    "LXDE Desktop"\

   "--kde4"    "KDE4 Desktop"\

   "--e17"    "Enlightenment Desktop"

   printf "\n"

 }

 # ********************************  

 # *** MAIN  

 # ********************************  

 if [ "$1" == "--help" ] || [ "$1" == "-h" ] || [ "$1" == "" ]; then

   usage

   exit

 fi

 echo "===================="  

 echo "== NGEO Wallpaper =="  

 echo "===================="  

 # Set date  

 TODAY=$(date +'%Y%m%d')  

 # If we don't have the image already today  

 if [ ! -e $PICTURES_DIR/${TODAY}_ngeo.jpg ]; then  

   echo "We don't have the picture saved, save it"  

   get_page  

   # Got the link to the image  

   PICURL=`/bin/cat /tmp/pic_url`

   PICURL2=`/bin/cat /tmp/pic_url2`

   echo "Picture URL is: ${PICURL}"

   echo "Picture URL 2 is: ${PICURL2}"  

   echo "Downloading images"

   wget --quiet $PICURL -O $PICTURES_DIR/${TODAY}_ngeo.jpg

   wget --quiet $PICURL2 -O $PICTURES_DIR/${TODAY}-1600x1200_ngeo.jpg

   if [ "$1" != "-d" ]; then

     echo "Setting image as wallpaper"

   fi

   # Uncomment (remove the #) in front of the appropriate command for your particular desktop environment

   # For Xfce

   if [ "$1" == "--xfce" ]; then

     xfce_wallpaper

   fi

   # For LXDE

   if [ "$1" == "--lxde" ]; then

     lxde_wallpaper

   fi

   # For Mate

   if [ "$1" == "--mate" ]; then

     mate_wallpaper

   fi

   # For KDE4

   if [ "$1" == "--kde4" ]; then

     kde_wallpaper

   fi

   # For e17

   if [ "$1" == "--e17" ]; then

     e17_wallpaper

   fi

   #

 # Else if we have it already, check if it's the most updated copy  

 else  

   get_page  

   # Got the link to the image  

   PICURL=`/bin/cat /tmp/pic_url`

   PICURL2=`/bin/cat /tmp/pic_url2`

   echo "Picture URL is: ${PICURL}"

   echo "Picture URL 2 is: ${PICURL2}"

   # Get the filesize  

   SITEFILESIZE=$(wget --spider $PICURL 2>&1 | grep Length | awk '{print $2}')  

   FILEFILESIZE=$(stat -c %s $PICTURES_DIR/${TODAY}_ngeo.jpg)  

   # If the picture has been updated  

   if [ $SITEFILESIZE != $FILEFILESIZE ]; then  

     echo "The pictures have been updated ... getting updated copy"  

     rm $PICTURES_DIR/${TODAY}_ngeo.jpg

     rm $PICTURES_DIR/${TODAY}-1600x1200_ngeo.jpg

     # Got the link to the image  

     PICURL=`/bin/cat /tmp/pic_url`

     PICURL2=`/bin/cat /tmp/pic_url2`

     echo "Downloading images"

     wget --quiet $PICURL -O $PICTURES_DIR/${TODAY}_ngeo.jpg

     wget --quiet $PICURL2 -O $PICTURES_DIR/${TODAY}-1600x1200_ngeo.jpg      

   if [ "$1" != "-d" ]; then

     echo "Setting image as wallpaper"

   fi

   # Uncomment (remove the #) in front of the appropriate command for your particular desktop environment

   # For Xfce

   if [ "$1" == "--xfce" ]; then

     xfce_wallpaper

   fi

   # For LXDE

   if [ "$1" == "--lxde" ]; then

     lxde_wallpaper

   fi

   # For Mate

   if [ "$1" == "--mate" ]; then

     mate_wallpaper

   fi

   # For KDE4

   if [ "$1" == "--kde4" ]; then

     kde_wallpaper

   fi

   # For e17

   if [ "$1" == "--e17" ]; then

     e17_wallpaper

   fi

   #

   # If the picture is the same  

   else  

     echo "Picture is the same, finishing up"  

   if [ "$1" != "-d" ]; then

     echo "Setting image as wallpaper"

   fi

   # For Xfce

   if [ "$1" == "--xfce" ]; then

     xfce_wallpaper

   fi

   # For LXDE

   if [ "$1" == "--lxde" ]; then

     lxde_wallpaper

   fi

   if [ "$1" == "--mate" ]; then

     mate_wallpaper

   fi

   # For KDE4

   if [ "$1" == "--kde4" ]; then

     kde_wallpaper

   fi

   # For e17

   if [ "$1" == "--e17" ]; then

     e17_wallpaper

   fi

   #

   fi  

 fi  

 clean_up


Modifications

My modifications include testing to see if the specified directory to store the images in exists or not. If not, the directory is created. For my personal use, I tend to keep all of my wallpaper images in their own separate directory in my /home directory, called Wallpaper (if you can imagine that). Because this directory will have a tendency to fill up rather quickly, I've set the script to save all of the images it downloads to the ~/Wallpaper/NatGeo directory. In the script above, the directory will be created if it doesn't already exist. You can change it to whatever location best suits your habits and needs.

I also commented out the GET_DESCRIPTION variable definition. I can't see where this is used anywhere in the script, so I don't see any reason to have an extra variable defined that isn't used.

There are two different sizes of the image available. One is the default size displayed on the website, while the other one is a 1600 x 1200 pixel sized version. I've modified the script to download both image sizes.

Had I been able to find a reliable way to determine the currently running desktop, it would be a relatively simple task to add a conditional test (or tests) to issue the correct command to set the desktop wallpaper for that particular desktop environment. As it is now, the script contains the commands to set the desktop wallpaper for the Xfce, LXDE, e17, Gnome and KDE4 desktop environments. Instead of detecting which desktop is currently running, you will specify the currently running desktop as a command switch when launching the script (see the "Usage" section of the article, below). The command switch tells the script exactly how to manipulate the graphics used for the desktop wallpaper.

Also, in the commands I used in the script to automatically change the desktop wallpaper, I've defaulted to using the smaller, lower resolution images. If you prefer to use the larger, higher resolution images, simply add -1600x1200 between the ${TODAY} and _ngeo.jpg part of the filename. Just keep in mind that there is not always a 1600x1200 image available every day. Using the 1600x1200 image as your automatic wallpaper may result in no background wallpaper image at all being displayed on the days that a 1600x1200 image is not available. As an added bonus, the smaller, lower resolution image is free of the National Geographic watermark branding, while the 1600x1200 image always displays the watermark.


Notes About The Desktops

I have tested this script in all five of the "major" desktop environments available under PCLinuxOS. I've tested it on bare metal installations of Xfce and KDE4, and in VirtualBox installations of LXDE, Mate and e17. It has worked flawlessly for me on all five desktop environments.

The hardest desktop environment to get to displaying the wallpaper was e17. Because e17 uses a very unique approach to displaying wallpaper, it wasn't a simple matter of just displaying a JPG or PNG file, as you do with most other desktop environments. After downloading the image(s), you then have to create a special template file (*.edc), and then compile that template file into a special *.edj file, and then set that file as the desktop wallpaper. Important Notice to e17 users: make sure that the DBus Extensions module (Settings > Module > System) is loaded, or otherwise, the script will not work to change the wallpaper.

I tip my hat to the KDE4 developers. They have succeeded in making it ridiculously and exceptionally difficult to change the KDE4 wallpaper from the command line. So much for simplicity. With KDE4, you have to create a Javascript script on the fly to write out the values for the KDE4 desktop wallpaper, and then clumsily execute that script. The process isn't pretty, but it does work. To pull this off for KDE4, you will need to install xdotool from Synaptic.

If you are running KDE on a computer with limited resources or a slower CPU, you may need to uncomment the sleep 2 command in the kde_wallpaper function to create a two second pause. The script runs fine without it on my Intel Duo-Core 2.2 GHz laptop that runs KDE4.

You will also notice that I've purposely left the line that starts with kbuildsycoca4 commented out. Under older versions of KDE4, this line appeared to be necessary. However, under my fully updated KDE 4.10, I've not found this line to be necessary. This line stops, then restarts, the KDE4 Plasma Desktop, in order to send the message to redraw the wallpaper. If you find that you are having difficulty getting the wallpapers to redraw on the screen, you might try uncommenting this line. I doubt, though, that you will need it. To say the least, this is a very clumsy and kludgy workaround to something that should be simple.

The Mate, LXDE and Xfce desktop environments were the "easy" ones to set the wallpaper from a command line or bash script. The developers of these three desktop environments had the foresight to include relatively easy, straightforward methods to manipulate the desktop wallpaper from a text-based interface, such as the command line or a bash script.


Usage

Like most command line utilities, the NatGeo-POD script has some command line options. To start with, entering NatGeo-POD.sh, NatGeo-POD.sh -h or NatGeo-POD.sh --help on the command line will display a brief set of help and usage data.



If you don't want the script to automatically change your wallpaper, simply use the -d command line switch. The script will still download the NatGeo POTD, but skip trying to set the desktop wallpaper.



When starting the script, you will do so using a command line switch to specify which desktop environment. The choices are --kde4, --xfce, --lxde, and --mate. In the previous image, you can see the script output when I ran it on one of my Xfce installations.



If you run the script more than once in a day, it will check to see if you have the most current version of the images. If you do, the script will skip downloading them again, and will set the images as the desktop wallpaper.


So ... What Do You Get?

Well, like I mentioned above, you will get two different sizes of the National Geographic Picture Of The Day. Below is an example.



To make use of this script, you could simply run it every day when you're at your computer. Even better yet, you could set this script to run automatically, via crontab, at the same time, every day. That way, you'll be sure to never miss out on any of the pictures -- provided you leave your computer on all the time (like I do). By running the script by way of a crontab task, you'll have new wallpaper images delivered straight to your computer, daily. Now, how awesome is that?



Previous Page              Top              Next Page
Copyright (c) 2013, The PCLinuxOS Magazine. All Rights Reserved.