@@ -0,0 +1,128 @@
1+## Photon (Map)
2+#
3+# @website https://photon.komoot.de
4+# @provide-api yes (https://photon.komoot.de/)
5+#
6+# @using-api yes
7+# @results JSON
8+# @stable yes
9+# @parse url, title
10+
11+from urllib import urlencode
12+from json import loads
13+from searx.utils import searx_useragent
14+
15+# engine dependent config
16+categories = ['map']
17+paging = False
18+language_support = True
19+number_of_results = 10
20+
21+# search-url
22+base_url = 'https://photon.komoot.de/'
23+search_string = 'api/?{query}&limit={limit}'
24+result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
25+
26+
27+# do search-request
28+def request(query, params):
29+ params['url'] = base_url +\
30+ search_string.format(query=urlencode({'q': query}),
31+ limit=number_of_results)
32+
33+ if params['language'] != 'all':
34+ params['url'] = params['url'] +\
35+ "&lang=" + params['language'].replace('_', '-')
36+
37+ # using searx User-Agent
38+ params['headers']['User-Agent'] = searx_useragent()
39+
40+ # FIX: SSLError: SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
41+ params['verify'] = False
42+
43+ return params
44+
45+
46+# get response from search-request
47+def response(resp):
48+ results = []
49+ json = loads(resp.text)
50+
51+ # parse results
52+ for r in json.get('features', {}):
53+
54+ properties = r.get('properties')
55+
56+ if not properties:
57+ continue
58+
59+ # get title
60+ title = properties['name']
61+
62+ # get osm-type
63+ if properties.get('osm_type') == 'N':
64+ osm_type = 'node'
65+ elif properties.get('osm_type') == 'W':
66+ osm_type = 'way'
67+ elif properties.get('osm_type') == 'R':
68+ osm_type = 'relation'
69+ else:
70+ # continue if invalide osm-type
71+ continue
72+
73+ url = result_base_url.format(osm_type=osm_type,
74+ osm_id=properties.get('osm_id'))
75+
76+ osm = {'type': osm_type,
77+ 'id': properties.get('osm_id')}
78+
79+ geojson = r.get('geometry')
80+
81+ if properties.get('extent'):
82+ boundingbox = [properties.get('extent')[3],
83+ properties.get('extent')[1],
84+ properties.get('extent')[0],
85+ properties.get('extent')[2]]
86+ else:
87+ # TODO: better boundingbox calculation
88+ boundingbox = [geojson['coordinates'][1],
89+ geojson['coordinates'][1],
90+ geojson['coordinates'][0],
91+ geojson['coordinates'][0]]
92+
93+ # address calculation
94+ address = {}
95+
96+ # get name
97+ if properties.get('osm_key') == 'amenity' or\
98+ properties.get('osm_key') == 'shop' or\
99+ properties.get('osm_key') == 'tourism' or\
100+ properties.get('osm_key') == 'leisure':
101+ address = {'name': properties.get('name')}
102+
103+ # add rest of adressdata, if something is already found
104+ if address.get('name'):
105+ address.update({'house_number': properties.get('housenumber'),
106+ 'road': properties.get('street'),
107+ 'locality': properties.get('city',
108+ properties.get('town', # noqa
109+ properties.get('village'))), # noqa
110+ 'postcode': properties.get('postcode'),
111+ 'country': properties.get('country')})
112+ else:
113+ address = None
114+
115+ # append result
116+ results.append({'template': 'map.html',
117+ 'title': title,
118+ 'content': '',
119+ 'longitude': geojson['coordinates'][0],
120+ 'latitude': geojson['coordinates'][1],
121+ 'boundingbox': boundingbox,
122+ 'geojson': geojson,
123+ 'address': address,
124+ 'osm': osm,
125+ 'url': url})
126+
127+ # return results
128+ return results