Changing the Gnome 3 Desktop with Images from Bing


This weekend I decided to take on a little challenge…

Question: What’s the Challenge?

Answer: Have any of you on Windows 7 or WIndows 2008 R2 noticed that there’s a background theme configuration selection that allows you to see a collection of Bing images? If so you probably already know that these images are downloaded from a Bing RSS feed and every minute of so and displays as the current wallpaper.

Well I wanted to see if I could get the same effect of building such a quick app for my Gnome3 desktops I run. It turns out that there are many ways to accomplish this. I could have written an Python script, Perl Script, Bash Script, C++ application, or just downloaded one of the few wallpaper changer Linux applications such as Webilder.

So, I decided to put on my developer hat and write one using one of the most difficult languages for a Microsoft raised and branded developer, Bash Script. The first thing I needed to do was determine, if it was indeed possible to do this. I binged around and found this: “Customizing the GNOME Shell”. by fpmurphy. I must say the read was thorough and complete enough for me to figure out all I needed was one line of code to change the desktop settings:

   1:  

   2: gsettings set org.gnome.desktop.background picture-uri “file:///home/dngoins/WALLPAPER/bingImage.jpg”

Basically now all I needed to do was to get this “BingImage.jpg”. Well this was rather simple as well. All I needed to do was figure out what and where the Bing RSS Feed uri is, then parse the RSS Feed to get the image url’s and simply download them every few minutes or so.

Question: How did you determine the Bing RSS Feed Url?

Answer: I used a little tool called Fiddler2. Well some of you know I’m a .Net Developer by trade and skill set, but a Linux developer by night, so I use tools from every platform to get a job done, and for my purpose of sniffing out the BIng RSS feed Url, this worked. I started up an instance of my Windows 2008 R2 vmware image, and I started Fiddler2. I then changed my background to use the Bing RSS feed wallpapers and I noticed this url:

http://themeserver.microsoft.com/default.aspx?p=Bing&c=Desktop&m=en-US

This Url yields the Bing RSS Feed which contain links to the Bing Images. I also noticed that this list does not contain the  “Today” image on the bing search site. The Current Bing image for “Today” can be found here:

http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1

Question: After this what did you do?

