apple_maps.py 3.4 KB

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