-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse_utility.py
85 lines (72 loc) · 3.11 KB
/
parse_utility.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
'''
Search by parsed_data.get("CountryCode-Province").get("Date") to get list of data.
Example: parsed_data.get("US-Washington").get("2020-04-14T00:00:00Z")
If you don't know the date, use list() to list all dates available.
Example: list(parsed_data.get("US-Washington"))
If the province was not provided, use an empty string for the province.
Example: parsed_data.get("ZA-").get("2020-04-14T00:00:00Z")
The list contains various data. You can simply refer them by specifying the index.
Example: parsed_data.get("US-Washington").get("2020-04-14T00:00:00Z")[0]
returns "United States of America"
index data
0 Country
1 CountryCode
2 Province
3 City
4 CityCode
5 Lat
6 Lon
7 Confirmed
8 Deaths
9 Recovered
10 Active
11 Date
'''
def read_and_parse(jdata):
data_dict = {}
for jobject in jdata:
parsed_object = []
curr_prov = jobject["Province"]
curr_cont_code = jobject["CountryCode"]
curr_date = jobject["Date"]
parsed_object.append(jobject["Country"])
parsed_object.append(curr_cont_code)
parsed_object.append(curr_prov)
parsed_object.append(jobject["City"])
parsed_object.append(jobject["CityCode"])
parsed_object.append(jobject["Lat"])
parsed_object.append(jobject["Lon"])
parsed_object.append(jobject["Confirmed"])
parsed_object.append(jobject["Deaths"])
parsed_object.append(jobject["Recovered"])
parsed_object.append(jobject["Active"])
parsed_object.append(curr_date)
name = curr_cont_code + "-" + curr_prov
data_dict.setdefault(name, {})
data_dict[name][curr_date] = parsed_object
return data_dict
def output_to_string(parsed_data, string1):
string = ""
string += "Country: " + str(parsed_data[0])
if parsed_data[2] != "":
string += "\nState: " + str(parsed_data[2])
string += "\nTotal confirmed cases: " + str(parsed_data[7])
string += "\nToday's confirmed cases: " +str(parsed_data[7] - string1[7])
string += "\nTotal deaths: " + str(parsed_data[8])
string += "\nToday's deaths: " + str(parsed_data[8] - string1[8])
string += "\nTotal recovered: " + str(parsed_data[9])
string += "\n\nData date: +" + str(parsed_data[11])[:10]
return string
## Output: string containing html formatted texts
## Input: list of parsed data
def output_to_html(parsed_data):
string = "<p><strong>COVID-19: Today's case report</strong></p>"
string += "\n<p>Country:</p>" + str(parsed_data[0])
if str(parsed_data[2]) != "":
string += "\n<p>Province:</p>" + str(parsed_data[2])
string += "\n<p>Date:</p>" + str(parsed_data[11])[:10]
string += "\n<p>Confirmed cases:</p>" + str(parsed_data[7])
string += "\n<p>Deaths:</p>" + str(parsed_data[8])
string += "\n<p>Recovered:</p>" + str(parsed_data[9])
string += "\n<p>Data retrieved by <a href=\"https://documenter.getpostman.com/view/10808728/SzS8rjbc?version=latest#7934d316-f751-4914-9909-39f1901caeb8\">Postman</a></p>\n"
return string