-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdatecovidtsgraph.py
143 lines (121 loc) · 5.35 KB
/
updatecovidtsgraph.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import matplotlib.pyplot as plt
import pandas as pd
import urllib
import json
import matplotlib as mpl
import datetime as dt
import time
def plot_covid(url, urlts):
apipage = urllib.request.urlopen(url)
result = apipage.read()
covidjson = json.loads(result)
apipagets = urllib.request.urlopen(urlts)
resultts = apipagets.read()
covidjsonts = json.loads(resultts)
datelist = []
clist = []
dlist = []
for i in range(0, len(covidjsonts), 1):
datelist.append(covidjsonts[i]['reportDate'])
clist.append(covidjsonts[i]['totalConfirmed'])
dlist.append(covidjsonts[i]['deaths']['total'])
coviduts = {'Date': datelist,
'Confirmed': clist,
'Death': dlist}
covidfts = pd.DataFrame(coviduts)
today = dt.datetime.now()
today
time = today.strftime("%Y-%m-%d %H:%M:%S")
today = today.strftime("%Y-%m-%d")
confirmedtext = 'Total Confirmed Cases: ' + str(covidjson['latest']['confirmed'])
deathtext = 'Total Death Cases: ' + str(covidjson['latest']['deaths'])
cfrtext = 'Mortality rate/%: ' + str(round(covidjson['latest']['deaths']/covidjson['latest']['confirmed']*100, 3))
updatext = 'Last Updated at: ' + str(time) + '(Beijing Time)'
plt.figure(figsize=(20, 12))
plt.title('The curve of COVID-19', fontsize=30)
plt.plot(covidfts['Date'], covidfts['Confirmed'], color ='yellow', label = 'Confirmed Cases')
plt.plot(covidfts['Date'], covidfts['Death'], color = 'blue', label = 'Death Cases')
plt.xticks(rotation=-90)
plt.legend()
plt.text('2020-01-25', 0.75*covidfts['Confirmed'].max(), confirmedtext, fontsize=20)
plt.text('2020-01-25', 0.6*covidfts['Confirmed'].max(), deathtext, fontsize=20)
plt.text('2020-01-25', 0.45*covidfts['Confirmed'].max(), cfrtext, fontsize=20)
plt.text('2020-01-25', 0.3*covidfts['Confirmed'].max(), updatext, fontsize=20)
plt.xlabel('Date', fontsize=20)
plt.ylabel('Persons', fontsize=20)
plt.savefig('website/img/architecture.png', dpi = 300)
def plot_jhuraw():
#covidc = pd.read_csv("https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
#covidd = pd.read_csv("https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")
covidc = pd.read_csv("website/coviddatacon.csv")
covidd = pd.read_csv("website/coviddatadea.csv")
covidc['World'] = "Worldwide"
covidd['World'] = "Worldwide"
covidcts = covidc.groupby('World').sum()
covidcpdf = pd.DataFrame(covidcts.ix[0][2:])
coviddts = covidd.groupby('World').sum()
coviddpdf = pd.DataFrame(coviddts.ix[0][2:])
covidcpdf = covidcpdf.reset_index()
coviddpdf = coviddpdf.reset_index()
today = dt.datetime.now()
today
time = today.strftime("%Y-%m-%d %H:%M:%S")
today = today.strftime("%Y-%m-%d")
confirmedtext = 'Total Confirmed Cases: ' + str(list(covidcpdf.Worldwide)[-1])
deathtext = 'Total Death Cases: ' + str(list(coviddpdf.Worldwide)[-1])
cfrtext = 'Mortality rate/%: ' + str(round(list(coviddpdf.Worldwide)[-1]/list(covidcpdf.Worldwide)[-1]*100, 3))
updatext = 'Last Updated at: ' + str(time) + '(Beijing Time)'
plt.figure(figsize=(20, 12))
plt.title('The curve of COVID-19', fontsize=30)
plt.plot(covidcpdf['index'], covidcpdf['Worldwide'], color ='yellow', label = 'Confirmed Cases')
plt.plot(coviddpdf['index'], coviddpdf['Worldwide'], color = 'blue', label = 'Death Cases')
plt.xticks(rotation=-90)
plt.legend()
plt.text(covidcpdf['index'][2], 0.75*covidcpdf['Worldwide'].max(), confirmedtext, fontsize=20)
plt.text(covidcpdf['index'][2], 0.6*covidcpdf['Worldwide'].max(), deathtext, fontsize=20)
plt.text(covidcpdf['index'][2], 0.45*covidcpdf['Worldwide'].max(), cfrtext, fontsize=20)
plt.text(covidcpdf['index'][2], 0.3*covidcpdf['Worldwide'].max(), updatext, fontsize=20)
plt.xlabel('Date', fontsize=20)
plt.ylabel('Persons', fontsize=20)
plt.savefig('website/img/architecture.png', dpi = 300)
sched_time= dt.datetime(2020, 10, 5, 3, 0, 0)
timedelta=dt.timedelta(hours=1)
while True:
nown=dt.datetime.now()
nown=str(nown)[:-7]
if nown==str(sched_time):
print(nown)
try:
#url = "https://coronavirus-tracker-api.herokuapp.com/v2/latest?source=jhu"
#urlts = "https://covid19.mathdro.id/api/daily"
#plot_covid(url, urlts)
plot_jhuraw()
except:
time.sleep(120)
plot_jhuraw()
#plot_covid(url, urlts)
#try:
# plot_jhuraw()
#except:
# time.sleep(120)
# plot_covid(url, urlts)
else:
if nown==str(sched_time+timedelta):
sched_time=sched_time+timedelta
print(nown)
try:
#plot_covid(url, urlts)
plot_jhuraw()
except:
time.sleep(120)
plot_jhuraw()
#plot_covid(url, urlts)
#try:
# plot_jhuraw()
#except:
# time.sleep(120)
# plot_covid(url, urlts)
else:
pass