This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
mods_to_dpla.py
162 lines (140 loc) · 5.87 KB
/
mods_to_dpla.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
from akara import logger
from akara import request, response
from akara.services import simple_service
from amara.lib.iri import is_absolute
from amara.thirdparty import json
from functools import partial
import base64
import sys
import re
from zen import dateparser
from dateutil.parser import parse as dateutil_parse
import timelib
from dplaingestion.selector import getprop as selector_getprop, exists
def getprop(d, p):
return selector_getprop(d, p, True)
CONTEXT = {
"@vocab": "http://purl.org/dc/terms/",
"dpla": "http://dp.la/terms/",
"edm": "http://www.europeana.eu/schemas/edm/",
"LCSH": "http://id.loc.gov/authorities/subjects",
"name": "xsd:string",
"collection" : "dpla:aggregation",
"aggregatedDigitalResource" : "dpla:aggregatedDigitalResource",
"originalRecord" : "dpla:originalRecord",
"state": "dpla:state",
"coordinates": "dpla:coordinates",
"stateLocatedIn" : "dpla:stateLocatedIn",
"aggregatedCHO" : "edm:aggregatedCHO",
"dataProvider" : "edm:dataProvider",
"hasView" : "edm:hasView",
"isShownAt" : "edm:isShownAt",
"object" : "edm:object",
"provider" : "edm:provider",
"begin" : {
"@id" : "dpla:dateRangeStart",
"@type": "xsd:date"
},
"end" : {
"@id" : "dpla:dateRangeEnd",
"@type": "xsd:date"
}
}
def physical_description_handler(d, p):
note = getprop(d, p)
out = {}
for _dict in note:
if isinstance(_dict, dict) and "displayLabel" in _dict:
if _dict["displayLabel"] == "size inches":
out["extent"] = _dict["#text"]
if _dict["displayLabel"] == "condition":
out["description"] = _dict["#text"]
return out
def subject_handler(d, p):
orig_subject = getprop(d, p)
subjects = {"subject": []}
for _dict in orig_subject:
if "topic" in _dict:
subjects["subject"].append(_dict["topic"])
if "name" in _dict:
subjects["subject"].append(getprop(_dict, "name/namePart"))
return subjects
def location_handler(d, p):
location = getprop(d, p)
format = getprop(d, "physicalDescription/internetMediaType")
out = {}
try:
for _dict in location:
if "url" in _dict:
for url_dict in _dict["url"]:
if url_dict and "access" in url_dict:
if url_dict["access"] == "object in context":
out["isShownAt"] = {"@id:": url_dict["#text"], "format": format}
if url_dict["access"] == "preview":
out["hasView"] = {"@id:": url_dict["#text"], "format": format}
out["object"] = {"@id:": url_dict["#text"], "format": format}
if "physicalLocation" in _dict and isinstance(_dict["physicalLocation"], basestring):
out["dataProvider"] = _dict["physicalLocation"]
except Exception as e:
logger.error(e)
finally:
return out
# Structure mapping the original property to a function returning a single
# item dict representing the new property and its value
CHO_TRANSFORMER = {
"recordInfo/languageOfCataloging/languageTerm/#text": lambda d, p: {"language": getprop(d, p)},
"name/namePart": lambda d, p: {"creator": getprop(d, p)},
"physicalDescription/note": physical_description_handler,
"physicalDescription/form/authority": lambda d, p: {"format": getprop(d, p)},
"originInfo/place/placeTerm": lambda d, p: {"spatial": getprop(d, p)},
"accessCondition": lambda d, p: {"rights": [s["#text"] for s in getprop(d, p) if "#text" in s]},
"subject": subject_handler,
"titleInfo/title": lambda d, p: {"title": getprop(d, p)},
"typeOfResource/#text": lambda d, p: {"type": getprop(d, p)},
"originInfo/dateCreated": lambda d, p: {"date": getprop(d, p)}
}
AGGREGATION_TRANSFORMER = {
"id" : lambda d, p: {"id": getprop(d, p), "@id" : "http://dp.la/api/items/" + getprop(d, p)},
"_id" : lambda d, p: {"_id": getprop(d, p)},
"originalRecord" : lambda d, p: {"originalRecord": getprop(d, p)},
"ingestType" : lambda d, p: {"ingestType": getprop(d, p)},
"ingestDate" : lambda d, p: {"ingestDate": getprop(d, p)},
"location": location_handler,
"recordInfo/recordContentSource": lambda d, p: {"provider": getprop(d, p)}
}
@simple_service('POST', 'http://purl.org/la/dp/mods-to-dpla', 'mods-to-dpla', 'application/ld+json')
def mods_to_dpla(body, ctype, geoprop=None):
"""
Convert output of JSON-ified MODS/METS format into the DPLA JSON-LD format.
Parameter "geoprop" specifies the property name containing lat/long coords
"""
try :
data = json.loads(body)
except:
response.code = 500
response.add_header('content-type','text/plain')
return "Unable to parse body as JSON"
global GEOPROP
GEOPROP = geoprop
out = {
"@context": CONTEXT,
"aggregatedCHO" : {}
}
# Apply all transformation rules from original document
for p in CHO_TRANSFORMER:
if exists(data, p):
out["aggregatedCHO"].update(CHO_TRANSFORMER[p](data, p))
for p in AGGREGATION_TRANSFORMER:
if exists(data, p):
out.update(AGGREGATION_TRANSFORMER[p](data, p))
if "spatial" in out["aggregatedCHO"]:
out["aggregatedCHO"]["spatial"]["currentLocation"] = "Virginia"
# Additional content not from original document
if 'HTTP_CONTRIBUTOR' in request.environ:
try:
out["provider"] = json.loads(base64.b64decode(request.environ['HTTP_CONTRIBUTOR']))
except Exception as e:
logger.debug("Unable to decode Contributor header value: "+request.environ['HTTP_CONTRIBUTOR']+"---"+repr(e))
# Strip out keys with None/null values?
out = dict((k,v) for (k,v) in out.items() if v)
return json.dumps(out)