Permalink
Browse files

Added edan_select_id module.

  • Loading branch information...
1 parent 589d019 commit 692f0342c161e8554139ce8b9aa8d504cbf99042 Szymon Guz committed Mar 19, 2013
Showing with 62 additions and 1 deletion.
  1. +2 −1 akara.conf
  2. +60 −0 lib/akamod/edan_select_id.py
View
@@ -132,7 +132,8 @@ MODULES = [
"dplaingestion.akamod.edan_to_dpla",
"dplaingestion.akamod.dpla-get-record",
"dplaingestion.akamod.primo-to-dpla",
- "dplaingestion.akamod.mwdl_enrich_state_located_in"
+ "dplaingestion.akamod.mwdl_enrich_state_located_in",
+ "dplaingestion.akamod.edan_select_id"
]
### Section 3: Other module configuration goes here
@@ -0,0 +1,60 @@
+import hashlib
+from amara.thirdparty import json
+from amara.lib.iri import is_absolute
+from akara.services import simple_service
+from akara.util import copy_headers_to_dict
+from akara import request, response, logger
+from dplaingestion.selector import getprop, setprop, exists
+
+COUCH_ID_BUILDER = lambda src, lname: "--".join((src,lname))
+COUCH_REC_ID_BUILDER = lambda src, id_handle: COUCH_ID_BUILDER(src,id_handle.strip().replace(" ","__"))
+
+@simple_service('POST', 'http://purl.org/la/dp/edan_select_id', 'edan_select_id', 'application/json')
+def selid(body,ctype,prop='descriptiveNonRepeating/record_link', alternative_prop='descriptiveNonRepeating/record_ID'):
+ '''
+ Service that accepts a JSON document and adds or sets the "id" property to the
+ value of the property named by the "prop" paramater
+ '''
+ tmpl="http://collections.si.edu/search/results.htm?q=record_ID%%3A%s&repo=DPLA"
+
+ if not prop:
+ response.code = 500
+ response.add_header('content-type','text/plain')
+ return "No id property has been selected"
+
+ try :
+ data = json.loads(body)
+ except:
+ response.code = 500
+ response.add_header('content-type','text/plain')
+ return "Unable to parse body as JSON"
+
+ request_headers = copy_headers_to_dict(request.environ)
+ source_name = request_headers.get('Source')
+
+ id = None
+
+ if exists(data, prop) or exists(data, alternative_prop):
+ v = getprop(data,prop, True)
+ if not v:
+ v = getprop(data, alternative_prop)
+ v = tmpl % v
+ if isinstance(v,basestring):
+ id = v
+ else:
+ if v:
+ for h in v:
+ if is_absolute(h):
+ id = h
+ if not id:
+ id = v[0]
+
+ if not id:
+ response.code = 500
+ response.add_header('content-type','text/plain')
+ return "No id property was found"
+
+ data[u'_id'] = COUCH_REC_ID_BUILDER(source_name, id)
+ data[u'id'] = hashlib.md5(data[u'_id']).hexdigest()
+
+ return json.dumps(data)

0 comments on commit 692f034

Please sign in to comment.