I am trying to upload some data to Dydra from a Sesame triplestore I have on my computer. While the download from Sesame works fine, the triples get mixed up (the s-p-o relationships change as the object of one becomes object of another). Can someone please explain why this is happening and how it can be resolved? The code is below:
#Querying the triplestore to retrieve all results
sesameSparqlEndpoint = 'http://my.ip.ad.here:8080/openrdf-sesame/repositories/rep_name'
sparql = SPARQLWrapper(sesameSparqlEndpoint)
queryStringDownload = 'SELECT * WHERE {?s ?p ?o}'
dataGraph = Graph()sparql.setQuery(queryStringDownload)
sparql.method = 'GET'
sparql.setReturnFormat(JSON)
output = sparql.query().convert()
print outputfor i in range(len(output['results']['bindings'])):#The encoding is necessary to parse non-English charactersoutput['results']['bindings'][i]['s']['value'].encode('utf-8')try:subject_extract = output['results']['bindings'][i]['s']['value']if 'http' in subject_extract:subject = "<" + subject_extract + ">"subject_url = URIRef(subject)print subject_urlpredicate_extract = output['results']['bindings'][i]['p']['value']if 'http' in predicate_extract:predicate = "<" + predicate_extract + ">"predicate_url = URIRef(predicate)print predicate_urlobjec_extract = output['results']['bindings'][i]['o']['value']if 'http' in objec_extract:objec = "<" + objec_extract + ">"objec_url = URIRef(objec)print objec_urlelse:objec = objec_extractobjec_wip = '"' + objec + '"'objec_url = URIRef(objec_wip)# Loading the data on a graph dataGraph.add((subject_url,predicate_url,objec_url))except UnicodeError as error: print error#Print all statements in dataGraph
for stmt in dataGraph:pprint.pprint(stmt)# Upload to Dydra
URL = 'http://dydra.com/login'
key = 'my_key'with requests.Session() as s:resp = s.get(URL)soup = BeautifulSoup(resp.text,"html5lib")csrfToken = soup.find('meta',{'name':'csrf-token'}).get('content')# print csrf_tokenpayload = {'account[login]':key,'account[password]':'','csrfmiddlewaretoken':csrfToken,'next':'/'}# print payloadp = s.post(URL,data=payload, headers=dict(Referer=URL))# print p.textr = s.get('http://dydra.com/username/rep_name/sparql')# print r.textdydraSparqlEndpoint = 'http://dydra.com/username/rep_name/sparql'for stmt in dataGraph:queryStringUpload = 'INSERT DATA {%s %s %s}' % stmtsparql = SPARQLWrapper(dydraSparqlEndpoint)sparql.setCredentials(key,key)sparql.setQuery(queryStringUpload)sparql.method = 'POST'sparql.query()