From: Nathan TeBlunthuis Date: Wed, 25 Mar 2020 01:04:22 +0000 (-0700) Subject: Untested code to get labels from wikidata in all languages. X-Git-Url: https://code.communitydata.science/covid19.git/commitdiff_plain/308d462e767920ef541f8ccef2942e87eb854be8 Untested code to get labels from wikidata in all languages. --- diff --git a/translations/src/find_entities.py b/translations/src/find_entities.py index eafb42a..6edce05 100644 --- a/translations/src/find_entities.py +++ b/translations/src/find_entities.py @@ -1,7 +1,7 @@ # generate a list of wikidata entities related to keywords from os import path from sys import stdout -from wikidata_api_calls import search_wikidata +from wikidata_api_calls import search_wikidata, get_wikidata_api class Wikidata_ResultSet(object): def __init__(self): @@ -13,7 +13,7 @@ class Wikidata_ResultSet(object): def to_csv(self, outfile=None): - header = ','.join(['search_term', 'entityid', 'pageid', 'search_position','timestamp']) + header = ','.join(['search_term', 'entityid', 'pageid', 'search_position','timestamp']) + '\n' if outfile is None: of = stdout diff --git a/translations/src/wikidata_api_calls.py b/translations/src/wikidata_api_calls.py index 022a9d4..0f70680 100644 --- a/translations/src/wikidata_api_calls.py +++ b/translations/src/wikidata_api_calls.py @@ -1,8 +1,8 @@ # File defines functions for making api calls to find translations and transliterations for key terms. import mwapi +import requests import sys -sys.path.append("..") from defaults import user_agent def get_wikidata_api(): @@ -27,3 +27,7 @@ def search_wikidata(session, term, *args, **kwargs): raise mwapi.session.APIError(f"No results for query: {term}") return results + +def run_sparql_query(q): + results = requests.get("https://query.wikidata.org/bigdata/namespace/wdq/sparql?query={q}&format=json") +