/
metadata.py
421 lines (397 loc) · 14.6 KB
/
metadata.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
import os
import psycopg2
from psycopg2.extras import Json, RealDictCursor
from celery.utils.log import get_task_logger
import spacy
from bs4 import BeautifulSoup
import requests
from .celery import app
from .storage.objects import storage
from .settings import db_configs
from .models.language_models import LanguageModel as lm
logger = get_task_logger(__name__)
@app.task(ignore_result=True)
def index_ckan_package(
crawler_package_key,
package_blob_name,
endpoint):
"""Register the crawled CKAN package into the centralized packages table for
all types of packages to make it searchable. The following processes are
performed:
1. Extract metadata such as title an description from the raw package
JSON file.
2. Create the fulltext search doc for keyword search.
3. Extract named entities from the title and description for
metadata enrichment.
4. Submit separate jobs for indexing ckan package files and
sketching.
"""
# Get package metadata
package = storage.get_object(package_blob_name)
# Get connection.
conn = psycopg2.connect(**db_configs)
# Get CKAN resources
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute("""SELECT key, filename, resource_blob, file_size, raw_metadata
FROM findopendata.ckan_resources where package_key = %s""",
(crawler_package_key,))
resources = [row for row in cur]
# Parse package metadata
created = package.get("metadata_created", None)
modified = package.get("metadata_modified", None)
title = BeautifulSoup(package.get("title", ""), "html.parser").get_text()
title_spacy = lm.process(title).to_json()
name = package.get("name", None)
description = BeautifulSoup(package.get("notes", ""), "html.parser").get_text()
description_spacy = lm.process(description).to_json()
tags = [tag["name"] for tag in package.get("tags", []) if "name" in tag]
license_title = package.get("license_title", None)
license_url = package.get("license_url", None)
organization_display_name = package.get("organization", {})\
.get("title", None)
organization_name = package.get("organization", {}).get("name", None)
if organization_name is not None:
organization_name = "{}:{}".format(endpoint, organization_name)
organization_image_url = package.get("organization", {})\
.get("image_url", None)
organization_description = package.get("organization", {})\
.get("description", None)
raw_metadata = package
crawler_table = "ckan_packages"
crawler_key = crawler_package_key
original_host = endpoint
num_files = len(resources)
# Save package
cur.execute(r"""INSERT INTO findopendata.packages
(
crawler_table,
crawler_key,
id,
original_host,
num_files,
created,
modified,
title,
title_spacy,
name,
description,
description_spacy,
tags,
license_title,
license_url,
organization_display_name,
organization_name,
organization_image_url,
organization_description,
raw_metadata
)
VALUES (
%s, %s, uuid_generate_v1mc(), %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
)
ON CONFLICT (crawler_table, crawler_key) DO UPDATE SET
original_host = EXCLUDED.original_host,
num_files = EXCLUDED.num_files,
updated = current_timestamp,
created = EXCLUDED.created,
modified = EXCLUDED.modified,
title = EXCLUDED.title,
title_spacy = EXCLUDED.title_spacy,
name = EXCLUDED.name,
description = EXCLUDED.description,
description_spacy = EXCLUDED.description_spacy,
tags = EXCLUDED.tags,
license_title = EXCLUDED.license_title,
license_url = EXCLUDED.license_url,
organization_display_name = EXCLUDED.organization_display_name,
organization_name = EXCLUDED.organization_name,
organization_image_url = EXCLUDED.organization_image_url,
organization_description = EXCLUDED.organization_description,
raw_metadata = EXCLUDED.raw_metadata
RETURNING key;""",
(
crawler_table,
crawler_key,
original_host,
num_files,
created,
modified,
title,
Json(title_spacy),
name,
description,
Json(description_spacy),
tags,
license_title,
license_url,
organization_display_name,
organization_name,
organization_image_url,
organization_description,
Json(raw_metadata),
))
package_key = cur.fetchone()["key"]
# Commit all changes.
conn.commit()
conn.close()
logger.info("Indexed CKAN package {} and {} package files".format(
crawler_package_key, len(resources)))
# Submit jobs for indexing package files.
for resource in resources:
index_ckan_package_file(crawler_resource_key=resource["key"],
package_key=package_key,
blob_name=resource["resource_blob"],
filename=resource["filename"],
file_size=resource["file_size"],
raw_metadata=resource["raw_metadata"])
@app.task(ignore_result=True)
def index_ckan_package_file(
crawler_resource_key,
package_key,
blob_name,
filename,
file_size,
raw_metadata):
"""Register the CKAN resource into the package_files table by doing the
following:
1. Extract metadata such as name and description from the source JSON.
Args:
crawler_resource_key: the primary key of the ckan_resources table.
package_key: the primary key of the packages table.
blob_name: the relative path to the blob of this package file.
filename: the UNIX-friendly filename.
file_size: the file size in bytes.
raw_metadata: the 'resource' extracted from the CKAN's package JSON
corresponding to this package file.
"""
# Extract metadata from raw_metadata
crawler_table = "ckan_resources"
created = raw_metadata.get("created", None)
modified = raw_metadata.get("last_modified", None)
name = raw_metadata.get("name", None)
description = BeautifulSoup(raw_metadata.get("description", ""),
"html.parser").get_text()
description_spacy = lm.process(description).to_json()
original_url = raw_metadata.get("url", None)
file_format = raw_metadata.get("format", None)
# Get connection.
conn = psycopg2.connect(**db_configs)
# Save CKAN pacakge file
cur = conn.cursor(cursor_factory=RealDictCursor)
cur.execute(r"""INSERT INTO findopendata.package_files
(
package_key,
crawler_table,
crawler_key,
id,
created,
modified,
filename,
name,
description,
description_spacy,
original_url,
format,
file_size,
blob_name,
raw_metadata
) VALUES
(%s, %s, %s, uuid_generate_v1mc(), %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s,
%s) ON CONFLICT (crawler_table, crawler_key) DO UPDATE SET
updated = current_timestamp,
created = EXCLUDED.created,
modified = EXCLUDED.modified,
filename = EXCLUDED.filename,
name = EXCLUDED.name,
description = EXCLUDED.description,
description_spacy = EXCLUDED.description_spacy,
original_url = EXCLUDED.original_url,
format = EXCLUDED.format,
file_size = EXCLUDED.file_size,
blob_name = EXCLUDED.blob_name,
raw_metadata = EXCLUDED.raw_metadata
RETURNING key;""",
(
package_key,
crawler_table,
crawler_resource_key,
created,
modified,
filename,
name,
description,
Json(description_spacy),
original_url,
file_format,
file_size,
blob_name,
Json(raw_metadata)
))
# Commit all changes.
conn.commit()
conn.close()
logger.info("Indexed CKAN resource {} into package_files".format(
crawler_resource_key))
@app.task(ignore_result=True)
def index_socrata_resource(
crawler_key,
domain,
metadata_blob_name,
resource_blob_name,
dataset_size):
"""Register the crawled Scorata resource into the centralized packages
table for all types of packages to make it searchable.
The following processes are performed:
1. Extract metadata such as title and description from the raw metadata
JSON file.
2. Create the fulltext search doc for keyword search.
3. Extract named entities from the title and description for
metadata enrichment.
"""
# Get raw metadata
metadata = storage.get_object(metadata_blob_name)
# Package fields extracted from raw metadata.
created = metadata["resource"].get("createdAt")
modified = metadata["resource"].get("updatedAt")
title = metadata["resource"].get("name")
title_spacy = None
name = None
description = metadata["resource"].get("description")
description_spacy = None
tags = metadata["classification"].get("domain_tags")
license_title = metadata["metadata"].get("license")
license_url = None
organization_display_name = metadata["resource"].get("attribution")
organization_name = None
organization_image_url = None
organization_description = None
# Package fields assigned
crawler_table = "socrata_resources"
original_host = domain
num_files = 1
# Get connection.
conn = psycopg2.connect(**db_configs)
# Get CKAN resources
cur = conn.cursor(cursor_factory=RealDictCursor)
# Save package
cur.execute(r"""INSERT INTO findopendata.packages
(
crawler_table,
crawler_key,
id,
original_host,
num_files,
created,
modified,
title,
title_spacy,
name,
description,
description_spacy,
tags,
license_title,
license_url,
organization_display_name,
organization_name,
organization_image_url,
organization_description,
raw_metadata
)
VALUES (
%s, %s, uuid_generate_v1mc(), %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
)
ON CONFLICT (crawler_table, crawler_key) DO UPDATE SET
original_host = EXCLUDED.original_host,
num_files = EXCLUDED.num_files,
updated = current_timestamp,
created = EXCLUDED.created,
modified = EXCLUDED.modified,
title = EXCLUDED.title,
title_spacy = EXCLUDED.title_spacy,
name = EXCLUDED.name,
description = EXCLUDED.description,
description_spacy = EXCLUDED.description_spacy,
tags = EXCLUDED.tags,
license_title = EXCLUDED.license_title,
license_url = EXCLUDED.license_url,
organization_display_name = EXCLUDED.organization_display_name,
organization_name = EXCLUDED.organization_name,
organization_image_url = EXCLUDED.organization_image_url,
organization_description = EXCLUDED.organization_description,
raw_metadata = EXCLUDED.raw_metadata
RETURNING key;""",
(
crawler_table,
crawler_key,
original_host,
num_files,
created,
modified,
title,
Json(title_spacy),
name,
description,
Json(description_spacy),
tags,
license_title,
license_url,
organization_display_name,
organization_name,
organization_image_url,
organization_description,
Json(metadata),
))
package_key = cur.fetchone()["key"]
# Package file fields extracted
created = metadata["resource"].get("createdAt")
modified = metadata["resource"].get("data_updated_at")
original_url = metadata["link"]
file_format = 'AVRO'
file_size = dataset_size
blob_name = resource_blob_name
raw_metadata = metadata["resource"]
# Save pacakge file:
cur.execute(r"""INSERT INTO findopendata.package_files
(
package_key,
crawler_table,
crawler_key,
id,
created,
modified,
original_url,
format,
file_size,
blob_name,
raw_metadata
) VALUES
(%s, %s, %s, uuid_generate_v1mc(), %s,
%s, %s, %s, %s, %s, %s)
ON CONFLICT (crawler_table, crawler_key) DO UPDATE SET
updated = current_timestamp,
created = EXCLUDED.created,
modified = EXCLUDED.modified,
original_url = EXCLUDED.original_url,
format = EXCLUDED.format,
file_size = EXCLUDED.file_size,
blob_name = EXCLUDED.blob_name,
raw_metadata = EXCLUDED.raw_metadata
RETURNING key;""",
(
package_key,
crawler_table,
crawler_key,
created,
modified,
original_url,
file_format,
file_size,
blob_name,
Json(raw_metadata)
))
# Commit all changes.
conn.commit()
conn.close()
logger.info("Indexed Socrata resource {}".format(crawler_key))