下载Bing每日壁纸并设置为macOS桌面

Bing 壁纸 API:http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1

使用Keyboard Maestro 或者 Launchd 定时运行以下代码即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import urllib.request
import json
import os
import datetime

apiUrl = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"
saveDir = os.path.expanduser("~/Pictures/Wallpapers/Bing/")
try:
r = urllib.request.urlopen(apiUrl)
except:
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format("Bing Wallpaper download error", "Error!"))
raise SystemExit
c = r.read()
cj = json.loads(c)
imageOriUrl = cj["images"][0]["url"]
imageUrl = "http://www.bing.com" + imageOriUrl
imageOriName = os.path.basename(imageOriUrl)
imageSaveName = datetime.date.today().strftime('%Y%m%d') + "_" + imageOriName
imageSaveAsPath = os.path.join(saveDir, imageSaveName)

try:
_, headers = urllib.request.urlretrieve(imageUrl, imageSaveAsPath)
except:
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format("Bing Wallpaper download error", "Error!"))
raise SystemExit

os.system("""
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "{}"'
""".format(imageSaveAsPath))