From 29426e2462294ade408ad59c9ff5e4c85e66ff2a Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 11 Dec 2023 15:46:49 +0000 Subject: [PATCH] Add workaround for Q68816332 mapping to Orphir --- lookup.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lookup.py b/lookup.py index 8871587..043adfb 100755 --- a/lookup.py +++ b/lookup.py @@ -27,8 +27,11 @@ database.init_app(app) setup_error_mail(app) Tags = typing.Mapping[str, str] +StrDict = dict[str, typing.Any] logging_enabled = True +fallback_qid_to_commons_cat = {"Q68816332": "Orphir"} + @app.errorhandler(werkzeug.exceptions.InternalServerError) def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str, int]: @@ -85,6 +88,20 @@ def do_lookup( return wikidata.build_dict(hit, lat, lon) +def add_missing_commons_cat(rows: list[StrDict]) -> None: + """Add missing details for Commons Categories to Wikidata query results.""" + for row in rows: + if "commonsSiteLink" in row or "commonsCat" in row: + continue + + qid = row["item"]["value"].rpartition("/")[2] + if qid not in fallback_qid_to_commons_cat: + continue + + commons_cat = fallback_qid_to_commons_cat[qid] + row["commonsCat"] = {"type": "literal", "value": commons_cat} + + def lat_lon_to_wikidata(lat: float, lon: float) -> dict[str, typing.Any]: """Lookup lat/lon and find most appropriate Wikidata item.""" scotland_code = scotland.get_scotland_code(lat, lon) @@ -92,6 +109,7 @@ def lat_lon_to_wikidata(lat: float, lon: float) -> dict[str, typing.Any]: elements: typing.Any if scotland_code: rows = wikidata.lookup_scottish_parish_in_wikidata(scotland_code) + add_missing_commons_cat(rows) hit = wikidata.commons_from_rows(rows) elements = [] result = wikidata.build_dict(hit, lat, lon)