#!/usr/local/bin/python import sys sys.path.append('/usr/home/bobd/lib/python/') # needed for hosted version from SPARQLWrapper import SPARQLWrapper, JSON import cgi def main(): form = cgi.FieldStorage() pictureSubject = form.getvalue('pictureSubject') sparql = SPARQLWrapper("http://DBpedia.org/sparql") queryString = """ PREFIX rdfs: PREFIX foaf: SELECT ?s ?pictureURI WHERE { { ?s rdfs:label "PICTURE-SUBJECT"@en . ?s foaf:depiction ?pictureURI . } UNION { ?altName rdfs:label "PICTURE-SUBJECT"@en ; ?s . ?s foaf:depiction ?pictureURI . } } """ queryString = queryString.replace("PICTURE-SUBJECT",pictureSubject) sparql.setQuery(queryString) sparql.setReturnFormat(JSON) try: ret = sparql.query() results = ret.convert() requestGood = True except Exception, e: results = str(e) requestGood = False print """Content-type: text/html results """ if requestGood == False: print "

Problem communicating with the server

" print "

" + results + "

" elif (len(results["results"]["bindings"]) == 0): print "

No results found.

" else: pictureURI = results["results"]["bindings"][0]["pictureURI"]["value"] dbpURI = results["results"]["bindings"][0]["s"]["value"] print "

" + pictureSubject + "

" print "

" print "

" + dbpURI + "

" print "" main()