1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/openstreetmap.py

100 lines
3 KiB
Python
Raw Normal View History

2015-09-08 23:05:37 +02:00
"""
OpenStreetMap (Map)
@website https://openstreetmap.org/
@provide-api yes (http://wiki.openstreetmap.org/wiki/Nominatim)
@using-api yes
@results JSON
@stable yes
@parse url, title
"""
2014-12-01 12:26:38 +01:00
from json import loads
2015-01-13 16:40:30 +01:00
from searx.utils import searx_useragent
2014-12-01 12:26:38 +01:00
# engine dependent config
categories = ['map']
paging = False
# search-url
2015-01-13 16:40:30 +01:00
base_url = 'https://nominatim.openstreetmap.org/'
search_string = 'search/{query}?format=json&polygon_geojson=1&addressdetails=1'
2014-12-01 12:26:38 +01:00
result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
# do search-request
def request(query, params):
2015-01-13 16:40:30 +01:00
params['url'] = base_url + search_string.format(query=query)
# using searx User-Agent
params['headers']['User-Agent'] = searx_useragent()
2014-12-01 12:26:38 +01:00
return params
# get response from search-request
def response(resp):
results = []
json = loads(resp.text)
# parse results
for r in json:
2015-02-17 12:45:54 +01:00
if 'display_name' not in r:
continue
2014-12-01 12:26:38 +01:00
title = r['display_name']
osm_type = r.get('osm_type', r.get('type'))
url = result_base_url.format(osm_type=osm_type,
osm_id=r['osm_id'])
2014-12-13 13:56:02 +01:00
osm = {'type': osm_type,
'id': r['osm_id']}
2014-12-01 12:26:38 +01:00
2014-12-13 13:56:02 +01:00
geojson = r.get('geojson')
2014-12-01 12:26:38 +01:00
# if no geojson is found and osm_type is a node, add geojson Point
2015-02-17 12:45:54 +01:00
if not geojson and osm_type == 'node':
geojson = {u'type': u'Point', u'coordinates': [r['lon'], r['lat']]}
2014-12-01 12:26:38 +01:00
address_raw = r.get('address')
address = {}
# get name
if r['class'] == 'amenity' or\
r['class'] == 'shop' or\
r['class'] == 'tourism' or\
r['class'] == 'leisure':
if address_raw.get('address29'):
2014-12-13 13:56:02 +01:00
address = {'name': address_raw.get('address29')}
2014-12-01 12:26:38 +01:00
else:
2014-12-13 13:56:02 +01:00
address = {'name': address_raw.get(r['type'])}
2014-12-01 12:26:38 +01:00
# add rest of adressdata, if something is already found
if address.get('name'):
2014-12-13 13:56:02 +01:00
address.update({'house_number': address_raw.get('house_number'),
'road': address_raw.get('road'),
'locality': address_raw.get('city',
2015-01-13 16:40:30 +01:00
address_raw.get('town', # noqa
address_raw.get('village'))), # noqa
2014-12-13 13:56:02 +01:00
'postcode': address_raw.get('postcode'),
'country': address_raw.get('country'),
'country_code': address_raw.get('country_code')})
2014-12-01 12:26:38 +01:00
else:
address = None
# append result
results.append({'template': 'map.html',
'title': title,
'content': '',
'longitude': r['lon'],
'latitude': r['lat'],
'boundingbox': r['boundingbox'],
'geojson': geojson,
'address': address,
'osm': osm,
'url': url})
# return results
return results