-
Notifications
You must be signed in to change notification settings - Fork 4
/
load.py
199 lines (140 loc) · 4.76 KB
/
load.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
import logging
import re
import ckanapi
from harvester.utils.util import sort_dataset
logger = logging.getLogger("harvester")
def load():
""" """
logger.info("Hello from harvester.load()")
# stub
pass
def create_ckan_extra_base(*args):
keys = ["publisher_hierarchy", "resource-type", "publisher"]
data = zip(keys, args)
return [{"key": d[0], "value": d[1]} for d in data]
def create_ckan_extras_additions(dcatus_dataset, additions):
extras = [
"accessLevel",
"bureauCode",
"identifier",
"modified",
"programCode",
"publisher",
]
output = []
for extra in extras:
data = {"key": extra, "value": None}
val = dcatus_dataset[extra]
if extra == "publisher":
data["value"] = val["name"]
else:
if isinstance(val, list): # TODO: confirm this is what we want.
val = val[0]
data["value"] = val
output.append(data)
return output + additions
def create_ckan_tags(keywords):
output = []
for keyword in keywords:
keyword = "-".join(keyword.split())
output.append({"name": keyword})
return output
def create_ckan_publisher_hierarchy(pub_dict, data=[]):
for k, v in pub_dict.items():
if k == "name":
data.append(v)
if isinstance(v, dict):
create_ckan_publisher_hierarchy(v, data)
return " > ".join(data[::-1])
def get_email_from_str(in_str):
res = re.search(r"[\w.+-]+@[\w-]+\.[\w.-]+", in_str)
if res is not None:
return res.group(0)
def create_ckan_resources(dcatus_dataset):
output = []
if "distribution" not in dcatus_dataset:
return output
for dist in dcatus_dataset["distribution"]:
url_key = "downloadURL" if "downloadURL" in dist else "accessURL"
resource = {"url": dist[url_key]}
if "mimetype" in dist:
resource["mimetype"] = dist["mediaType"]
output.append(resource)
return output
def simple_transform(dcatus_dataset):
output = {
"name": "-".join(dcatus_dataset["title"].lower().split()),
"owner_org": "test", # TODO: CHANGE THIS!
"identifier": dcatus_dataset["identifier"],
}
mapping = {
"contactPoint": {"fn": "maintainer", "hasEmail": "maintainer_email"},
"description": "notes",
"title": "title",
}
for k, v in dcatus_dataset.items():
if k not in mapping:
continue
if isinstance(mapping[k], dict):
temp = {}
to_skip = ["@type"]
for k2, v2 in v.items():
if k2 == "hasEmail":
v2 = get_email_from_str(v2)
if k2 in to_skip:
continue
temp[mapping[k][k2]] = v2
output = {**output, **temp}
else:
output[mapping[k]] = v
return output
def create_defaults():
return {
"author": None,
"author_email": None,
}
def dcatus_to_ckan(dcatus_dataset, harvest_source_name):
"""
example:
- from this:
- https://catalog.data.gov/harvest/object/cb22fea9-0c90-43e9-94bf-903eacd37c92
- to this:
- https://catalog.data.gov/api/action/package_show?id=fdic-failed-bank-list
"""
output = simple_transform(dcatus_dataset)
resources = create_ckan_resources(dcatus_dataset)
tags = create_ckan_tags(dcatus_dataset["keyword"])
pubisher_hierarchy = create_ckan_publisher_hierarchy(
dcatus_dataset["publisher"], []
)
extras_base = create_ckan_extra_base(
pubisher_hierarchy, "Dataset", dcatus_dataset["publisher"]["name"]
)
extras = create_ckan_extras_additions(dcatus_dataset, extras_base)
defaults = create_defaults()
output["resources"] = resources
output["tags"] = tags
output["extras"] = extras_base
output["extras"] += extras
output["extras"] += [
{
"key": "dcat_metadata",
"value": str(sort_dataset(dcatus_dataset)),
}
]
output["extras"] += [{"key": "harvest_source_name", "value": harvest_source_name}]
return {**output, **defaults}
def create_ckan_entrypoint(url, api_key):
return ckanapi.RemoteCKAN(url, apikey=api_key)
def create_ckan_package(ckan, package_data):
return ckan.action.package_create(**package_data)
def patch_ckan_package(ckan, patch_data):
# partially updates the package
return ckan.action.package_patch(**patch_data)
def update_ckan_package(ckan, update_data):
# fully replaces the package
return ckan.action.package_update(**update_data)
def purge_ckan_package(ckan, package_data):
return ckan.action.dataset_purge(**package_data)
def search_ckan(ckan, query):
return ckan.action.package_search(**query)