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