Created
April 6, 2021 11:25
-
-
Save svdamani/637ac9b5de6150a745d3006626dd420f to your computer and use it in GitHub Desktop.
Bing wallpaper as desktop background on macOS
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
#!/usr/local/bin/python3 | |
from sys import argv | |
from os import makedirs, system, path | |
from json import loads | |
from urllib.request import urlopen, urlretrieve | |
from subprocess import Popen, PIPE | |
def ascript(cmd): | |
return Popen(['osascript', '-', '2', '2'], stdin=PIPE, stdout=PIPE, stderr=PIPE, encoding='utf-8').communicate(cmd) | |
try: | |
dir = path.join(path.dirname(path.realpath(__file__)), 'Bing') | |
idx = argv[1] if len(argv) > 1 else -1 | |
mkt = argv[2] if len(argv) > 2 else 'en-IN' | |
bing = f'http://bing.com/HPImageArchive.aspx?format=js&n=1&idx={idx}&mkt={mkt}' | |
json = loads(urlopen(bing).read()) or {} | |
makedirs(dir, exist_ok=True) | |
for image in json.get('images', []): | |
title = image.get('title', '').replace('"', '') | |
url = f'http://bing.com' + image.get('url', '') | |
img = path.join(dir, f'{title}.jpg') | |
msg = image.get('copyright', '') | |
if not path.isfile(img): | |
urlretrieve(url, img) | |
ascript(f'tell application "System Events" to tell every desktop to set picture to "{img}"') | |
system(f'terminal-notifier -group bing-desktop -sound default -message "{msg}" -title "{title}" -contentImage "{img}" -open "{url}" &>/dev/null') | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment