DEV Community

Cover image for Setting bing image of the day as desktop wallpaper using Python script
Anurag Rana
Anurag Rana

Posted on

Setting bing image of the day as desktop wallpaper using Python script

Originally published at pythoncircle.com

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 which bing itself uses to fetch the image details using AJAX. We will send 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 returned 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 on Github.

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

Originally published at pythoncircle.com

Top comments (0)