-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
99 lines (84 loc) · 3.92 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pathlib import *
import calendar
from queue import Queue
import requests
from FileUtils import FileUtils
from Images import Images
class Gallery:
def __init__(self, region):
self.bing_url = "https://www.bing.com"
self.url = "https://global.bing.com/HPImageArchive.aspx?format=js&idx=0&n=9&pid=hp&FORM=BEHPTB&uhd=1&uhdwidth=3840&uhdheight=2160&setmkt={}&setlang=en"
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.42"
}
self.region = region
if region == "zh-CN":
self.readmefile = "README.md"
else:
self.readmefile = region + ".md"
self.jsonfile = region + ".json"
def get_gallery(self):
result = requests.get(self.url.format(self.region), headers=self.headers).json()
file = FileUtils(self.jsonfile, self.readmefile)
all = file.loadJson()
if not result.get("images"):
print("API接口出错啦! 请检查接口!")
json_list = result["images"]
items = [ item for item in json_list ]
for item in items[::-1]:
item = Images(item, self.bing_url)
if item.date not in [ row["enddate"] for row in all["images"] ]:
file.dumpImagesJson(all, item)
print("{}的json数据更新完成!".format(self.region))
today = Images(result["images"][0], self.bing_url)
temp = [ item["enddate"][:6] for item in all["images"] ]
months = list(set(temp))
months.sort(key=temp.index)
months.reverse()
file.writeToReadme(all, today, months, self.region)
print("主目录{}的README已重新生成!".format(self.region))
print("-------------------------------")
def archive(self):
file = FileUtils(self.jsonfile, self.readmefile)
all = file.loadJson()
if not all["months"]["archive"]:
if all["months"]["active"]:
months = all["months"]["active"]
else:
temp = [ item["enddate"][:6] for item in all["images"] ]
months = list(set(temp))
months.sort(key=temp.index)
elif all["months"]["archive"]:
if all["months"]["active"]:
months = all["months"]["active"]
else:
old_archive = all["months"]["archive"]
total = [ item["enddate"][:6] for item in all["images"] ]
months = list(set(total) - set(old_archive))
for month in months:
path = PurePosixPath("archive", self.region, month)
Path(path).mkdir(parents=True, exist_ok=True)
file.writeToArchive(all, path, month)
print("{}历史存档整理完成!".format(self.region))
print("*******************************")
lastdate = month + str(calendar.monthrange(int(month[:4]), int(month[-2:]))[1])
if lastdate in [ item["enddate"] for item in all["images"] ]:
if all["months"]["active"]:
all["months"]["active"].remove(month)
all["months"]["archive"].append(month)
else:
if not all["months"]["active"]:
all["months"]["active"].append(month)
file.dumpMonthsJson(all, month)
regions = ["en-US", "zh-CN", "ja-JP", "en-IN", "pt-BR", "fr-FR", "de-DE", "en-CA", "en-GB", "it-IT", "es-ES", "fr-CA"]
# 中文 | English(US) | 日本語 | Hindi/हिन्दी/India | Português do Brasil | Français | Deutsch | English(CA) | English(GB) | Italiano | Español | Français(CA) |
q = Queue()
if __name__ == "__main__":
for region in regions:
gallery = Gallery(region)
q.put(gallery.get_gallery())
q.put(gallery.archive())
q.get()
q.get()