This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
164 lines (146 loc) · 4.73 KB
/
main.py
File metadata and controls
164 lines (146 loc) · 4.73 KB
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
import camelot
import re
import json
import requests
from datetime import datetime
import os
import sys
disricts = [
{"name": "Thiruvananthapuram", "alt": ["Thiriruvanathapuram"]},
{"name": "Kollam", "alt": []},
{"name": "Pathanamthitta", "alt": []},
{"name": "Idukki", "alt": []},
{"name": "Kottayam", "alt": []},
{"name": "Alappuzha", "alt": []},
{"name": "Ernakulam", "alt": []},
{"name": "Thrissur", "alt": []},
{"name": "Palakkad", "alt": []},
{"name": "Malappuram", "alt": []},
{"name": "Kozhikode", "alt": []},
{"name": "Wayanad", "alt": []},
{"name": "Kannur", "alt": ["Kannu"]},
{"name": "Kasaragod", "alt": ["Kasargod"]},
]
def parse(url):
tables = camelot.read_pdf(url, pages="all")
surv, chro, dist = 0, [], []
for table in tables:
if len(table.df.columns) in [2, 3]:
dist.append(table.df)
if len(table.df.columns) in [4, 5]:
if table.df[len(table.df.columns) - 1][0] == "Remarks":
chro.append(table.df)
if table.df[0][0] == "District":
surv = table.df
data = init_data()
num, rem, dis = "", "", ""
# manual fixes
# for bule_25032020.pdf
if "bule_25032020" in url:
chro[0][2][4] = "Pathanamthitta – 4\nKottayam – 2 \nErnakulam -2"
# for bule_20032020.pdf
if "bule_20032020" in url:
chro[0][1][9] = "Thiruvananthapuram -3"
chro[0][1][10] = "Thiruvananthapuram -1"
# Parses table: Chronology of Positive cases
i = 1
if "patient" in chro[0][0][0]:
i = 0
def dis_parse(s):
return list(
map(
re.compile("[-–]").split,
re.sub(
"[\(\[].*?[\)\]]",
"",
s.replace(" ", "").replace(",", ""),
flags=re.DOTALL,
).splitlines(),
)
)
def add(n):
data[t[0]]["corona_positive"] += n
if "Negative" in rem:
data[t[0]]["cured_discharged"] += n
if "Expired" in status:
data[t[0]]["deaths"] += n
for ch in chro:
for row in ch.iterrows():
if "persons have been" in row[1][0]:
continue
if "patient" in row[1][i]:
continue
if row[1][i].isnumeric():
num = int(row[1][i])
if row[1][i + 1]:
dis = dis_parse(row[1][i + 1])
else:
continue
if row[1][i + 2]:
rem = row[1][i + 2]
status = row[1][i + 3]
if len(dis) > 1:
if len(dis[0]) > 1:
for t in dis:
t[0] = check_alt(t[0])
add(int(t[1]))
else:
for t in dis:
t[0] = check_alt(t[0])
add(1)
else:
if len(dis[0]) > 1:
for t in dis:
t[0] = check_alt(t[0])
add(int(t[1]))
else:
t = dis[0]
t[0] = check_alt(t[0])
inc = 1
if row[1][i]:
inc = int(num)
add(inc)
# Parses table: Details of persons under Surveillance
for row in surv.iterrows():
if any(x in row[1][0] for x in ["District", "Total"]):
continue
t = check_alt(row[1][0].strip())
data[t]["under_observation"] += int(row[1][1])
data[t]["under_home_isolation"] += int(row[1][2])
data[t]["total_hospitalised"] += int(row[1][3])
data[t]["hospitalised_today"] += int(row[1][4])
# Parses table: District wise distribution based on hospital admission
for di in dist:
for row in di.iterrows():
if any(x in row[1][0] for x in ["District", "Total"]):
continue
t = check_alt(row[1][0].strip())
data[t]["positive_admitted"] += int(row[1][1])
data = {
"kerala": data,
"time": datetime.now().isoformat(),
"file_url": url,
}
return json.dumps(data)
def init_data():
data = {}
for d in disricts:
data[d["name"]] = {
"under_observation": 0,
"under_home_isolation": 0,
"total_hospitalised": 0,
"hospitalised_today": 0,
"corona_positive": 0,
"cured_discharged": 0,
"deaths": 0,
"positive_admitted": 0,
}
return data
def check_alt(dist):
for d in disricts:
if dist in d["alt"]:
return d["name"]
return dist
if __name__ == "__main__":
url = sys.argv[1]
print(parse(url))