forked from yarysh/cian_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.py
30 lines (26 loc) · 1.02 KB
/
distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import requests
from config import GOOGLE_DISTANCE_API_KEY
class Distance:
@staticmethod
def calc(origin):
result = {'text': None, 'value': None}
origin += ', Москва, Россия'
resp = requests.get(
'https://maps.googleapis.com/maps/api/distancematrix/json',
params={
'origins': origin,
'destinations': '55.792589, 37.527588',
'mode': 'walking',
'language': 'ru',
'key': GOOGLE_DISTANCE_API_KEY,
}
)
if resp.status_code != 200: return result
data = resp.json()
for route in data['rows']:
for elem in route['elements']:
if elem['status'] == 'OK' and elem['duration']['value']:
if not result['value'] or elem['duration']['value'] < result['value']:
result['value'] = elem['duration']['value']
result['text'] = elem['duration']['text']
return result