Skip to content

Commit 3398263

Browse files
committed
last resort lookup if no other options worked (raw coords regexing)
1 parent 4cdf3ce commit 3398263

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

app/gtw.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,30 @@ def get_coordinates_from_place_id(place_id, api_key):
371371
return None
372372

373373

374-
def extract_coordinates_with_regex(url):
374+
def extract_coordinates_with_regex(url, last_resort=False):
375375
try:
376376
# Sometimes /places link may contain coordinates of the location, which
377377
# user browsed after he chosed the destination.
378378
# Lets ensure we will not try to parse it.
379379
# Same for /dir/ locations - it is google maps directions (route).
380-
if url and ("/place/" in url or "/dir/" in url):
381-
logger.debug("fastrrack: it is a 'places' or 'dir' link, parsing skipped")
382-
return None
380+
if not url:
381+
logger.error("extract_crds: no url passed")
382+
if not last_resort:
383+
if "/place/" in url or "/dir/" in url:
384+
logger.debug("extract_crds: it is a 'places' or 'dir' link, parsing skipped")
385+
return None
386+
if last_resort:
387+
logger.info("extract_crds_last_resort: lemme try to find any coords no matter what")
388+
383389
# Regex pattern to match latitude and longitude pairs
384390
pattern = r"([-+]?\d+(?:\.\d+)?),\s*([-+]?\d+(?:\.\d+)?)"
385391
match = re.search(pattern, url)
386392
if match:
387393
# Extract latitude and longitude from groups
388394
latitude, longitude = match.groups()
389-
return {"latitude": latitude, "longitude": longitude}
395+
return {"extract_crds: latitude": latitude, "longitude": longitude}
396+
else:
397+
logger.debug("extract_crds: failed to parse cords")
390398
except Exception as e:
391399
logger.error(f"Error extracting coordinates: {e}")
392400
return None
@@ -414,6 +422,8 @@ def get_wise_link(google_link: str, api_key):
414422
logger.debug("get_wise_link: Trying places API")
415423
cid = places_api_parse_cid(resolved_url)
416424
crds = get_coordinates_from_place_id(cid, api_key)
425+
if not crds:
426+
crds = extract_coordinates_with_regex(resolved_url, last_resort=True)
417427
if not crds:
418428
logger.error("get_wise_link: Every attempt to get coordinates failed")
419429
return None

0 commit comments

Comments
 (0)