From 5dce299b2292f9b32a2c993f16a7fced827eb81b Mon Sep 17 00:00:00 2001 From: ta Date: Thu, 25 Aug 2022 17:05:40 +0700 Subject: [PATCH 1/2] add apple maps engine --- searx/engines/apple_maps.py | 89 +++++++++++++++++++++++++++++++++++++ searx/settings.yml | 6 +++ 2 files changed, 95 insertions(+) create mode 100644 searx/engines/apple_maps.py diff --git a/searx/engines/apple_maps.py b/searx/engines/apple_maps.py new file mode 100644 index 000000000..08dd15ac6 --- /dev/null +++ b/searx/engines/apple_maps.py @@ -0,0 +1,89 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""Apple Maps""" + +from json import loads +from time import time +from urllib.parse import urlencode + +from searx.network import get as http_get + +about = { + "website": 'https://www.apple.com/maps/', + "wikidata_id": 'Q276101', + "official_api_documentation": None, + "use_official_api": True, + "require_api_key": False, + "results": 'JSON', +} + +token = {'value': '', 'last_updated': None} + +categories = ['map'] +paging = False + +search_url = "https://api.apple-mapkit.com/v1/search?{query}&mkjsVersion=5.72.53" + + +def obtain_token(): + update_time = time() - (time() % 1800) + try: + # use duckduckgo's mapkit token + token_response = http_get('https://duckduckgo.com/local.js?get_mk_token=1', timeout=2.0) + actual_token = http_get( + 'https://cdn.apple-mapkit.com/ma/bootstrap?apiVersion=2&mkjsVersion=5.72.53&poi=1', + timeout=2.0, + headers={'Authorization': 'Bearer ' + token_response.text}, + ) + token['value'] = loads(actual_token.text)['authInfo']['access_token'] + token['last_updated'] = update_time + # pylint: disable=bare-except + except: + pass + return token + + +def init(_engine_settings=None): + obtain_token() + + +def request(query, params): + if time() - (token['last_updated'] or 0) > 1800: + obtain_token() + + params['url'] = search_url.format(query=urlencode({'q': query, 'lang': params['language']})) + + params['headers'] = {'Authorization': 'Bearer ' + token['value']} + + return params + + +def response(resp): + results = [] + + resp_json = loads(resp.text) + + for result in resp_json['results']: + box = result['displayMapRegion'] + + results.append( + { + 'template': 'map.html', + 'title': result['name'], + 'latitude': result['center']['lat'], + 'longitude': result['center']['lng'], + 'url': result['placecardUrl'], + 'boundingbox': [box['southLat'], box['northLat'], box['westLng'], box['eastLng']], + 'geojson': {'type': 'Point', 'coordinates': [result['center']['lng'], result['center']['lat']]}, + 'address': { + 'name': result['name'], + 'house_number': result.get('subThoroughfare', {}), + 'road': result.get('thoroughfare', {}), + 'locality': result.get('locality', {}), + 'postcode': result.get('postCode', {}), + 'country': result.get('country', {}), + }, + } + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index ec03819a5..668a92689 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -540,6 +540,12 @@ engines: timeout: 3.0 disabled: true + - name: apple maps + engine: apple_maps + shortcut: apm + disabled: true + timeout: 5.0 + - name: emojipedia engine: emojipedia timeout: 4.0 From 525946d7dd89c4a8470d2bdcd5e68642c43fe2bb Mon Sep 17 00:00:00 2001 From: ta Date: Sat, 27 Aug 2022 06:17:58 +0700 Subject: [PATCH 2/2] add poi's website and phone number, doesn't crash when there is no `displayMapRegion`, query the token on the first request --- searx/engines/apple_maps.py | 46 ++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/searx/engines/apple_maps.py b/searx/engines/apple_maps.py index 08dd15ac6..eb4af422e 100644 --- a/searx/engines/apple_maps.py +++ b/searx/engines/apple_maps.py @@ -7,6 +7,7 @@ from time import time from urllib.parse import urlencode from searx.network import get as http_get +from searx.engines.openstreetmap import get_key_label about = { "website": 'https://www.apple.com/maps/', @@ -43,10 +44,6 @@ def obtain_token(): return token -def init(_engine_settings=None): - obtain_token() - - def request(query, params): if time() - (token['last_updated'] or 0) > 1800: obtain_token() @@ -63,25 +60,52 @@ def response(resp): resp_json = loads(resp.text) + user_language = resp.search_params['language'] + for result in resp_json['results']: - box = result['displayMapRegion'] + boundingbox = None + if 'displayMapRegion' in result: + box = result['displayMapRegion'] + boundingbox = [box['southLat'], box['northLat'], box['westLng'], box['eastLng']] + + links = [] + if 'telephone' in result: + telephone = result['telephone'] + links.append( + { + 'label': get_key_label('phone', user_language), + 'url': 'tel:' + telephone, + 'url_label': telephone, + } + ) + if result.get('urls'): + url = result['urls'][0] + links.append( + { + 'label': get_key_label('website', user_language), + 'url': url, + 'url_label': url, + } + ) results.append( { 'template': 'map.html', + 'type': result.get('poiCategory'), 'title': result['name'], + 'links': links, 'latitude': result['center']['lat'], 'longitude': result['center']['lng'], 'url': result['placecardUrl'], - 'boundingbox': [box['southLat'], box['northLat'], box['westLng'], box['eastLng']], + 'boundingbox': boundingbox, 'geojson': {'type': 'Point', 'coordinates': [result['center']['lng'], result['center']['lat']]}, 'address': { 'name': result['name'], - 'house_number': result.get('subThoroughfare', {}), - 'road': result.get('thoroughfare', {}), - 'locality': result.get('locality', {}), - 'postcode': result.get('postCode', {}), - 'country': result.get('country', {}), + 'house_number': result.get('subThoroughfare'), + 'road': result.get('thoroughfare'), + 'locality': result.get('locality'), + 'postcode': result.get('postCode'), + 'country': result.get('country'), }, } )