Spaces:
Running
Running
brian-yu-nexusflow
commited on
Commit
β’
d082a11
1
Parent(s):
24a8d18
Update tools.py
Browse files
tools.py
CHANGED
@@ -25,12 +25,6 @@ class Tools:
|
|
25 |
self.gmaps = Client(config.gmaps_client_key)
|
26 |
self.client_ip: str | None = None
|
27 |
|
28 |
-
def capitalize(self, s: str) -> str:
|
29 |
-
if s.lower() != s:
|
30 |
-
return s
|
31 |
-
|
32 |
-
return s.title()
|
33 |
-
|
34 |
def haversine(self, lon1, lat1, lon2, lat2) -> float:
|
35 |
"""
|
36 |
Calculate the great circle distance in kilometers between two points on the earth (specified in decimal degrees).
|
@@ -98,13 +92,26 @@ class Tools:
|
|
98 |
|
99 |
- location: This can be a city like 'Austin', or a place like 'Austin Airport', etc.
|
100 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
-
# For response
|
103 |
-
|
104 |
-
|
105 |
-
for r in results:
|
106 |
-
r["name"] = location
|
107 |
-
return results
|
108 |
|
109 |
def get_distance(self, place_1: str, place_2: str):
|
110 |
"""
|
@@ -182,25 +189,28 @@ class Tools:
|
|
182 |
- location (str): The location for the search. This can be a city's name, region, or anything that specifies the location.
|
183 |
- radius_miles (int): Optional. The max distance from the described location to limit the search. Distance is specified in miles.
|
184 |
"""
|
185 |
-
|
186 |
-
|
187 |
-
geocode_result = self.gmaps.geocode(location)
|
188 |
-
if geocode_result:
|
189 |
-
latlong = geocode_result[0]["geometry"]["location"]
|
190 |
-
location = (latlong["lat"], latlong["lng"])
|
191 |
-
else:
|
192 |
return []
|
|
|
|
|
|
|
193 |
|
194 |
type_of_place = " ".join(type_of_place)
|
195 |
# Perform the search using Google Places API
|
196 |
# For response format, see https://developers.google.com/maps/documentation/places/web-service/search-nearby#nearby-search-responses
|
197 |
-
|
198 |
-
location=
|
|
|
|
|
199 |
)
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
204 |
distance = self.haversine(
|
205 |
latlong["lng"],
|
206 |
latlong["lat"],
|
@@ -211,10 +221,9 @@ class Tools:
|
|
211 |
continue
|
212 |
|
213 |
distance = distance * 0.621371
|
214 |
-
|
215 |
-
|
216 |
|
217 |
-
places = new_places
|
218 |
if len(places) == 0:
|
219 |
return []
|
220 |
|
@@ -251,25 +260,16 @@ class Tools:
|
|
251 |
elif isinstance(place_name, dict) and "name" in place_name:
|
252 |
place_name = place_name["name"]
|
253 |
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
if len(search_results.get("candidates", [])) == 0:
|
260 |
-
return []
|
261 |
|
262 |
-
|
263 |
-
place_id = search_results["candidates"][0]["place_id"]
|
264 |
-
# For response format, see https://developers.google.com/maps/documentation/places/web-service/details#PlaceDetailsResponses
|
265 |
-
place_details = self.gmaps.place(place_id=place_id)
|
266 |
-
reviews = place_details["result"].get("reviews", [])
|
267 |
|
268 |
for review in reviews:
|
269 |
review["for_location"] = place_name
|
270 |
-
review["formatted_address"] = place_details["
|
271 |
-
"formatted_address"
|
272 |
-
]
|
273 |
|
274 |
all_reviews.extend(reviews)
|
275 |
|
|
|
25 |
self.gmaps = Client(config.gmaps_client_key)
|
26 |
self.client_ip: str | None = None
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def haversine(self, lon1, lat1, lon2, lat2) -> float:
|
29 |
"""
|
30 |
Calculate the great circle distance in kilometers between two points on the earth (specified in decimal degrees).
|
|
|
92 |
|
93 |
- location: This can be a city like 'Austin', or a place like 'Austin Airport', etc.
|
94 |
"""
|
95 |
+
if (
|
96 |
+
isinstance(location, list)
|
97 |
+
and len(location) != 0
|
98 |
+
and isinstance(location[0], dict)
|
99 |
+
):
|
100 |
+
return location
|
101 |
+
|
102 |
+
# For response content, see https://developers.google.com/maps/documentation/places/web-service/search-find-place#find-place-responses
|
103 |
+
results = self.gmaps.find_place(
|
104 |
+
location, input_type="textquery", location_bias="ipbias"
|
105 |
+
)
|
106 |
+
if results["status"] != "OK":
|
107 |
+
return []
|
108 |
+
|
109 |
+
# We always use the first candidate
|
110 |
+
place_id = results["candidates"][0]["place_id"]
|
111 |
|
112 |
+
# For response format, see https://developers.google.com/maps/documentation/places/web-service/details#PlaceDetailsResponses
|
113 |
+
place_details = self.gmaps.place(place_id=place_id)["result"]
|
114 |
+
return [place_details]
|
|
|
|
|
|
|
115 |
|
116 |
def get_distance(self, place_1: str, place_2: str):
|
117 |
"""
|
|
|
189 |
- location (str): The location for the search. This can be a city's name, region, or anything that specifies the location.
|
190 |
- radius_miles (int): Optional. The max distance from the described location to limit the search. Distance is specified in miles.
|
191 |
"""
|
192 |
+
place_details = self.get_latitude_longitude(location)
|
193 |
+
if len(place_details) == 0:
|
|
|
|
|
|
|
|
|
|
|
194 |
return []
|
195 |
+
place_details = place_details[0]
|
196 |
+
location = place_details["name"]
|
197 |
+
latlong = place_details["geometry"]["location"]
|
198 |
|
199 |
type_of_place = " ".join(type_of_place)
|
200 |
# Perform the search using Google Places API
|
201 |
# For response format, see https://developers.google.com/maps/documentation/places/web-service/search-nearby#nearby-search-responses
|
202 |
+
places_nearby = self.gmaps.places_nearby(
|
203 |
+
location=(latlong["lat"], latlong["lng"]),
|
204 |
+
keyword=type_of_place,
|
205 |
+
radius=radius_miles * 1609.34,
|
206 |
)
|
207 |
+
if places_nearby["status"] != "OK":
|
208 |
+
return []
|
209 |
+
|
210 |
+
places_nearby = places_nearby["results"]
|
211 |
+
places = []
|
212 |
+
for place_nearby in places_nearby:
|
213 |
+
place_location = place_nearby["geometry"]["location"]
|
214 |
distance = self.haversine(
|
215 |
latlong["lng"],
|
216 |
latlong["lat"],
|
|
|
221 |
continue
|
222 |
|
223 |
distance = distance * 0.621371
|
224 |
+
place_nearby["distance"] = f"{distance} miles from {location}"
|
225 |
+
places.append(place_nearby)
|
226 |
|
|
|
227 |
if len(places) == 0:
|
228 |
return []
|
229 |
|
|
|
260 |
elif isinstance(place_name, dict) and "name" in place_name:
|
261 |
place_name = place_name["name"]
|
262 |
|
263 |
+
place_details = self.get_latitude_longitude(place_name)
|
264 |
+
if len(place_details) == 0:
|
265 |
+
continue
|
266 |
+
place_details = place_details[0]
|
|
|
|
|
|
|
267 |
|
268 |
+
reviews = place_details.get("reviews", [])
|
|
|
|
|
|
|
|
|
269 |
|
270 |
for review in reviews:
|
271 |
review["for_location"] = place_name
|
272 |
+
review["formatted_address"] = place_details["formatted_address"]
|
|
|
|
|
273 |
|
274 |
all_reviews.extend(reviews)
|
275 |
|