-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflatten.py
233 lines (195 loc) · 8 KB
/
flatten.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import inflect
import json
import logging
import os
import pandas as pd
import zipfile
from bs4 import BeautifulSoup
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("debug.log"),
logging.StreamHandler()
]
)
p = inflect.engine()
# Parse the HTML file
def read_html(file_content):
try:
soup = BeautifulSoup(file_content, 'html.parser')
except:
logging.error("Failed to parse HTML page for representative")
raise
return soup
# Extract the name of the representative
def get_name(soup):
name = {}
div = soup.find_all('div', class_='mp-name')[0]
name["Name"] = div.get_text(strip=True)
return name
# Extract basic information about the representative
def get_basic_info(soup):
basicInfo = {}
div = soup.find_all('div', class_='mp-basic-info')[0]
for child_div in div:
try:
text = child_div.get_text(strip=True)
key, value = text.split(':', 1)
basicInfo[key.lstrip().rstrip()] = value.lstrip().rstrip()
except:
continue
return basicInfo
# Extract personal profile of the representative
def get_personal_profile(soup):
personalProfile = {}
div = soup.find_all('div', class_='personal_profile_parent')[1]
for child_div in div:
try:
text = child_div.get_text(strip=True)
key, value = text.split(':', 1)
personalProfile[key.lstrip().rstrip()] = value.lstrip().rstrip()
except:
continue
return personalProfile
# Extract minister status of the representative
def get_minister_status(soup):
ministerStatus = {}
div = soup.find_all('div', class_='field-item')[0]
text = div.get_text(strip=True)
if "Minister" in text:
ministerStatus["Minister"] = "Yes"
else:
ministerStatus["Minister"] = "No"
ministerStatus["Comment"] = text.lstrip().rstrip()
return ministerStatus
# Extract details about the representative
def get_representative(soup):
representative = {}
try:
representative.update(get_name(soup))
representative.update(get_basic_info(soup))
representative.update(get_personal_profile(soup))
representative.update(get_minister_status(soup))
except:
logging.error("failed to extract information about the representative")
raise
return representative
# Parse the HTML table and return a DataFrame with table contents
def parse_table(table):
try:
headers = [th.text.strip() for th in table.find_all('th')]
rows = table.find_all('tr')
data = []
for row in rows[1:]: # skip the header row
cols = row.find_all('td')
data_dict = {headers[i]: cols[i].text.strip() for i in range(len(headers))}
if row.find('a'):
try:
data_dict["link"] = row.find('a').get('href')
except:
logging.warn("No link found for row")
pass
data.append(data_dict)
return pd.DataFrame(data)
except:
logging.warn("could not parse table from html page for representative")
raise
# Initialize list of table details in multiple dataFrame elements
def init_dataframes(soup):
try:
tables = soup.find_all('table', class_='views-table')
dataFrames = []
for table in tables:
df = parse_table(table)
if not df.empty:
dataFrames.append(df)
return dataFrames
except:
logging.error("Could not create dataframe for representative")
raise
# Get legislative activity details from dataFrames and return a dictionary containing the details
def get_legislative_activity(dataFrames):
try:
details = {}
dataFrameTypes = ["Attendance", "Debates", "Questions", "Private Member Bills"]
for dataFrameType in dataFrameTypes:
details[dataFrameType] = pd.DataFrame()
for dataFrame in dataFrames:
if "Attendance" in dataFrame.columns:
details["Attendance"] = dataFrame
if "Debate Type" in dataFrame.columns:
details["Debates"] = dataFrame
if "Ministry or Category" in dataFrame.columns:
details["Questions"] = dataFrame
if "Bill title" in dataFrame.columns:
details["Private Member Bills"] = dataFrame
return details
except:
logging.error("Failed to get representative details")
raise
# Initialize a JSON for the representative
def init_json(representative, legislativeActivity):
json = {}
json["Name"] = representative["Name"]
json["Constituency"] = representative["Constituency"]
json["Minister"] = representative["Minister"]
try:
attendance_average = (legislativeActivity["Attendance"]["Attendance"].str.rstrip('%').astype(float) / 100).mean()
json["Attendance"] = f"{attendance_average * 100:.2f}%"
except:
json["Attendance"] = ""
json["Debates"] = len(legislativeActivity["Debates"])
json["Questions"] = len(legislativeActivity["Questions"])
json["Private Member Bills"] = len(legislativeActivity["Private Member Bills"])
representative["State"] = representative["State"][:representative["State"].rfind("(") - 1]
representative["Party"] = representative["Party"][:representative["Party"].rfind("(") - 1]
json.update(representative)
json["Activity"] = {}
json["Activity"]["Attendance"] = (legislativeActivity["Attendance"].to_dict(orient='records'))
json["Activity"]["Debates"] = (legislativeActivity["Debates"].to_dict(orient='records'))
json["Activity"]["Questions"] = (legislativeActivity["Questions"].to_dict(orient='records'))
json["Activity"]["Private Member Bills"] = (legislativeActivity["Private Member Bills"].to_dict(orient='records'))
return json
# Build the JSON containing information on all the representatives in the Lok Sabha
def build_json(lok_sabha):
zip_file_path = "raw/{}.zip".format(lok_sabha)
dataFrame = pd.DataFrame()
lokSabhaJson = []
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
for file_name in zip_ref.namelist():
if "index.html" in file_name:
file_content = zip_ref.read(file_name)
try:
# Read the file as HTML
soup = read_html(file_content)
# Get representative details from the HTML
representative = get_representative(soup)
# Add the Lok Sabha number for the representative
representative["Lok Sabha"] = p.ordinal(lok_sabha.split("/")[1])
# Initialize dataFrames with legislative activity information from HTML tables
dataFrames = init_dataframes(soup)
# Get legislativeActivity details from the dataFrames
legislativeActivity = get_legislative_activity(dataFrames)
# Initialize the JSON using the representative and legislativeActivity details
json = init_json(representative, legislativeActivity)
# Append the JSON to the combined list
lokSabhaJson.append(json)
except:
logging.info("Skipping {}".format(file_name))
return lokSabhaJson
# Flatten the raw HTMLs into a JSON file
def flatten_lok_sabhas():
for lok_sabha in range(15, 19):
try:
lokSabhaJson = build_json("Lok Sabha/{}".format(lok_sabha))
os.makedirs("json/Lok Sabha", exist_ok=True)
lokSabhaJsonString = json.dumps(lokSabhaJson, separators=(',', ':'))
with open("json/Lok Sabha/{}.json".format(p.ordinal(lok_sabha)), 'w') as file:
file.write(lokSabhaJsonString)
except:
logging.info("Failed to flatten for Lok Sabha {}".format(lok_sabha))
def flatten():
flatten_lok_sabhas()
if __name__ == "__main__":
flatten()