# HG changeset patch # User ymh # Date 1416162912 -3600 # Node ID 6129e7390abef36cd0be172181af1cece7c7a5a0 # Parent 46ad324f6fe4d720e604748168198806e7075bbb factorise code diff -r 46ad324f6fe4 -r 6129e7390abe src/hdalab/management/commands/query_dbpedia.py --- a/src/hdalab/management/commands/query_dbpedia.py Sun Nov 16 18:09:42 2014 +0100 +++ b/src/hdalab/management/commands/query_dbpedia.py Sun Nov 16 19:35:12 2014 +0100 @@ -69,6 +69,12 @@ help='the tag to query'), ) + def query_dbpedia(self, query, fmt=N3): + endpoint = SPARQLWrapper(settings.DBPEDIA_URI_TEMPLATE % ( 'sparql', '' ), returnFormat=fmt) + endpoint.setQuery(query) + return Graph().parse(data=endpoint.queryAndConvert(), format=fmt) + + def handle_noargs(self, **options): self.style = no_style() @@ -126,9 +132,6 @@ print "dbpedia query cancelled" return - #endpoint = SPARQLWrapper("http://dbpedia.org/sparql", returnFormat=RDF) - endpoint = SPARQLWrapper(settings.DBPEDIA_URI_TEMPLATE % ( 'sparql', '' ), returnFormat=N3) - writer = None for i,tag in enumerate(queryset): writer = show_progress(i+1, count, tag.label, 50, writer) @@ -145,27 +148,21 @@ labels = {} thumbnail = None with transaction.commit_on_success(): - endpoint.setQuery("select distinct ?y where {<%s> ?y}" % (tag.dbpedia_uri)) - res_abstracts = Graph().parse(data=endpoint.queryAndConvert(), format='n3') + res_abstracts = self.query_dbpedia("select distinct ?y where {<%s> ?y}" % (tag.dbpedia_uri), N3) for _,_,o in res_abstracts.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): abstracts[o.language] = (unicode(o), True) logger.debug("Abstracts: %r" % abstracts) - endpoint.setQuery("select distinct ?y where {<%s> ?y}" % (tag.dbpedia_uri)) - #res_labels = endpoint.queryAndConvert() - res_labels = Graph().parse(data=endpoint.queryAndConvert(), format='n3') + res_labels = self.query_dbpedia("select distinct ?y where {<%s> ?y}" % (tag.dbpedia_uri), N3) for _,_,o in res_labels.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): labels[o.language] = (unicode(o), True) logger.debug("Labels: %r" % labels) - endpoint.setQuery("select distinct ?y where {<%s> ?y} limit 1" % (tag.dbpedia_uri)) - res_thumbnails = Graph().parse(data=endpoint.queryAndConvert(), format='n3') + res_thumbnails = self.query_dbpedia("select distinct ?y where {<%s> ?y} limit 1" % (tag.dbpedia_uri), N3) for _,_,o in res_thumbnails.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): thumbnail = unicode(o) - #endpoint.setQuery('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^http://dbpedia.org/resource")}' % (tag.dbpedia_uri)) - endpoint.setQuery('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^%s")}' % (tag.dbpedia_uri, settings.DBPEDIA_URI_TEMPLATE % ( 'resource', '' ))) - res_links = Graph().parse(data=endpoint.queryAndConvert(), format='n3') + res_links = self.query_dbpedia('select distinct ?y where { <%s> ?p ?y . FILTER regex(?y, "^%s")}' % (tag.dbpedia_uri, settings.DBPEDIA_URI_TEMPLATE % ( 'resource', '' )), N3) for _,_,o in res_links.triples((None, URIRef('http://www.w3.org/2005/sparql-results#value'), None)): tagqs = Tag.objects.filter(dbpedia_uri=unicode(o)) if tagqs: