-
Notifications
You must be signed in to change notification settings - Fork 5
/
parliament_parser.py
211 lines (175 loc) · 8.42 KB
/
parliament_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
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
import json
import requests
from requests.packages.urllib3.util.retry import Retry
from bs4 import BeautifulSoup
from member import Member
from meeting import Meeting
import json
from os import path, makedirs
import functools
from util import normalize_str
def member_to_URI(base_path, base_URI, member):
return member.dump_json(base_path, base_URI)
def meeting_to_URI(base_path, base_URI, meeting):
return meeting.dump_json(base_path, base_URI)
class ParliamentarySession:
'''
A ParliamentarySession object is the main entryway to the scraper.
It is constructed based on the Session one wants to obtain information on.
From there information can be obtained on the members of the parliament in that session and its meetings.
'''
sessions = {
55: {'from': '2019-06-20', 'to': '2024-06-19'},
54: {'from': '2014-06-19', 'to': '2019-04-25'},
53: {'from': '2010-06-13', 'to': '2014-04-24'},
52: {'from': '2007-06-10', 'to': '2010-05-06'},
}
def dump_json(self, output_path: str, base_URI="/"):
import concurrent.futures
self.get_members()
self.get_plenary_meetings()
base_path = path.join(output_path, "sessions", f'{self.session}')
base_URI = f'{base_URI}sessions/{self.session}/'
makedirs(base_path, exist_ok=True)
# Limiting the workers helps with reducing the lock contention.
# With more workers there is little to gain.
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
meeting_URIs = list(executor.map(functools.partial(
meeting_to_URI, base_path, base_URI), self.plenary_meetings))
members_URIs = list(executor.map(functools.partial(
member_to_URI, base_path, base_URI), self.members))
makedirs(path.join(base_path, "legislation"), exist_ok=True)
makedirs(path.join(base_path, "questions"), exist_ok=True)
for question in self.questions.values():
question.json(base_path, base_URI)
for document in self.documents.values():
document.json(base_path, base_URI)
with open(path.join(base_path, 'legislation', 'unfolded.json'), 'w') as fp:
json.dump(
{
document.document_number: document.json_representation(base_URI)
for document in self.documents.values()
},
fp
)
with open(path.join(base_path, 'questions', 'unfolded.json'), 'w') as fp:
json.dump(
{
document.document_number: document.json_representation(base_URI)
for document in self.questions.values()
},
fp
)
with open(path.join(base_path, 'legislation', 'index.json'), 'w') as fp:
json.dump({document.document_number: f'{base_URI}{document.uri()}' for document in self.documents.values()}, fp)
with open(path.join(base_path, 'questions', 'index.json'), 'w') as fp:
json.dump({question.document_number: f'{base_URI}{question.uri()}' for question in self.questions.values()}, fp)
with open(path.join(base_path, 'session.json'), 'w') as fp:
json.dump({
'id': self.session,
'start': self.start,
'end': self.end,
'members': members_URIs,
'legislation': f'{base_URI}legislation/index.json',
'questions': f'{base_URI}questions/index.json',
'meetings': {'plenary': meeting_URIs}}, fp)
return path.join(base_URI, 'session.json')
def __init__(self, session: int):
"""Initialize a new instance of the scraper
Args:
session (int): Specify the session for which the scraper should be constructed
Note:
Only sessions 55 to 52 are supported for now.
"""
assert (session < 56 and session >
51), 'Only sessions 52-55 are available via this API'
self.session = session
self.plenary_meetings = []
self.members_dict = {}
self.questions = {}
self.documents = {}
self.members = []
self.start = ParliamentarySession.sessions[session]['from']
self.end = ParliamentarySession.sessions[session]['to']
self._members_fn_ln = {}
self._members_ln_fn = {}
self._requests_session = requests.Session()
retry_strategy = Retry(total=5, backoff_factor=1)
self._requests_session.mount('https://', requests.adapters.HTTPAdapter(pool_connections=5, pool_maxsize=5, max_retries=retry_strategy))
@property
def requests_session(self):
return self._requests_session
def find_member(self, query: str):
"""Using their name as listed in the meeting notes
find the Member object related.
Args:
query (str): String formatted as is typical in the meeting notes ("{last_name} {first_name}")
Returns:
Member: returns the related Member if one is found.
"""
if not self.members:
self.get_members()
normalized = normalize_str(query)
if normalized in self._members_fn_ln:
return self._members_fn_ln[normalized]
for member in self.members:
if member.has_name(query):
return member
print(f'Undefined member: {query}')
def get_members_dict(self):
if not self.members_dict:
self.members_dict = {}
for member in self.members:
first_name = normalize_str(member.first_name).decode()
last_name = normalize_str(member.last_name).decode()
self.members_dict[f'{first_name}, {last_name}'] = member
self.members_dict[f'{first_name}, {last_name} {member.party}'] = member
self.members_dict[f'{first_name}, {last_name}, {member.party}'] = member
self.members_dict[f'{first_name}, {last_name}'.replace('-', ' ')] = member
if member.party == "Vooruit":
self.members_dict[f'{first_name}, {last_name}, sp.a'] = member
self.members_dict[f'{first_name}, {last_name} sp.a'] = member
return self.members_dict
def get_plenary_meetings(self, refresh=False):
"""This API returns an overview of all Plenary meetings in the session.
A list of Meeting objects is returned.
Args:
refresh (bool, optional): Should we fully reparse the scraped document? Defaults to False.
Returns:
list(Meeting): List of all known plenary meetings.
"""
if refresh or not self.plenary_meetings:
URL = 'https://www.dekamer.be/kvvcr/showpage.cfm?section=/cricra&language=nl&cfm=dcricra.cfm?type=plen&cricra=cri&count=all&legislat=%02d' % (
self.session)
page = self.requests_session.get(URL)
soup = BeautifulSoup(page.content, 'lxml')
meetings = soup.find_all('tr')
self.plenary_meetings = []
for meeting in meetings:
self.plenary_meetings.append(Meeting.from_soup(meeting, self))
#self.plenary_meetings = self.plenary_meetings[99:100]
return self.plenary_meetings
def get_members(self):
"""Get all known parliament members within this session
Returns:
list(Member): list of all known members within the session
"""
if not self.members:
with open(f'data/composition/{self.session}.json') as json_file:
data = json.load(json_file)
for entry in data:
member = Member.from_json(entry)
self.members.append(member)
# Now that we have all members, link them
for member, entry in zip(self.members, data):
if 'replaces' in entry:
replaces = entry['replaces']
for replacement in replaces:
referenced_member = self.find_member(
replacement['name'])
del replacement['name']
replacement['member'] = referenced_member.uuid
member.set_replaces(replaces)
self._members_fn_ln = {normalize_str(
f'{member.last_name} {member.first_name}'): member for member in self.members}
return self.members