8from urllib.parse
import urlencode
9from functools
import partial
11from flask_babel
import gettext
13from searx.data import OSM_KEYS_TAGS, CURRENCIES
20 "website":
'https://www.openstreetmap.org/',
21 "wikidata_id":
'Q936',
22 "official_api_documentation":
'http://wiki.openstreetmap.org/wiki/Nominatim',
23 "use_official_api":
True,
24 "require_api_key":
False,
31language_support =
True
32send_accept_language_header =
True
35base_url =
'https://nominatim.openstreetmap.org/'
36search_string =
'search?{query}&polygon_geojson=1&format=jsonv2&addressdetails=1&extratags=1&dedupe=1'
37result_id_url =
'https://openstreetmap.org/{osm_type}/{osm_id}'
38result_lat_lon_url =
'https://www.openstreetmap.org/?mlat={lat}&mlon={lon}&zoom={zoom}&layers=M'
40route_url =
'https://graphhopper.com/maps/?point={}&point={}&locale=en-US&vehicle=car&weighting=fastest&turn_costs=true&use_miles=false&layer=Omniscale'
41route_re = re.compile(
'(?:from )?(.+) to (.+)')
43wikidata_image_sparql =
"""
44select ?item ?itemLabel ?image ?sign ?symbol ?website ?wikipediaName
46 hint:Query hint:optimizer "None".
47 values ?item { %WIKIDATA_IDS% }
48 OPTIONAL { ?item wdt:P18|wdt:P8517|wdt:P4291|wdt:P5252|wdt:P3451|wdt:P4640|wdt:P5775|wdt:P2716|wdt:P1801|wdt:P4896 ?image }
49 OPTIONAL { ?item wdt:P1766|wdt:P8505|wdt:P8667 ?sign }
50 OPTIONAL { ?item wdt:P41|wdt:P94|wdt:P154|wdt:P158|wdt:P2910|wdt:P4004|wdt:P5962|wdt:P8972 ?symbol }
51 OPTIONAL { ?item wdt:P856 ?website }
52 SERVICE wikibase:label {
53 bd:serviceParam wikibase:language "%LANGUAGE%,en".
54 ?item rdfs:label ?itemLabel .
57 ?wikipediaUrl schema:about ?item;
58 schema:isPartOf/wikibase:wikiGroup "wikipedia";
59 schema:name ?wikipediaName;
60 schema:inLanguage "%LANGUAGE%" .
73 if value.startswith(http):
74 value =
'https://' + value[len(http) :]
79 value = value.split(
';')[0]
84 value = value.split(
':', 1)
85 return (
'https://{0}.wikipedia.org/wiki/{1}'.
format(*value),
'{1} ({0})'.
format(*value))
89 return (prefix + value, value)
93 'website': value_to_website_link,
94 'contact:website': value_to_website_link,
95 'email': partial(value_with_prefix,
'mailto:'),
96 'contact:email': partial(value_with_prefix,
'mailto:'),
97 'contact:phone': partial(value_with_prefix,
'tel:'),
98 'phone': partial(value_with_prefix,
'tel:'),
99 'fax': partial(value_with_prefix,
'fax:'),
100 'contact:fax': partial(value_with_prefix,
'fax:'),
101 'contact:mastodon': value_to_https_link,
102 'facebook': value_to_https_link,
103 'contact:facebook': value_to_https_link,
104 'contact:foursquare': value_to_https_link,
105 'contact:instagram': value_to_https_link,
106 'contact:linkedin': value_to_https_link,
107 'contact:pinterest': value_to_https_link,
108 'contact:telegram': value_to_https_link,
109 'contact:tripadvisor': value_to_https_link,
110 'contact:twitter': value_to_https_link,
111 'contact:yelp': value_to_https_link,
112 'contact:youtube': value_to_https_link,
113 'contact:webcam': value_to_website_link,
114 'wikipedia': value_wikipedia_link,
115 'wikidata': partial(value_with_prefix,
'https://wikidata.org/wiki/'),
116 'brand:wikidata': partial(value_with_prefix,
'https://wikidata.org/wiki/'),
124 'opening_hours:covid19',
135 'internet_access:ssid',
137KEY_RANKS = {k: i
for i, k
in enumerate(KEY_ORDER)}
141 """do search-request"""
142 params[
'url'] = base_url + search_string.format(query=urlencode({
'q': query}))
143 params[
'route'] = route_re.match(query)
144 params[
'headers'][
'User-Agent'] = searx_useragent()
145 if 'Accept-Language' not in params[
'headers']:
146 params[
'headers'][
'Accept-Language'] =
'en'
151 """get response from search-request"""
153 nominatim_json = loads(resp.text)
154 user_language = resp.search_params[
'language']
156 if resp.search_params[
'route']:
159 'answer': gettext(
'Get directions'),
160 'url': route_url.format(*resp.search_params[
'route'].groups()),
165 for result
in nominatim_json:
166 if not isinstance(result.get(
'extratags'), dict):
167 result[
"extratags"] = {}
173 for result
in nominatim_json:
182 links, link_keys =
get_links(result, user_language)
183 data =
get_data(result, user_language, link_keys)
187 'template':
'map.html',
190 'address_label': get_key_label(
'addr', user_language),
194 'thumbnail': thumbnail,
197 'type':
get_tag_label(result.get(
'category'), result.get(
'type',
''), user_language),
198 'type_icon': result.get(
'icon'),
200 'longitude': result[
'lon'],
201 'latitude': result[
'lat'],
202 'boundingbox': result[
'boundingbox'],
212 return get_external_url(
'wikimedia_image', raw_value)
216 """Update nominatim_json using the result of an unique to wikidata
218 For result in nominatim_json:
219 If result['extratags']['wikidata'] or r['extratags']['wikidata link']:
220 Set result['wikidata'] to { 'image': ..., 'image_sign':..., 'image_symbal':... }
221 Set result['extratags']['wikipedia'] if not defined
222 Set result['extratags']['contact:website'] if not defined
226 for result
in nominatim_json:
227 extratags = result[
'extratags']
229 wd_id = extratags.get(
'wikidata', extratags.get(
'wikidata link'))
230 if wd_id
and wd_id
not in wikidata_ids:
231 wikidata_ids.append(
'wd:' + wd_id)
232 wd_to_results.setdefault(wd_id, []).append(result)
235 user_language =
'en' if user_language ==
'all' else user_language.split(
'-')[0]
236 wikidata_ids_str =
" ".join(wikidata_ids)
237 query = wikidata_image_sparql.replace(
'%WIKIDATA_IDS%', sparql_string_escape(wikidata_ids_str)).replace(
238 '%LANGUAGE%', sparql_string_escape(user_language)
240 wikidata_json = send_wikidata_query(query)
241 for wd_result
in wikidata_json.get(
'results', {}).get(
'bindings', {}):
242 wd_id = wd_result[
'item'][
'value'].replace(
'http://www.wikidata.org/entity/',
'')
243 for result
in wd_to_results.get(wd_id, []):
244 result[
'wikidata'] = {
245 'itemLabel': wd_result[
'itemLabel'][
'value'],
251 wikipedia_name = wd_result.get(
'wikipediaName', {}).get(
'value')
253 result[
'extratags'][
'wikipedia'] = user_language +
':' + wikipedia_name
255 website = wd_result.get(
'website', {}).get(
'value')
258 and not result[
'extratags'].get(
'contact:website')
259 and not result[
'extratags'].get(
'website')
261 result[
'extratags'][
'contact:website'] = website
265 """Return title and address
269 address_raw = result.get(
'address')
275 result[
'category'] ==
'amenity'
276 or result[
'category'] ==
'shop'
277 or result[
'category'] ==
'tourism'
278 or result[
'category'] ==
'leisure'
280 if address_raw.get(
'address29'):
282 address_name = address_raw.get(
'address29')
284 address_name = address_raw.get(result[
'category'])
285 elif result[
'type']
in address_raw:
286 address_name = address_raw.get(result[
'type'])
293 'name': address_name,
294 'house_number': address_raw.get(
'house_number'),
295 'road': address_raw.get(
'road'),
296 'locality': address_raw.get(
297 'city', address_raw.get(
'town', address_raw.get(
'village'))
299 'postcode': address_raw.get(
'postcode'),
300 'country': address_raw.get(
'country'),
301 'country_code': address_raw.get(
'country_code'),
305 title = result.get(
'display_name')
307 return title, address
311 """Get url, osm and geojson"""
312 osm_type = result.get(
'osm_type', result.get(
'type'))
313 if 'osm_id' not in result:
316 url = result_lat_lon_url.format(lat=result[
'lat'], lon=result[
'lon'], zoom=12)
319 url = result_id_url.format(osm_type=osm_type, osm_id=result[
'osm_id'])
320 osm = {
'type': osm_type,
'id': result[
'osm_id']}
322 geojson = result.get(
'geojson')
324 if not geojson
and osm_type ==
'node':
325 geojson = {
'type':
'Point',
'coordinates': [result[
'lon'], result[
'lat']]}
327 return url, osm, geojson
331 """Get image URL from either wikidata or r['extratags']"""
334 if 'wikidata' in result:
335 img_src = result[
'wikidata'][
'image']
337 img_src = result[
'wikidata'][
'image_symbol']
339 img_src = result[
'wikidata'][
'image_sign']
342 extratags = result[
'extratags']
343 if not img_src
and extratags.get(
'image'):
344 img_src = extratags[
'image']
345 del extratags[
'image']
346 if not img_src
and extratags.get(
'wikimedia_commons'):
347 img_src = get_external_url(
'wikimedia_image', extratags[
'wikimedia_commons'])
348 del extratags[
'wikimedia_commons']
354 """Return links from result['extratags']"""
357 extratags = result[
'extratags']
360 return links, link_keys
361 for k, mapping_function
in VALUE_TO_LINK.items():
362 raw_value = extratags.get(k)
365 url, url_label = mapping_function(raw_value)
366 if url.startswith(
'https://wikidata.org'):
367 url_label = result.get(
'wikidata', {}).get(
'itemLabel')
or url_label
370 'label': get_key_label(k, user_language),
372 'url_label': url_label,
376 return links, link_keys
380 """Return key, value of result['extratags']
382 Must be call after get_links
384 Note: the values are not translated
387 for k, v
in result[
'extratags'].items():
392 k_label = get_key_label(k, user_language)
401 data.sort(key=
lambda entry: (
get_key_rank(entry[
'key']), entry[
'label']))
408 The rank defines in which order the key are displayed in the HTML result
410 key_rank = KEY_RANKS.get(k)
413 key_rank = KEY_RANKS.get(k.split(
':')[0] +
':*')
418 """Get label from labels in OSM_KEYS_TAGS
420 in OSM_KEYS_TAGS, labels have key == '*'
422 tag_label = labels.get(lang.lower())
423 if tag_label
is None:
425 tag_label = labels.get(lang.split(
'-')[0])
426 if tag_label
is None and lang !=
'en':
428 tag_label = labels.get(
'en')
429 if tag_label
is None and len(labels.values()) > 0:
431 tag_label = labels.values()[0]
436 """Get tag label from OSM_KEYS_TAGS"""
437 tag_name =
'' if tag_name
is None else tag_name
438 tag_labels = OSM_KEYS_TAGS[
'tags'].get(tag_category, {}).get(tag_name, {})
443 """Get key label from OSM_KEYS_TAGS"""
444 if key_name.startswith(
'currency:'):
450 currency = key_name.split(
':')
451 if len(currency) > 1:
452 o = CURRENCIES[
'iso4217'].get(currency[1])
457 labels = OSM_KEYS_TAGS[
'keys']
458 for k
in key_name.split(
':') + [
'*']:
459 labels = labels.get(k)
value_with_prefix(prefix, value)
get_data(result, user_language, ignore_keys)
get_links(result, user_language)
get_url_osm_geojson(result)
get_tag_label(tag_category, tag_name, lang)
value_to_https_link(value)
fetch_wikidata(nominatim_json, user_language)
get_title_address(result)
value_wikipedia_link(value)
get_key_label(key_name, lang)
value_to_website_link(value)
get_wikipedia_image(raw_value)