generated from hcp-uw/react-fastapi-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
find_restaurants.py
188 lines (150 loc) · 5.29 KB
/
find_restaurants.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import random
import geocoder
import requests
import json
import geopy.distance
from geopy import distance
my_api_key = "&key=AIzaSyCxduNEld5Ek1zYcr7nlrVLhJBBwlH1Fy4"
# my_api_key = "&key=AIzaSyBsfTYeutSAt0mTeJ-_tSWas2lhlymwIlE" # (Mayee's Key)
def get_user_coordinates(api_key, address):
url = f'https://maps.googleapis.com/maps/api/geocode/json?address={address}&key={api_key}'
response = requests.get(url)
if(response.status_code == 200): # 200 means no error code
data = response.json()
if(data['status'] == 'OK'):
# extracting the coordinates
location = data['results'][0]['geometry']['location']
latitude = location['lat']
longitude = location['lng']
return latitude, longitude
else:
print(f"Error: {data['status']}")
return None
else:
print(f"Request failed with status code {response.status_code}")
return None
address = input("Enter Address: ")
currLocCoords= get_user_coordinates(my_api_key[5:], address)
print('User coordinates: ' + str(currLocCoords))
currLocCoordsURL = ""
if(currLocCoords):
currLocCoordsURL = str(currLocCoords[0]) + "%2C" + str(currLocCoords[1])
else:
print("Unable to fetch your location. Set default location to the University of Washington")
currLocCoords = (47.655860161693504, -122.30954131041663)
currLocCoordsURL = str(currLocCoords[0]) + "%2C" + str(currLocCoords[1])
url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + currLocCoordsURL
keyword = input("What type of cuisine? (Hit enter for all types) ").lower().replace(" ", "%20")
keyword = "&keyword=" + keyword
url = url + keyword
minprice = input("Minimum price? Enter $ for inexpensive (under $10), $$ for moderately expensive ($10 - $25), $$$ for expensive ($25 - $45), $$$$ for very expensive ($50 +). ")
if minprice != "":
maxprice = "&minprice=" + minprice
url = url + maxprice
maxprice = input("Maximum price? Enter $ for inexpensive (under $10), $$ for moderately expensive ($10 - $25), $$$ for expensive ($25 - $45), $$$$ for very expensive ($50 +). ")
if maxprice != "":
maxprice = "&maxprice=" + maxprice
url = url + maxprice
opennow = input("Do you want it to be open now? (Y or N) ").lower()
if opennow == "y":
opennow = "&opennow=true"
url = url + opennow
elif opennow == "n":
opennow = "&opennow=false"
url = url + opennow
radius = input("What is the maximum distance you are willing to travel in miles? ")
if radius != "":
radius = str(round(float(radius) * 1609.34))
radius = "&radius=" + radius
url = url + radius
else:
url = url + "&radius=3200"
type = "&type=restaurant"
url = url + type
url = url + my_api_key
response = requests.request("GET", url)
response = json.loads(response.text)
status = response['status']
if status == "OK":
results = response["results"]
else:
print(status)
exit()
numPlaces = len(results)
restAttributesList = ["name", "businessStatus", "openNow", "priceLevel", "rating", "totalUserRatings", "distance", "address"]
restInstVarList = []
class restaurant:
def __init__(self):
self.name = ""
self.businessStatus = ""
self.openNow = ""
self.priceLevel = ""
self.rating = -1
self.totalUserRatings = -1
self.distance = -1
self.address = ""
for i in range(numPlaces):
currPlace = results[i]
rest = restaurant()
try:
name = currPlace["name"]
rest.name = name
except KeyError as e:
pass
try:
businessStatus = currPlace["business_status"]
rest.businessStatus = businessStatus
except KeyError as e:
pass
try:
openNow = currPlace["opening_hours"]["open_now"]
rest.openNow = openNow
except KeyError as e:
pass
try:
priceLevel = currPlace["price_level"]
rest.priceLevel = priceLevel
except KeyError as e:
pass
try:
rating = currPlace["rating"]
rest.rating = rating
except KeyError as e:
pass
try:
totalUserRatings = currPlace["user_ratings_total"]
rest.totalUserRatings = totalUserRatings
except KeyError as e:
pass
try:
placeCoords = currPlace["geometry"]["location"]
currPlaceCoords = (placeCoords["lat"], placeCoords["lng"])
distance = geopy.distance.geodesic(currLocCoords, currPlaceCoords).miles
# print(currLocCoords, currPlaceCoords)
# distance2 = distance.distance(currPlaceCoords, currLocCoords).miles
# print('distance: ' + distance)
# print('distance2: ' + distance2)
rest.distance = distance
except KeyError as e:
pass
try:
address = currPlace["vicinity"]
rest.address = address
except KeyError as e:
pass
restInstVars = vars(rest)
restInstVarList.append(restInstVars)
for r in restInstVarList:
print(r["name"] + ": ")
print(currLocCoords)
print(placeCoords)
print()
for i in range(1, 8):
a = restAttributesList[i]
print("\t" + a + ": " + str(r[a]))
print("\n")
randomChoice = random.choice(restInstVarList)
print(randomChoice["name"] + ": ")
for i in range(1, 8):
a = restAttributesList[i]
print("\t" + a + ": " + str(randomChoice[a]))