Answer:  I parsed the RSS Feeds for the themes, and current today images, and simply downloaded and overwrote the “BingImage.jpg” file. You can see the BASH script here:

   1: #!/bin/bash

   2:  

   3: #Read more: http://blog.fpmurphy.com/2011/03/customizing-the-gnome-3-shell.html#ixzz1QdBESiym

   4:  

   5:  

   6: #change this to your user location

   7: cd /home/dngoins/WALLPAPER

   8:  

   9: #change this to set the time between background downloads in seconds

  10: changeBackgroundTime=300

  11:  

  12: #while [! ifconfig eth0 | grep "inet addr"] &;& [! ifconfig wlan0 | grep "init addr"  ] ; do

  13: #    sleep 1

  14: #done

  15:  

  16: #wait for the network to start...

  17: sleep 15

  18:  

  19:  

  20: #get the current Bing image on the search window

  21: #the current Search Bing Image can be found at HPImageArchive.aspx

  22: #extract the <;url/> content for the image link

  23: currentBingImage=$(curl "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1" |grep "<url>.*<.url>" |sed -e "s/^.*<url/<url/" | cut -f2 -d">"| cut -f1 -d"<")

  24:  

  25: #extract the <;copyright> information for the description of the current bing search image

  26: currentCopyright=$(curl "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1" |grep "<copyright>.*<.copyright>" |sed -e "s/^.*<copyright/<copyright/" | cut -f2 -d">"|cut -f1 -d"<")

  27:  

  28: #the InfoAboutBackground needs to have permissions so that this script can overwrite the contents

  29: echo $currentBingImage": "$currentCopyright >; /home/dngoins/Desktop/InfoAboutBackground

  30: #echo $currentBingImage

  31:  

  32: #set the whole currentImage path

  33: bingUrl="http://www.bing.com"

  34: currentImage=${bingUrl}${currentBingImage}

  35:  

  36: #download the current bing image and overwrite the bingImage.jpg file

  37: curl $currentImage -o bingImage.jpg

  38:  

  39: #use the gnome 3 settings utility: gsettings to change the background

  40: gsettings set org.gnome.desktop.background picture-uri "file:///home/dngoins/WALLPAPER/bingImage.jpg"

  41:  

  42: #get the rssFeed <;link ref="http://blah.com/image.jpg" /> link

  43: rssFeed=$(curl "http://themeserver.microsoft.com/default.aspx?p=Bing&c=Desktop&m=en-US"  |grep -o '<;link ref="[^"]*' | grep -o '[^"]*$' |sed -e "s/http:\/\/.*\.jpg/<url>&<\/url>,/" |sed -e "s/http:\/\/.*\.JPG/<url>&<\/url>,/" )

  44:  

  45: rssFeedParamountPictures=$(curl "http://themeserver.microsoft.com/default.aspx?p=Paramount&c=Dynamic&m=en-US"  |grep -o '<;link ref="[^"]*' | grep -o '[^"]*$' |sed -e "s/http:\/\/.*\.jpg/<url>&<\/url>,/" |sed -e "s/http:\/\/.*\.JPG/<url>&<\/url>,/" )

  46:  

  47: rssFeedAerial=$(curl "http://themeserver.microsoft.com/default.aspx?p=Bing&c=Aerial&m=en-US"  |grep -o '<;link ref="[^"]*' | grep -o '[^"]*$' |sed -e "s/http:\/\/.*\.jpg/<url>&<\/url>,/" |sed -e "s/http:\/\/.*\.JPG/<url>&<\/url>,/" )

  48:  

  49: #TODO: get the rssFeed desciption for each url - 

  50: #rssFeedAbout=$(curl "http://themeserver.microsoft.com/default.aspx?p=Bing&c=Desktop&m=en-US" |grep "<title>.*<.title>" |sed -e "s/<title>.*<.title>/&,/" )

  51:  

  52:  

  53: #remove all the spaces for the time being and replace with '~'

  54: removespaces=$(echo $rssFeed | tr ' ' '~')

  55: removespacesParamount=$(echo $rssFeedParamountPictures | tr ' ' '~')

  56: removespacesAerial=$(echo $rssFeedAerial | tr ' ' '~')

  57:  

  58: #create an array of <;url/>'s

  59: declare -a urlArray

  60: urlArray=(`echo $removespaces | tr ',' ' '`)

  61:  

  62: declare -a urlArrayParamount

  63: urlArrayParamount=(`echo $removespacesParamount | tr ',' ' '`)

  64:  

  65: declare -a urlArrayAerial

  66: urlArrayAerial=(`echo $removespacesAerial | tr ',' ' '`)

  67:  

  68:  

  69: #for each url in the array

  70: for url in ${urlArray[@]}; do

  71:  

  72:  

  73: #replace the '~' with url encoded %20 for spaces, and return just the actual url link: http://blah.com/image.jpg

  74: image=$(echo $url |grep "<url>.*<.url>" |sed -e "s/^.*<url/<url/" | cut -f2 -d">"| cut -f1 -d"<" | sed -e "s/~/%20/" | sed -e "s/~/%20/" )

  75:  

  76: #echo "Image: $image"

  77: #get the filename

  78: fileName=$(echo $image |sed -e "s/http:\/\/themeserver.microsoft.com\/themeserver\/\/Bing\/Desktop\/en-US\/Images\///" | tr '/' '_')

  79:  

  80: #echo "FileName = $fileName"

  81:  

  82: if [ -a /home/dngoins/WALLPAPER/$fileName ]; then

  83:  

  84:     echo "$fileName already found in directory, so skipping download"

  85: else

  86:     #download the image and save it to the WALLPAPER directory as [name of file].jpg

  87:     #for this to work you need to give permission to overwrite this file in the /home/user/WALLPAPER directory

  88:     curl `echo $image` -o $fileName

  89: #    echo "Doing Curl for $image and $fileName"

  90:  

  91: fi

  92:  

  93: #use the gnome 3 shell settings utility: gsettings to set the background image

  94: #gsettings set org.gnome.desktop.background picture-uri "file:///home/dngoins/WALLPAPER/"$fileName

  95:  

  96: #loop over and download the next image in Bing RSS Theme

  97: done

  98:  

  99: #wait for the allocated time to change to the Bing RSS Theme images

 100: sleep $changeBackgroundTime

 101:  

 102: length=$#

 103: random_num=$(( $RANDOM % ($length + 1) ))

 104:  

 105: if [ -a "/home/dngoins/WALLPAPER/${!random_num}" ] &;& [ "${!random_num}" != "currentWallpaper.jpg" ]; then 

 106:     cp "/home/dngoins/WALLPAPER/${!random_num}" ./currentWallpaper.jpg 

 107:     gsettings set org.gnome.desktop.background picture-uri "file:///home/dngoins/WALLPAPER/currentWallpaper.jpg"

 108: fi

 109:  

 110:  

 111: #for each url in the array

 112: for url in ${urlArrayParamount[@]}; do

 113:  

 114:  

 115: #replace the '~' with url encoded %20 for spaces, and return just the actual url link: http://blah.com/image.jpg

 116: image=$(echo $url |grep "<url>.*<.url>" |sed -e "s/^.*<url/<url/" | cut -f2 -d">"| cut -f1 -d"<" | sed -e "s/~/%20/" | sed -e "s/~/%20/" )

 117:  

 118: #echo "Image: $image"

 119: #get the filename

 120: fileName=$(echo $image |sed -e "s/http:\/\/themeserver.microsoft.com\/themeserver\/\/Paramount\/Dynamic\/en-US\/Images\///" | tr '/' '_')

 121:  

 122: #echo "FileName = $fileName"

 123:  

 124: if [ -a /home/dngoins/WALLPAPER/$fileName ]; then

 125:  

 126:     echo "$fileName already found in directory, so skipping download"

 127: else

 128:     #download the image and save it to the WALLPAPER directory as [name of file].jpg

 129:     #for this to work you need to give permission to overwrite this file in the /home/user/WALLPAPER directory

 130:     curl `echo $image` -o $fileName

 131: #    echo "Doing Curl for $image and $fileName"

 132:  

 133: fi

 134:  

 135: #use the gnome 3 shell settings utility: gsettings to set the background image

 136: #gsettings set org.gnome.desktop.background picture-uri "file:///home/dngoins/WALLPAPER/"$fileName

 137:  

 138: #loop over and download the next image in Bing RSS Theme

 139: done

 140:  

 141:  

 142: #wait for the allocated time to change to the Bing RSS Theme images

 143: sleep $changeBackgroundTime

 144:  

 145: length=$#

 146: random_num=$(( $RANDOM % ($length + 1) ))

 147:  

 148: if [ -a "/home/dngoins/WALLPAPER/${!random_num}" ] &;& [ "${!random_num}" != "currentWallpaper.jpg" ]; then 

 149:     cp "/home/dngoins/WALLPAPER/${!random_num}" ./currentWallpaper.jpg 

 150:     gsettings set org.gnome.desktop.background picture-uri "file:///home/dngoins/WALLPAPER/currentWallpaper.jpg"

 151: fi

 152:  

 153:  

 154: #for each url in the array

 155: for url in ${urlArrayAerial[@]}; do

 156:  

 157:  

 158: #replace the '~' with url encoded %20 for spaces, and return just the actual url link: http://blah.com/image.jpg

 159: image=$(echo $url |grep "<url>.*<.url>" |sed -e "s/^.*<url/<url/" | cut -f2 -d">"| cut -f1 -d"<" | sed -e "s/~/%20/" | sed -e "s/~/%20/" )

 160:  

 161: #echo "Image: $image"

 162: #get the filename

 163: fileName=$(echo $image |sed -e "s/http:\/\/themeserver.microsoft.com\/themeserver\/\/Bing\/Aerial\/en-US\/Images\///" | tr '/' '_')

 164:  

 165: #echo "FileName = $fileName"

 166:  

 167: if [ -a /home/dngoins/WALLPAPER/$fileName ]; then

 168:  

 169:     echo "$fileName already found in directory, so skipping download"

 170: else

 171:     #download the image and save it to the WALLPAPER directory as [name of file].jpg

 172:     #for this to work you need to give permission to overwrite this file in the /home/user/WALLPAPER directory

 173:     curl `echo $image` -o $fileName

 174: #    echo "Doing Curl for $image and $fileName"

 175:  

 176: fi

 177:  

 178: #use the gnome 3 shell settings utility: gsettings to set the background image

 179: #gsettings set org.gnome.desktop.background picture-uri "file:///home/dngoins/WALLPAPER/"$fileName

 180:  

 181: #loop over and download the next image in Bing RSS Theme

 182: done

 183:  

 184:  

 185:  

 186: #all images downloaded so now loop through images

 187: while [ 1 ]

 188: do

 189: set — *

 190: length=$#

 191:  

 192: random_num=$(( $RANDOM % ($length + 1) ))

 193:  

 194: #echo "file:///home/dngoins/WALLPAPER/${!random_num}"

 195: if [ -a "/home/dngoins/WALLPAPER/${!random_num}" ] &;& [ "${!random_num}" != "currentWallpaper.jpg" ]; then 

 196:     cp "/home/dngoins/WALLPAPER/${!random_num}" ./currentWallpaper.jpg 

 197:     gsettings set org.gnome.desktop.background picture-uri "file:///home/dngoins/WALLPAPER/currentWallpaper.jpg"

 198: fi

 199:  

 200: sleep $changeBackgroundTime

 201:  

 202: done

 203:  

 204: exit

