Spaces:
Sleeping
Sleeping
| # planmate/attractions.py | |
| import requests | |
| from typing import List, Dict | |
| from .config import OPENTRIPMAP_BASE, get_opentripmap_key | |
| def _get(path: str, params: Dict): | |
| url = f"{OPENTRIPMAP_BASE}{path}" | |
| params = dict(params or {}) | |
| params["apikey"] = get_opentripmap_key() | |
| r = requests.get(url, params=params, timeout=30) | |
| try: | |
| r.raise_for_status() | |
| except requests.HTTPError as e: | |
| # make API errors easier to debug in Streamlit | |
| snippet = (r.text or "")[:300] | |
| raise RuntimeError(f"OpenTripMap error {r.status_code}: {snippet}") from e | |
| return r.json() | |
| def get_poi_radius(lat: float, lon: float, radius=7000, kinds="interesting_places", limit=50) -> List[Dict]: | |
| return _get( | |
| "/places/radius", | |
| { | |
| "lat": lat, | |
| "lon": lon, | |
| "radius": radius, | |
| "kinds": kinds, | |
| "format": "json", | |
| "limit": limit, | |
| "rate": 2, | |
| }, | |
| ) | |
| def get_details(xid: str) -> Dict: | |
| return _get(f"/places/xid/{xid}", {}) | |
| def enrich_pois(pois: List[Dict], fetch_details=False) -> List[Dict]: | |
| out = [] | |
| for p in pois: | |
| item = { | |
| "name": p.get("name") or "(Unnamed)", | |
| "dist": p.get("dist"), | |
| "rate": p.get("rate"), | |
| "kinds": p.get("kinds", ""), | |
| "xid": p.get("xid"), | |
| "point": p.get("point", {}), | |
| } | |
| if fetch_details and item["xid"]: | |
| try: | |
| det = get_details(item["xid"]) | |
| item["wikipedia"] = det.get("wikipedia") | |
| item["url"] = det.get("url") | |
| item["address"] = det.get("address", {}) | |
| item["otm"] = det.get("otm") | |
| except Exception: | |
| pass | |
| out.append(item) | |
| return out | |
| def get_attractions_and_stays(lat: float, lon: float, radius=7000, limit=40): | |
| # Attractions remain unchanged | |
| attractions_kinds = "interesting_places,cultural,historic,museums,architecture" | |
| # IMPORTANT: Use taxonomy accepted by OpenTripMap. | |
| # "accomodations" (sic) is the umbrella category; for hotels use "other_hotels", not "hotels". | |
| # See OTM catalog: Accomodations (accomodations), Hotels (other_hotels), Hostels (hostels). | |
| # https://dev.opentripmap.org/catalog | |
| stays_kinds = ( | |
| "accomodations,other_hotels,hostels,apartments,guest_houses," | |
| "resorts,motels,villas_and_chalet,alpine_hut" | |
| ) | |
| attractions = get_poi_radius(lat, lon, radius, attractions_kinds, limit) | |
| stays = get_poi_radius(lat, lon, radius, stays_kinds, limit=30) | |
| return { | |
| "attractions": enrich_pois(attractions, fetch_details=False), | |
| "stays": enrich_pois(stays, fetch_details=False), | |
| } |