forked from ApoorvGuptaAi/iitk-india-covid-data-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgujarat_gandhinagar_parser.py
71 lines (57 loc) · 2.17 KB
/
gujarat_gandhinagar_parser.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
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from dateutil import parser
from hospital import Hospital, Resource, ResourceType
URLS = {
ResourceType.ICU_WITH_VENTILATOR:
"https://vmc.gov.in/HospitalModuleGMC/HospitalBedsDetails.aspx?tid=2",
ResourceType.ICU_WITHOUT_VENTILATOR:
"https://vmc.gov.in/HospitalModuleGMC/HospitalBedsDetails.aspx?tid=3",
ResourceType.BED_WITH_OXYGEN:
"https://vmc.gov.in/HospitalModuleGMC/HospitalBedsDetails.aspx?tid=4",
ResourceType.BED_WITHOUT_OXYGEN:
"https://vmc.gov.in/HospitalModuleGMC/HospitalBedsDetails.aspx?tid=5"
}
def createBaseHospital(tableRowEl, hospitals):
tds = tableRowEl.find_all("td")
nameEl = tds[0]
name = nameEl.get_text()
if name not in hospitals:
hospital = Hospital(name)
hospital.last_updated = parser.parse(tds[7].get_text() + "+05:30",
dayfirst=True)
hospital.url = nameEl.find('a')['href']
hospital.address = tds[1].get_text()
hospital.state = "Gujarat"
hospital.district = "Gandhinagar"
hospitals[name] = hospital
hospital.resources = []
return hospitals[name]
def parseResource(tableRowEl, hospital, r_type):
tds = tableRowEl.find_all("td")
total = int(tds[2].get_text())
available = int(tds[4].get_text())
r = Resource(r_type, '', available, total)
hospital.resources.append(r)
def get_data():
hospitals = {}
for r_type in URLS:
url = URLS[r_type]
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
tableEl = soup.find("table")
for hospitalEl in tableEl.find_all("tr")[1:]:
hospital = createBaseHospital(hospitalEl, hospitals)
parseResource(hospitalEl, hospital, r_type)
return {
"https://vmc.gov.in/HospitalModuleGMC/Default.aspx": hospitals.values()
}
def main():
hospitals = get_data()
hospital_list = hospitals["https://vmc.gov.in/HospitalModuleGMC/Default.aspx"]
print(len(hospital_list))
for hospital in hospital_list:
print(hospital.last_updated)
if __name__ == "__main__":
main()