Register
It is currently Thu Oct 23, 2014 2:36 pm

Script to download and set daily Hi-Res Bing wallpaper


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun Oct 21, 2012 1:52 pm   

Joined: Sun Oct 21, 2012 1:38 pm
Posts: 1
I wrote this script because I like the daily Bing wallpapers and didn't think it right that Windows 7 & 8 users should have all the fun. This script does not download from an RSS file of collected Bing wallpapers like others, but rather allows the user, by setting variables in the script to choose the Bing market, image resolution, and save directory they wish to use. After the daily Bing wallpaper is downloaded, the script sets the GNOME3 desktop wallpaper.

Copy the code into your favorite text editor and save it as some-file.sh Be sure to make the file executable with chmod +x so that you can run it from a cron job.

*NOTE* I am very new to bash scripting and would very much appreciate any advice on how to improve and tighten my code!

Code:
#!/bin/bash

# $bing is needed to form the fully qualified URL for
# the Bing pic of the day
bing="www.bing.com"

# $xmlURL is needed to get the xml data from which
# the relative URL for the Bing pic of the day is extracted
#
# The mkt parameter determines which Bing market you would like to
# obtain your images from.
# Valid values are: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ, en-CA.
#
# The idx parameter determines where to start from. 0 is the current day,
# 1 the previous day, etc.
xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US"

# $saveDir is used to set the location where Bing pics of the day
# are stored.  $HOME holds the path of the current user's home directory
saveDir=$HOME'/Pictures/BingPics/'

# Create saveDir if it does not already exist
mkdir -p $saveDir

# Set picture options
# Valid options are: none,wallpaper,centered,scaled,stretched,zoom,spanned
picOpts="zoom"

# The desired Bing picture resolution to download
# Valid options: "_1024x768" "_1280x720" "_1366x768" "_1920x1200"
picRes="_1920x1200"

# The file extension for the Bing pic
picExt=".jpg"

# Extract the relative URL of the Bing pic of the day from
# the XML data retrieved from xmlURL, form the fully qualified
# URL for the pic of the day, and store it in $picURL
picURL=$bing$(echo $(curl -s $xmlURL) | grep -oP "<urlBase>(.*)</urlBase>" | cut -d ">" -f 2 | cut -d "<" -f 1)$picRes$picExt

# $picName contains the filename of the Bing pic of the day
picName=${picURL#*2f}

# Download the Bing pic of the day
curl -s -o $saveDir$picName $picURL

# Set the GNOME3 wallpaper
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$saveDir$picName'"'

# Set the GNOME3 wallpaper picture options
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-options $picOpts

# Exit the script
exit


Top
 Profile  
 PostPosted: Tue Nov 05, 2013 4:41 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Looks good to me.

----
Code:
# Create saveDir if it does not already exist
mkdir -p $saveDir

I would check whether the directory exists before creating it.

Don't forget to quote the parameter expansion.
You don't know what crap the user can enter as saveDir's value.

----
Code:
# Extract the relative URL of the Bing pic of the day from
# the XML data retrieved from xmlURL, form the fully qualified
# URL for the pic of the day, and store it in $picURL
picURL=$bing$(echo $(curl -s $xmlURL) | grep -oP "<urlBase>(.*)</urlBase>" | cut -d ">" -f 2 | cut -d "<" -f 1)$picRes$picExt

You can change the above to:
Code:
picBase=$(curl -s "$xmlURL" | awk -F '</?urlBase>' '{ print $2 }')
picURL=$bing$picBase$picRes$picExt

----
Code:
# $picName contains the filename of the Bing pic of the day
picName=${picURL#*2f}

You probably want the following to get the filename in any case:
Code:
picName=${picURL##*/}

----
Code:
# Download the Bing pic of the day
curl -s -o $saveDir$picName $picURL

# Set the GNOME3 wallpaper
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$saveDir$picName'"'

# Set the GNOME3 wallpaper picture options
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-options $picOpts

Put quotes around mutable parameter expansions set by the user.

Also, you can write the file:// part this way:
Code:
"file://$saveDir$picName"

----

Instead of asking the user to type a resolution of the form « _XxY »,
why not let him write "XxY" and add the underscore later on?

Same remark for the filename extension.

picURL's definition becomes:
Code:
picURL=${bing}${picBase}_${picRes}.${picExt}


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP