Python script to set bing image of the day as desktop wallpaper

This article was originally published on https://pythoncircle.com/post/773/how-to-validate-an-uploaded-image-in-python-django/

Using this python script, we will try to download the bing image of the day and set it as desktop wallpaper.

There is a URL that bing itself uses to fetch the image details using AJAX. We will send a GET request to this URL to fetch image details.

# get image url
response = requests.get("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")
image_data = json.loads(response.text)

Response return will be in below format.

{
    "images": [{
        "startdate": "20190823",
        "fullstartdate": "201908230700",
        "enddate": "20190824",
        "url": "/th?id=OHR.FarmlandLandscape_EN-US6661316442_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
        "urlbase": "/th?id=OHR.FarmlandLandscape_EN-US6661316442",
        "copyright": "Farmland in Washington state's Palouse region (© Art Wolfe/Getty Images)",
        "copyrightlink": "https://www.bing.com/search?q=palouse+region&form=hpcapt&filters=HpDate%3a%2220190823_0700%22",
        "title": "Harvest time in the Palouse",
        "quiz": "/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20190823_FarmlandLandscape%22&FORM=HPQUIZ",
        "wp": true,
        "hsh": "44238c657b35ce3c11fbf3f59881474e",
        "drk": 1,
        "top": 1,
        "bot": 1,
        "hs": []
    }],
    "tooltips": {
        "loading": "Loading...",
        "previous": "Previous image",
        "next": "Next image",
        "walle": "This image is not available to download as wallpaper.",
        "walls": "Download this image. Use of this image is restricted to wallpaper only."
    }
}

Get the image URL from the response JSON and download it.

# download and save image
img_data = requests.get(full_image_url).content
with open(image_name, 'wb') as handler:
    handler.write(img_data)

The image will be saved in the present working directory. Set this image as desktop background using below command.

`which gsettings` set org.gnome.desktop.background picture-uri file:$PWD/imagename

This command will work for Ubuntu. For other operating systems, please update accordingly.

The script is available at Github.

You can automate the process by scheduling this script to run on system startup.

Here is the complete code.

import datetime
import json
import os

import requests

# get image url
response = requests.get("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")
image_data = json.loads(response.text)

image_url = image_data["images"][0]["url"]
image_url = image_url.split("&")[0]
full_image_url = "https://www.bing.com" + image_url

# image's name
image_name = datetime.date.today().strftime("%Y%m%d")
image_extension = image_url.split(".")[-1]
image_name = image_name + "." + image_extension

# download and save image
img_data = requests.get(full_image_url).content
with open(image_name, 'wb') as handler:
    handler.write(img_data)

# ubuntu command to set wallpaper
os.system("`which gsettings` set org.gnome.desktop.background picture-uri file:$PWD/" + image_name)

Host your Django Application for free on  PythonAnyWhere.
If you want complete control of your application and server, you should consider 
DigitalOcean. Create an account with  this link  and get $200 credits.