一款更新windows桌面壁纸软件

1.环境 & 声明

python 3.7.5(32bit) 兼容32和64位系统

交流与分享,请勿用于非法途径

2.灵感 & 说明

得益于一个偶然的浏览,发现有博主利用php实现对必应每日一图的解析和获取,于是利用python对其进行复现(虽然有可直接用的接口,而毕竟是别人的!😥),并添加一定的功能!

21/06/04 :初代版本 ,提供图片下载并可自定义设为壁纸.

21/07/06 : 期间太忙,后续使用中发现代码过于臃肿,控制窗口显示非必要信息过多,精简并添加对图片信息和链接文件的储存!

正片开始

首先是博主提供的php版解析接口,使用要求较高,尤其是环境的搭建。

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
<?php
/*
* @author www.liumingye.cn
*/
$filename = "./cache.json";
if (file_exists($filename) === false) {
file_put_contents($filename, "");
}
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$contents = json_decode($contents, true);

if (filesize($filename) === 0) {
// echo "获取\r\n";
getBingImg();
} else {
if ($contents['time'] === date("Ymd")) {
// echo "缓存\r\n";
Header("Location: ".$contents['url']);
} else {
// echo "过期\r\n";
getBingImg();
}
}
function getBingImg() {
$str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1');
if (preg_match("/<url>(.+?)<\/url>/ies", $str, $matches)) {
$imgurl = 'https://cn.bing.com' . $matches[1];
}
if ($imgurl) {
global $contents;
if($contents['url'] !== $imgurl){
global $filename;
$data = array(
"time" => date("Ymd") ,
"url" => $imgurl
);
$data = json_encode($data);
file_put_contents($filename, $data);
}
Header("Location: ".$imgurl);
exit();
} else {
exit('error');
}
}

2021/6/4/ bing_lym项目源码

尚有优化,待优化……

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
71
72
73
#!/usr/bin/python
# -*- coding:utf-8 -*-
# @File : bing_lym.py
# @Time : 2021/6/4 8:53

import re
import os
import win32api
import win32con
import win32gui
import requests

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'
}
''


def downloads_img(time_info, img_d_url, img_text):
path = os.getcwd() + f'/{time_info}.jpg'
with open(path, 'wb') as f:
f.write(requests.get(img_d_url, headers=headers).content)

path_ = os.getcwd() + f'/{time_info}.txt'
with open(path_, 'w', encoding='utf-8') as f_:
f_.write(img_text + '\n' + img_d_url)

print("*-> 文件链接", img_d_url)
print(f"*-> 文件 {time_info}.jpg --> 下载成功!")


def get_res_html():
aspx_url = 'http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1'
img_html = requests.get(url=aspx_url, headers=headers).text
# print(img_html)

date_info = re.findall('<fullstartdate>(.*?)</fullstartdate>', img_html)[0]
img_info = re.findall('<url>(.*?)</url>', img_html)[0]

# 07_06 加入关于图片说明
img_text = re.findall('<copyright>(.*?)</copyright>', img_html)[0]
print(' date_info -> %s\n img_info -> %s\n img_text -> %s' % (date_info, img_info, img_text))

return date_info, img_info, img_text


def setWallPaper(pic):
# open register
regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(regKey, "WallpaperStyle", 0, win32con.REG_SZ, "2")
win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
# refresh screen
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, pic, win32con.SPIF_SENDWININICHANGE)


def img_main():
bing_url = 'https://cn.bing.com'
date_info, img_info, img_text = get_res_html()
# 下载函数
downloads_img(time_info=date_info, img_d_url=bing_url + img_info, img_text=img_text)
# 绝对路径储存
path = os.getcwd() + f'/{date_info}.jpg'
# print(type(path), path)

pd = input('> 更换壁纸? y/n :')
if pd == "y" or "Y":
setWallPaper(pic=path)
else:
pass


if __name__ == '__main__':
img_main()

文件地址

码云:https://gitee.com/xyhshell/bing_lym
成品可执行文件:https://jingmo.lanzoui.com/ida7Ur2z33a