[fix] openstreetmap engine

It seems there is an API change:
extratags can be either a dictionnary or None.

This commit avoid crash when extratags is None

Test query "!osm gare du nord"
This commit is contained in:
Alexandre Flament 2023-08-22 17:52:12 +00:00 committed by MatthieuBarbu
parent 4c4ee13196
commit 159b7c30fe
1 changed files with 38 additions and 26 deletions

View File

@ -162,8 +162,15 @@ def response(resp):
} }
) )
# simplify the code below: make sure extratags is a dictionnary
for result in nominatim_json:
if not isinstance(result.get('extratags'), dict):
result["extratags"] = {}
# fetch data from wikidata
fetch_wikidata(nominatim_json, user_language) fetch_wikidata(nominatim_json, user_language)
# create results
for result in nominatim_json: for result in nominatim_json:
title, address = get_title_address(result) title, address = get_title_address(result)
@ -218,12 +225,11 @@ def fetch_wikidata(nominatim_json, user_language):
wikidata_ids = [] wikidata_ids = []
wd_to_results = {} wd_to_results = {}
for result in nominatim_json: for result in nominatim_json:
e = result.get("extratags") extratags = result['extratags']
if e:
# ignore brand:wikidata # ignore brand:wikidata
wd_id = e.get("wikidata", e.get("wikidata link")) wd_id = extratags.get('wikidata', extratags.get('wikidata link'))
if wd_id and wd_id not in wikidata_ids: if wd_id and wd_id not in wikidata_ids:
wikidata_ids.append("wd:" + wd_id) wikidata_ids.append('wd:' + wd_id)
wd_to_results.setdefault(wd_id, []).append(result) wd_to_results.setdefault(wd_id, []).append(result)
if wikidata_ids: if wikidata_ids:
@ -334,12 +340,13 @@ def get_img_src(result):
img_src = result['wikidata']['image_sign'] img_src = result['wikidata']['image_sign']
# img_src # img_src
if not img_src and result.get('extratags', {}).get('image'): extratags = result['extratags']
img_src = result['extratags']['image'] if not img_src and extratags.get('image'):
del result['extratags']['image'] img_src = extratags['image']
if not img_src and result.get('extratags', {}).get('wikimedia_commons'): del extratags['image']
img_src = get_external_url('wikimedia_image', result['extratags']['wikimedia_commons']) if not img_src and extratags.get('wikimedia_commons'):
del result['extratags']['wikimedia_commons'] img_src = get_external_url('wikimedia_image', extratags['wikimedia_commons'])
del extratags['wikimedia_commons']
return img_src return img_src
@ -348,9 +355,14 @@ def get_links(result, user_language):
"""Return links from result['extratags']""" """Return links from result['extratags']"""
links = [] links = []
link_keys = set() link_keys = set()
extratags = result['extratags']
if not extratags:
# minor optimization : no need to check VALUE_TO_LINK if extratags is empty
return links, link_keys
for k, mapping_function in VALUE_TO_LINK.items(): for k, mapping_function in VALUE_TO_LINK.items():
raw_value = result['extratags'].get(k) raw_value = extratags.get(k)
if raw_value: if not raw_value:
continue
url, url_label = mapping_function(raw_value) url, url_label = mapping_function(raw_value)
if url.startswith('https://wikidata.org'): if url.startswith('https://wikidata.org'):
url_label = result.get('wikidata', {}).get('itemLabel') or url_label url_label = result.get('wikidata', {}).get('itemLabel') or url_label