Bash script to use Bing’s background as wallpaper

I’ve been using a static wallpaper on my Linux machine, but using Bing’s desktop application on my Windows machine to change the wallpaper. The application sets the wallpaper to the current Bing background and changes it on a daily basis. Since there is no equivalent application for Linux, I wrote a small shell script to download and set the picture as a wallpaper. The script is over at GitHub, but also pasted below (check GitHub first). It requires wget and feh.

#!/bin/bash
#################################################################
#Ver.  Date        Name        Description                      #
#v0.1  2014-12-07  Marchione   Initial script                   #
#                                                               #
#                                                               #
#################################################################

#################################################################
# Check required programs are installed 
#################################################################
if which wget > /dev/null;
 then echo "wget Installed"
 else echo "wget Not Installed"
 exit
fi

if which feh > /dev/null;
 then echo "feh Installed"
 else echo "feh Not Installed"
 exit
fi

#################################################################
# Set some variables
#################################################################
directory=~/wallpapers/bing/
mkdir $directory

#Base Bing URL
bing="www.bing.com"

#What day to start from. 0 is the current day,1 the previous day, etc...
day="&idx=0"

#Number of images to get
#May change this script later to get more images and rotate between them
number="&n=1"

#Set market, other options are:
#"&mkt=zh-CN"
#"&mkt=ja-JP"
#"&mkt=en-AU"
#"&mkt=en-UK"
#"&mkt=de-DE"
#"&mkt=en-NZ"
#"&mkt=en-CA"
market="&mkt=en-US"

xmlURL=$bing"/HPImageArchive.aspx?format=xml"$day$number$market

#Set resolution, other options are:
#"_1024x768"
#"_1280x720"
#"_1366x768"
#"_1920x1200"
resolution="_1920x1080"

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

#The URL for the desired pic resolution
pic_desired=$bing$(echo $(wget -q -O - $xmlURL) | grep -oP "<urlBase>(.*)</urlBase>" | cut -d ">" -f 2 | cut -d "<" -f 1)$resolution$extension

#Form the URL for the default pic resolution
pic_default=$bing$(echo $(wget -q -O - $xmlURL) | grep -oP "<url>(.*)</url>" | cut -d ">" -f 2 | cut -d "<" -f 1)

#Set options in feh, other options are:
#--bg-tile
#--bg-center
#--bg-max
#--bg-fill
options="--bg-scale"

#################################################################
# Download the desired image resolution
# If it doesn't exist then download the default image resolution
#################################################################
if wget -q --spider "$pic_desired"
then
    pic_name=${pic_desired##*/}
    wget -q -O $directory$pic_name $pic_desired
else
    pic_name=${pic_default##*/}
    wget -q -O $directory$pic_name $pic_default
fi

#################################################################
# Set wallpaper in feh
#################################################################
feh $options $directory$pic_name
exit

 

This script could run out of cron, but I’m running it out of my ~/.config/openbox/autostart file.

##Autostart File ##

##Background
(sleep 2s && ~/wallpapers/wallscript.sh) &

##tint2 panel
(sleep 1s && tint2) &

##Sound Icon
(sleep 1s && volumeicon) &

##Conky
(sleep 3s && conky) &

##Clipboard
(sleep 1s && parcellite) &

 

Check it out!

-Logan

6 thoughts on “Bash script to use Bing’s background as wallpaper”

  1. Thanks! I have added the output of gcalcli to get my calendar added to this:

    gcalcli –cal “calname” –nocolor –width 12 calw 4 > ~/cal.txt
    convert $directory$pic_name -resize 1080x -size 1000×800 -background transparent -fill blue -pointsize 10 -font Liberation-Mono-Bold -stroke ‘#000C’ -strokewidth 2 -gravity SouthEast label:@/home/user/cal.txt -size 1000×800 -stroke none -fill white label:@/home/user/cal.txt -flatten ~/wallpaper.jpg

  2. When I run it on my system (fedora 21) I get this errors:
    bing.sh: line 90: let: number = 21958 % 0: division by 0 (error token is “0”)
    bing.sh: line 92: &n=1: syntax error: operand expected (error token is “&n=1”)

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.