Skip to content

Instantly share code, notes, and snippets.

@fernandotakai
Created January 29, 2021 13:07
Show Gist options
  • Save fernandotakai/7e2cf778c612deef6a37426953f792f9 to your computer and use it in GitHub Desktop.
Save fernandotakai/7e2cf778c612deef6a37426953f792f9 to your computer and use it in GitHub Desktop.
import os
import httpx
import subprocess
from bs4 import BeautifulSoup
BING_BASE = 'https://bing.com'
BING_WALLPAPER_ENDPOINT = 'http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US'
WALLPAPER_HOME = os.path.expanduser('~/bing_wallpapers')
response = httpx.get(BING_WALLPAPER_ENDPOINT)
soup = BeautifulSoup(response.text, 'html.parser')
url = soup.find('url').text
date = soup.find('startdate').text
copyright = soup.find('copyright').text
path = f'{WALLPAPER_HOME}/{date}.jpg'
if not os.path.exists(path):
print(f'Downloading wallpaper "{copyright}" to {path}')
wallpaper = httpx.get(f'{BING_BASE}{url}')
with open(path, 'wb') as f:
f.write(wallpaper.content)
print('Done!')
print('Setting wallpaper..')
subprocess.call(['feh', '--bg-fill', path])
print('Done!')
else:
print(f'Wallpaper "{copyright}" already exists, skipping')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment