-
Notifications
You must be signed in to change notification settings - Fork 0
/
baserow_utils.py
343 lines (325 loc) · 12.6 KB
/
baserow_utils.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
import requests
import geocoder
import json
from acdh_id_reconciler import gnd_to_wikidata_custom, geonames_to_gnd, geonames_to_wikidata
from AcdhArcheAssets.uri_norm_rules import get_normalized_uri
from acdh_obj2xml_pyutils import ObjectToXml
from config import (BASEROW_URL, BASEROW_TOKEN, br_client)
def enrich_data(br_table_id, uri, field_name_input, field_name_update):
table = [x for x in br_client.yield_rows(br_table_id=br_table_id)]
br_rows_url = f"{BASEROW_URL}database/rows/table/{br_table_id}/"
v_wd = 0
v_pmb = 0
v_geo = 0
for x in table:
update = {}
if uri == "gnd":
try:
norm_id = get_normalized_uri(x[field_name_input["gnd"]])
print(norm_id)
except Exception as err:
print(err)
try:
wdc = gnd_to_wikidata_custom(norm_id, "P12483")
wd = wdc["wikidata"]
update[field_name_update["wikidata"]] = wd
if len(wdc["custom"]) > 0:
pmb = wdc["custom"]
update[field_name_update["pmb"]] = f"https://pmb.acdh.oeaw.ac.at/entity/{pmb}"
v_pmb += 1
print(f"gnd id matched with pmb: https://pmb.acdh.oeaw.ac.at/entity/{pmb}")
v_wd += 1
print(f"gnd id matched with wikidata: {wd}")
except Exception as err:
print(err)
print(f"no match for {norm_id} found.")
if uri == "geonames":
try:
norm_id = get_normalized_uri(x[field_name_input["geonames"]])
print(norm_id)
update[field_name_update["geonames"]] = norm_id
except Exception as err:
print(err)
try:
geo = geonames_to_gnd(norm_id)
gnd = geo["gnd"]
update[field_name_update["gnd"]] = f"https://d-nb.info/gnd/{gnd}"
wd = geo["wikidata"]
update[field_name_update["wikidata"]] = wd
v_geo += 1
print(f"geonames id matched with gnd: {gnd} and wikidata: {wd}")
except Exception:
try:
wd = geonames_to_wikidata(norm_id)
wd = wd["wikidata"]
update[field_name_update["wikidata"]] = wd
print(f"geonames id matched with wikidata id: {wd}")
except Exception:
print(f"no wikidata match for {norm_id} found.")
print(f"no gnd match for {norm_id} found.")
if update:
print(update)
update["updated"] = True
row_id = x["id"]
url = f"{br_rows_url}{row_id}/?user_field_names=true"
print(url)
try:
requests.patch(
url,
headers={
"Authorization": f"Token {BASEROW_TOKEN}",
"Content-Type": "application/json"
},
json=update
)
except Exception as err:
print(err)
print(f"""{str(v_wd)} wikidata uri, {str(v_pmb)} pmb uri and {str(v_geo)}
geonames uri of {len(table)} table rows matched""")
def geonames_to_location(br_table_id, user, field_name_input, field_name_update):
table = [x for x in br_client.yield_rows(br_table_id=br_table_id)]
br_rows_url = f"{BASEROW_URL}database/rows/table/{br_table_id}/"
geo_u = 0
for x in table:
update = {}
if (x["updated_geonames"] is False):
try:
norm_id = get_normalized_uri(x[field_name_input["geonames"]])
print(norm_id)
geo_id = norm_id.split('/')[-2]
except Exception as err:
print(err)
try:
g = geocoder.geonames(geo_id, method='details', key=user)
lat = g.lat
lng = g.lng
typ = g.class_description
typ_c = g.feature_class
ctry_c = g.country_code
ctry = g.country
if lat and lng:
update[field_name_update["coordinates"]] = f"{lat}, {lng}"
if typ:
update[field_name_update["place_type"]] = typ
if typ_c:
update[field_name_update["place_type_class"]] = typ_c
if ctry:
update[field_name_update["country"]] = ctry
if ctry_c:
update[field_name_update["country_code"]] = ctry_c
geo_u += 1
print(f"geonames id {geo_id} found. Updating lat: {lat} and lng: {lng}")
except Exception:
print(f"no match for {norm_id} found.")
if update:
update["updated_geonames"] = True
row_id = x["id"]
url = f"{br_rows_url}{row_id}/?user_field_names=true"
print(url)
try:
requests.patch(
url,
headers={
"Authorization": f"Token {BASEROW_TOKEN}",
"Content-Type": "application/json"
},
json=update
)
except Exception as err:
print(err)
print(f"{geo_u} geonames uri and of {len(table)} table rows matched")
def make_xml(input, fn, clmn, temp):
with open(input, "rb") as f:
file = json.load(f)
arr = []
for f in file:
obj = file[f]
try:
any_id = obj[clmn]
norm_id = get_normalized_uri(any_id)
obj[clmn] = norm_id
except KeyError as err:
print(err)
if clmn == "is_linked_with":
try:
uri = obj["is_linked_with"].split("/")[2]
except IndexError:
uri = "none"
if uri == "www.wikidata.org":
obj["wikidata"] = obj["is_linked_with"]
elif uri == "d-nb.info":
obj["gnd"] = obj["is_linked_with"]
elif uri != "baserow.acdh-dev.oeaw.ac.at":
obj["other"] = obj["is_linked_with"]
arr.append(obj)
filename = fn
template_file = f"templates/{temp}.xml"
obj_cl = ObjectToXml(br_input=arr, filename=filename, template_path=template_file)
tei = obj_cl.make_xml_single(save=True)
print(f"{fn}.xml created")
return tei
def make_geojson(input, fn, clmn1, clmn2, clm3):
geojson = {
"type": "FeatureCollection",
"features": []
}
with open(input, "rb") as f:
file = json.load(f)
for f in file:
obj = file[f]
try:
loc = obj[clmn1]
if loc:
if len(loc) != 0:
coords = loc
coords = coords.split(",")
feature_point = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [float(coords[1]), float(coords[0])]
},
"properties": {
"title": obj["name"],
"id": obj["amp_id"],
"country_code": obj["country_code"]
}
}
geojson["features"].append(feature_point)
except KeyError as err:
print(err)
try:
loc = obj[clmn2]
if loc:
if len(loc) != 0:
coords = loc
coords = coords.split(",")
feature_point = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [float(coords[1]), float(coords[0])]
},
"properties": {
"title": obj["name"],
"id": obj["amp_id"],
"country_code": obj["country_code"]
}
}
geojson["features"].append(feature_point)
except KeyError as err:
print(err)
try:
loc = obj[clm3]
except KeyError as err:
print(err)
loc = []
if len(loc) > 0:
nm = obj["name"]
o_id = obj["amp_id"]
for x in loc:
try:
coords = x["data"][clmn1]
except KeyError:
coords = {}
if coords:
coords = coords.split(",")
feature_point = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [float(coords[1]), float(coords[0])]
},
"properties": {
"title": nm,
"id": o_id,
"title_plc": x["data"]["name"],
"id_plc": x["data"]["amp_id"],
"country_code": x["data"]["country_code"]
}
}
geojson["features"].append(feature_point)
else:
try:
coords = x["data"][clmn2]
except KeyError:
coords = {}
if len(coords) > 0:
coords = coords.split(",")
feature_point = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [float(coords[1]), float(coords[0])]
},
"properties": {
"title": nm,
"id": o_id,
"title_plc": x["data"]["name"],
"id_plc": x["data"]["amp_id"],
"country_code": x["data"]["country_code"]
}
}
geojson["features"].append(feature_point)
with open(f"out/{fn}.geojson", "w") as f:
json.dump(geojson, f)
return geojson
def load_lockup(path, mapping):
files = {}
for x in mapping:
ldn = mapping[x].split(".")[0]
with open(f"{path}/{mapping[x]}", "rb") as fb:
files[ldn] = json.load(fb)
# with open(f"{path}/test_{mapping[x]}", "w") as fb:
# json.dump(files, fb)
return files
def load_base(fn):
with open(fn, "rb") as fb:
data = json.load(fb)
return data
def denormalize_json(fn, path, mapping):
save_and_open = f"{path}/{fn}.json"
print(f"updating {save_and_open}")
# load mapping file
mpg = mapping
# load lockup file to match with
files = load_lockup(path, mpg)
# load base json file for matching
dta = load_base(save_and_open)
for m in mpg:
# if mapping key is found in base json
for d in dta:
if dta[d][m]:
# get filename without ext
ldn = mpg[m].split(".")[0]
print(ldn, "json file found")
# get specific mapping from lockup file
lockup = files[ldn]
# iterate over mapping entity array
for i in dta[d][m]:
i_id = i["id"]
print(i_id, "id found")
# use id for lockup file
try:
i_upt = lockup[str(i_id)]
# create normalized data
norm = {n: i_upt[n] for n in i_upt
if n != "id" and n != "order"}
i["data"] = norm
i["data"]["filename"] = mpg[m]
except KeyError:
continue
with open(save_and_open.replace(".json", "__denorm.json"), "w") as w:
json.dump(dta, w)
print(f"finished update of {save_and_open} and save as {save_and_open}.")
return dta
def merge_relationships_json(*args):
new_dict = []
for x in args:
with open(x, "rb") as fb:
file = json.load(fb)
for f in file:
new_dict.append(file[f])
with open("json_dumps/merged_relationships.json", "w") as f:
json.dump(new_dict, f)
return file