-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlogistic.py
159 lines (139 loc) · 6.28 KB
/
logistic.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
import json
import time
import requests
from pyecharts.charts import Map, Timeline
from pyecharts import options as opts
def update_news():
url = 'https://lab.isaaclin.cn/nCoV/api/news?num=5'
news_data = []
res = requests.get(url)
while res.status_code != 200:
res = requests.get(url)
data = json.loads(res.text)
for r in reversed(data['results']):
news_data.append({
'title': r['title'],
'sourceUrl': r['sourceUrl'],
'infoSource': time.strftime('%m-%d %H:%M:%S', time.localtime(r['pubDate'] / 1000)) + ' ' + r[
'infoSource']
})
return news_data
def update_overall():
url = 'https://lab.isaaclin.cn/nCoV/api/overall?latest=1'
res = requests.get(url)
while res.status_code != 200:
res = requests.get(url)
overall_data = json.loads(res.text)
overall_data['time'] = time.strftime("%m-%d %H:%M", time.localtime(time.time()))
return overall_data
def update_map(unit=3600 * 2):
map_data = {}
start_time = 1579701600
url = 'https://lab.isaaclin.cn/nCoV/api/area?latest=1'
# params = {'country': '中国'}
res = requests.get(url)
while res.status_code != 200:
res = requests.get(url)
# print(res.status_code)
data = json.loads(res.text)
time_zone = set()
provinces = set()
start_num = {}
for r in data['results']:
if 'confirmedCount' not in r or r['countryName']!="中国":
continue
mtime = int(r['updateTime']) / 1000
if mtime < start_time:
continue
provinces.add(r['provinceShortName'])
if r['provinceShortName'] not in map_data:
map_data[r['provinceShortName']] = {}
start_num[r['provinceShortName']] = {
'确诊人数': 100000,
'治愈人数': 100000,
'死亡人数': 100000,
'疑似感染人数': 100000
}
time_str = time.strftime("%m-%d %H:%M", time.localtime(mtime - mtime % unit))
time_zone.add(time_str)
if time_str not in map_data[r['provinceShortName']]:
map_data[r['provinceShortName']][time_str] = {
}
map_data[r['provinceShortName']][time_str]['确诊人数'] = r['confirmedCount']
map_data[r['provinceShortName']][time_str]['疑似感染人数'] = r['suspectedCount']
map_data[r['provinceShortName']][time_str]['治愈人数'] = r['curedCount']
map_data[r['provinceShortName']][time_str]['死亡人数'] = r['deadCount']
start_num[r['provinceShortName']]['确诊人数'] = min(start_num[r['provinceShortName']]['确诊人数'], r['confirmedCount'])
try:
start_num[r['provinceShortName']]['疑似感染人数'] = min(start_num[r['provinceShortName']]['疑似感染人数'],
r['suspectedCount'])
except Exception as e:
start_num[r['provinceShortName']]['疑似感染人数'] = min(start_num[r['provinceShortName']]['疑似感染人数'],
0)
print(r)
start_num[r['provinceShortName']]['治愈人数'] = min(start_num[r['provinceShortName']]['治愈人数'], r['curedCount'])
start_num[r['provinceShortName']]['死亡人数'] = min(start_num[r['provinceShortName']]['死亡人数'], r['deadCount'])
# 修正宁夏起始数量
start_num['宁夏'] = {
'确诊人数': 2,
'治愈人数': 0,
'死亡人数': 0,
'疑似感染人数': 0
}
time_zone = sorted(list(time_zone))
process_data = {}
for i in range(len(time_zone)):
t = time_zone[i]
process_data[t] = {
'确诊人数': [],
'治愈人数': [],
'死亡人数': [],
'疑似感染人数': []
}
for p in provinces:
if p not in map_data:
process_data[t]['确诊人数'] += [[p, start_num[p]['确诊人数']]]
process_data[t]['治愈人数'] += [[p, start_num[p]['治愈人数']]]
process_data[t]['死亡人数'] += [[p, start_num[p]['死亡人数']]]
process_data[t]['疑似感染人数'] += [[p, start_num[p]['疑似感染人数']]]
continue
if t in map_data[p]:
process_data[t]['确诊人数'] += [[p, map_data[p][t]['确诊人数']]]
process_data[t]['治愈人数'] += [[p, map_data[p][t]['治愈人数']]]
process_data[t]['死亡人数'] += [[p, map_data[p][t]['死亡人数']]]
process_data[t]['疑似感染人数'] += [[p, map_data[p][t]['疑似感染人数']]]
else:
flag = i - 1
for j in range(i - 1, -1, -1):
tj = time_zone[j]
if tj in map_data[p]:
process_data[t]['确诊人数'] += [[p, map_data[p][tj]['确诊人数']]]
process_data[t]['治愈人数'] += [[p, map_data[p][tj]['治愈人数']]]
process_data[t]['死亡人数'] += [[p, map_data[p][tj]['死亡人数']]]
process_data[t]['疑似感染人数'] += [[p, map_data[p][tj]['疑似感染人数']]]
break
flag -= 1
if flag == -1:
process_data[t]['确诊人数'] += [[p, start_num[p]['确诊人数']]]
process_data[t]['治愈人数'] += [[p, start_num[p]['治愈人数']]]
process_data[t]['死亡人数'] += [[p, start_num[p]['死亡人数']]]
process_data[t]['疑似感染人数'] += [[p, start_num[p]['疑似感染人数']]]
return process_data
def confirmed_map(map_data):
tl = Timeline()
for t in map_data:
map0 = (
Map()
.add(
"确诊人数", [p for p in map_data[t]['确诊人数'] if p[1] != 0],maptype='china'
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(max_=2000),
legend_opts=opts.LegendOpts(is_show=False)
)
)
tl.add(map0, t)
return tl
if __name__ == '__main__':
print(update_map())