def genIIIFManifest(self, on_demand=False): # run singleObjectPackage ''' A bit of a hack here: creating getParams{} with pid as list[] as expected by singleObjectPackage(), simulates normal WSUDOR_API use of singleObjectPackage() ''' getParams = {} getParams['PID'] = [self.pid] # run singleObjectPackage() from API if on_demand == True: getParams['on_demand'] = True single_json = json.loads(singleObjectPackage(getParams)) else: single_json = json.loads(singleObjectPackage(getParams)) # create root mani obj try: manifest = iiif_manifest_factory_instance.manifest( label=single_json['objectSolrDoc']['mods_title_ms'][0] ) except: manifest = iiif_manifest_factory_instance.manifest( label="Unknown Title" ) manifest.viewingDirection = "left-to-right" # build metadata ''' Order of preferred fields is the order they will show on the viewer NOTE: solr items are stored here as strings so they won't evaluate ''' preferred_fields = [ ("Title", "single_json['objectSolrDoc']['mods_title_ms'][0]"), ("Description", "single_json['objectSolrDoc']['mods_abstract_ms'][0]"), ("Year", "single_json['objectSolrDoc']['mods_key_date_year'][0]"), ("Item URL", "\"<a href='{url}'>{url}</a>\".format(url=single_json['objectSolrDoc']['mods_location_url_ms'][0])"), ("Original", "single_json['objectSolrDoc']['mods_otherFormat_note_ms'][0]") ] for field_set in preferred_fields: try: manifest.set_metadata({ field_set[0]:eval(field_set[1]) }) except: print "Could Not Set Metadata Field, Skipping",field_set[0] # start anonymous sequence seq = manifest.sequence(label="default sequence") # get component parts image_list = [ds for ds in self.ohandle.ds_list if ds.endswith('JP2')] image_list.sort(key=natural_sort_key) print image_list # iterate through component parts for image in image_list: print "adding",image # generate obj|ds self.pid as defined in loris TemplateHTTP extension fedora_http_ident = "fedora:%s|%s" % (self.pid,image) # fedora_http_ident = "%s|%s" % (self.pid,image) #loris_dev # Create a canvas with uri slug of page-1, and label of Page 1 cvs = seq.canvas(ident=fedora_http_ident, label=image) # Create an annotation on the Canvas anno = cvs.annotation() # Add Image: http://www.example.org/path/to/image/api/p1/full/full/0/native.jpg img = anno.image(fedora_http_ident, iiif=True) # OR if you have a IIIF service: img.set_hw_from_iiif() cvs.height = img.height cvs.width = img.width # insert into Redis and return JSON string print "Inserting manifest for",self.pid,"into Redis..." redisHandles.r_iiif.set(self.pid,manifest.toString()) return manifest.toString()
def genIIIFManifest(self, on_demand=False): ''' Currently generates IIIF manifest with one sequence, handful of canvases for each image. ''' # Wait until risearch catches up with constituent objects print "waiting for risearch to catch up..." stime = time.time() ttime = 0 while ttime < 30 : sparql_count = fedora_handle.risearch.sparql_count('select $page where {{ $page <fedora-rels-ext:isConstituentOf> <info:fedora/%s> . }}' % (self.pid)) print sparql_count if sparql_count < len(self.pages_from_objMeta): time.sleep(.5) ttime = time.time() - stime print ttime continue else: print 'constituent objects found in risearch, continuing' break # get solr_doc solr_doc = self.SolrDoc.asDictionary() # create root mani obj try: manifest = iiif_manifest_factory_instance.manifest( label=solr_doc['mods_title_ms'][0] ) except: manifest = iiif_manifest_factory_instance.manifest( label="Unknown Title" ) manifest.viewingDirection = "left-to-right" # build metadata ''' Order of preferred fields is the order they will show on the viewer NOTE: solr items are stored here as strings so they won't evaluate ''' preferred_fields = [ ("Title", "solr_doc['mods_title_ms'][0]"), ("Description", "solr_doc['mods_abstract_ms'][0]"), ("Year", "solr_doc['mods_key_date_year'][0]"), ("Item URL", "\"<a href='%s'>%s</a>\" % (solr_doc['mods_location_url_ms'][0],solr_doc['mods_location_url_ms'][0])"), ("Original", "solr_doc['mods_otherFormat_note_ms'][0]") ] for field_set in preferred_fields: try: manifest.set_metadata({ field_set[0] : eval(field_set[1]) }) except: print "Could Not Set Metadata Field, Skipping",field_set[0] # start anonymous sequence seq = manifest.sequence(label="default sequence") # write constituent pages ''' This is tricky. When ingesting new objects, the rels-ext relationships are written at a slight delay. As such, without waiting / confirming all relationships are indexed in risearch, results in only subsets of the whole book getting added. ''' # self.pages_from_rels for page_num in self.pages_from_rels: # open wsudor handle page_handle = WSUDOR_ContentTypes.WSUDOR_Object(self.pages_from_rels[page_num]) print "Working on:",page_handle.ohandle.label # generate obj|ds self.pid as defined in loris TemplateHTTP extension fedora_http_ident = "fedora:%s|JP2" % (page_handle.pid) # Instantiate canvas under sequence cvs = seq.canvas(ident=fedora_http_ident, label=page_handle.ohandle.label) # Create an annotation on the Canvas anno = cvs.annotation() # Add Image Annotation img = anno.image(fedora_http_ident, iiif=True) img.id = fedora_http_ident img.set_hw_from_iiif() # set canvas dimensions cvs.height = img.height cvs.width = img.width # create annotationsList for page object annol = cvs.annotationList("%s" % (page_handle.pid)) # create annotations for HTML and ALTOXML content # HTML anno = annol.annotation() anno.text(ident="https://%s/WSUAPI/bitStream/%s/HTML" % (localConfig.APP_HOST, page_handle.pid), format="text/html") # ALTOXML anno = annol.annotation() anno.text(ident="https://%s/WSUAPI/bitStream/%s/ALTOXML" % (localConfig.APP_HOST, page_handle.pid), format="text/xml") # push annotationList to page object ''' Automatically updates / overwrites ''' print "Inserting annotation list for",page_handle.pid,"as object datastream..." ds_handle = eulfedora.models.DatastreamObject(page_handle.ohandle, "IIIF_ANNOLIST", "IIIF_ANNOLIST", mimetype="application/json", control_group="M") ds_handle.label = "IIIF_ANNOLIST" ds_handle.content = annol.toString() ds_handle.save() # create datastream with IIIF manifest and return JSON string print "Inserting manifest for",self.pid,"as object datastream..." ds_handle = eulfedora.models.DatastreamObject(self.ohandle, "IIIF_MANIFEST", "IIIF_MANIFEST", mimetype="application/json", control_group="M") ds_handle.label = "IIIF_MANIFEST" ds_handle.content = manifest.toString() ds_handle.save() return manifest.toString()
def genIIIFManifest(self): stime = time.time() # create root mani obj try: manifest = iiif_manifest_factory_instance.manifest( label=self.SolrDoc.asDictionary()['mods_title_ms'][0] ) except: manifest = iiif_manifest_factory_instance.manifest( label="Unknown Title" ) manifest.viewingDirection = "left-to-right" # build metadata ''' Order of preferred fields is the order they will show on the viewer NOTE: solr items are stored here as strings so they won't evaluate ''' preferred_fields = [ ("Title", "self.SolrDoc.asDictionary()['mods_title_ms'][0]"), ("Description", "self.SolrDoc.asDictionary()['mods_abstract_ms'][0]"), ("Year", "self.SolrDoc.asDictionary()['mods_key_date_year'][0]"), ("Item URL", "\"<a href='{url}'>{url}</a>\".format(url=self.SolrDoc.asDictionary()['mods_location_url_ms'][0])"), ("Original", "self.SolrDoc.asDictionary()['mods_otherFormat_note_ms'][0]") ] for field_set in preferred_fields: try: manifest.set_metadata({ field_set[0]:eval(field_set[1]) }) except: print "Could Not Set Metadata Field, Skipping",field_set[0] # start anonymous sequence seq = manifest.sequence(label="collection thumbs") # get component parts ''' For collection object, this will be all children objects. Interesting hack here: use API functions without traversing twisted HTTP cycle ''' obj_list = json.loads( WSUDOR_API.functions.availableFunctions.hasMemberOfCollection({'PID':[self.pid]}) ) # iterate through component parts for obj in obj_list['results']: print "adding",obj['memberTitle'] # generate obj|ds self.pid as defined in loris TemplateHTTP extension fedora_http_ident = "fedora:%s|%s" % (obj['object'], obj['isRepBy']+"_JP2") # fedora_http_ident = "%s|%s" % (obj['object'], obj['isRepBy']+"_JP2") #loris_dev # Create a canvas with uri slug cvs = seq.canvas(ident=fedora_http_ident, label=obj['memberTitle']) # Create an annotation on the Canvas anno = cvs.annotation() # Add Image: http://www.example.org/path/to/image/api/p1/full/full/0/native.jpg img = anno.image(fedora_http_ident, iiif=True) # OR if you have a IIIF service: img.set_hw_from_iiif() cvs.height = img.height cvs.width = img.width # insert into Redis and return JSON string print "Inserting manifest for",self.pid,"into Redis..." # report time etime = time.time() ttime = etime - stime print "total time",ttime # redisHandles.r_iiif.set(self.pid,manifest.toString()) return manifest.toString()
def genIIIFManifest(self, on_demand=False): # run singleObjectPackage ''' A bit of a hack here: creating getParams{} with pid as list[] as expected by singleObjectPackage(), simulates normal WSUDOR_API use of singleObjectPackage() ''' getParams = {} getParams['PID'] = [self.pid] # run singleObjectPackage() from API if on_demand == True: getParams['on_demand'] = True single_json = json.loads(singleObjectPackage(getParams)) else: single_json = json.loads(singleObjectPackage(getParams)) # create root mani obj try: manifest = iiif_manifest_factory_instance.manifest( label=single_json['objectSolrDoc']['mods_title_ms'][0] ) except: manifest = iiif_manifest_factory_instance.manifest( label="Unknown Title" ) manifest.viewingDirection = "left-to-right" # build metadata ''' Order of preferred fields is the order they will show on the viewer NOTE: solr items are stored here as strings so they won't evaluate ''' preferred_fields = [ ("Title", "single_json['objectSolrDoc']['mods_title_ms'][0]"), ("Description", "single_json['objectSolrDoc']['mods_abstract_ms'][0]"), ("Year", "single_json['objectSolrDoc']['mods_key_date_year'][0]"), ("Item URL", "\"<a href='%s'>%s</a>\" % (single_json['objectSolrDoc']['mods_location_url_ms'][0],single_json['objectSolrDoc']['mods_location_url_ms'][0])"), ("Original", "single_json['objectSolrDoc']['mods_otherFormat_note_ms'][0]") ] for field_set in preferred_fields: try: manifest.set_metadata({ field_set[0]:eval(field_set[1]) }) except: print "Could Not Set Metadata Field, Skipping",field_set[0] # start anonymous sequence seq = manifest.sequence(label="default sequence") # iterate through component parts for image in single_json['parts_imageDict']['sorted']: # generate obj|ds identifier as defined in loris TemplateHTTP extension fedora_http_ident = "fedora:%s|%s" % (self.pid,image['jp2']) # fedora_http_ident = "%s|%s" % (self.pid,image['jp2']) #loris_dev # Create a canvas with uri slug of page-1, and label of Page 1 cvs = seq.canvas(ident=fedora_http_ident, label=image['ds_id']) # Create an annotation on the Canvas anno = cvs.annotation() # Add Image: http://www.example.org/path/to/image/api/p1/full/full/0/native.jpg img = anno.image(fedora_http_ident, iiif=True) # OR if you have an IIIF service: img.set_hw_from_iiif() cvs.height = img.height cvs.width = img.width # create datastream with IIIF manifest and return JSON string print "Inserting manifest for",self.pid,"as object datastream..." ds_handle = eulfedora.models.DatastreamObject(self.ohandle, "IIIF_MANIFEST", "IIIF_MANIFEST", mimetype="application/json", control_group="M") ds_handle.label = "IIIF_MANIFEST" ds_handle.content = manifest.toString() ds_handle.save() return manifest.toString()