Results:

BingRssGnomeWallpaper

Notice in the image above I have the WIndows Server 2008 R2 image with Bing as the wallpaper, along with the Gnome 3 desktop wallpaper on the Linux host.

4 thoughts on “Changing the Gnome 3 Desktop with Images from Bing

  1. Thanks a ton!! Was looking for this. Actually, thinking of doing it but you saved me a lot of effort.. Grateful!

    Like

  2. I simplified your script and left only the essential bits:

    #!/bin/bash
    # Read more
    # http://blog.fpmurphy.com/2011/03/customizing-the-gnome-3-shell.html#ixzz1QdBESiym

    # Stop if any of the steps fail
    set -e

    # Set some variables to use later
    bingUrl=”http://www.bing.com”
    pictureDir=$HOME/wallpapers

    # get the current Bing Image from HPImageArchive.aspx
    #extract the for the image link
    currentBingImage=$(curl -sS “http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1” |sed -e “s|.*\(.*\).*|$bingUrl\1|”)

    #echo $currentBingImage

    #download the current bing image and overwrite the bingImage.jpg file
    curl -sS –create-dirs $currentBingImage -o $pictureDir/bingImage.jpg

    # gsettings to change the background
    gsettings set org.gnome.desktop.background picture-uri “file:///$pictureDir/bingImage.jpg”

    exit 0

    Like

Leave a comment