Skip to content

Commit

Permalink
Single, global URL
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Feb 16, 2023
1 parent 2a4a2d7 commit 2015287
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions scholia/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

from six import u

SPARQL_ENDPOINT = "https://query.wikidata.org/sparql"

USER_AGENT = 'Scholia'

HEADERS = {'User-Agent': USER_AGENT}
Expand Down Expand Up @@ -173,7 +175,7 @@ def query_to_bindings(query):
Data as list of dicts.
"""
url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -284,7 +286,7 @@ def identifier_to_qs(property, identifier):
identifier=escape_string(identifier),
)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
try:
response = requests.get(url, params=params, headers=HEADERS)
Expand Down Expand Up @@ -336,7 +338,7 @@ def count_scientific_articles():
query = """
SELECT (COUNT(*) AS ?count) WHERE { [] wdt:P31 wd:Q13442814 }"""

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -374,7 +376,7 @@ def doi_to_qs(doi):
query = 'select ?work where {{ ?work wdt:P356 "{doi}" }}'.format(
doi=escape_string(doi.upper()))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -413,7 +415,7 @@ def doi_prefix_to_qs(doi):
query = 'select ?work where {{ ?work wdt:P1662 "{doi}" }}'.format(
doi=escape_string(doi.upper()))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -455,7 +457,7 @@ def iso639_to_q(language):
else:
raise ValueError('ISO639 language code not recognized')

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -496,7 +498,7 @@ def pubchem_to_qs(cid):
query = 'select ?chemical where {{ ?chemical wdt:P662 "{cid}" }}'.format(
cid=cid)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -557,7 +559,7 @@ def pubmed_to_qs(pmid):
query = 'select ?work where {{ ?work wdt:P698 "{pmid}" }}'.format(
pmid=pmid)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -590,7 +592,7 @@ def ror_to_qs(rorid):
query = 'select ?work where {{ ?work wdt:P6782 "{rorid}" }}'.format(
rorid=rorid)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -626,7 +628,7 @@ def uniprot_to_qs(protein):
query = 'select ?protein where {{ ?protein wdt:P352 "{protein}" }}'.format(
protein=protein)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -662,7 +664,7 @@ def ncbi_gene_to_qs(gene):
query = 'select ?gene where {{ ?gene wdt:P351 "{gene}" }}'.format(
gene=gene)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -698,7 +700,7 @@ def ncbi_taxon_to_qs(taxon):
query = 'select ?work where {{ ?work wdt:P685 "{taxon}" }}'.format(
taxon=taxon)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -732,7 +734,7 @@ def wikipathways_to_qs(wpid):
'?work wdt:P2410 ?wpid }}').format(
wpid=wpid)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -763,7 +765,7 @@ def issn_to_qs(issn):
query = 'select ?author where {{ ?author wdt:P236 "{issn}" }}'.format(
issn=escape_string(issn))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -794,7 +796,7 @@ def orcid_to_qs(orcid):
query = 'select ?author where {{ ?author wdt:P496 "{orcid}" }}'.format(
orcid=escape_string(orcid))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -825,7 +827,7 @@ def mesh_to_qs(meshid):
query = 'select ?cmp where {{ ?cmp wdt:P486 "{meshid}" }}'.format(
meshid=meshid)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -859,7 +861,7 @@ def q_to_dois(q):
"""
query = """SELECT ?doi {{ wd:{q} wdt:P356 ?doi }}""".format(q=q)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -894,7 +896,7 @@ def q_to_label(q, language='en'):
FILTER (LANG(?label) = "{language}") }}""".format(
q=q, language=language)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -954,7 +956,7 @@ def search_article_titles(q, search_string=None):
# addition during query.
article_count = count_scientific_articles() + 1000

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT

batch_size = 500000
loops = article_count // batch_size + 1
Expand Down Expand Up @@ -1026,7 +1028,7 @@ def viaf_to_qs(viaf):
query = 'select ?author where {{ ?author wdt:P214 "{viaf}" }}'.format(
viaf=escape_string(viaf))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1060,7 +1062,7 @@ def q_to_class(q):
query = 'SELECT ?class {{ wd:{q} wdt:P31 ?class }}'.format(
q=escape_string(q))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
try:
Expand Down Expand Up @@ -1295,7 +1297,7 @@ def q_to_class(q):
query = 'select ?class where {{ wd:{q} wdt:P279+ ?class }}'.format(
q=escape_string(q))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1343,7 +1345,7 @@ def twitter_to_qs(twitter):
twitter=escape_string(twitter), lower=escape_string(twitter).lower()
)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1376,7 +1378,7 @@ def github_to_qs(github):
where {{ ?item wdt:P2037 "{github}" }}""".format(
github=escape_string(github))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1409,7 +1411,7 @@ def inchikey_to_qs(inchikey):
where {{ ?item wdt:P235 "{inchikey}" }}""".format(
inchikey=escape_string(inchikey))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1442,7 +1444,7 @@ def cordis_to_qs(cordis):
where {{ ?item wdt:P3400 "{cordis}" }}""".format(
cordis=escape_string(cordis))

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1475,7 +1477,7 @@ def cas_to_qs(cas):
where {{ ?item wdt:P231 "{cas}" }}""".format(
cas=cas)

url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1509,7 +1511,7 @@ def lipidmaps_to_qs(lmid):
query = """select ?item
where {{ ?item wdt:P2063 "{lmid}" }}""".format(
lmid=lmid)
url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1542,7 +1544,7 @@ def atomic_number_to_qs(atomic_number):
WHERE {{ ?item wdt:P31 wd:Q11344 ; wdt:P1086 ?number .
FILTER (STR(?number) = "{atomic_number}") }}""".format(
atomic_number=atomic_number)
url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1574,7 +1576,7 @@ def atomic_symbol_to_qs(symbol):
query = """SELECT ?item
WHERE {{ ?item wdt:P246 "{symbol}" }}""".format(
symbol=symbol)
url = 'https://query.wikidata.org/sparql'
url = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url, params=params, headers=HEADERS)
data = response.json()
Expand Down Expand Up @@ -1608,7 +1610,7 @@ def website_to_qs(url):
query = 'SELECT ?work WHERE {{ ?work wdt:P856 <{url}> }}'.format(
url=url.strip())

url_ = 'https://query.wikidata.org/sparql'
url_ = SPARQL_ENDPOINT
params = {'query': query, 'format': 'json'}
response = requests.get(url_, params=params, headers=HEADERS)
data = response.json()
Expand Down

0 comments on commit 2015287

Please sign in to comment.