def process(self, input, output=None): if (output == None): output = '.'.join(input.split('.')[:-1]) + '.kml' graph = self.parse(input) #sparql query sparqlGr = sparql.sparqlGraph.SPARQLGraph(graph) select = ('?name', '?lat', '?lon', '?pic') where = sparql.GraphPattern( [('?x', RDF['type'], SIOC['User']), ('?x', SIOC['name'], '?name'), ('?x', FOAF['based_near'], "?y"), ('?y', GEO['long'], '?lon'), ('?y', GEO['lat'], '?lat')]) opt = sparql.GraphPattern([('?x', SIOC['avatar'], "?pic")]) users = sparqlGr.query(select, where, opt) n = len(users) if (n > 0): kml = KML() #create places for (name, lat, lon, pic) in users: kml.addPlace(lat, lon, str(name), pic) #and dump to disk try: kml_file = open(output, 'w+') kml.write(kml_file) kml_file.flush() kml_file.close() print 'new KML file created in', output, 'with', n, 'points' except IOError, detail: print 'Error exporting coordinates to KML: ' + str(detail)
def query(self): try: sparqlGr = sparql.sparqlGraph.SPARQLGraph(self.graph) select = ('?post', '?postTitle', '?date', '?userName', '?content', '?parent') where = sparql.GraphPattern([('?post', RDF['type'], SIOC['Post']), ('?post', SIOC['title'], '?postTitle'), ('?post', DCTERMS['created'], '?date'), ('?post', SIOC['content'], '?content'), ('?post', SIOC['has_creator'],'?user'), ('?user', SIOC['name'], '?userName')]) opt = sparql.GraphPattern([('?post', SIOC['reply_of'], '?parent')]) posts = sparqlGr.query(select, where, opt) return self.orderByDate(posts) except Exception, details: buxon.messageBar('unknow problem parsing RDF at ' + self.uri) print 'parsing exception:', str(details) return None
def process(self, input, output=None): graph = self.parse(input) if not self.enriched(graph): if (output == None): output = '.'.join( input.split('.')[:-1]) + '.foaf.enrichment.rdf' #sparql query sparqlGr = sparql.sparqlGraph.SPARQLGraph(graph) select = ('?user', '?email_sha1sum') where = sparql.GraphPattern([('?user', RDF['type'], SIOC['User']), ('?user', SIOC['email_sha1sum'], '?email_sha1sum')]) users = sparqlGr.query(select, where) if (len(users) > 0): foafserv = FoafUtils() n = 0 graph.bind('foaf', FOAF) graph.bind('sioc', SIOC) graph.bind('geo', GEO) graph.bind('rdfs', RDFS) for (user, email_sha1sum) in users: foaf = foafserv.getFoafFromSha(email_sha1sum) if (foaf != None): n += 1 graph.add((user, RDFS['seeAlso'], URIRef(foaf))) lat, lon = foafserv.getGeoPosition(foaf, email_sha1sum) if (lat != None and lon != None): geo = BNode() graph.add((user, FOAF['based_near'], geo)) graph.add((geo, RDF.type, GEO['Point'])) graph.add((geo, GEO['lat'], Literal(lat))) graph.add((geo, GEO['long'], Literal(lon))) pic = foafserv.getPic(foaf, email_sha1sum) if (pic != None): graph.add((user, SIOC['avatar'], URIRef(pic))) #and dump to disk try: rdf_file = open(output, 'w+') graph.serialize(destination=rdf_file, format="pretty-xml") rdf_file.flush() rdf_file.close() print 'new subscriber RDF file created in', output, 'enriched with', n, 'FOAF files' except IOError, detail: print 'Error exporting subscriber to RDF: ' + str(detail) else: print 'Nobody with FOAF description available in', input
def enriched(self, graph): sparqlGr = sparql.sparqlGraph.SPARQLGraph(graph) select = ('?foaf') where = sparql.GraphPattern([('?user', RDF['type'], SIOC['User']), ('?user', RDFS['seeAlso'], '?foaf')]) foafs = sparqlGr.query(select, where) return (len(foafs) > 0)
def __listPosts(self): try: sparqlGr = sparql.sparqlGraph.SPARQLGraph(self.graph) select = ('?post', '?title') where = sparql.GraphPattern([('?post', RDF['type'], SIOC['Post']), ('?post', DC['title'], '?title')]) posts = sparqlGr.query(select, where) print len(posts), 'posts:' for post, title in posts: print post, try: print title except: print '(bad formed title)' except Exception, details: print 'parsing exception:', str(details) return None
FORUM POSTS: ============ """ for (title, userName) in posts: print "==>", title, "by", userName ############################################################ if __name__ == '__main__': if len(sys.argv) > 1: mailingListUri = sys.argv[1] else: print 'you must to indicate a mailing list URI' sys.exit() graph = loadMailingList(mailingListUri) loadAdditionalData(graph, mailingListUri) sparqlGr = sparql.sparqlGraph.SPARQLGraph(graph) select = ("?postTitle", "?userName") where = sparql.GraphPattern([("?x", SIOC["container_of"], "?post"), ("?post", SIOC["title"], "?postTitle"), ("?post", SIOC["has_creator"], "?user")]) opt = sparql.GraphPattern([("?user", SIOC["name"], "?userName")]) posts = sparqlGr.query(select, where, opt) printPosts(graph, posts)