apple_maps.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Apple Maps"""
  3. from json import loads
  4. from time import time
  5. from urllib.parse import urlencode
  6. from searx.network import get as http_get
  7. from searx.engines.openstreetmap import get_key_label
  8. about = {
  9. "website": 'https://www.apple.com/maps/',
  10. "wikidata_id": 'Q276101',
  11. "official_api_documentation": None,
  12. "use_official_api": True,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. token = {'value': '', 'last_updated': None}
  17. categories = ['map']
  18. paging = False
  19. search_url = "https://api.apple-mapkit.com/v1/search?{query}&mkjsVersion=5.72.53"
  20. def obtain_token():
  21. update_time = time() - (time() % 1800)
  22. try:
  23. # use duckduckgo's mapkit token
  24. token_response = http_get('https://duckduckgo.com/local.js?get_mk_token=1', timeout=2.0)
  25. actual_token = http_get(
  26. 'https://cdn.apple-mapkit.com/ma/bootstrap?apiVersion=2&mkjsVersion=5.72.53&poi=1',
  27. timeout=2.0,
  28. headers={'Authorization': 'Bearer ' + token_response.text},
  29. )
  30. token['value'] = loads(actual_token.text)['authInfo']['access_token']
  31. token['last_updated'] = update_time
  32. # pylint: disable=bare-except
  33. except:
  34. pass
  35. return token
  36. def request(query, params):
  37. if time() - (token['last_updated'] or 0) > 1800:
  38. obtain_token()
  39. params['url'] = search_url.format(query=urlencode({'q': query, 'lang': params['language']}))
  40. params['headers'] = {'Authorization': 'Bearer ' + token['value']}
  41. return params
  42. def response(resp):
  43. results = []
  44. resp_json = loads(resp.text)
  45. user_language = resp.search_params['language']
  46. for result in resp_json['results']:
  47. boundingbox = None
  48. if 'displayMapRegion' in result:
  49. box = result['displayMapRegion']
  50. boundingbox = [box['southLat'], box['northLat'], box['westLng'], box['eastLng']]
  51. links = []
  52. if 'telephone' in result:
  53. telephone = result['telephone']
  54. links.append(
  55. {
  56. 'label': get_key_label('phone', user_language),
  57. 'url': 'tel:' + telephone,
  58. 'url_label': telephone,
  59. }
  60. )
  61. if result.get('urls'):
  62. url = result['urls'][0]
  63. links.append(
  64. {
  65. 'label': get_key_label('website', user_language),
  66. 'url': url,
  67. 'url_label': url,
  68. }
  69. )
  70. results.append(
  71. {
  72. 'template': 'map.html',
  73. 'type': result.get('poiCategory'),
  74. 'title': result['name'],
  75. 'links': links,
  76. 'latitude': result['center']['lat'],
  77. 'longitude': result['center']['lng'],
  78. 'url': result['placecardUrl'],
  79. 'boundingbox': boundingbox,
  80. 'geojson': {'type': 'Point', 'coordinates': [result['center']['lng'], result['center']['lat']]},
  81. 'address': {
  82. 'name': result['name'],
  83. 'house_number': result.get('subThoroughfare'),
  84. 'road': result.get('thoroughfare'),
  85. 'locality': result.get('locality'),
  86. 'postcode': result.get('postCode'),
  87. 'country': result.get('country'),
  88. },
  89. }
  90. )
  91. return results