Created
September 6, 2022 16:42
-
-
Save mrflobow/0ba8c6af398b2643b4a1b9f280a41b9f to your computer and use it in GitHub Desktop.
Bing Wallpaper of the Day (Mac OSX)
This file contains hidden or 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 | |
import os | |
from urllib.parse import urlparse, parse_qs | |
import subprocess | |
# Sets daily wallpaper from BING | |
bing_url = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1' | |
user_home = os.getenv("HOME") | |
bingbong_download_folder = f'{user_home}/BingBong' | |
SET_DESKTOPBACKGROUND_OSASCRIPT = """/usr/bin/osascript<<END | |
tell application "System Events" | |
tell every desktop | |
set picture to "{}" | |
end tell | |
end tell | |
END""" # apple script | |
def download_wp(): | |
r = requests.get(bing_url) | |
resp = r.json() | |
image_url = resp['images'][0]['urlbase'] | |
purl = urlparse(image_url) | |
qdict = parse_qs(purl.query) | |
image_id = qdict['id'][0] | |
image_name = f'{image_id}_UHD.jpg' | |
image_fpath = f'{bingbong_download_folder}/{image_name}' | |
download_url = f'https://www.bing.com/th?id={image_name}' | |
img_resp = requests.get(download_url) | |
open(image_fpath,"wb").write(img_resp.content) | |
subprocess.Popen(SET_DESKTOPBACKGROUND_OSASCRIPT.format( | |
image_fpath), shell=True) # run the apple script | |
# Press the green button in the gutter to run the script. | |
if __name__ == '__main__': | |
download_wp() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment