From 5ae38bafdf06518b30d092d8b68d5a41f5539249 Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Tue, 16 Dec 2014 15:01:05 +0100 Subject: [enh] add photon engine --- searx/engines/photon.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 searx/engines/photon.py (limited to 'searx/engines/photon.py') diff --git a/searx/engines/photon.py b/searx/engines/photon.py new file mode 100644 index 000000000..f4a1254a7 --- /dev/null +++ b/searx/engines/photon.py @@ -0,0 +1,121 @@ +## Photon (Map) +# +# @website https://photon.komoot.de +# @provide-api yes (https://photon.komoot.de/) +# +# @using-api yes +# @results JSON +# @stable yes +# @parse url, title + +from urllib import urlencode +from json import loads +from searx.utils import searx_useragent + +# engine dependent config +categories = ['map'] +paging = False +language_support = True +number_of_results = 10 + +# search-url +search_url = 'https://photon.komoot.de/api/?{query}&limit={limit}' +result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}' + + +# do search-request +def request(query, params): + params['url'] = search_url.format(query=urlencode({'q': query}), + limit=number_of_results) + + if params['language'] != 'all': + params['url'] = params['url'] + "&lang=" + params['language'].replace('_', '-') + + # using searx User-Agent + params['headers']['User-Agent'] = searx_useragent() + + # FIX: SSLError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed + params['verify'] = False + + return params + + +# get response from search-request +def response(resp): + results = [] + json = loads(resp.text) + + # parse results + for r in json.get('features', {}): + + properties = r.get('properties') + + if not properties: + continue + + # get title + title = properties['name'] + + # get osm-type + if properties.get('osm_type') == 'N': + osm_type = 'node' + elif properties.get('osm_type') == 'W': + osm_type = 'way' + elif properties.get('osm_type') == 'R': + osm_type = 'relation' + else: + # continue if invalide osm-type + continue + + url = result_base_url.format(osm_type=osm_type, + osm_id=properties.get('osm_id')) + + osm = {'type': osm_type, + 'id': properties.get('osm_id')} + + geojson = r.get('geometry') + + if properties.get('extent'): + boundingbox = [properties.get('extent')[3], properties.get('extent')[1], properties.get('extent')[0], properties.get('extent')[2]] + else: + # TODO: better boundingbox calculation + boundingbox = [geojson['coordinates'][1], geojson['coordinates'][1], geojson['coordinates'][0], geojson['coordinates'][0]] + + # TODO: address calculation + address = {} + + # get name + if properties.get('osm_key') == 'amenity' or\ + properties.get('osm_key') == 'shop' or\ + properties.get('osm_key') == 'tourism' or\ + properties.get('osm_key') == 'leisure': + address = {'name': properties.get('name')} + + # add rest of adressdata, if something is already found + if address.get('name'): + address.update({'house_number': properties.get('housenumber'), + 'road': properties.get('street'), + 'locality': properties.get('city', + properties.get('town', + properties.get('village'))), + 'postcode': properties.get('postcode'), + 'country': properties.get('country')}) + else: + address = None + + # append result + results.append({'template': 'map.html', + 'title': title, + 'content': '', + 'longitude': geojson['coordinates'][0], + 'latitude': geojson['coordinates'][1], + 'boundingbox': boundingbox, + 'geojson': geojson, + 'address': address, + 'osm': osm, + 'url': url}) + + print r['properties']['name'] + + # return results + return results -- cgit v1.2.3 From 48058075c06af1f4a0a21045b983dffe14ab9bff Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Tue, 16 Dec 2014 16:32:59 +0100 Subject: [fix] remove useless print statement --- searx/engines/photon.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'searx/engines/photon.py') diff --git a/searx/engines/photon.py b/searx/engines/photon.py index f4a1254a7..853a14980 100644 --- a/searx/engines/photon.py +++ b/searx/engines/photon.py @@ -115,7 +115,5 @@ def response(resp): 'osm': osm, 'url': url}) - print r['properties']['name'] - # return results return results -- cgit v1.2.3 From 672f1c674ca63b7589c1bc6b0c32caf439bd16db Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Tue, 16 Dec 2014 17:01:25 +0100 Subject: [fix] pep8 --- searx/engines/photon.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) (limited to 'searx/engines/photon.py') diff --git a/searx/engines/photon.py b/searx/engines/photon.py index 853a14980..03ad2a085 100644 --- a/searx/engines/photon.py +++ b/searx/engines/photon.py @@ -19,22 +19,25 @@ language_support = True number_of_results = 10 # search-url -search_url = 'https://photon.komoot.de/api/?{query}&limit={limit}' +base_url = 'https://photon.komoot.de/' +search_string = 'api/?{query}&limit={limit}' result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}' # do search-request def request(query, params): - params['url'] = search_url.format(query=urlencode({'q': query}), - limit=number_of_results) + params['url'] = base_url +\ + search_string.format(query=urlencode({'q': query}), + limit=number_of_results) if params['language'] != 'all': - params['url'] = params['url'] + "&lang=" + params['language'].replace('_', '-') + params['url'] = params['url'] +\ + "&lang=" + params['language'].replace('_', '-') # using searx User-Agent params['headers']['User-Agent'] = searx_useragent() - - # FIX: SSLError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed + + # FIX: SSLError: SSL3_GET_SERVER_CERTIFICATE:certificate verify failed params['verify'] = False return params @@ -47,15 +50,15 @@ def response(resp): # parse results for r in json.get('features', {}): - + properties = r.get('properties') - + if not properties: continue - + # get title title = properties['name'] - + # get osm-type if properties.get('osm_type') == 'N': osm_type = 'node' @@ -66,7 +69,7 @@ def response(resp): else: # continue if invalide osm-type continue - + url = result_base_url.format(osm_type=osm_type, osm_id=properties.get('osm_id')) @@ -74,14 +77,20 @@ def response(resp): 'id': properties.get('osm_id')} geojson = r.get('geometry') - - if properties.get('extent'): - boundingbox = [properties.get('extent')[3], properties.get('extent')[1], properties.get('extent')[0], properties.get('extent')[2]] + + if properties.get('extent'): + boundingbox = [properties.get('extent')[3], + properties.get('extent')[1], + properties.get('extent')[0], + properties.get('extent')[2]] else: # TODO: better boundingbox calculation - boundingbox = [geojson['coordinates'][1], geojson['coordinates'][1], geojson['coordinates'][0], geojson['coordinates'][0]] - - # TODO: address calculation + boundingbox = [geojson['coordinates'][1], + geojson['coordinates'][1], + geojson['coordinates'][0], + geojson['coordinates'][0]] + + # address calculation address = {} # get name @@ -90,14 +99,14 @@ def response(resp): properties.get('osm_key') == 'tourism' or\ properties.get('osm_key') == 'leisure': address = {'name': properties.get('name')} - + # add rest of adressdata, if something is already found if address.get('name'): address.update({'house_number': properties.get('housenumber'), 'road': properties.get('street'), 'locality': properties.get('city', - properties.get('town', - properties.get('village'))), + properties.get('town', # noqa + properties.get('village'))), # noqa 'postcode': properties.get('postcode'), 'country': properties.get('country')}) else: -- cgit v1.2.3