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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
import tempfile
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import requests
import win32api
import win32con
import win32gui
from PIL import Image
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.globals import ThemeType
URL = "https://lab.isaaclin.cn/nCoV/api/area"
MAP = {
"不丹": "Bhutan",
"东帝汶": "Timor-Leste",
"东萨摩亚(美)": "Samoa Eastern",
"中国": "China",
"中非共和国": "Central African Rep.",
"丹麦": "Denmark",
"乌克兰": "Ukraine",
"乌兹别克斯坦": "Uzbekistan",
"乌干达": "Uganda",
"乌拉圭": "Uruguay",
"乍得": "Chad",
"也门共和国": "Yemen",
"亚美尼亚": "Armenia",
"以色列": "Israel",
"伊拉克": "Iraq",
"伊朗": "Iran",
"伯利兹": "Belize",
"俄罗斯": "Russia",
"保加利亚": "Bulgaria",
"克罗地亚": "Croatia",
"关岛": "Guam",
"冈比亚": "Gambia",
"冰岛": "Iceland",
"几内亚": "Guinea",
"几内亚比绍": "Guinea-Bissau",
"列支敦士登": "Liechtenstein",
"刚果(布)": "Congo",
"刚果(金)": "Dem. Rep. Congo",
"利比亚": "Libya",
"利比里亚": "Liberia",
"加拿大": "Canada",
"加纳": "Ghana",
"加蓬": "Gabon",
"匈牙利": "Hungary",
"北爱尔兰": "Northern Ireland",
"北马其顿": "Macedonia",
"南苏丹": "S. Sudan",
"南非": "South Africa",
"博茨瓦纳": "Botswana",
"卡塔尔": "Qatar",
"卢旺达": "Rwanda",
"卢森堡": "Luxembourg",
"印度": "India",
"印度尼西亚": "Indonesia",
"危地马拉": "Guatemala",
"厄瓜多尔": "Ecuador",
"厄立特里亚": "Eritrea",
"叙利亚": "Syria",
"古巴": "Cuba",
"吉尔吉斯斯坦": "Kyrgyzstan",
"吉布提": "Djibouti",
"哈萨克斯坦": "Kazakhstan",
"哥伦比亚": "Colombia",
"哥斯达黎加": "Costa Rica",
"喀麦隆": "Cameroon",
"土库曼斯坦": "Turkmenistan",
"土耳其": "Turkey",
"圣卢西亚": "St.Lucia",
"圣多美和普林西比": "Sao Tome and Principe",
"圣巴泰勒米": "Saint Barthelemy",
"圣文森特": "St.Vincent",
"圣文森特和格林纳丁斯": "Saint Vincent and the Grenadines",
"圣文森特岛": "Saint Vincent",
"圣马丁岛": "Sint Maarten",
"圣马力诺": "San Marino",
"圭亚那": "Guyana",
"坦桑尼亚": "Tanzania",
"埃及": "Egypt",
"埃塞俄比亚": "Ethiopia",
"塔吉克斯坦": "Tajikistan",
"塞内加尔": "Senegal",
"塞尔维亚": "Serbia",
"塞拉利昂": "Sierra Leone",
"塞浦路斯": "Cyprus",
"塞舌尔": "Seychelles",
"墨西哥": "Mexico",
"多哥": "Togo",
"多米尼克": "Dominica",
"多米尼加": "Dominican Rep.",
"奥地利": "Austria",
"委内瑞拉": "Venezuela",
"孟加拉国": "Bangladesh",
"安哥拉": "Angola",
"安圭拉岛": "Anguilla",
"安提瓜和巴布达": "Antigua and Barbuda",
"安道尔": "Andorra",
"安道尔共和国": "Andorra",
"尼加拉瓜": "Nicaragua",
"尼日利亚": "Nigeria",
"尼日尔": "Niger",
"尼泊尔": "Nepal",
"巴勒斯坦": "Palestine",
"巴哈马": "Bahamas",
"巴基斯坦": "Pakistan",
"巴巴多斯": "Barbados",
"巴布亚新几内亚": "Papua New Guinea",
"巴拉圭": "Paraguay",
"巴拿马": "Panama",
"巴林": "Bahrain",
"巴西": "Brazil",
"布基纳法索": "Burkina Faso",
"布隆迪共和国": "Burundi",
"希腊": "Greece",
"库克群岛": "Cook Is",
"开曼群岛": "Cayman Is",
"德国": "Germany",
"意大利": "Italy",
"所罗门群岛": "Solomon Is",
"扎伊尔": "Zaire",
"拉脱维亚": "Latvia",
"挪威": "Norway",
"捷克": "Czech Rep.",
"摩尔多瓦": "Moldova",
"摩洛哥": "Morocco",
"摩纳哥": "Monaco",
"文莱": "Brunei",
"斐济": "Fiji",
"斯威士兰": "Swaziland",
"斯洛伐克": "Slovakia",
"斯洛文尼亚": "Slovenia",
"斯里兰卡": "Sri Lanka",
"新加坡": "Singapore",
"新西兰": "New Zealand",
"日本": "Japan",
"智利": "Chile",
"朝鲜": "North Korea",
"柬埔寨": "Cambodia",
"根西岛": "Guernsey",
"格林纳达": "Grenada",
"格陵兰": "Greenland",
"格鲁吉亚": "Georgia",
"梵蒂冈": "Status Civitatis Vaticanae",
"比利时": "Belgium",
"毛里塔尼亚": "Mauritania",
"毛里求斯": "Mauritius",
"汤加": "Tonga",
"沙特阿拉伯": "Saudi Arabia",
"法国": "France",
"法属圭亚那": "French Guiana",
"法属玻利尼西亚": "French Polynesia",
"法罗群岛": "Faroe",
"波兰": "Poland",
"波多黎各": "Puerto Rico",
"波黑": "Bosnia and Herz.",
"泰国": "Thailand",
"泽西岛": "Bailiwick of Jersey",
"津巴布韦": "Zimbabwe",
"洪都拉斯": "Honduras",
"海地": "Haiti",
"澳大利亚": "Australia",
"爱尔兰": "Ireland",
"爱沙尼亚": "Estonia",
"牙买加": "Jamaica",
"特立尼达和多巴哥": "Trinidad and Tobago",
"玻利维亚": "Bolivia",
"瑙鲁": "Nauru",
"瑞典": "Sweden",
"瑞士": "Switzerland",
"留尼旺": "Reunion",
"白俄罗斯": "Belarus",
"百慕大群岛": "Bermuda Is",
"直布罗陀": "Gibraltar",
"科威特": "Kuwait",
"科特迪瓦": "Côte d'Ivoire",
"秘鲁": "Peru",
"突尼斯": "Tunisia",
"立陶宛": "Lithuania",
"索马里": "Somalia",
"约旦": "Jordan",
"纳米比亚": "Namibia",
"缅甸": "Myanmar",
"罗马尼亚": "Romania",
"美国": "United States",
"老挝": "Lao PDR",
"肯尼亚": "Kenya",
"至尊公主邮轮": "Grand Princess",
"芬兰": "Finland",
"苏丹": "Sudan",
"苏里南": "Suriname",
"英国": "United Kingdom",
"荷兰": "Netherlands",
"荷属安的列斯": "Netheriands Antilles",
"莫桑比克": "Mozambique",
"莱索托": "Lesotho",
"菲律宾": "Philippines",
"萨尔瓦多": "El Salvador",
"葡萄牙": "Portugal",
"蒙古": "Mongolia",
"蒙特塞拉特岛": "Montserrat Is",
"西班牙": "Spain",
"西萨摩亚": "Samoa Western",
"贝宁": "Benin",
"赞比亚共和国": "Zambia",
"赤道几内亚": "Eq. Guinea",
"越南": "Vietnam",
"钻石公主号邮轮": "Diamond Princess Cruise Ship",
"阿塞拜疆": "Azerbaijan",
"阿富汗": "Afghanistan",
"阿尔及利亚": "Algeria",
"阿尔巴尼亚": "Albania",
"阿拉伯联合酋长国": "United Arab Emirates",
"阿曼": "Oman",
"阿根廷": "Argentina",
"阿森松": "Ascension",
"阿联酋": "United Arab Emirates",
"韩国": "Korea",
"马尔代夫": "Maldives",
"马拉维": "Malawi",
"马提尼克": "Martinique",
"马来西亚": "Malaysia",
"马耳他": "Malta",
"马达加斯加": "Madagascar",
"马里": "Mali",
"马里亚那群岛": "Mariana Is",
"黎巴嫩": "Lebanon",
"黑山": "Montenegro",
}
def get_data(url, place):
data = requests.get(url).json()
p = pd.json_normalize(data["results"])[[
"countryName",
"provinceShortName",
"currentConfirmedCount",
"confirmedCount",
"deadCount",
"curedCount",
]]
p = p.rename(
columns={
"countryName": "country",
"provinceShortName": "province",
"currentConfirmedCount": "现存确诊",
"confirmedCount": "累计确诊",
"deadCount": "累计死亡",
"curedCount": "累计治愈",
})
if place == "country":
p = p[p["country"] == p["province"]]
p.drop(["province"], axis=1, inplace=True)
elif place == "province":
p = p[p["country"] != p["province"]]
p.drop(["country"], axis=1, inplace=True)
return p.groupby(
place,
as_index=False,
).sum().sort_values(
by=["累计确诊"],
ascending=False,
)
def make_plot(data, place):
if place == "country":
t = "全球累计确诊 TOP-N\n(累计确诊 = 现存确诊 + 累计死亡 + 累计治愈)"
data = data[[
"country",
"现存确诊",
"累计死亡",
"累计治愈",
]]
elif place == "province":
t = "全国现存确诊 TOP-N\n"
data = data[[
"province",
"现存确诊",
]].sort_values(
by=["现存确诊"],
ascending=False,
)
plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"]
plt.style.use("dark_background")
ax = data[:15].plot(
kind="barh",
x=place,
title=t,
stacked=True,
grid=True,
color=["#0fc0fc", "#ff1a1a", "#00b300"],
)
ax.xaxis.tick_top()
ax.set_ylabel("")
ax.invert_yaxis()
ax.set_facecolor("#293441")
# plt.xticks(rotation=15)
plt.savefig(
os.path.join(tempfile.gettempdir(), "plot.png"),
bbox_inches="tight",
facecolor="#293441",
)
def make_map(data, place):
w = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
h = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
if place == "country":
s = "world"
t = "COVID-19 全球现存确诊分布"
elif place == "province":
s = "china"
t = "COVID-19 全国现存确诊分布"
x = [MAP[i] if i in MAP else i for i in data[place].tolist()]
y = [np.log10(i) if i > 0 else i for i in np.array(data["现存确诊"].tolist())]
m = Map(init_opts=opts.InitOpts(
width=str(w // 2) + "px",
height=str(h // 2) + "px",
theme=ThemeType.CHALK,
)).add(
series_name="",
data_pair=[list(z) for z in zip(x, y)],
maptype=s,
is_map_symbol_show=False,
).set_series_opts(label_opts=opts.LabelOpts(
is_show=False)).set_global_opts(
title_opts=opts.TitleOpts(
title=t,
subtitle=time.strftime("%Y-%m-%d %H:00",
time.localtime(time.time())),
pos_left="center",
),
visualmap_opts=opts.VisualMapOpts(
is_show=False,
max_=max(y),
),
tooltip_opts=opts.TooltipOpts(is_show=False),
legend_opts=opts.LegendOpts(is_show=False),
)
make_snapshot(
snapshot,
m.render(),
os.path.join(tempfile.gettempdir(), "map.png"),
is_remove_html=True,
)
def make_wallpaper():
m = Image.open(os.path.join(tempfile.gettempdir(), "map.png"))
x, y = m.size
p = Image.open(os.path.join(tempfile.gettempdir(), "plot.png")).resize(
(x // 4, y // 3),
Image.ANTIALIAS,
)
n = Image.new("RGBA", m.size, (240, 240, 240))
n.paste(m, (0, 0, x, y), m)
n.paste(p, (0, y - y // 3 - 50, x // 4, y - 50))
n.save(os.path.join(tempfile.gettempdir(), "wallpaper.png"))
def set_wallpaper():
k = win32api.RegOpenKeyEx(
win32con.HKEY_CURRENT_USER,
"Control Panel\\Desktop",
0,
win32con.KEY_SET_VALUE,
)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2")
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(
win32con.SPI_SETDESKWALLPAPER,
os.path.join(tempfile.gettempdir(), "wallpaper.png"),
win32con.SPIF_SENDWININICHANGE,
)
def main():
ch = random.choice(["country", "province"])
data = get_data(URL, ch)
make_plot(data, ch)
make_map(data, ch)
make_wallpaper()
set_wallpaper()
if __name__ == "__main__":
main()
|