/
covid19.py
90 lines (39 loc) · 1.35 KB
/
covid19.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
import requests
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')
import process
def getImageFilename(options):
startDate = options["from"]
endDate = options["to"]
days = getDailyMeasurements(startDate, endDate)
measurementName = options["metric"]
measurements = process.extract(days, measurementName)
if not options["isTotal"]:
measurements= process.dailyIncrease(measurements)
if not options["isRaw"]:
measurements= process.sevenDayAverage(measurements)
numMeasurements = len(measurements)
dayCounter = range(1, numMeasurements+1)
filename = saveImage(dayCounter, measurements)
return filename
def getDailyMeasurements(startDate, endDate):
endpoint = 'https://api.covid19api.com/'
resource = 'total/country/united-states'
parameters = '?from=%s&to=%s' % (startDate, endDate)
response = requests.get(endpoint+resource+parameters)
return response.json()
def saveImage(xValues, yValues):
dpi = 72
width = 640
height = 480
plt.figure(figsize=(width/dpi, height/dpi), dpi=dpi)
style = 'ro'
xMax = xValues[len(xValues)-1]
yMax = max(yValues)
plt.plot(xValues, yValues, style)
plt.axis([0, xMax, 0, yMax])
filename = 'graph.png'
plt.savefig(filename)
plt.clf()
return filename