Bing wallpapers on Linux

I would like to share my small script, which allows me to pull daily new wallpaper and lock screen image from the Bing service. This functionality exists in the Windows 10 and I wanted to have similar thing on Linux.

Before we go into the script itself, I want to expain you my setup:

  • Manjaro Linux.
  • Xmonad window manager.
  • nitrogen for the setting of the wallpaper.
  • betterlockscreen (based on the i3lock) for setting my lock screen image and lock screen itself.

I am using Bing wallpapers for my lock screen only, but you can adopt this script to your desktop wallpapers.

Prerequisites

In order to make this script working, following tools shall be installed: curl, jq, imagemagick (we need convert from it) and notify-send.

  • curl is needed for requesting the data about current Bing picture.
  • jq is needed to extract necessary JSON fields.
  • convert is needed to put copyright information on the downloaded picture (its name, author’s name). This step is optional.
  • notify-send is used to send notification to the system (which will pop up from the system tray) that wallpaper or lock screen image has been changed. This step is also optional.

The Bing API

Thanks to Microsoft, there is a standard API request to the Bing server, which gives away the JSON data containing necessary information: https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=de-DE. You can see here following parameters:

  • format - the format in which answer is given. We use JSON - js.
  • idx - This parameter I don’t know. It shall be 0.
  • n - Number of pictures I request information about.
    • 0 means request only today’s image.
    • 1 means today’s and yesterday’s images.
    • … and so on.
  • mkt - the country for which you are requesting the image. Bing displays different images for different countries. You can either use country where you live (I use Germany) or select country you like.

The script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/sh

# It is needed to make `betterlockscreen` working
export DISPLAY=":0"

# My folder for wallpapers and lockscreen images
WP_DIR=$HOME/wallpapers/lockscreen

# I put BING API url in the separate variable.
# It is more convenient, because if API format will change in the future, 
# then I need to update only this parameter.
BING_URL="https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=de-DE"

# In order not to request JSON data for every value I want to extract
# I put JSON answer into the variable.
JSON_DATA=$(curl -s "${BING_URL}" | jq)

# Using saved JSON answer extract information about image and remove quote signs.
PIC_CPRT=$(echo "${JSON_DATA}" | jq ".images[0].copyright" | sed 's/"//g')

# Copyright information about previous picture is saved in the
# $HOME/wallpapers/lockscreen/bing_wallpaper_copyright.txt 
# I extract it for comparison only.
OLD_PIC_CPRT=$(cat $HOME/wallpapers/lockscreen/bing_wallpaper_copyright.txt)

# Here I compare received copyright data and saved one (to check if picture has been changed).
# And only if picture has been changed (and its information as well), then I trigger the lock screen image update.
if [ "${PIC_CPRT}" != "${OLD_PIC_CPRT}" ]
then

# write new copyright information
    echo "${PIC_CPRT}" > $HOME/wallpapers/lockscreen/bing_wallpaper_copyright.txt

# Extract slug for download (the part after bing.com/)
    PIC_LOC=$(echo "${JSON_DATA}" | jq ".images[0].url" | sed 's/"//g' | cut -d'&' -f1)

# Create the request URL
    URL="http://bing.com${PIC_LOC}"

# remove old lock screen pictures (original and with annotation)
    [ -f $HOME/wallpapers/lockscreen/bing_wallpaper_orig.jpg ] && rm $HOME/wallpapers/lockscreen/bing_wallpaper_orig.jpg
    [ -f $HOME/wallpapers/lockscreen/bing_wallpaper.jpg ] && rm $HOME/wallpapers/lockscreen/bing_wallpaper.jpg

# download new image
    curl -Lso "${WP_DIR}"/bing_wallpaper_orig.jpg "${URL}"

# put copyright information on the image
    convert -font arial -fill white -pointsize 14 -undercolor '#00000080' -gravity north-east -annotate +10+10 ${PIC_CPRT} " "${WP_DIR}"/bing_wallpaper_orig.jpg "${WP_DIR}"/bing_wallpaper.jpg

# apply new wallpaper to betterlockscreen
    betterlockscreen -u "${WP_DIR}"/bing_wallpaper.jpg

# send notification
    notify-send -t 5000 "Lock screen:" "Picture has been updated."
fi

Here is how lock screen image (or wallpaper) looks with the copyright annotation.

Bing wallpaper with annotation

Bing wallpaper with annotation

Cron job

In order to have regular update of the picture, you just need to create the cron job for this script. I put my script in the home directory for scripts: ~/.scripts/lockscreenpicture. Because I don’t know when my laptop will be open or when it will be in sleep, I made a cron job which checks picture for update every 30 min from 7:00 till 19:00.

1
2
# {min} {hour} {day of month} {moth} {day of week}  command to be executed
0,30 7-19 * * * ~/.scripts/lockscreenpicture

I hope my small script is going to be helpful for someone. 😊