-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvotes.py
47 lines (41 loc) · 1.68 KB
/
votes.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
import urllib
import re
from logging import getLogger
logger = getLogger(__name__)
class HtmlVote(object):
def __init__(self, page):
self.page = page
@property
def member_votes(self):
"""
Returns a tuple of (member_id, vote_result_code) describing the vote found in page, where:
member_id = link to dataservice member id
vote_result_code = "voted for"|"voted against"|"abstain"|"did not vote" - matching the codes in dataservice.votes.VoteMember.vote_result_code
"""
results = []
pattern = re.compile("""Vote_Bord""")
match = pattern.split(self.page)
for i in match:
vote_result_code = ""
if re.match("""_R1""", i):
vote_result_code = "voted for"
if re.match("""_R2""", i):
vote_result_code = "voted against"
if re.match("""_R3""", i):
vote_result_code = "abstain"
if re.match("""_R4""", i):
vote_result_code = "did not vote"
if vote_result_code != "":
try:
member_id = re.search("""MKID=(\d+)""", i).group(1)
results.append((member_id, vote_result_code))
except AttributeError as e:
logger.exception('Failed to find html vote for specific mk %s' % i)
continue
return results
@classmethod
def get_from_vote_id(cls, vote_id):
url = 'http://www.knesset.gov.il/vote/heb/Vote_Res_Map.asp?vote_id_t=%s' % vote_id
logger.info('Trying to scrape member votes from %s', url)
page = urllib.request.urlopen(url).read()
return cls(page)