From d5fec58ad56efbfa07e9f8f4eba33d1dc709b6c0 Mon Sep 17 00:00:00 2001 From: itskovacs Date: Sat, 1 Nov 2025 17:48:04 +0100 Subject: [PATCH] add gmaps utils --- backend/trip/utils/gmaps.py | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 backend/trip/utils/gmaps.py diff --git a/backend/trip/utils/gmaps.py b/backend/trip/utils/gmaps.py new file mode 100644 index 0000000..53d46a1 --- /dev/null +++ b/backend/trip/utils/gmaps.py @@ -0,0 +1,46 @@ +# https://developers.google.com/maps/documentation/places/web-service/nearby-search +# https://developers.google.com/maps/documentation/places/web-service/text-search +# https://developers.google.com/maps/documentation/places/web-service/place-details +# https://developers.google.com/maps/documentation/places/web-service/place-photos + +from typing import Any +import httpx +from fastapi import HTTPException + + +def compute_avg_price(price_range: dict | None) -> float | None: + if not price_range: + return None + + start = price_range.get("startPrice", {}).get("units") + end = price_range.get("endPrice", {}).get("units") + + if start and end: + return (int(start) + int(end)) / 2 + elif start: + return int(start) + elif end: + return int(end) + else: + return None + + +async def gmaps_textsearch(search: str, api_key: str) -> list[dict[str, Any]]: + url = "https://places.googleapis.com/v1/places:searchText" + body = { + "textQuery": search + } + headers = { + "Content-Type": "application/json", + "X-Goog-Api-Key": api_key, + "X-Goog-FieldMask": "places.id,places.types,places.location,places.priceRange,places.displayName,places.allowsDogs" + } + + try: + async with httpx.AsyncClient(timeout=10.0) as client: + response = await client.post(url, json=body, headers=headers) + response.raise_for_status() + data = response.json() + return data.get('places', []) + except Exception: + raise HTTPException(status_code=400, detail="Bad Request") \ No newline at end of file