[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
parent 0647f83a3e
commit e16c007c22
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)
# create results
for result in nominatim_json:
title, address = get_title_address(result)
@ -218,12 +225,11 @@ def fetch_wikidata(nominatim_json, user_language):
wikidata_ids = []
wd_to_results = {}
for result in nominatim_json:
e = result.get("extratags")
if e:
extratags = result['extratags']
# 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:
wikidata_ids.append("wd:" + wd_id)
wikidata_ids.append('wd:' + wd_id)
wd_to_results.setdefault(wd_id, []).append(result)
if wikidata_ids:
@ -334,12 +340,13 @@ def get_img_src(result):
img_src = result['wikidata']['image_sign']
# img_src
if not img_src and result.get('extratags', {}).get('image'):
img_src = result['extratags']['image']
del result['extratags']['image']
if not img_src and result.get('extratags', {}).get('wikimedia_commons'):
img_src = get_external_url('wikimedia_image', result['extratags']['wikimedia_commons'])
del result['extratags']['wikimedia_commons']
extratags = result['extratags']
if not img_src and extratags.get('image'):
img_src = extratags['image']
del extratags['image']
if not img_src and extratags.get('wikimedia_commons'):
img_src = get_external_url('wikimedia_image', extratags['wikimedia_commons'])
del extratags['wikimedia_commons']
return img_src
@ -348,9 +355,14 @@ def get_links(result, user_language):
"""Return links from result['extratags']"""
links = []
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():
raw_value = result['extratags'].get(k)
if raw_value:
raw_value = extratags.get(k)
if not raw_value:
continue
url, url_label = mapping_function(raw_value)
if url.startswith('https://wikidata.org'):
url_label = result.get('wikidata', {}).get('itemLabel') or url_label