/
run.py
254 lines (238 loc) · 14.3 KB
/
run.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
# -*- coding: utf-8 -*-
import argparse
from time import sleep
from datetime import date
import os
import urllib2
import json
import vpapi
import dateutil.parser
from app.mod_scraper.ukraine import ukraine_scraper
from app.mod_scraper.moldova import moldova_scraper
from app.mod_scraper.armenia import armenia_scraper
from app.mod_scraper.georgia import georgia_scraper
from app.mod_scraper.belarus_lowerhouse import belarus_lowerhouse_scraper
from app.mod_scraper.belarus_upperhouse import belarus_upperhouse_scraper
from progressbar import ProgressBar, Percentage, ETA, Counter, Bar
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
WEBCACHE_PATH = os.path.join(os.path.realpath(__file__), 'webcache')
def internet_on():
try:
response = urllib2.urlopen("https://www.google.com/?gws_rd=ssl", timeout=1)
if response:
return True
except urllib2.URLError as err: pass
return False
def local_to_utc(dt_str):
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 scrape(countries, people, votes):
global effective_date
effective_date = date.today().isoformat()
# execute MP's bio data.
georgia = georgia_scraper.GeorgiaScraper()
armenia = armenia_scraper.ArmeniaScraper()
ukraine = ukraine_scraper.UkraineScraper()
belarus_lowerhouse = belarus_lowerhouse_scraper.BelarusLowerhouseScraper()
belarus_upperhouse = belarus_upperhouse_scraper.BelarusUpperhouseScraper()
moldova = moldova_scraper.MoldovaScraper()
references = {"georgia": georgia, "armenia": armenia, "ukraine": ukraine,
"belarus-lowerhouse": belarus_lowerhouse, "moldova": moldova,
"belarus-upperhouse": belarus_upperhouse}
countries_array = []
if countries == "all":
for key in references:
countries_array.append(key)
else:
countries_array = countries.split(',')
indexes = []
for country in countries_array:
if country.lower() not in references:
indexes.append(countries_array.index(country))
if len(indexes) > 0:
countries_array.pop(indexes)
with open(os.path.join(BASE_DIR, 'access.json')) as f:
creds = json.load(f)
if len(countries_array) > 0:
for item in sorted(countries_array):
if internet_on(): # scrape and post data from parliaments if there's internet connection
print "\n\tPosting and updating data from %s parliament" % item
print "\tThis may take a few minutes..."
vpapi.parliament(creds[item.lower()]['parliament'])
vpapi.timezone(creds[item.lower()]['timezone'])
vpapi.authorize(creds[item.lower()]['api_user'], creds[item.lower()]['password'])
if people == "yes":
members = references[item.lower()].scrape_mp_bio_data()
chamber = references[item.lower()].scrape_chamber()
parliamentary_groups = references[item.lower()].scrape_parliamentary_groups()
committee = references[item.lower()].scrape_committee()
data_collections = {
"a-people": members,
"b-chamber": chamber,
"c-parliamentary_groups": parliamentary_groups,
"d-committe": committee
}
# inserts data for each data collection in Visegrad+ Api
for collection in sorted(set(data_collections)):
widgets = [' Progress: ', Percentage(), ' ', Bar(marker='#', left='[', right=']'),
' ', ETA(), " - Processed: ", Counter(), ' items ']
pbar = ProgressBar(widgets=widgets)
print "\n\tPosting and updating data to the Visegrad+ from %s data collection\n\n" % \
collection[2:]
if len(data_collections[collection]) > 0:
for json_doc in pbar(data_collections[collection]):
if collection == "a-people":
where_condition = {'identifiers': {'$elemMatch': json_doc['identifiers'][0]}}
collection_of_data = "people"
elif collection == "c-parliamentary_groups" or collection == "d-committe":
if item.lower() == "armenia" or item.lower() == "belarus-upperhouse"\
or item.lower() == "ukraine":
where_condition = {'name': json_doc['name'], "parent_id": json_doc['parent_id']}
else:
where_condition = {'name': json_doc['name']}
collection_of_data = "organizations"
elif collection == "b-chamber":
where_condition = {'identifiers': {'$elemMatch': json_doc['identifiers'][0]}}
collection_of_data = "organizations"
existing = vpapi.getfirst(collection_of_data, where=where_condition)
if not existing:
resp = vpapi.post(collection_of_data, json_doc)
else:
json_obj_id = existing['id']
items_to_delete = ["created_at", "updated_at", "_links", "id"]
for item_delete in items_to_delete:
del existing[item_delete]
if json.loads(json.dumps(json_doc)) == existing:
continue
else:
resp = vpapi.put(collection_of_data, json_obj_id, json_doc, effective_date=effective_date)
# update by PUT is preferred over PATCH to correctly remove properties that no longer exist now
if resp["_status"] != "OK":
raise Exception("Invalid status code")
print "\n\tFinished Posting and updating data from %s data collection\n" % collection[2:]
if item.lower() != "georgia":
memberships = {
"chambers": references[item.lower()].scrape_membership(),
"parliamentary_groups": references[item.lower()].scrape_parliamentary_group_membership(),
"committees": references[item.lower()].scrape_committee_members()
}
elif item.lower() == "georgia":
memberships = {
"chambers": references[item.lower()].scrape_membership()
}
for data_collection in memberships:
widgets_stat = [' Progress: ', Percentage(), ' ', Bar(marker='#', left='[', right=']'),
' ', ETA(), " - Processed: ", Counter(), ' items ']
prog_bar = ProgressBar(widgets=widgets_stat)
if len(memberships[data_collection]) > 0:
print "\n\tPosting and updating data from %s membership data collection\n" % data_collection
for json_doc in prog_bar(memberships[data_collection]):
existing = vpapi.getfirst("memberships", where={'organization_id': json_doc['organization_id'],
"person_id": json_doc['person_id']})
if not existing:
resp = vpapi.post("memberships", json_doc)
else:
json_obj_id = existing['id']
items_to_delete = ["created_at", "updated_at", "_links", "id"]
for item_delete in items_to_delete:
del existing[item_delete]
if json.loads(json.dumps(json_doc)) == existing:
continue
else:
resp = vpapi.put("memberships", json_obj_id, json_doc, effective_date=effective_date)
if resp["_status"] != "OK":
raise Exception("Invalid status code")
print "\n\tFinished Posted and updated data from %s membership data collection\n" % data_collection
else:
print "\n\tThere is no data from %s membership data collection\n" % data_collection
continue
if votes == "yes":
if item.lower() == "ukraine":
events = references[item.lower()].scrape_events()
try:
if len(events) > 0:
widgets_events = [' Progress: ', Percentage(), ' ', Bar(marker='#', left='[', right=']'),
' ', ETA(), " - Processed: ", Counter(), ' items ']
pbar_events = ProgressBar(widgets=widgets_events)
for json_doc in pbar_events(events):
existing_event = vpapi.getfirst("events", where={'identifier': json_doc['identifier']})
if not existing_event:
resp = vpapi.post("events", json_doc)
else:
resp = vpapi.put("events", json_doc['id'], json_doc, effective_date=effective_date)
if resp["_status"] != "OK":
raise Exception("Invalid status code")
print "\n\tFinished Posting and updating data from events data collection"
else:
print "\n\tThere are no new events"
except BaseException as ex:
print ex.message
else:
print "\tThere's not any event to post from %s parliament" % item
motions_vote_events = references[item.lower()].vote_events()
voting_results = references[item.lower()].scrape_votes()
try:
if len(voting_results) > 0:
resp = vpapi.post("votes", voting_results)
if resp["_status"] != "OK":
raise Exception("Invalid status code")
print "\n\tFinished Posting and updating data from votes data collection"
except BaseException as ex:
print ex.message
elif item.lower() == "georgia":
voting_data_collections = {
"amotions": references[item.lower()].motions(),
"bvote-events": references[item.lower()].vote_events(),
}
votes = references[item.lower()].scrape_votes()
for collection in sorted(voting_data_collections):
try:
if len(voting_data_collections[collection]) > 0:
resp = vpapi.post(collection[1:], voting_data_collections[collection])
if resp["_status"] != "OK":
raise Exception("Invalid status code")
print "\n\tFinished Posting and updating data from %s data collection" % collection[1:]
except BaseException as ex:
print ex.message
print "\n\tPosting voting records from Georgia Parliament\n"
try:
if len(votes) > 0:
vpapi.post("votes", votes)
print "\n\tFinished Posting and updating data from votes data collection"
except BaseException as ex:
print ex.message
else:
print "\n\tThere are no voting records for %s" % item
vpapi.deauthorize()
else:
print "\n\tInternet connection problems for %s official parliament web page" % item
continue
else:
print "\n\tInvalid country/ies added"
# Define the arguments.
if __name__ == "__main__":
parser = argparse.ArgumentParser("\nArguments should be written like this: \n\t$1-countries $2-people $3-votes")
parser.add_argument("--countries", help="Import countries data..", default="all")
parser.add_argument("--people", help="Import the persons data..", default="yes")
parser.add_argument("--votes", help="Import the votes data..", default="yes")
parser.add_argument("--time_out_seconds", help="TimeOut seconds..", default="86400")
# Parse arguments and run the app.
args = parser.parse_args()
countries = args.countries
people = args.people
votes = args.votes
time_out_seconds = args.time_out_seconds
if time_out_seconds != "":
while True:
try:
scrape(countries, people.lower(), votes.lower())
except BaseException as e:
print e.message
# Wait for a bit before checking if there are any new edits.
# But not too much that we would risk missing an edits (because we only look at the latest edit for now)
if "d" in time_out_seconds:
time_out_seconds = float(time_out_seconds.replace('d', "")) * 86400
sleep(float(time_out_seconds))