-
Notifications
You must be signed in to change notification settings - Fork 2
/
haryana_parser.py
117 lines (103 loc) · 3.72 KB
/
haryana_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
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
import requests
from bs4 import BeautifulSoup
import datetime
from dateutil import parser
import dateutil
import json
import re
from hospital import Hospital, Resource, ResourceType
from database_helper import upload_hospitals
HARYANA_URL = 'https://coronaharyana.in'
def get_updated_timestamp(updated_text):
return parser.parse(updated_text[len('Updated On: '):], dayfirst=True)
def parse_hospital(hospital_div, district):
entry_div = hospital_div.find('div', {'class': 'entry-content'})
# print(entry_div)
headings = entry_div.find_all('h6')
name = headings[0].string[len('Facility Name: '):].strip()
address = headings[0].attrs['title']
beds_div = entry_div.find('p')
beds_text = ''
if beds_div:
beds_text = beds_div.text
else:
print(entry_div)
meta_info_div = hospital_div.find_all('li')
updated_at = meta_info_div[0].string
location = meta_info_div[1].find('a').attrs['onclick']
beds_text = beds_text.replace('Beds Over Utilized ', '-')
numbers = re.findall('-?(?:\d+?)+', beds_text)
allocated_numbers = []
for div_text in entry_div.find_all('div'):
if not "Allocated" in div_text.text:
continue
allocated_numbers = re.findall('-?(?:\d+?)+', div_text.text)
break
resources = [
Resource(ResourceType.BEDS, 'total available beds',
int(numbers[0])),
Resource(ResourceType.BED_WITH_OXYGEN, 'oxygen beds',
int(numbers[1]), int(allocated_numbers[0])),
Resource(ResourceType.BED_WITHOUT_OXYGEN, 'non-oxygen beds',
int(numbers[2]), int(allocated_numbers[1])),
Resource(ResourceType.ICUS, 'icus',
int(numbers[3]), int(allocated_numbers[2])),
Resource(ResourceType.ICU_WITH_VENTILATOR, 'ventilators',
int(numbers[4]), int(allocated_numbers[3]))
]
hospital = Hospital(name, address, district, '', 'Haryana', location,
get_updated_timestamp(updated_at), resources,
'HaryanaParser', HARYANA_URL)
return hospital
def get_hospital_list(district_name, district_index):
hospital_list = []
district_url = 'https://coronaharyana.in/?city=' + str(district_index)
district_response = requests.get(district_url)
district_soup = BeautifulSoup(district_response.text, 'html.parser')
hospitals_div = district_soup.find('div', {
'id': 'tab0'
}).find_all('div', {'class': 'community-post'})
for hospital_div in hospitals_div:
hospital_list.append(parse_hospital(hospital_div, district_name))
return hospital_list
def get_haryana_hospitals():
all_hospitals = []
haryana_districts = {
"Ambala": 1,
"Bhiwani": 2,
"Chandigarh": 24,
"Charki Dadri": 3,
"Faridabad": 4,
"Fatehabad": 5,
"Gurugram": 6,
"Hisar": 7,
"Jhajjar": 8,
"Jind": 9,
"Kaithal": 10,
"Karnal": 11,
"Kurukshetra": 12,
"Mahendragarh": 13,
"Nuh": 23,
"Palwal": 15,
"Panchkula": 16,
"Panipat": 17,
"Rewari": 18,
"Rohtak": 19,
"Sirsa": 20,
"Sonipat": 21,
"Yamunanagar": 22
}
for district in haryana_districts.items():
district_hospitals = get_hospital_list(district[0], district[1])
all_hospitals.extend(district_hospitals)
return {HARYANA_URL: all_hospitals}
def main():
hospital_data = get_haryana_hospitals()
print(len(hospital_data))
print(len(hospital_data[HARYANA_URL]))
print(hospital_data[HARYANA_URL][0])
for hospital in hospital_data[HARYANA_URL]:
print(hospital)
#upload_hospitals(hospital_data)
if __name__ == "__main__":
main()