-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
72 lines (57 loc) · 2.27 KB
/
utils.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
62
63
64
65
66
67
68
69
70
71
72
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import subprocess
import re
from bs4 import BeautifulSoup
def maps_encode(start, finish):
#start_enc = re.sub(' ', '+', start)
start_enc = '1+Fanatical+Pl,+San+Antonio,+TX+78218'
finish_enc = re.sub(' ', '+', finish)
url = 'https://www.google.com/maps/dir/%s/%s' % (start_enc, finish_enc)
return url
def get_gmaps_html(req):
p = subprocess.Popen(["phantomjs", "spooky.js", req],
stdout=subprocess.PIPE)
gmaps_html = {'html': p.communicate()[0]}
return gmaps_html
def get_trip_stats(html):
soup = BeautifulSoup(html)
li = soup.find_all("li", class_="dir-altroute altroute-current")
td = soup.find_all("td", class_="ddw-addr")
results = {}
if li:
info = BeautifulSoup(str(li[0]))
parsed_info = [text for text in info.stripped_strings]
if len(parsed_info) >= 5:
results['distance'] = str(parsed_info[0])
results['eta_in_current_traffic'] = str(parsed_info[3].split(':')[-1]).strip()
results['route'] = str(parsed_info[4])
else:
print 'Hard Coded divs have changed :('
else:
print 'Could Not Find Route!'
if len(td) >= 1 :
dest = BeautifulSoup(str(td[1]))
parsed_dest = [text for text in dest.stripped_strings]
if len(parsed_dest) >= 3:
results['destination_name'] = str(parsed_dest[0])
results['street_address'] = str(parsed_dest[1])
results['city_state'] = str(parsed_dest[2])
try:
results['phone_number'] = str(parsed_dest[3])
except:
pass
else:
print 'Could Not Find Dest'
return results
def trip_stats(source, dest):
request_url = maps_encode(source, dest)
html = get_gmaps_html(request_url)['html']
d = get_trip_stats(html)
destination = d.get('destination_name', dest)
street = d.get('street_address', None)
city_state = d.get('city_state', None)
distance = d.get('distance', None)
eta = d.get('eta_in_current_traffic', None)
route = d.get('route', None)
trip_info = {'distance': distance, 'eta': eta, 'route': route}
return {'destination': destination, 'street': street, 'trip_info': trip_info}