-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
48 lines (36 loc) · 1.39 KB
/
main.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
# -*- coding: utf-8 -*-
import json
import os
import re
import time
from datetime import datetime
import requests
import bing_image
from fileUtils import writeToReadme
def main():
data = bing_image.get_bing_image()
if not data:
# 休眠 1 分钟后再次执行main函数
time.sleep(60)
return main()
date = datetime.strptime(data.get('date'), '%Y-%m-%d')
image_folder = './images'
image_path = date.strftime("%Y/%m/%d")
new_folder = os.path.join(image_folder, image_path)
if not os.path.exists(new_folder):
os.makedirs(new_folder)
# &rf=LaDigue_UHD.jpg&pid=hp&w=3840&h=2160&rs=1&c=4
# 将图片保存到对应的日期文件夹下
image_url = data.get('imageUrl')
image_name = re.search(r'OHR\.(.*)\.webp', image_url).group(1) + '_' + date.strftime("%Y-%m-%d")
image_path_webp = os.path.join(new_folder, image_name + '.webp')
image_path_jpg = os.path.join(new_folder, image_name + '.jpg')
with open(image_path_webp, 'wb') as webp, open(image_path_jpg, 'wb') as jpg:
webp.write(requests.get(image_url).content)
jpg.write(requests.get(image_url.replace('webp', 'jpg')).content)
filename = os.path.join(new_folder, date.strftime("%Y-%m-%d") + '.json')
with open(filename, 'w') as f:
f.write(json.dumps(data, ensure_ascii=False, indent=2))
if __name__ == '__main__':
main()
writeToReadme()