Last active
July 4, 2020 21:52
-
-
Save glof2/b490f5df1961d5d471271ae930aee316 to your computer and use it in GitHub Desktop.
Sets your wallpaper to NASA's picture of the day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
from os import path, getcwd | |
import ctypes | |
import time | |
def windowswallpaper(image): | |
SPI_SETDESKWALLPAPER = 20 | |
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image, 0) | |
def getimage(url): | |
#Get the data from the website | |
page = requests.get(url) | |
soup = BeautifulSoup(page.content, "html.parser") | |
date = soup.findAll("p")[1].text.strip() | |
image = soup.find("img") | |
url += image["src"] | |
page = requests.get(url) | |
filename = f"image {date}.jpg" | |
# Create the .jpg file | |
with open(filename, "wb") as f: | |
f.write(page.content) | |
#Set the wallpaper to the .jpg file | |
windowswallpaper(path.abspath(filename)) | |
url = "https://apod.nasa.gov/" | |
getimage(url) | |
#Change the extension from .pyw to .py if you want the console window | |
# | |
#If you want the program to be run at startup go to: | |
#C:\Users\current_user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup | |
#And drag the file .pyw/.py file in there | |
# | |
#If you want the program to be ran between some time periods you should use Windows Task Scheduler | |
#If you do not know how to check out http://theautomatic.net/2017/10/03/running-python-task-scheduler/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this is great!
If anyone has issues running on Windows, this may help:
pip3 install requests
pip3 install beautifulsoup4