In [24]:
from wikidataintegrator import wdi_core, wdi_login
import os
import pandas as pd
import pprint
In [13]:
def sparql(query, endpoint):
query=query
return wdi_core.WDItemEngine.execute_sparql_query(query, endpoint=endpoint)
def sparqlpandas(query, endpoint):
return wdi_core.WDItemEngine.execute_sparql_query(query, endpoint=endpoint, as_dataframe=True)
The code snippet below is to login in to Wikidata. First it checks if a local variable containing the wikidata user/bot account and password has been set. In this code example in the jupyter Notebook is use the %env magic. In jenkins the variables are set when configuring the jenkins recipe.
Running a script like this on Wikidata requires a bot account.
In [9]:
print("Logging in...")
if "WDUSER" in os.environ and "WDPASS" in os.environ:
WDUSER = os.environ['WDUSER']
WDPASS = os.environ['WDPASS']
else:
raise ValueError("WDUSER and WDPASS must be specified in local.py or as environment variables")
login = wdi_login.WDLogin(WDUSER, WDPASS)
In [19]:
## Find the Wikidata identifier for WP716
query = """
SELECT * WHERE {?pathway wdt:P2888 <http://identifiers.org/wikipathways/WP716>}
"""
sparqlpandas(query, endpoint="https://query.wikidata.org/sparql")
Out[19]:
In [20]:
wdwpid = sparql(query, endpoint="https://query.wikidata.org/sparql")["results"]["bindings"][0]["pathway"]["value"]
print(wdwpid)
In [32]:
wdPage = wdi_core.WDItemEngine(wd_item_id=wdwpid.replace("http://www.wikidata.org/entity/", ""))
#pprint.pprint(wdPage.get_wd_json_representation())
In [26]:
query = """
prefix dcterms: <http://purl.org/dc/terms/>
SELECT ?item ?description WHERE { <"""
query += wdwpid + "> "
query += """wdt:P2410 ?wpid ;
wdt:P2888 ?wpiri .
SERVICE <http://sparql.wikipathways.org/> {
?version dc:identifier ?wpiri ;
dcterms:description ?description .
}
}
"""
sparqlpandas(query, endpoint="https://query.wikidata.org/sparql")
Out[26]:
In [29]:
description = sparql(query, endpoint="https://query.wikidata.org/sparql")["results"]["bindings"][0]["description"]["value"]
print(description)
In [31]:
wdPage.set_description(description, lang="en")
wdPage.write(login)
In [ ]: