-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_run_rosnicka.py
404 lines (365 loc) · 15.8 KB
/
first_run_rosnicka.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import scrapeutils
import vpapi
import io
import scrapeutils
vpapi.parliament('cz/psp')
vpapi.authorize("admin","secret")
def save(scraped):
import json
r = vpapi.get('organizations', where={'identifiers': {'$elemMatch': scraped["identifiers"][0]}})
if not r['_items']:
r = vpapi.post('organizations', scraped)
else:
# update by PUT is preferred over PATCH to correctly remove properties that no longer exist now
existing = r['_items'][0]
r = vpapi.put('organizations/%s' % existing['id'], scraped)
if r['_status'] != 'OK':
raise Exception(self.name, resp)
return r['id']
#zfile = scrapeutils.download('http://www.psp.cz/eknih/cdrom/opendata/poslanci.zip',zipped=True)
#organy = scrapeutils.zipfile2rows(zfile,'organy.unl')
## chamber:
#for row in organy:
# if row[2] == '11':
# term = row[3][3:]
# org = {
# "name": row[4].strip(),
# 'classification': 'chamber',
# 'identifiers': [
# {"identifier": term, 'scheme': 'psp.cz/term'},
# {"identifier": row[0].strip(), "scheme": 'psp.cz/organy'}
# ],
# 'other_names': [
# {'name': 'PSP','note':'abbreviation'}
# ],
# 'founding_date': scrapeutils.cs2iso(row[6].strip())
# }
# if (row[7].strip() != ''):
# org["dissolution_date"] = scrapeutils.cs2iso(row[7].strip())
# save(org)
## political groups
#for row in organy:
# if row[2] == '1':
# org = {
# "name": row[4].strip(),
# 'classification': 'political group',
# 'identifiers': [
# {"identifier": row[0].strip(), "scheme": 'psp.cz/organy'}
# ],
# 'other_names': [
# {'name': row[3].strip(), 'note':'abbreviation'}
# ],
# 'founding_date': scrapeutils.cs2iso(row[6].strip())
# }
# if (row[7].strip() != ''):
# org["dissolution_date"] = scrapeutils.cs2iso(row[7].strip())
# # get parent
# r_parent = vpapi.get('organizations', where={'identifiers': {'$elemMatch': {"identifier": row[1].strip(), "scheme": "psp.cz/organy"}}})
# org["parent_id"] = r_parent["_items"][0]["id"]
# save(org)
## people (we want mps only)
## there are several times in poslanec (for every election term)
#def saveperson(scraped):
# import json
# for ident in scraped["identifiers"]:
# if ident["scheme"] == "psp.cz/osoby":
# identifier = ident
# break
#
# r = vpapi.get('people', where={'identifiers': {'$elemMatch': identifier}})
# if not r['_items']:
# r = vpapi.post('people', scraped)
# else:
# # update by PUT is preferred over PATCH to correctly remove properties that no longer exist now
# existing = r['_items'][0]
# r = vpapi.put('people/%s' % existing['id'], scraped)
# if r['_status'] != 'OK':
# raise Exception(self.name, resp)
# return r['id']
#zfile = scrapeutils.download('http://www.psp.cz/eknih/cdrom/opendata/poslanci.zip',zipped=True)
#osoby = scrapeutils.zipfile2rows(zfile,'osoby.unl')
#poslanec = scrapeutils.zipfile2rows(zfile,'poslanec.unl')
#oosoby = {}
#for row in osoby:
# oosoby[row[0].strip()] = row
#persons = {}
#terms = {}
#for row in poslanec:
# oid = row[1].strip()
# try:
# terms[row[4].strip()]
# except:
# r_t = vpapi.get("organizations", where={'identifiers': {'$elemMatch': {"identifier": row[4].strip(), "scheme": "psp.cz/organy"}}})
# for ident in r_t["_items"][0]["identifiers"]:
# if ident["scheme"] == "psp.cz/term":
# terms[row[4].strip()] = ident["identifier"]
# try:
# persons[row[1].strip()]
# except:
# person = {
# "name" : oosoby[oid][3].strip() + " " + oosoby[oid][2].strip(),
# "sort_name": oosoby[oid][2].strip() + ", " + oosoby[oid][3].strip(),
# "family_name" : oosoby[oid][2].strip(),
# "given_name" : oosoby[oid][3].strip(),
# "birth_date": scrapeutils.cs2iso(oosoby[oid][5].strip()),
# "identifiers": [
# {"identifier": row[0].strip(), "scheme": "psp.cz/poslanec/"+terms[row[4].strip()]},
# {"identifier": row[1].strip(), "scheme": "psp.cz/osoby"}
# ]
# }
# if oosoby[oid][6].strip() == "M":
# person['gender'] = 'male'
# else:
# person['gender'] = 'female'
# if oosoby[oid][1].strip() != "":
# person['honorific_prefix'] = oosoby[oid][1].strip()
# if oosoby[oid][4].strip() != "":
# person['honorific_suffix'] = oosoby[oid][4].strip()
# if oosoby[oid][8].strip() != "":
# person['death_date'] = scrapeutils.cs2iso(oosoby[oid][8].strip())
# persons[row[1].strip()] = person
# else:
# persons[row[1].strip()]["identifiers"].append(
# {"identifier": row[0].strip(), "scheme": "psp.cz/poslanec/"+terms[row[4].strip()]}
# )
#
#for k in persons:
# saveperson(persons[k])
## memberships
#def savemembership(self):
# r = vpapi.get('memberships',where={'person_id': self["person_id"], 'organization_id': self["organization_id"], "role": "member", "start_date": self["start_date"]})
# if not r['_items']:
# r = vpapi.post("memberships",self)
# else:
# r = vpapi.put('memberships/%s' % r['_items'][0]['id'],self)
# if r['_status'] != 'OK':
# raise Exception(self.name, r)
#zfile = scrapeutils.download('http://www.psp.cz/eknih/cdrom/opendata/poslanci.zip',zipped=True)
#zarazeni = scrapeutils.zipfile2rows(zfile,'zarazeni.unl')
#from datetime import datetime
#for row in zarazeni:
# r_org = vpapi.get('organizations', where={'identifiers': {'$elemMatch': {"identifier": row[1].strip(), "scheme": "psp.cz/organy"}}})
# if len(r_org["_items"]) > 0:
# r_pers = vpapi.get('people', where={'identifiers': {'$elemMatch': {"identifier": row[0].strip(), "scheme": "psp.cz/osoby"}}})
# if len(r_pers["_items"]) > 0:
# membership = {
# "label": "Člen",
# "role": "member",
# "person_id": r_pers["_items"][0]['id'],
# "organization_id": r_org["_items"][0]['id'],
# "start_date": datetime.strptime(row[3].strip(), '%Y-%m-%d %H').strftime('%Y-%m-%d')
# }
# if row[4].strip() != "":
# membership["end_date"] = datetime.strptime(row[4].strip(), '%Y-%m-%d %H').strftime('%Y-%m-%d')
#
# savemembership(membership)
#motions, vote-events, votes:
def guess_majority(quorum,present):
if int(quorum) == 120:
return 'two-thirds representatives majority'
if int(quorum) == 101 and int(present)<200:
return 'all representatives majority'
else:
return 'simple majority'
def result2result(res):
if res == "A":
return "pass"
else:
return "fail"
#now faster (just including)
def savemotion(self):
#r = vpapi.get('motions', where={'identifiers': {'$elemMatch': self["identifiers"][0]}})
# if not r['_items']:
r = vpapi.post("motions",self)
# else:
# r = vpapi.put('motions/%s' % r['_items'][0]['id'],self)
if r['_status'] != 'OK':
raise Exception(self.name, r)
else:
return r
#now faster (just including)
def savevoteevent(self):
#r = vpapi.get('vote-events', where={'identifier':self["identifier"]})
#if not r['_items']:
r = vpapi.post("vote-events",self)
#else:
# r = vpapi.patch('vote-events/%s' % r['_items'][0]['id'],self)
if r['_status'] != 'OK':
raise Exception(self.name, r)
else:
return r
def option2option(opt):
if opt == "A":
return "yes"
if opt == "B":
return "no"
if opt == "C" or opt == "F":
return "abstain"
if opt == "K":
return "not voting"
else: #M, @, W
return "absent"
def saveallmotionsandvoteevents(hl_hlasovani):
organizations = {}
for row in hl_hlasovani:
try:
organizations[row[1].strip()]
except:
organizations[row[1].strip()] = vpapi.get('organizations', where={'identifiers': {'$elemMatch': {"identifier": row[1].strip(), "scheme": "psp.cz/organy"}}})
r_org = organizations[row[1].strip()]
#r_org = vpapi.get('organizations', where={'identifiers': {'$elemMatch': {"identifier": row[1].strip(), "scheme": "psp.cz/organy"}}})
motion = {
"id": row[0].strip(),
"organization_id": r_org["_items"][0]['id'],
"requirement": guess_majority(row[12],row[11]),
"result": result2result(row[14].strip()),
"text": row[15].strip(),
#'identifiers': [{'identifier': row[0].strip(), 'scheme': 'psp.cz/hlasovani'}]
"sources": [{'url':"http://www.psp.cz/sqw/hlasy.sqw?g=" + row[0].strip()}]
}
print(motion)
r_motion = savemotion(motion)
#r_motion = vpapi.get('motions', where={'sources': {'$elemMatch': {"identifier": row[0].strip(), "scheme": "psp.cz/hlasovani"}}}) #<-wrong: should be with "sources"
if r_motion["_status"] == "OK":
vote_event = {
#"id": row[0].strip(),
"motion_id": r_motion['id'],
'identifier': row[0].strip(),
"legislative_session": row[2].strip(),
"start_date": scrapeutils.cs2iso(row[5].strip() + " " + row[6].strip()),
"result": result2result(row[14].strip()),
}
r_voteevent = savevoteevent(vote_event)
rosnicka_vote_events = ["6858", "2901", "9096", "10163", "10165", "10294", "12667", "13454", "12670", "12671", "12672", "12673", "13187", "13508", "14938", "20233", "24341", "27824", "28018", "38655", "39516", "39522", "45019", "45531", "45908", "52918", "55672", "57625", "58818", "58869"]
terms = [1993, 1996, 1998, 2002, 2006, 2010, 2013]
#terms = [2013]
#for term in terms:
# import io
# zfile = scrapeutils.download('http://www.psp.cz/eknih/cdrom/opendata/hl-'+str(term)+'ps.zip',zipped=True)
# hl_hlasovani = scrapeutils.zipfile2rows(zfile,'hl'+str(term)+'s.unl')
# saveallmotionsandvoteevents(hl_hlasovani)
def savevotes(hl_poslanec):
votes = {}
voteevents = {}
people = {}
organizations = {}
terms = {}
for rowp in hl_poslanec:
#if rowp[0] == 0: chybne hlasovani v db, viz http://www.psp.cz/sqw/hlasy.sqw?g=58297
# try:
# terms[hl_hlasovani[i][1].strip()]
# except:
# r_t = vpapi.get("organizations", where={'identifiers': {'$elemMatch': {"identifier": hl_hlasovani[0][1].strip(), "scheme": "psp.cz/organy"}}})
# for ident in r_t["_items"][0]["identifiers"]:
# if ident["scheme"] == "psp.cz/term":
# terms[hl_hlasovani[0][1].strip()] = ident["identifier"]
try:
voteevents[rowp[1].strip()]
except:
voteevents[rowp[1].strip()] = vpapi.get('vote-events', where={'identifier': rowp[1].strip()})
r_voteevent = voteevents[rowp[1].strip()]
try:
people[rowp[0].strip()]
except:
people[rowp[0].strip()] = vpapi.get('people', where={"identifiers": {"$elemMatch": {"identifier": rowp[0].strip(), "scheme": {"$regex": "psp.cz/poslanec/*", "$options": "i"} }}})
r_pers = people[rowp[0].strip()]
try:
organizations[r_pers["_items"][0]["id"]]
except:
organizations[r_pers["_items"][0]["id"]] = vpapi.get('memberships',where={"person_id":r_pers["_items"][0]["id"]},embed=["organization"])
r_org = organizations[r_pers["_items"][0]["id"]]
for rowo in r_org["_items"]:
if rowo["organization"]["classification"] == "political group" and rowo["start_date"] <= r_voteevent["_items"][0]["start_date"]:
try:
rowo["end_date"]
except:
fine = True
else:
if rowo["end_date"] >= r_voteevent["_items"][0]["start_date"]:
fine = True
else:
fine = False
# 9 lines to overcome no python's function "isset" ... )-:
if fine:
organization = rowo["organization"]
break
vote = {
"voter_id": r_pers["_items"][0]["id"],
"option": option2option(rowp[2].strip()),
"group_id": organization["id"],
"vote_event_id": r_voteevent["_items"][0]["id"]
}
try:
votes[r_voteevent["_items"][0]["id"]]
except:
votes[r_voteevent["_items"][0]["id"]] = []
votes[r_voteevent["_items"][0]["id"]].append(vote.copy())
# for k in votes:
# vpapi.post("votes",votes[k])
vpapi.post("votes",votes)
j = 0
for term in terms:
print(term)
zfile = scrapeutils.download('http://www.psp.cz/eknih/cdrom/opendata/hl-'+str(term)+'ps.zip',zipped=True)
#hl_hlasovani = scrapeutils.zipfile2rows(zfile,'hl'+str(term)+'s.unl')
for i in range(1,4):
print(i)
try:
hl_poslanec = scrapeutils.zipfile2rows(zfile,'hl'+str(term)+'h'+str(i)+'.unl')
#savevotes(hl_poslanec)
#savevotes(hl_poslanec)
votes = {}
votesli = []
voteevents = {}
people = {}
organizations = {}
terms = {}
for rowp in hl_poslanec:
if rowp[1] in rosnicka_vote_events:
try:
voteevents[rowp[1].strip()]
except:
voteevents[rowp[1].strip()] = vpapi.get('vote-events', where={'identifier': rowp[1].strip()})
r_voteevent = voteevents[rowp[1].strip()]
try:
people[rowp[0].strip()]
except:
people[rowp[0].strip()] = vpapi.get('people', where={"identifiers": {"$elemMatch": {"identifier": rowp[0].strip(), "scheme": {"$regex": "psp.cz/poslanec/*", "$options": "i"} }}})
r_pers = people[rowp[0].strip()]
try:
organizations[r_pers["_items"][0]["id"]]
except:
organizations[r_pers["_items"][0]["id"]] = vpapi.get('memberships',where={"person_id":r_pers["_items"][0]["id"]},embed=["organization"])
r_org = organizations[r_pers["_items"][0]["id"]]
for rowo in r_org["_items"]:
if rowo["organization"]["classification"] == "political group" and rowo["start_date"] <= r_voteevent["_items"][0]["start_date"]:
try:
rowo["end_date"]
except:
fine = True
else:
if rowo["end_date"] >= r_voteevent["_items"][0]["start_date"]:
fine = True
else:
fine = False
# 9 lines to overcome no python's function "isset" ... )-:
if fine:
organization = rowo["organization"]
break
vote = {
"voter_id": r_pers["_items"][0]["id"],
"option": option2option(rowp[2].strip()),
"group_id": organization["id"],
"vote_event_id": r_voteevent["_items"][0]["id"]
}
try:
votes[r_voteevent["_items"][0]["id"]]
except:
votes[r_voteevent["_items"][0]["id"]] = []
votes[r_voteevent["_items"][0]["id"]].append(vote.copy())
for k in votes:
vpapi.post("votes",votes[k])
print(j)
j = j + 1
except:
nothing = 1