From 1cbecf5fe844a579b2a9d934014c43b0a15ff118 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Thu, 15 Apr 2021 12:25:14 +0100 Subject: [PATCH] Set user-agent --- lookup.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lookup.py b/lookup.py index 54a7426..1877186 100755 --- a/lookup.py +++ b/lookup.py @@ -16,6 +16,10 @@ east = 0.3536 north = 53.7984 west = -2.7296 +headers = { + 'User-Agent': 'UK gecode/0.1 (edward@4angle.com)', +} + OVERPASS_URL = 'https://lz4.overpass-api.de' wikidata_query_api_url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql' wikidata_url = 'https://www.wikidata.org/w/api.php' @@ -50,6 +54,12 @@ samples = [ (52.766, 0.31, 'Terrington St Clement'), ] +class QueryError(Exception): + def __init__(self, query, r): + self.query = query + self.r = r + + app = Flask(__name__) app.debug = True @@ -91,7 +101,7 @@ def wikidata_api_call(params): **params, } - r = requests.get(wikidata_url, params=call_params) + r = requests.get(wikidata_url, params=call_params, headers=headers) return r def get_entity(qid): @@ -115,16 +125,21 @@ def qid_to_commons_category(qid): def wdqs(query): r = requests.post(wikidata_query_api_url, - data={'query': query, 'format': 'json'}) - - return r.json() + data={'query': query, 'format': 'json'}, + headers=headers) + try: + return r.json() + except simplejson.errors.JSONDecodeError: + raise QueryError(query, r) def endpoint(): return OVERPASS_URL + '/api/interpreter' def run_query(oql, error_on_rate_limit=True): - return requests.post(endpoint(), data=oql.encode('utf-8')) + return requests.post(endpoint(), + data=oql.encode('utf-8'), + headers=headers) def get_elements(oql): return run_query(oql).json()['elements']