-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
93 lines (73 loc) · 2.88 KB
/
search.py
File metadata and controls
93 lines (73 loc) · 2.88 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import requests
from app.models import Service
import json
import pdb
import traceback
import re
from app import app
from datetime import datetime
def striphtml(data):
p = re.compile(r'<.*?>')
return p.sub(' ', data)
def get_route(address, service):
request_string = 'https://maps.googleapis.com/maps/api/directions/json?origin=' + address +'&destination='+service.house+'%20'+service.street + '%20San%20Francisco&key=AIzaSyD_nj66yBswG4f3erU5foaVxAsJagST-9M&mode=walking'
response = requests.get(request_string)
#if response status code
response = json.loads(response.text)
#pdb.set_trace()
directions = []
for steps in response['routes'][0]['legs'][0]['steps']:
directions.append(striphtml(steps['html_instructions']))
data = {
'service' : service,
'start': response['routes'][0]['legs'][0]['start_location'],
'distance': response['routes'][0]['legs'][0]['distance']['text'].split(" ")[0],
'duration': response['routes'][0]['legs'][0]['duration']['text'],
'directions': directions
}
return data
def get_closest_service(service_type, address):
try:
address = address.split(" ")
house = address[0].strip()
street = address[1].strip()
city = 'San%20Francisco'
address_string = house+'%20'+street+'%20'+city
options = Service.query.filter(Service.service_type==service_type).all()
min_distance_service = {'service':None,'distance': 1000000}
for service in options:
#pdb.set_trace()
service_data = get_route(address_string, service)
if float(service_data['distance']) < float(min_distance_service['distance']):
min_distance_service = service_data
#pdb.set_trace()
re = (min_distance_service,True)
geotag = [
str(datetime.utcnow()),
str(service_data['start']['lat']),
str(service_data['start']['lng'])]
app.logger.info('|'.join(geotag))
return re
except:
traceback.print_exc()
re = ({'service':None}, False)
return re
def get_details(data):
if(data[0]['service'] is None):
answer = """Unfortunately none of the services are currently open. Let's try later!"""
else:
answer = """The closest open {} service to your location is {} ~{} away at {} {}. For directions press 5.""".format(
data[0]['service'].service_type,
data[0]['service'].name,
data[0]['duration'],
data[0]['service'].house,
data[0]['service'].street
)
return answer
def get_directions(data):
#pdb.set_trace()
if(data[0]['service'] is None):
answer = """Undortunately detailed directions are not available at the moment."""
else:
answer = '. '.join(data[0]['directions'])
return answer