Created
November 21, 2012 19:53
-
-
Save lawlesst/4127251 to your computer and use it in GitHub Desktop.
Creating the RDF for images in VIVO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Jython image loader. | |
- requires the VIVO harvester or Jena to be on your CLASSPATH | |
- requires a get_next_uri utility not included | |
- requires Jython | |
- leave a comment or contact lawlesst AT gmail dot com if you have questions. | |
This will create a file of N3 RDF that can be loaded into a VIVO instance. | |
After the RDF is added, rsync your local directory of images to the VIVO data directory. | |
E.g. rsync -avz /work/vivo_ingest/images/prepared/file_storage_root/ [email protected]:/opt/local/vivo/data/uploads/file_storage_root/ | |
""" | |
import glob | |
import sys | |
import os | |
from datetime import datetime | |
from com.hp.hpl.jena.rdf.model import * | |
from com.hp.hpl.jena.vocabulary import * | |
from com.mysql.jdbc import * | |
from java.sql import * | |
from com.hp.hpl.jena.db import DBConnection; | |
from com.hp.hpl.jena.sdb.sql import SDBConnection | |
from com.hp.hpl.jena.sdb.store import LayoutType | |
from com.hp.hpl.jena.sdb.store import DatabaseType | |
from com.hp.hpl.jena.rdf.model.impl import ResourceImpl | |
from com.hp.hpl.jena.ontology import OntModel; | |
from com.hp.hpl.jena.ontology import OntModelSpec; | |
from com.hp.hpl.jena.util import FileManager; | |
vivo = 'http://vivoweb.org/ontology/core#' | |
from utils import get_next_uri | |
import utils | |
def get_thumbnails(path): | |
path = path.rstrip('/') | |
thumbs = [] | |
thumbs += glob.glob('%s/*_thumb.jpg' % path) | |
return thumbs | |
pth = sys.argv[1] | |
thumbs = get_thumbnails(pth) | |
ns = 'http://vivo.brown.edu/individual/' | |
vitro = 'http://vitro.mannlib.cornell.edu/ns/vitro/public#' | |
model = ModelFactory.createDefaultModel() | |
vivo_model = dataset.getNamedModel('http://vitro.mannlib.cornell.edu/default/vitro-kb-2') | |
#List to hold new uris we assign to temp graph. | |
used_uris = [] | |
def vitro_property(value): | |
return model.createProperty(vitro + value) | |
def literal(value): | |
return model.createLiteral(value) | |
for n, thumb in enumerate(thumbs): | |
print thumb | |
mod_time = model.createLiteral(datetime.now().isoformat()) | |
person = thumb.split(os.path.sep)[-1].split('_')[0] | |
main_filename = '%s.jpg' % person | |
thumb_filename = os.path.split(thumb)[-1] | |
thumb_path = thumb | |
main_path = os.path.join(pth, main_filename) | |
#Create FileBytestreams | |
#Thumbnail | |
thumb_uri = get_next_uri(ns, vivo_model, used_uris=used_uris) | |
used_uris.append(thumb_uri) | |
thumb_id = thumb_uri.replace(ns, '') | |
thumb_resource = model.createResource(thumb_uri) | |
thumb_resource.addProperty(vitro_property('modTime'), mod_time) | |
#vitro-public:directDownloadUrl "/file/n1233699/cbriant_thumb.jpg" ; | |
thumb_resource.addProperty( | |
vitro_property('directDownloadUrl'), | |
literal('/file/%s/%s' % (thumb_id, thumb_filename)) | |
) | |
thumb_resource.addProperty( | |
RDF.type, | |
vitro_property('FileByteStream') | |
) | |
#Main | |
main_uri = get_next_uri(ns, vivo_model, used_uris=used_uris) | |
used_uris.append(main_uri) | |
main_id = main_uri.replace(ns, '') | |
main_resource = model.createResource(main_uri) | |
main_resource.addProperty(vitro_property('modTime'), mod_time) | |
main_resource.addProperty( | |
vitro_property('directDownloadUrl'), | |
literal('/file/%s/%s' % (main_id, main_filename)) | |
) | |
main_resource.addProperty( | |
RDF.type, | |
vitro_property('FileByteStream') | |
) | |
#Thumbnail surrogate | |
thumb_surrogate_uri = get_next_uri(ns, model, used_uris=used_uris) | |
used_uris.append(thumb_surrogate_uri) | |
thumb_surrogate_resource = model.createResource(thumb_surrogate_uri) | |
thumb_surrogate_resource.addProperty( | |
vitro_property('downloadLocation'), | |
thumb_resource | |
) | |
thumb_surrogate_resource.addProperty( | |
vitro_property('mimeType'), | |
literal('image/jpeg') | |
) | |
thumb_surrogate_resource.addProperty( | |
vitro_property('filename'), | |
thumb_filename | |
) | |
thumb_surrogate_resource.addProperty( | |
vitro_property('modTime'), | |
mod_time | |
) | |
thumb_surrogate_resource.addProperty( | |
RDF.type, | |
vitro_property('File') | |
) | |
#Image surrogate | |
main_surrogate_uri = get_next_uri(ns, model, used_uris=used_uris) | |
used_uris.append(main_surrogate_uri) | |
main_surrogate_resource = model.createResource(main_surrogate_uri) | |
main_surrogate_resource.addProperty( | |
vitro_property('thumbnailImage'), | |
thumb_surrogate_resource | |
) | |
main_surrogate_resource.addProperty( | |
vitro_property('downloadLocation'), | |
main_resource, | |
) | |
main_surrogate_resource.addProperty( | |
vitro_property('mimeType'), | |
literal('image/jpeg') | |
) | |
main_surrogate_resource.addProperty( | |
vitro_property('filename'), | |
main_filename | |
) | |
main_surrogate_resource.addProperty( | |
vitro_property('modTime'), | |
mod_time | |
) | |
main_surrogate_resource.addProperty( | |
RDF.type, | |
vitro_property('File') | |
) | |
#Link person | |
ind = model.createProperty(ns + person) | |
ind.addProperty( | |
vitro_property('mainImage'), | |
main_surrogate_resource | |
) | |
#Copy the images to the vivo path. | |
#Copy thumbnail | |
VIVO_UPLOADS = 'prepared/file_storage_root/b~n/' | |
utils.copy_image(thumb_id, thumb_path, VIVO_UPLOADS) | |
#Copy main image | |
utils.copy_image(main_id, main_path, VIVO_UPLOADS) | |
#if n == 2: | |
# break | |
#We should write this to a file in case we want to rmove some or all later. | |
#model.write(System.out, "N3"); | |
outfile = open('faculty_images.n3', 'w') | |
model.write(outfile, "N3") | |
outfile.close() | |
#Can write directly to SDB if you like. | |
# vivo_model.begin() | |
# #Add the temp graph | |
# vivo_model.add(model) | |
# vivo_model.commit() | |
# vivo_model.close() | |
store.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment