Errol/src/wikidata.py

69 lines
2.6 KiB
Python

import requests
from SPARQLWrapper import SPARQLWrapper, JSON
class Wikidata():
def __init__(self):
self.agent = "Errol/0.1 (https://git.bksp.space/Tjiho/Errol; tjiho@ppsfleet.navy) SPARQLWrapper/2.0.0"
self.sparqlRequest = SPARQLWrapper(
"https://query.wikidata.org/sparql",
agent="Errol/0.1 (https://git.bksp.space/Tjiho/Errol; tjiho@ppsfleet.navy) SPARQLWrapper/2.0.0"
)
self.sparqlRequest.setReturnFormat(JSON)
def search_subject(self,name):
res = requests.get(
'https://www.wikidata.org/w/api.php?action=wbsearchentities&language=fr&search={0}&type=item&format=json&sort=create_timestamp_asc'.format(name),
headers={'User-Agent': self.agent}
)
return res.json()['search']
def search_attribute(self,name):
res = requests.get(
'https://www.wikidata.org/w/api.php?action=wbsearchentities&language=fr&search={0}&type=property&format=json'.format(name),
headers={'User-Agent': self.agent}
)
return res.json()['search']
def request_triple(self,triple):
match triple:
case (subject, attribute, '?'):
try:
return self.get_label(self.need_value(
self.request_triple(subject),
self.request_triple(attribute)
))
except Exception as e:
print(e)
return "hum something wrong"
return triple
def get_label(self,result):
if(len(result) == 0):
return None
else:
return result[0]['itemLabel']['value']
def need_value(self,subject, attribute):
print(subject, attribute)
search = self.search_subject(subject)
if(len(search) == 0):
raise Exception('unknown subject')
subject_wikidata = search[0]['concepturi']
res = None
for attribute_wikidata in self.search_attribute(attribute):
query = """
SELECT ?item ?itemLabel
WHERE
{{
<{0}> <http://www.wikidata.org/prop/direct/{1}> ?item.
SERVICE wikibase:label {{ bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,en". }}
}}
""".format(subject_wikidata, attribute_wikidata['id'])
print(query)
self.sparqlRequest.setQuery(query)
res = self.sparqlRequest.queryAndConvert()['results']['bindings']
if(len(res) > 0):
print('ok')
break
return res