-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_maps.py
61 lines (50 loc) · 2.58 KB
/
google_maps.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
import json
import time
import emailer
BASE_GOOGLE_MAPS_URI = 'https://maps.googleapis.com/maps/api/directions/json'
def get_time_to_work(selected_properties, house_memeber_details):
# get the name-value pairs of house members and their addresses from the details
addresses = {}
for house_member in house_memeber_details:
addresses[house_member["name"]] = house_member["commute_destination"]
email_message_body = ''
# read each property
for prop in selected_properties:
email_message_body += 'Property address: ' + prop['displayable_address'] + '\n'
email_message_body += 'Link: ' + 'http://www.zoopla.co.uk/to-rent/details/' + prop['listing_id'] + '\n'
origin_address = prop['displayable_address'].replace(' ', '+')
for address in addresses:
email_message_body += address + ' travel: ' + '\n'
destination_address = addresses[address].replace(' ', '+')
url_driving = '{0}?origin={1}&destination={2}'.format(BASE_GOOGLE_MAPS_URI, origin_address, destination_address)
dict_data = send_request(url_driving)
email_message_body += ('Driving: {0} ({1})'.format(dict_data['routes'][0]['legs'][0]['duration']['text'],
dict_data['routes'][0]['legs'][0]['distance']['text']) + '\n')
url_transit = '{0}?origin={1}&destination={2}&mode=transit'.format(BASE_GOOGLE_MAPS_URI,
origin_address, destination_address)
dict_data = send_request(url_transit)
email_message_body += 'Transit: {0} ({1})'.format(dict_data['routes'][0]['legs'][0]['duration']['text'],
dict_data['routes'][0]['legs'][0]['distance']['text']) + '\n'
email_message_body += '\n'
# send email notifying that new properties have been found
print(email_message_body)
emailer.send_notification_emails(email_message_body)
def send_request(url):
"""
Google Maps limits searches to 10 per second.
If limit is reached, wait 2 seconds and retry
:param url:
:return:
"""
response = requests.get(url)
dict_data = json.loads(response.content)
if dict_data['status'] == 'OVER_QUERY_LIMIT':
time.sleep(2)
response = requests.get(url)
dict_data = json.loads(response.content)
if dict_data['status'] == 'OVER_QUERY_LIMIT':
time.sleep(2)
response = requests.get(url)
dict_data = json.loads(response.content)
return dict_data