forked from IllDepence/Curation-Tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
371 lines (317 loc) · 12.2 KB
/
crawler.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
import datetime
import dateutil.parser
import json
import requests
import os
from sqlalchemy import text as sqla_text
from sqlalchemy import create_engine
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from config import Cfg
def requests_retry_session(retries=5, backoff_factor=0.2,
status_forcelist=(500, 502, 504),
session=None):
""" Method to use instead of requests.get to allow for retries during the
crawling process. Ideally the crawler should, outside of this method,
keep track of resources that could not be dereferenced, and offer some
kind of way to retry for those resources at a later point in time (e.g.
the next crawling run.
Code from and discussion at:
https://www.peterbe.com/plog/best-practice-with-retries-with-requests
"""
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
def get_attrib_uri(json_dict, attrib):
""" Get the URI for an attribute.
"""
url = None
if type(json_dict[attrib]) == str:
url = json_dict[attrib]
elif type(json_dict[attrib]) == dict:
if json_dict[attrib].get('id', False):
url = json_dict[attrib]['id']
elif json_dict[attrib].get('@id', False):
url = json_dict[attrib]['@id']
return url
def get_referenced(json_dict, attrib):
""" Get a value (of an attribute in a dict) that is not included in its
entirety but just referenced by a URI or an object with a URI as its
id.
"""
url = get_attrib_uri(json_dict, attrib)
try:
resp = requests_retry_session().get(url)
except Exception as e:
log('Could not dereference resource at {}. Error {}.'.format(
url,
e.__class__.__name__
)
)
return '{}'
return resp.json()
def process_curation_create(activity, db_engine):
""" Process a create activity that has a cr:Curation as its object.
"""
new_canvases = 0
log('Retrieving curation {}'.format(activity['object']['@id']))
cur_dict = get_referenced(activity, 'object')
cur_id = cur_dict['@id']
index_curation(cur_id, db_engine)
log('Entering ranges')
for ran in cur_dict.get('selections', []):
manifest_id = get_attrib_uri(ran, 'within')
canvases = ran.get('members', []) + ran.get('canvases', [])
canvas_id_region_tups = [can['@id'].split('#') for can in canvases]
# DB entries
for can_id_region_tup in canvas_id_region_tups:
can_id = can_id_region_tup[0]
xywh = can_id_region_tup[1].replace('xywh=', '')
index_canvas(can_id, manifest_id, db_engine)
index_curation_element(cur_id, can_id, manifest_id, xywh, db_engine)
log('done')
return len(canvas_id_region_tups)
def index_canvas(uri, manifest_uri, db_engine):
q = sqla_text('''
INSERT INTO canvases(jsonld_id, manifest_jsonld_id)
VALUES (:cid, :mid)
ON CONFLICT (jsonld_id, manifest_jsonld_id)
DO NOTHING''')
db_engine.execute(q, cid=uri, mid=manifest_uri)
def index_curation(uri, db_engine):
q = sqla_text('''
INSERT INTO curations(jsonld_id)
VALUES (:cid)
ON CONFLICT (jsonld_id)
DO NOTHING''')
db_engine.execute(q, cid=uri)
def index_curation_element(cur_id, can_id, manifest_id, xywh, db_engine):
s_cur = sqla_text('''
SELECT id
FROM curations
WHERE jsonld_id=:cid''')
cur_db_id = db_engine.execute(s_cur, cid=cur_id).fetchone()[0]
s_can = sqla_text('''
SELECT id
FROM canvases
WHERE jsonld_id=:cid AND manifest_jsonld_id=:mid''')
can_db_id = db_engine.execute(s_can, cid=can_id, mid=manifest_id).fetchone()[0]
x, y, w, h = [int(elem) for elem in xywh.split(',')]
poly = ('ST_GeomFromText('
'\'POLYGON(({} {}, {} {}, {} {}, {} {}, {} {}))\')').format(
x, y,
x+w, y,
x+w, y+h,
x, y+h,
x, y
)
# because the IDs are only numbers and the PostGIS polygon won't go
# through sqla_text we use format here ¯\_(ツ)_/¯
q = '''INSERT INTO curation_elements(canvas_id, curation_id, area)
VALUES ({}, {}, {})'''.format(can_db_id, cur_db_id, poly)
db_engine.execute(q)
def deintex_curation(cur_uri, db_engine):
# get Curation DB ID
s_cur = sqla_text('''
SELECT id
FROM curations
WHERE jsonld_id=:cid''')
cur_db_id = db_engine.execute(s_cur, cid=cur_uri).fetchone()
if cur_db_id:
cur_db_id = cur_db_id[0]
# delete Curation elements ("cutouts")
d_cel = sqla_text('''
DELETE FROM curation_elements
WHERE curation_id=:cdbid''')
db_engine.execute(d_cel, cdbid=cur_db_id)
# delete Curation
d_cur = sqla_text('''
DELETE FROM curations
WHERE jsonld_id=:jid''')
db_engine.execute(d_cur, jid=cur_uri)
# TODO delete orphaned Canvases
def process_curation_delete(activity, db_engine):
""" Process a delete activity that has a cr:Curation as its object.
"""
log(('Deletion triggered through activity {}').format(activity['id']))
cur_uri = get_attrib_uri(activity, 'object')
deintex_curation(cur_uri, db_engine)
def crawl_single(as_url, db_engine):
""" Crawl, given a URL to an Activity Stream
"""
last_activity_query = sqla_text('''
SELECT last_activity
FROM last_activity_times
WHERE acticity_stream_url=:as_url
''')
last_activity_db = db_engine.execute(
last_activity_query,
as_url=as_url
).fetchone()
if not last_activity_db:
last_activity_time_db = datetime.datetime.fromtimestamp(0)
log('First time crawling this Activity Stream.')
else:
last_activity_time_db = dateutil.parser.parse(last_activity_db[0])
log('Last cralwed activity at {}.'.format(last_activity_time_db))
log('Retrieving Activity Stream ({})'.format(as_url))
try:
resp = requests.get(as_url)
except requests.exceptions.RequestException as e:
msg = 'Could not access Activity Stream. ({})'.format(e)
log(msg)
print(msg)
return
if resp.status_code != 200:
msg = ('Could not access Activity Stream. (HTTP {})'
).format(resp.status_code)
log(msg)
print(msg)
return
as_oc = resp.json()
log('Start iterating over Activity Stream pages')
as_ocp = get_referenced(as_oc, 'last')
new_canvases = 0
new_activity = False
# NOTE: seen_activity_objs is used to prevent processing obsolete
# activities. Since we go through the Activity Stream backwards, we
# only process the most recent Activity per IIIF doc.
# (Not doing so might lead to for example trying to process a Create
# for a document for which a Delete was processed just before.)
seen_activity_objs = []
last_activity_time_as = None
# for all AS pages
while True:
# for all AC items
log('going through AS page {}'.format(as_ocp['id']))
for activity in as_ocp['orderedItems']:
if activity['type'] in ['Create', 'Update', 'Delete']:
# Reduce noise
log('going through {} item {}'.format(activity['type'],
activity['id']))
activity_end_time = dateutil.parser.parse(activity['endTime'])
# if we haven't seen it yet and it's about a Curation
if activity_end_time > last_activity_time_db and \
activity['object']['@type'] == 'cr:Curation' and \
activity['object'] not in seen_activity_objs:
if last_activity_time_as == None:
# b/c we're going backwards (i.e. from new to old)
last_activity_time_as = activity_end_time
new_activity = True
if activity['type'] == 'Create':
log('Create')
new_canvases += process_curation_create(activity, db_engine)
elif activity['type'] == 'Update':
log('Update')
log(' ≈Delete')
process_curation_delete(activity, db_engine)
log(' +Create')
new_canvases += process_curation_create(activity, db_engine)
elif activity['type'] == 'Delete':
log('Delete')
process_curation_delete(activity, db_engine)
seen_activity_objs.append(activity['object'])
else:
if activity['type'] in ['Create', 'Update', 'Delete']:
# Reduce noise
log('skipping')
if not as_ocp.get('prev', False):
break
as_ocp = get_referenced(as_ocp, 'prev')
if last_activity_time_as == None:
last_activity_time_as = last_activity_time_db
# persist crawl log
if not last_activity_db:
last_activity_update = sqla_text('''
INSERT INTO last_activity_times(acticity_stream_url, last_activity)
VALUES (:as_url, :new_time)''')
else:
last_activity_update = sqla_text('''
UPDATE last_activity_times
SET last_activity=:new_time
WHERE acticity_stream_url=:as_url
''')
last_activity_db = db_engine.execute(
last_activity_update,
new_time=last_activity_time_as.isoformat(),
as_url=as_url)
if new_activity:
pass
# foo
else:
pass
# bar
def db_setup(uri):
""" Setup DB
"""
db_engine = create_engine(uri)
create_canvases_table = '''
CREATE TABLE IF NOT EXISTS canvases (
id SERIAL UNIQUE,
jsonld_id TEXT,
manifest_jsonld_id TEXT,
PRIMARY KEY(jsonld_id, manifest_jsonld_id)
);'''
create_curations_table = '''
CREATE TABLE IF NOT EXISTS curations (
id SERIAL UNIQUE,
jsonld_id TEXT PRIMARY KEY
);'''
create_curation_elements_table = '''
CREATE TABLE IF NOT EXISTS curation_elements (
id SERIAL PRIMARY KEY,
canvas_id INTEGER REFERENCES canvases(id),
curation_id INTEGER REFERENCES curations(id),
area GEOMETRY(Polygon)
);'''
create_last_activities_table = '''
CREATE TABLE IF NOT EXISTS last_activity_times (
acticity_stream_url TEXT PRIMARY KEY,
last_activity TEXT -- ISO format UTC time
);'''
db_engine.execute(create_canvases_table)
db_engine.execute(create_curations_table)
db_engine.execute(create_curation_elements_table)
db_engine.execute(create_last_activities_table)
return db_engine
def log(msg):
""" Write a log message.
"""
cfg = Cfg()
timestamp = str(datetime.datetime.now()).split('.')[0]
fn = cfg.cfg['log_file']
# make /dev/stdout usable as log file
# https://www.bugs.python.org/issue27805
# side note: stat.S_ISCHR(os.stat(fn).st_mode) doesn't seem to work in an
# alpine linux docker container running canvas indexer with
# gunicorn although manually executing it on a python shell in
# the container works
if fn == '/dev/stdout':
mode = 'w'
else:
mode = 'a'
with open(fn, mode) as f:
f.write('[{}] {}\n'.format(timestamp, msg))
def crawl():
""" Crawl all the Activity Streams.
"""
cfg = Cfg()
activity_stream_urls = cfg.cfg['activity_stream_list']
db_engine = db_setup(cfg.cfg['db_uri'])
log('- - - - - - - - - - START - - - - - - - - - -')
log('Going through {} activity stream(s).'.format(len(activity_stream_urls)))
# crawl
for url in activity_stream_urls:
crawl_single(url, db_engine)
log('- - - - - - - - - - END - - - - - - - - - -')
if __name__ == '__main__':
crawl()