Ways4eu WordPress.com Blog

SPA View of ways4eu.wordpress.com

How to Automatically Set NASA’s Picture of the Day as Your Windows Background

By iftttauthorways4eu

on Sat Feb 11 2023

Here’s a Python script that downloads the NASA Picture of the Day and sets it as the background image in Windows:

Read more: How to Automatically Set NASA’s Picture of the Day as Your Windows Background

Sourcecode

import requests

import ctypes

import json

# URL for NASA's Picture of the Day API

# for test purpose use DEMO_KEY as api_key

# url = "https://api.nasa.gov/planetary/apod?api_key=YOUR_API_KEY"

url = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"

# windows full file path

# adjust this to your environment

WIN_FILEPATH = "D:\\scripts\\win\\setBackgroundPicNASA\\bg_wallpaper.jpg"

# Request the data from the API

response = requests.get(url)

data = json.loads(response.text)

# Get the URL of the image

img_url = data["hdurl"]

# Download the image

response = requests.get(img_url)

open(WIN_FILEPATH, "wb").write(response.content)

# Set the image as the background

SPI_SETDESKWALLPAPER = 20

ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, WIN_FILEPATH, 0)

Note that you will need to replace “YOUR_API_KEY” in the URL with your own NASA API key, which you can obtain from the NASA API website (https://api.nasa.gov/).

The script first makes a request to the NASA API to retrieve the data for the Picture of the Day. It then parses the data and extracts the URL of the image. The image is then downloaded and saved as image-file. Finally, the script sets the image as the Windows desktop background using the ctypes library.

Usage

You can use the Windows Task Scheduler to run the script at a specific time. Here are the steps to do that:
Open the Windows Task Scheduler by typing “Task Scheduler” in the Start menu search bar and clicking on the “Task Scheduler” app.
In the Task Scheduler, click on “Create Basic Task…” in the Actions panel on the right.
In the “Create Basic Task Wizard”, give a name to the task and a description (optional), then click on “Next”.
Choose when you want the task to run (e.g., daily, weekly, etc.), then click on “Next”.
In the next step, choose the start time and date for the task, then click on “Next”.
In the “Action” step, choose “Start a program”, then click on “Next”.
In the “Program/script” field, enter the path to the Python executable (e.g., C:\Python3x\python.exe).
In the “Add arguments (optional)” field, enter the path to your script (e.g., C:\scripts\nasa_picture_of_the_day_background.py).
Click on “Finish” to create the task.
Now, the script will run automatically at the specified time using the Windows Task Scheduler. Note that you may need to configure the task scheduler to run with administrative privileges to allow it to change the desktop background.

Download

You can download the sourcecode and find more details on my github-repository:

https://github.com/smartDevel/set_nasa_pic_as_background

The post How to Automatically Set NASA’s Picture of the Day as Your Windows Background appeared first on Strictly Confidential.

source https://www.ways4.eu/blog/2023/02/11/how-to-automatically-set-nasas-picture-of-the-day-as-your-windows-background/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-automatically-set-nasas-picture-of-the-day-as-your-windows-background