-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
1361 lines (1185 loc) · 54.4 KB
/
scrape.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import re
import os.path
import argparse
import logging
import unittest
import sys
import io
import json
from datetime import date, datetime, timedelta
import subprocess
import time
import lxml.html
import pytz
import dateutil.parser
import vpapi
import parse
import scrapeutils
import test
BASE_DIR = os.path.dirname(__file__)
CONF_DIR = os.path.join(BASE_DIR, 'conf')
LOGS_DIR = '/var/log/scrapers/sk/nrsr'
SK_MONTHS = {
'jan': 1,
'feb': 2,
'mar': 3,
'apr': 4,
'máj': 5,
'jún': 6,
'júl': 7,
'aug': 8,
'sep': 9,
'okt': 10,
'nov': 11,
'dec': 12,
}
SK_MONTHS_REGEX = \
'januára?|februára?|mar(ec|ca)|apríla?|mája?|júna?|' + \
'júla?|augusta?|septemb(er|ra)|októb(er|ra)|novemb(er|ra)|decemb(er|ra)'
def sk_to_utc(dt_str):
"""Converts Slovak date(-time) string into ISO format in UTC time."""
match = re.search(SK_MONTHS_REGEX, dt_str, re.IGNORECASE)
if match:
month = match.group(0)
dt_str = dt_str.replace(month, '%s.' % SK_MONTHS[month[:3].lower()])
dt = dateutil.parser.parse(dt_str, dayfirst=True)
if ':' in dt_str:
return vpapi.local_to_utc(dt, to_string=True)
else:
return dt.strftime('%Y-%m-%d')
def datestring_add(date_str, days):
"""Returns the date specified as string in ISO format with given
number of days added.
"""
return (datetime.strptime(date_str, '%Y-%m-%d') + timedelta(days=days)).date().isoformat()
def get_or_create(resource, item, key=None):
"""Unless the item already exists in the resource (identified by
`key` fields) create it. Return id of the item and a bool whether
the item was newly created or not. If key is not given, all fields
of the item are used as a key.
"""
if key is None:
key = item.keys()
query = {field: item[field] for field in key}
existing = vpapi.getfirst(resource, where=query)
if existing:
return existing['id'], False
resp = vpapi.post(resource, item)
return resp['id'], True
def get_chamber_id(term):
"""Return chamber id of the given term."""
chamber = vpapi.getfirst('organizations', where={
'classification': 'chamber',
'identifiers': {'$elemMatch': {'identifier': term, 'scheme': 'nrsr.sk'}}})
return chamber['id'] if chamber else None
def normalize_parlgroup_name(name):
"""Makes some corrections on name of a parliament group."""
if not name:
return name
out = re.sub(r'\s*([-–])\s*', '-', name)
if not out.startswith('Klub'):
out = 'Klub %s' % out
return out
class Person:
@staticmethod
def _guess_gender(name):
given_name, family_name = name.split(' ', 1)
if family_name[-1] == 'á' or given_name in ('Edit', 'Ágnes', 'Klára', 'Anna', 'Erzsébet', 'Erzébet', 'Edita'):
return 'female'
else:
return 'male'
@staticmethod
def scrape(id, term):
source = parse.mp(id, term)
p = Person()
p.name = source['meno'] + ' ' + source['priezvisko']
p.identifiers = [{'identifier': str(id), 'scheme': 'nrsr.sk'}]
p.family_name = source['priezvisko']
s = source['meno'].split(' ', 1)
p.given_name = s[0]
if len(s) > 1:
p.additional_name = s[1]
if source['titul']:
s = source['titul'].split(',')
p.honorific_prefix = s[0].strip()
if len(s) > 1:
p.honorific_suffix = s[1].strip()
p.sort_name = source['priezvisko'] + ', ' + source['meno']
if source['e-mail']:
p.email = source['e-mail']
p.gender = Person._guess_gender(p.name)
if source[r'narodený(á)']:
p.birth_date = sk_to_utc(source[r'narodený(á)'])
if source['fotka']:
p.image = source['fotka']
if source['národnosť']:
p.national_identity = source['národnosť']
if source['bydlisko'] or source['kraj']:
cd = {
'label': 'Bydlisko',
'type': 'address',
}
cd['value'] = source['bydlisko'] if source['bydlisko'] else '?'
if source['kraj']:
cd['note'] = source['kraj'] + ('' if source['kraj'].endswith('kraj') else ' kraj')
p.contact_details = [cd]
if source['www']:
p.links = [{
'url': source['www'],
'note': 'Osobná webstránka'
}]
p.sources = [{
'url': source['url'],
'note': 'Profil na webe NRSR'
}]
return p
def save(self):
scraped = self.__dict__
existing = vpapi.getfirst('people', where={'identifiers': {'$elemMatch': self.identifiers[0]}})
if not existing:
resp = vpapi.post('people', scraped)
else:
# update by PUT is preferred over PATCH to correctly remove properties that no longer exist now
resp = vpapi.put('people', existing['id'], scraped, effective_date=effective_date)
if resp['_status'] != 'OK':
raise Exception(self.name, resp)
return resp['id']
class Organization:
@staticmethod
def make_chamber(term):
o = Organization()
o.classification = 'chamber'
o.founding_date = parse.terms[term]['start_date']
if parse.terms[term]['end_date']:
o.dissolution_date = parse.terms[term]['end_date']
o.name = '%s. Národná rada %s-%s' % (term, o.founding_date[:4], getattr(o, 'dissolution_date', '')[:4])
o.identifiers = [{'identifier': term, 'scheme': 'nrsr.sk'}]
o.contact_details = [
{
'type': 'address',
'value': 'Národná rada Slovenskej republiky, Námestie Alexandra Dubčeka 1, 812 80 Bratislava 1',
},
{
'label': 'Ústredňa',
'type': 'tel',
'value': '+421 2 59721111',
},
{
'label': 'Informácie pre verejnosť',
'type': 'tel',
'value': '+421 2 59722463, +421 2 59722460',
},
{
'type': 'email',
'value': 'info@nrsr.sk',
},
]
o.sources = [{
'url': 'http://www.nrsr.sk/web/default.aspx?SectionId=152',
'note': 'Kontaktné informácie na webe NRSR'
}]
return o
@staticmethod
def scrape(type, id):
source = parse.group(type, id)
o = Organization()
o.name = source['názov']
if type == 'parliamentary group':
o.name = normalize_parlgroup_name(o.name)
o.identifiers = [{'identifier': str(id), 'scheme': 'nrsr.sk'}]
o.classification = type
cds = []
for ctype in ('tel', 'fax', 'email', 'kontakt'):
if ctype in source:
cds.append({'type': ctype if ctype != 'kontakt' else 'person', 'value': source[ctype]})
if cds:
o.contact_details = cds
if 'ďalšie dokumenty' in source:
o.links = [{'url': source['ďalšie dokumenty'], 'note': 'Ďalšie dokumenty'}]
o.sources = [{
'url': source['url'],
'note': 'Profil na webe NRSR'
}]
return o
def set_dates(self, group):
if group.get('od', '') not in ('', '...', '1. 1. 0001'):
self.founding_date = sk_to_utc(group['od'])
if group.get('do', '') not in ('', '...', '1. 1. 0001'):
self.dissolution_date = sk_to_utc(group['do'])
def save(self):
scraped = self.__dict__
existing = vpapi.getfirst('organizations', where={
'classification': self.classification,
'identifiers': {'$elemMatch': self.identifiers[0]}})
if not existing:
resp = vpapi.post('organizations', scraped)
else:
# update by PUT is preferred over PATCH to correctly remove properties that no longer exist now
resp = vpapi.put('organizations', existing['id'], scraped, effective_date=effective_date)
if resp['_status'] != 'OK':
raise Exception(self.name, resp)
return resp['id']
class Membership:
@staticmethod
def scrape_chamber_changes_and_save(term):
"""Scrape list of changes of memberships in the parliament chamber
and save (or update) the respective memberships.
If an MP referred by the membership does not exist, scrape and save him/her.
"""
change_list = parse.change_list(term)
oid = get_chamber_id(term)
for change in reversed(change_list['_items']):
logging.info('Scraping mandate change of `%s` at %s' % (change['poslanec']['meno'], change['dátum']))
# if MP is not scraped yet, scrape and save him
existing = vpapi.getfirst('people',
where={'identifiers': {'$elemMatch': {'identifier': change['poslanec']['id'], 'scheme': 'nrsr.sk'}}},
projection={'id': 1})
if existing:
pid = existing['id']
else:
p = Person.scrape(change['poslanec']['id'], term)
pid = p.save()
# create or update the membership
m = Membership()
m.label = 'Poslanec Národnej rady SR'
m.role = 'member'
m.person_id = pid
m.organization_id = oid
m.sources = [{
'url': change_list['url'],
'note': 'Zmeny v poslaneckom zbore na webe NRSR'
}]
if change['zmena'] in ('Mandát vykonávaný (aktívny poslanec)', 'Mandát náhradníka vykonávaný'):
m.start_date = sk_to_utc(change['dátum'])
m.save()
# close previous membership of Izák, Jaroslav (9. 9. 2008 - 20. 5. 2009 - 12. 6. 2010)
if term == '4' and change['poslanec']['meno'] == 'Izák, Jaroslav' and change['dátum'] == '20. 5. 2009':
del m.start_date
m.end_date = change['dátum']
m.save()
elif change['zmena'] in ('Mandát zaniknutý', 'Mandát sa neuplatňuje', 'Mandát náhradníka zaniknutý'):
m.end_date = sk_to_utc(change['dátum'])
# only close an existing membership (counterexample: Érsek, Árpád, 27. 9. 2010 - 10. 3. 2012)
existing_only = True
# except an inaccuracy in source data for Šimko, Ivan (15. 10. 2002 - 15. 10. 2002)
if term == '3' and change['poslanec']['meno'] == 'Šimko, Ivan':
existing_only = False
m.save(existing_only)
elif change['zmena'] in ('Mandát nadobudnutý vo voľbách', 'Mandát náhradníka získaný'):
pass
else:
raise RuntimeError("unknown change '%s' of a membership in chamber" % change['zmena'])
logging.info('Scraped %s mandate changes' % len(change_list['_items']))
@staticmethod
def scrape_from_group_and_save(group_type, id, term):
"""Scrape memberships in a given group and save (or update) them.
If group or MP referred by the membership does not exist, scrape
and save it/him/her.
"""
group = parse.group(group_type, id)
# if group is not scraped yet, scrape and save it
g = vpapi.getfirst('organizations',
where={
'classification': group_type,
'identifiers': {'$elemMatch': {'identifier': id, 'scheme': 'nrsr.sk'}}},
projection={'id': 1})
if g:
oid = g['id']
else:
o = Organization.scrape(group_type, id)
oid = o.save()
roles = {
'člen': 'member',
'členka': 'member',
'deputy': 'member',
'predseda': 'chairman',
'predsedníčka': 'chairwoman',
'podpredseda': 'vice-chairman',
'podpredeseda': 'voce-chairman',
'podpredsedníčka': 'vice-chairwoman',
'vedúci': 'chairman',
'vedúca': 'chairwoman',
'náhradník': 'substitute',
'náhradníčka': 'substitute',
'overovateľ': 'verifier',
'overovateľka': 'verifier',
'poverený vedením klubu': 'chairman',
'podpredseda poverený vedením výboru': 'vice-chairman',
'náhradný člen': 'substitute',
'náhradná členka': 'substitute',
}
for member in group['členovia']:
logging.info('Scraping membership of `%s`' % member['meno'])
# if member MP is not scraped yet, scrape and save him
existing = vpapi.getfirst('people',
where={'identifiers': {'$elemMatch': {'identifier': member['id'], 'scheme': 'nrsr.sk'}}},
projection={'id': 1})
if existing:
pid = existing['id']
else:
p = Person.scrape(member['id'], term)
pid = p.save()
m = Membership()
m.person_id = pid
m.organization_id = oid
m.sources = [{
'url': group['url'],
'note': 'Profil na webe NRSR'
}]
# create or update all periods of the membership
for period in member['obdobia']:
if period.get('rola'):
m.label = period['rola'].capitalize() + ' v skupine ' + group['názov']
m.role = roles[period['rola'].lower()]
else:
m.label = 'V skupine ' + group['názov']
if period.get('od'):
m.start_date = sk_to_utc(period.get('od'))
if period.get('do'):
m.end_date = sk_to_utc(period.get('do'))
m.save()
for attr in ('role', 'start_date', 'end_date'):
if hasattr(m, attr):
delattr(m, attr)
logging.info('Scraped %s memberships' % len(group['členovia']))
# close all open memberships in this group that were not updated
logging.info('Closing not updated open memberships')
present = datetime.utcnow() - timedelta(minutes=10)
query = {
'organization_id': oid,
'$or': [{'end_date': {'$exists': False}}, {'end_date': {'$in': [None, '']}}],
'updated_at': {'$lt': present.isoformat()}
}
to_close = vpapi.getall('memberships', where=query)
for m in to_close:
vpapi.patch('memberships', m['id'], {'end_date': datestring_add(effective_date, -1)})
def save(self, update_only=False):
"""If a compatible membership already exists, update it. Otherwise,
create a new one. If `update_only` is True, only existing memberships
are updated, no new one is created.
Memberships are compatible if their fields `start_date`, `role` and `post`
are compatible. Field 'end_date' is not checked to allow for later corrections
of guessed end dates used when a member disappears from a group profile.
"""
memberships = vpapi.getall('memberships',
where={'person_id': self.person_id, 'organization_id': self.organization_id},
sort='-start_date')
to_save = self.__dict__.copy()
id = None
for existing in memberships:
if self._merge_values('start_date', to_save, existing) \
and to_save.get('end_date', '9999-12-31') >= existing.get('start_date', '0001-01-01') \
and self._merge_values('role', to_save, existing) \
and self._merge_values('post', to_save, existing):
id = existing['id']
self._merge_values('end_date', to_save, existing)
break
else:
to_save = self.__dict__.copy()
if id:
resp = vpapi.put('memberships', id, to_save)
else:
if update_only: return
resp = vpapi.post('memberships', self.__dict__)
if resp['_status'] != 'OK':
raise Exception(self.name, resp)
@staticmethod
def _merge_values(key, candidate, existing):
"""Check for compatibility of values candidate[key] and existing[key]
and store the more specific value into candidate[key].
Values are compatible if at least one of them is empty
(ie. None, "", or key is not present) or they are equal.
A more specific value is the non-empty one.
"""
c = candidate.get(key)
e = existing.get(key)
if c and e and c != e:
return False
if key in candidate or key in existing:
candidate[key] = e or c
return True
def scrape_people(term):
"""Scrape and save people, organizations and memberships for the
given term.
"""
logging.info('Scraping people, organizations and memberships of term `%s`' % term)
global effective_date
effective_date = date.today().isoformat() if term == parse.current_term() else parse.terms[term]['end_date']
# get or make chamber
chamber_id = get_chamber_id(term) or Organization.make_chamber(term).save()
# scrape MPs
mps = parse.mp_list(term)
for mp in mps['_items']:
logging.info('Scraping person `%s` (id=%s)' % (mp['meno'], mp['id']))
p = Person.scrape(mp['id'], term)
p.save()
logging.info('Scraped %s people' % len(mps['_items']))
# scrape memberships of MPs in the chamber
logging.info('Scraping mandate changes')
Membership.scrape_chamber_changes_and_save(term)
# scrape groups and memberships in them
for type in ('committee', 'parliamentary group', 'delegation', 'friendship group'):
groups = parse.group_list(type, term)
for group in groups['_items']:
if term == '2' and type == 'parliamentary group' and \
('nie sú členmi' in group['názov'] or 'Nezávislý' in group['názov']):
continue
logging.info('Scraping %s `%s` (id=%s)' % (type, group['názov'], group['id']))
o = Organization.scrape(type, group['id'])
o.set_dates(group)
o.parent_id = chamber_id
o.save()
logging.info('Scraping its memberships')
Membership.scrape_from_group_and_save(type, group['id'], term)
logging.info('Scraped %s %ss' % (len(groups['_items']), type))
def scrape_motions(term):
"""Scrape and save motions from the given term that are not scraped
yet starting from the oldest ones. One Motion item, one VoteEvent
item and many Vote items are created for each scraped motion detail
page.
Returns number of scraped motions.
"""
logging.info('Scraping motions of term `%s`' % term)
# prepare mappings from source identifier to id for MPs and parliamentary groups
chamber_id = get_chamber_id(term)
people = vpapi.getall('people', projection={'identifiers': 1})
mps = {mp['identifiers'][0]['identifier']: mp['id'] for mp in people if 'identifiers' in mp}
orgs = vpapi.getall('organizations', where={'classification': 'parliamentary group', 'parent_id': chamber_id})
parl_groups = {c['name']: c['id'] for c in orgs}
# add differently spelled parliamentary groups
group_corrections = {
'2': {
'Klub HZDS': 'Klub ĽS-HZDS',
'Klub SMK': 'Klub SMK-MKP',
'Klub Nezávislí': 'Klub Nezávislý',
},
'3': {
'Klub HZDS': 'Klub ĽS-HZDS',
'Klub SDKÚ': 'Klub SDKÚ-DS',
'Klub Smer': 'Klub SMER-SD',
'Klub Smer-SD': 'Klub SMER-SD',
'Klub KNP': 'Klub nezávislých poslancov NR SR',
'Klub Nezávislý': 'Klub nezávislých poslancov NR SR',
},
}
for k, v in group_corrections.get(term, {}).items():
parl_groups[k] = parl_groups[v]
# prepare list of sessions that are not completely scraped yet
sessions_to_scrape = []
session_list = parse.session_list(term)
for session in session_list['_items']:
motions = parse.session(session['číslo'], term)
if len(motions['_items']) == 0: continue
last_motion_id = motions['_items'][-1]['id']
m_url = 'http://www.nrsr.sk/web/Default.aspx?sid=schodze/hlasovanie/hlasklub&ID=%s' % last_motion_id
existing = vpapi.getfirst('motions', where={'sources.url': m_url})
if existing: break
sessions_to_scrape.append((session, motions))
# scrape motions from those sessions
scraped_motions_count = 0
for s, motions in reversed(sessions_to_scrape):
logging.info('Scraping session `%s`' % s['názov'])
# insert the session event unless it already exists
session = {
'name': s['názov'],
'identifier': s['číslo'],
'organization_id': chamber_id,
'type': 'session',
}
try:
session['start_date'] = sk_to_utc(s['trvanie']) + 'T00:00:00'
session['end_date'] = session['start_date']
except ValueError:
# multiday session contains votes; dates are set by debates scraping
pass
key = ('organization_id', 'type', 'identifier')
session_id, _ = get_or_create('events', session, key)
for i, m in enumerate(motions['_items']):
# check if the motion is already present
m_id = re.search(r'ID=(\d+)', m['url']['výsledok']).group(1)
# we not use directly m['url']['kluby'] because it is not always present
m_url = 'http://www.nrsr.sk/web/Default.aspx?sid=schodze/hlasovanie/hlasklub&ID=%s' % m_id
existing = vpapi.getfirst('motions', where={'sources.url': m_url})
if existing: continue
try:
motion_id = None
vote_event_id = None
# insert motion
logging.info('Scraping motion %s of %s (voted at %s)' % (i+1, len(motions['_items']), m['dátum']))
parsed_motion = parse.motion(m['id'])
motion = {
'organization_id': chamber_id,
'legislative_session_id': session_id,
'identifier': parsed_motion['číslo'],
'text': parsed_motion['názov'],
'date': sk_to_utc(m['dátum']),
'sources': [{
'url': parsed_motion['url'],
'note': 'Hlasovanie na webe NRSR'
}],
}
if 'výsledok' in parsed_motion:
motion['result'] = 'pass' if parsed_motion['výsledok'] == 'Návrh prešiel' else 'fail'
resp = vpapi.post('motions', motion)
motion_id = resp['id']
# insert vote event
vote_event = {
'motion_id': motion_id,
'organization_id': chamber_id,
'legislative_session_id': session_id,
'identifier': parsed_motion['číslo'],
'start_date': motion['date'],
'sources': [{
'url': parsed_motion['url'],
'note': 'Hlasovanie na webe NRSR'
}],
}
if 'výsledok' in parsed_motion:
vote_event['result'] = motion['result']
if 'súčty' in parsed_motion:
options = {
'yes': '[z] za',
'no': '[p] proti',
'abstain': '[?] zdržalo sa',
'absent': '[0] neprítomní',
'not voting': '[n] nehlasovalo'
}
vote_event['counts'] = [
{'option': o, 'value': int(parsed_motion['súčty'][s])}
for o, s in options.items() if parsed_motion['súčty'][s] != ''
]
if len(vote_event['counts']) == 0:
del vote_event['counts']
resp = vpapi.post('vote-events', vote_event)
vote_event_id = resp['id']
# insert votes
if 'hlasy' in parsed_motion and len(parsed_motion['hlasy']) > 0:
vote_options = {
'z': 'yes',
'p': 'no',
'?': 'abstain',
'n': 'not voting',
'0': 'absent'
}
votes = []
for v in parsed_motion['hlasy']:
# skip MPs not applying their mandate
if v['hlas'] == '-': continue
pg = normalize_parlgroup_name(v['klub'])
votes.append({
'vote_event_id': vote_event_id,
'option': vote_options[v['hlas']],
'voter_id': mps.get(v['id']),
'group_id': parl_groups.get(pg),
})
if len(votes) > 0:
resp = vpapi.post('votes', votes)
# delete incomplete data if insertion of the motion, vote event or votes failed
except:
if motion_id:
vpapi.delete('motions', motion_id)
if vote_event_id:
vpapi.delete('vote-events', vote_event_id)
raise
scraped_motions_count += 1
logging.info('Scraped %s motions of term `%s`' % (scraped_motions_count, term))
return scraped_motions_count
def scrape_old_debates(term):
"""Scrape and save speeches from debates of the given term, one
of those older terms where transcripts of debates are stored in
RTF files.
Returns number of scraped speeches.
"""
def insert_speech(type):
"""Insert a speech entity with the given type and data
from parent scope variables and update end date of the
corresponding session and sitting. Delete `text`
variable."""
nonlocal text, position
if not text: return
position = position + 1
speech = {
'text': text.strip().replace('[', '(').replace(']', ')'),
'type': type,
'position': position,
'event_id': sitting_id,
'sources' : [{
'url': debate['url'],
'note': 'Prepis debaty v Digitálnej knižnici na webe NRSR'
}]
}
if type != 'scene':
speech['creator_id'] = speaker_id
speech['attribution_text'] = attribution.strip()
speeches.append(speech)
text = ''
if date > session_end_date:
vpapi.patch('events', session_id, {'end_date': date})
if date > sitting_end_date:
vpapi.patch('events', sitting_id, {'end_date': date})
logging.info('Scraping debates of term `%s`' % term)
chamber_id = get_chamber_id(term)
# prepare mapping from MP's name to id
people = vpapi.getall('people', projection={'given_name': 1, 'additional_name': 1, 'family_name': 1})
mps = {}
for mp in people:
if 'additional_name' in mp:
name = '%s. %s. %s' % (mp['given_name'][0], mp['additional_name'][0], mp['family_name'])
else:
name = '%s. %s' % (mp['given_name'][0], mp['family_name'])
mps[name] = mp['id']
# load name corrections
with open(os.path.join(CONF_DIR, 'name_corrections.json'), encoding='utf8') as f:
name_corrections = json.load(f)
# scrape list of debates
debates = parse.old_debates_list(term)
# add the debate missing in the list
if term == '4':
debates['_items'].append({
'názov': 'Autorizovaná rozprava, 48. schôdza NR SR, 3. 2. 2010',
'id': '2010_02_03',
'url': 'http://www.nrsr.sk/dl/Browser/DsDocument?documentId=391413'
})
speech_count = 0
session_identifier = None
for debate in debates['_items']:
# skip obsolete debates in the list
if term == '1':
if (debate['názov'] == 'Stenozáznam' and debate['id'] != '198550' or
debate['id'] in ('65890', '65945', '65949')):
continue
elif term == '2':
if debate['názov'].startswith('Stenografická') and debate['id'] != '92098':
continue
elif term == '3':
if debate['id'] == '181047':
continue
logging.info('Scraping debate `%s` (id=%s)' % (debate['názov'], debate['id']))
if term == '1':
paragraphs = parse.debate_of_term1(debate['id'])
else:
paragraphs = parse.debate_of_terms234(debate['id'])
# normalize header of the debate transcript
if term == '2':
# join first 4 paragraphs and add trailing underscores to mark the header
paragraphs = ['%s %s %s %s\n___' % (paragraphs[0], paragraphs[1], paragraphs[2],
paragraphs[3])] + paragraphs[4:]
elif term in ('3', '4'):
# join first paragraphs until " hodine" ending is found
# and add trailing underscores to mark the header
p = ''
while True:
p += ' ' + paragraphs.pop(0)
if p.endswith('hodine'): break
if paragraphs[0].startswith('___'):
paragraphs.pop(0)
paragraphs.insert(0, p + '\n___')
# extract speeches from the debate
speeches = []
text = ''
within_scene = False
for par in paragraphs:
par = par.replace('\n', ' ').strip()
if not par: continue
# fix last scene
if re.search(r'\b(skončil.|skončené|prerušené|Prerušenie rokovani[ae])\s+o\s+(.*?)\s+hodine.', par):
if not par[0] in ('(', '[', '/'):
par = '(%s)' % par
# convert brackets to parentheses
par = re.sub(r'\[(.*?)\]', r'(\1)', par)
# slash pairs are converted to parentheses too in term 1
if term == '1':
par = re.sub(r'(^|\s)/(.*?)/(\s|$)', r'\1(\2)\3', par)
# convert all inner nested parentheses to brackets
n = 1
while n >= 1:
(par, n) = re.subn(r'\((.*?)\((.*?)\)(.*?)\)', r'(\1[\2]\3)', par, flags=re.DOTALL)
# process eventual multiparagraph scene
if par.startswith('(') and par.count('(') > par.count(')'):
# save eventual previous speech
insert_speech('speech')
text = '<p>%s</p>' % par[1:]
within_scene = True
continue
if within_scene:
if par.endswith(')') and par.count(')') > par.count('('):
text += '\n\n<p>%s</p>' % par[:-1]
insert_speech('scene')
within_scene = False
else:
text += '\n\n<p>%s</p>' % par
continue
# process eventual header
header_pattern = r'((\(?(\d+)\.\)?\s+schôdz)|slávnostn).*?(\d+)\..*\b(\w{3,})\s+(\d{4})(.*?)_{3,}$'
hd = re.search(header_pattern, par, re.DOTALL)
if hd:
# save eventual previous speech
insert_speech('speech')
sk_date = '%s. %s %s' % (hd.group(4), hd.group(5), hd.group(6))
initial_time = re.search(r'\s+o\s+(.*?)\s+hodine', hd.group(7), re.DOTALL)
if initial_time and initial_time.group(1) != '??':
h, m = initial_time.group(1).strip('.').split('.')
date = sk_to_utc(sk_date + ' %s:%s:00' % (h.strip().zfill(2), m.strip().zfill(2)))
else:
date = sk_to_utc(sk_date) + 'T00:00:00'
if hd.group(1).startswith('sláv'):
new_session_name = 'Mimoriadna schôdza'
if term == '1':
new_session_identifier = debate['časť']
elif term == '2':
new_session_identifier = '1000'
else:
sl = parse.session_list(term)
d = '%s. %s. %s' % (int(date[8:10]), int(date[5:7]), int(date[0:4]))
new_session_identifier = next((s['číslo'] for s in sl['_items'] if s['trvanie'] == d))
else:
new_session_name = '%s. schôdza' % hd.group(3)
new_session_identifier = hd.group(3)
if new_session_identifier != session_identifier:
# create new session event
session = {
'name': new_session_name,
'identifier': new_session_identifier,
'organization_id': chamber_id,
'type': 'session',
'start_date': date,
}
key = ('organization_id', 'type', 'identifier')
session_id, _ = get_or_create('events', session, key)
session_identifier = new_session_identifier
session_end_date = date
sitting_count = 0
# create new sitting event
sitting_count += 1
sitting = {
'name': '%s. deň rokovania, %s' % (sitting_count, sk_date),
'identifier': str(sitting_count),
'organization_id': chamber_id,
'type': 'sitting',
'start_date': date,
'parent_id': session_id,
}
key = ('parent_id', 'type', 'identifier')
sitting_id, created = get_or_create('events', sitting, key)
sitting_end_date = date
position = 0
# delete existing speeches of the sitting
if not created:
obsolete = vpapi.getall('speeches', where={'event_id': sitting_id})
for speech in obsolete:
vpapi.delete('speeches', speech['id'])
continue
# process eventual start of a speech
if date < '2001-09-04':
# format `Foreign minister J. Doe:`
speech_start_pattern = r'(.*?)\b([^\W\d])\.[\s_]+((\w)\.[\s_]+)?([\w-]+):$'
else:
# format `J. Doe, foreign minister: speech`
speech_start_pattern = r'([^\W\d])\.[\s_]+((\w)\.[\s_]+)?([\w-]+),\s+(.+?):(.+)$'
sp = re.match(speech_start_pattern, par, re.DOTALL)
if sp:
# save eventual previous speech
insert_speech('speech')
# identify speaker
if date < '2001-09-04':
name = '%s. %s' % (sp.group(2), sp.group(5))
if (sp.group(4)):
name = name.replace(' ', ' %s. ' % sp.group(4))
attribution = sp.group(1)
par = ''
else:
name = '%s. %s' % (sp.group(1), sp.group(4))
if (sp.group(3)):
name = name.replace(' ', ' %s. ' % sp.group(3))
attribution = sp.group(5)
par = sp.group(6)
if name in name_corrections:
name = name_corrections[name]
attribution = attribution[0].lower() + attribution[1:].strip()
speaker_id = mps.get(name)
# create unknown speakers
if not speaker_id:
logging.warn('Speaker `%s, %s` not found, creating new Person' % (name, attribution))
name_parts = re.match(r'(\w)\. ((\w)\. )?(\w+)', name)
person = {
'name': name,
'family_name': name_parts.group(4),
'given_name': name_parts.group(1)
}
person['sort_name'] = '%s, %s.' % (person['family_name'], person['given_name'])
if name_parts.group(3):
person['additional_name'] = name_parts.group(3)
person['sort_name'] += ' %s.' % person['additional_name']
resp = vpapi.post('people', person)
speaker_id = resp['id']
mps[name] = speaker_id
# recognize date(-time) stamps in transcripts
ds = re.match(r'^\s*(\d+\.\s\w+\s\d{4})(.*hodine)?\s*$', par)
if ds:
dt = ds.group(1).strip()
tm = re.search(r'o\s+(.*?)\s+', ds.group(2) or '')
try:
if tm:
h, m = tm.group(1).strip('.').split('.')
date = sk_to_utc('%s %s:%s:00' % (dt, h.strip().zfill(2), m.strip().zfill(2)))
else:
date = sk_to_utc(dt) + 'T00:00:00'
continue
except ValueError:
pass
# process eventual scene in this paragraph
scene_pattern = r'(.*?)\(\s*([\d%s][^\(\)]{2,}[\.?!“])\s*\)(.*)$' % scrapeutils.CS_UPPERS
while True:
scene = re.match(scene_pattern, par, re.DOTALL)
if not scene: break
if scene.group(1):
text += '\n\n<p>%s</p>' % scene.group(1).strip()
insert_speech('speech')
text = '<p>%s</p>' % scene.group(2).strip()
insert_speech('scene')
par = scene.group(3)
if par:
text += '\n\n<p>%s</p>' % par.strip()
insert_speech('speech')
# extract end time of the session
final_time = re.search(
r'\b(skončil.|skončené|prerušené|Prerušenie rokovani[ae])\s+o\s+(.*?)\s+hodine.',
speeches[-1]['text'])
if final_time:
tm = final_time.group(2)
tm = tm.replace('O', '0').replace(',', '.')
h, m = tm.strip('.').split('.')
final_date = '%s.%s.%s %s:%s:00' % (date[8:10], date[5:7], date[0:4], h.strip().zfill(2), m.strip().zfill(2))
final_date = sk_to_utc(final_date)
vpapi.patch('events', session_id, {'end_date': final_date})
vpapi.patch('events', sitting_id, {'end_date': final_date})
vpapi.post('speeches', speeches)
logging.info('Scraped %s speeches' % len(speeches))
speech_count += len(speeches)
logging.info('Scraped %s speeches in total' % speech_count)
def scrape_new_debates(term):
"""Scrape and save speeches from debates of the given term, one
of those newer terms where transcripts of debates are published
in parts assigned to individual speakers.
Returns number of scraped speeches.
"""
debate_part_kinds = {
'Uvádzajúci uvádza bod': 'speech',
'Vstup predsedajúceho': 'speech',
'Vystúpenie spoločného spravodajcu': 'speech',
'Vystúpenie': 'speech',
'Vystúpenie v rozprave': 'speech',
'Vystúpenie s faktickou poznámkou': 'speech',