-
Notifications
You must be signed in to change notification settings - Fork 0
/
getbing2.py
70 lines (62 loc) · 2.11 KB
/
getbing2.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# coding=utf-8
import os
import re
import time
import win32api
import win32con
import win32gui
import requests
from PIL import Image
from lxml.html import fromstring
_MAIN_PAGES = [
"https://cn.bing.com/",
"https://www2.bing.com/",
"https://www4.bing.com/",
"https://www.bing.com/",
]
def set_wallpaper(imagepath):
k = win32api.RegOpenKeyEx(
win32con.HKEY_CURRENT_USER,
"Control Panel\\Desktop",
0,
win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(
k, "WallpaperStyle", 0, win32con.REG_SZ, "2") # 2拉伸适应桌面,0桌面居中
win32api.RegSetValueEx(
k, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(
win32con.SPI_SETDESKWALLPAPER, imagepath, 3)
def main():
sess = requests.session()
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
}
for page in _MAIN_PAGES:
try:
bing = sess.get(page, timeout=5, headers=headers)
if bing.status_code == 200:
html = fromstring(bing.content.decode())
break
else:
print("HTTP error: {}".format(bing.status_code))
exit(1)
except:
print(" [!] {} connection error!".format(page))
continue
wallpaper_url = html.find(".//link[@id]").get('href')
if not re.findall("\\d+x\\d+\.jpg", wallpaper_url):
wallpaper_url = html.find(".//link[@href]").get('href')
if not wallpaper_url.startswith("https://"):
wallpaper_url = page + wallpaper_url
print(wallpaper_url)
wallpaper = sess.get(wallpaper_url, timeout=10)
date = time.strftime('%Y-%m-%d', time.localtime())
pat = re.compile("\w*?\.jpg")
name = re.findall(pat, wallpaper_url)[0]
temp_jpeg = "wallpaper/[{}]{}.jpg".format(date, name)
with open(temp_jpeg, 'wb') as fp:
fp.write(wallpaper.content)
Image.open(temp_jpeg).save('mywallpaper.bmp')
set_wallpaper(os.getcwd() + '\\mywallpaper.bmp')
if __name__ == '__main__':
main()