-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
269 lines (222 loc) · 9.75 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
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
import json
import re
import os
from pyecharts.charts import Line, Pie, Map, Page
from pyecharts import options as opts
from colour import Color
def get_province_data(month, day, province_name=None):
file = open('jsons/%d%d.json' % (month, day), 'r', encoding='UTF-8')
json_array = json.loads(file.read())
file.close()
if not province_name:
sort_arr = [json_obj for json_obj in json_array]
return sorted(sort_arr, key= lambda x: x['confirmedCount'], reverse=True)
for json_object in json_array:
if json_object['provinceName'] == province_name:
return json_object
if json_object['provinceShortName'] == province_name:
return json_object
return None
def get_total_statistic(month, day):
file = open('jsons/%d%d-总计.json' % (month, day), 'r', encoding='UTF-8')
json_object = json.loads(file.read())
file.close()
return json_object
def get_province_status(month, day, province_name=None):
if province_name:
print(province_name)
json_object = get_province_data(month, day, province_name)
data = []
for city in json_object['cities']:
data.append((city['cityName'], city['confirmedCount']))
data.sort(key=lambda x: -x[1])
else:
json_array = get_province_data(month, day, province_name)
data = []
for province in json_array:
data.append((province['provinceShortName'], province['confirmedCount']))
data.sort(key=lambda x: -x[1])
labels = [d[0] for d in data]
counts = [d[1] for d in data]
return labels, counts
def create_dir(root):
if not os.path.exists(root):
os.makedirs(root)
def get_html(month, day):
import requests
url = 'http://3g.dxy.cn/newh5/view/pneumonia'
response = requests.get(url)
html = str(response.content, 'UTF-8')
html_file = open('htmls/%d%d.html' % (month, day), 'w', encoding='UTF-8')
html_file.write(html)
html_file.close()
# 省份数据
json_file = open('jsons/%d%d.json' % (month, day), 'w', encoding='UTF-8')
matches = re.findall('\[[^>]+\]', html)
for match in matches:
try:
json_array = json.loads(match)
json_object = json_array[0]
if 'provinceName' in json_object and json_object['provinceName'] == '湖北省':
json_file.write(match)
break
except:
continue
json_file.close()
# 总体统计数据
total_statistic_file = open('jsons/%d%d-总计.json' % (month, day), 'w', encoding='UTF-8')
matches = re.findall('\{"id":1,[^(>})]+\}', html)
for match in matches:
if "infectSource" in match:
index = match.find(',"marquee":[')
if index != -1:
match = match[:index] + '}'
total_statistic_file.write(match)
break
total_statistic_file.close()
def draw_tendency(month, day):
dates = ['1-%d' % i for i in range(22, 31 + 1)]
dates.extend(['2-%02d' % i for i in range(1, day)])
v0 = [131, 259, 444, 688, 769, 1771, 1459, 1737, 1982, 2102, 2590, 2829, 3235, 3887, 3143, 3399, 2656, 3062, 2478,
2015, 15152, 5090, 2641, 2009, 2051, 1886, 1749, 385, 397, 643, 416, 517, 406, 433]
v1 = [69, 105, 180, 323, 371, 1291, 840, 1032, 1220, 1347, 1921, 2103, 2345, 3156, 2447, 2841, 2147, 2618, 2097,
1638, 14840, 4823, 2420, 1843, 1933, 1807, 1693, 349, 366, 630, 398, 499, 401, 409]
v2 = [v0[i] - v1[i] for i in range(len(v0))]
print(v2)
c = (
Line(init_opts=opts.InitOpts(width='1000px', height='500px'))
.add_xaxis(dates)
.add_yaxis("全国新增确诊病例", v0,
is_smooth=True,
linestyle_opts=opts.LineStyleOpts(width=4, color='#B44038'),
itemstyle_opts=opts.ItemStyleOpts(
color='#B44038', border_color="#B44038", border_width=5
))
.add_yaxis("湖北新增确诊病例", v1, is_smooth=True,
linestyle_opts=opts.LineStyleOpts(width=2, color='#4E87ED'),
label_opts=opts.LabelOpts(position='bottom'),
itemstyle_opts=opts.ItemStyleOpts(
color='#4E87ED', border_color="#4E87ED", border_width=3
))
.add_yaxis("其他省份新增病例", v2, is_smooth=True,
linestyle_opts=opts.LineStyleOpts(width=2, color='#F1A846'),
label_opts=opts.LabelOpts(position='bottom'),
itemstyle_opts=opts.ItemStyleOpts(
color='#F1A846', border_color="#F1A846", border_width=3
))
.set_global_opts(title_opts=opts.TitleOpts(title=""),
yaxis_opts=opts.AxisOpts(
max_=16000,
min_=1,
type_="log",
name="y",
splitline_opts=opts.SplitLineOpts(is_show=True),
is_scale=True,
axisline_opts=opts.AxisLineOpts(is_show=False)
))
)
root = 'html-charts/%d%d' % (month, day)
create_dir(root)
c.render('%s/新增病例趋势图.html' % root)
def get_map(labels, counts, where, title, size, pieces):
my_map = Map(init_opts=opts.InitOpts(width=size[0], height=size[1]))
my_map.add("", [list(z) for z in zip(labels, counts)], where)
my_map.set_series_opts(label_opts=opts.LabelOpts(font_size=8))
my_map.set_global_opts(
title_opts=opts.TitleOpts(title=title),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
pieces=pieces,
is_piecewise=True,
is_show=False
),
)
return my_map
def get_city_name(name):
if name.endswith('市') or name.endswith('区') or name.endswith('县') or name.endswith('自治州'):
return name
return name + '市'
def get_city_map(name, defined_cities):
for d_name in defined_cities:
if len((set(d_name) & set(name))) == len(name):
return get_city_name(d_name)
return None
def draw_multiple_map(month, day):
page = Page(layout=Page.SimplePageLayout)
size = ('350px', '380px')
# 全国疫情地图
labels, counts = get_province_status(month, day)
country_map = get_map(labels=labels, counts=counts,
title='全国-%s例' % get_total_statistic(month, day)['confirmedCount'], size=size, where='china',
pieces=get_default_pieces())
page.add(country_map)
# 已知城市名称
defined_cities = [line.strip() for line in open('py_echarts_city_names.txt', 'r', encoding='UTF-8').readlines()]
# 省份疫情地图
for p in get_province_data(month, day):
title = '%s-%d例' % (p['provinceShortName'], p['confirmedCount'])
# 城市映射
labels = []
for city in p['cities']:
name = city['cityName']
map_name = get_city_map(name, defined_cities)
if not map_name:
map_name = get_city_map(name[:-1], defined_cities)
labels.append(name if not map_name else map_name)
# 数量
counts = [city['confirmedCount'] for city in p['cities']]
if len(labels) == 0:
continue
pieces = get_pieces(min(counts), max(counts))
province_map = get_map(labels=labels, counts=counts, title=title, size=size, where=p['provinceShortName'],
pieces=pieces)
page.add(province_map)
root = 'html-charts/%d%d' % (month, day)
create_dir(root)
page.render('%s/省份地图.html' % root)
def get_pieces(min_value, max_value, ranges=10):
colors = list(Color('#EE9678').range_to(Color('#B72D28'), ranges))
step = (max_value - min_value) / ranges
pieces = []
for i in range(ranges):
start, end = i * step + min_value, (i + 1) * step + min_value
pieces.append({'min': start, 'color': colors[i].hex, 'max': None if end >= max_value else end})
return pieces
def get_default_pieces():
return [
{'min': 1000, 'color': '#450704'},
{'max': 999, 'min': 100, 'color': '#75140B'},
{'max': 99, 'min': 10, 'color': '#AD2217'},
{'max': 9, 'min': 1, 'color': '#DE605B'},
{'max': 0, 'color': '#FFFEE7'},
]
def get_pie(labels, counts, title, size):
pie = Pie(init_opts=opts.InitOpts(width=size[0], height=size[1]))
pie.set_global_opts(legend_opts=opts.LegendOpts(is_show=False), title_opts=opts.TitleOpts(title))
pie.add(
title,
[list(z) for z in zip(labels, counts)],
radius=[5, 80],
).set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"), tooltip_opts=opts.TooltipOpts())
return pie
def draw_multiple_pie(month, day):
page = Page(layout=Page.SimplePageLayout)
labels, counts = get_province_status(month, day)
page.add(
get_pie(labels, counts, '全国-%s例' % get_total_statistic(month, day)['confirmedCount'], size=('360px', '330px')))
for p in get_province_data(month, day):
title = '%s-%d例' % (p['provinceShortName'], p['confirmedCount'])
labels = [city['cityName'] for city in p['cities']]
counts = [city['confirmedCount'] for city in p['cities']]
if len(labels) == 0:
continue
page.add(get_pie(labels, counts, title, size=('350px', '320px')))
root = 'html-charts/%d%d' % (month, day)
create_dir(root)
page.render('%s/省份信息.html' % root)
if __name__ == '__main__':
m, d = 2, 26
get_html(m, d)
draw_tendency(m, d)
draw_multiple_pie(m, d)
draw_multiple_map(m, d)