Update planmate/attractions.py

#3
by Memoona648 - opened
Files changed (1) hide show
  1. planmate/attractions.py +27 -53
planmate/attractions.py CHANGED
@@ -2,78 +2,52 @@
2
 
3
  import requests
4
  from typing import List, Dict
5
- from .config import OPENTRIPMAP_BASE, get_opentripmap_key
6
 
7
- def _get(path: str, params: Dict):
8
- url = f"{OPENTRIPMAP_BASE}{path}"
 
9
  params = dict(params or {})
10
- params["apikey"] = get_opentripmap_key()
11
- r = requests.get(url, params=params, timeout=30)
12
  try:
13
  r.raise_for_status()
14
  except requests.HTTPError as e:
15
- # make API errors easier to debug in Streamlit
16
  snippet = (r.text or "")[:300]
17
- raise RuntimeError(f"OpenTripMap error {r.status_code}: {snippet}") from e
18
  return r.json()
19
 
20
- def get_poi_radius(lat: float, lon: float, radius=7000, kinds="interesting_places", limit=50) -> List[Dict]:
21
  return _get(
22
- "/places/radius",
23
  {
24
- "lat": lat,
25
- "lon": lon,
26
- "radius": radius,
27
- "kinds": kinds,
28
- "format": "json",
29
- "limit": limit,
30
- "rate": 2,
31
- },
32
- )
33
-
34
- def get_details(xid: str) -> Dict:
35
- return _get(f"/places/xid/{xid}", {})
36
 
37
- def enrich_pois(pois: List[Dict], fetch_details=False) -> List[Dict]:
38
  out = []
39
  for p in pois:
 
40
  item = {
41
- "name": p.get("name") or "(Unnamed)",
42
- "dist": p.get("dist"),
43
- "rate": p.get("rate"),
44
- "kinds": p.get("kinds", ""),
45
- "xid": p.get("xid"),
46
- "point": p.get("point", {}),
47
  }
48
- if fetch_details and item["xid"]:
49
- try:
50
- det = get_details(item["xid"])
51
- item["wikipedia"] = det.get("wikipedia")
52
- item["url"] = det.get("url")
53
- item["address"] = det.get("address", {})
54
- item["otm"] = det.get("otm")
55
- except Exception:
56
- pass
57
  out.append(item)
58
  return out
59
 
60
  def get_attractions_and_stays(lat: float, lon: float, radius=7000, limit=40):
61
- # Attractions remain unchanged
62
- attractions_kinds = "interesting_places,cultural,historic,museums,architecture"
63
-
64
- # IMPORTANT: Use taxonomy accepted by OpenTripMap.
65
- # "accomodations" (sic) is the umbrella category; for hotels use "other_hotels", not "hotels".
66
- # See OTM catalog: Accomodations (accomodations), Hotels (other_hotels), Hostels (hostels).
67
- # https://dev.opentripmap.org/catalog
68
- stays_kinds = (
69
- "accomodations,other_hotels,hostels,apartments,guest_houses,"
70
- "resorts,motels,villas_and_chalet,alpine_hut"
71
- )
72
 
73
- attractions = get_poi_radius(lat, lon, radius, attractions_kinds, limit)
74
- stays = get_poi_radius(lat, lon, radius, stays_kinds, limit=30)
75
 
76
  return {
77
- "attractions": enrich_pois(attractions, fetch_details=False),
78
- "stays": enrich_pois(stays, fetch_details=False),
79
- }
 
2
 
3
  import requests
4
  from typing import List, Dict
5
+ from .config import OPENTRIPMAP_API_KEY # Geoapify key stored under this name
6
 
7
+ GEOAPIFY_BASE = "https://api.geoapify.com/v2/places"
8
+
9
+ def _get(params: Dict):
10
  params = dict(params or {})
11
+ params["apiKey"] = OPENTRIPMAP_API_KEY # keep the same name
12
+ r = requests.get(GEOAPIFY_BASE, params=params, timeout=30)
13
  try:
14
  r.raise_for_status()
15
  except requests.HTTPError as e:
 
16
  snippet = (r.text or "")[:300]
17
+ raise RuntimeError(f"Geoapify error {r.status_code}: {snippet}") from e
18
  return r.json()
19
 
20
+ def get_poi_radius(lat: float, lon: float, radius=7000, categories="tourism.sights", limit=50) -> List[Dict]:
21
  return _get(
 
22
  {
23
+ "categories": categories,
24
+ "filter": f"circle:{lon},{lat},{radius}",
25
+ "limit": limit
26
+ }
27
+ ).get("features", [])
 
 
 
 
 
 
 
28
 
29
+ def enrich_pois(pois: List[Dict]) -> List[Dict]:
30
  out = []
31
  for p in pois:
32
+ props = p.get("properties", {})
33
  item = {
34
+ "name": props.get("name") or "(Unnamed)",
35
+ "dist": props.get("distance"),
36
+ "kinds": props.get("categories", ""),
37
+ "point": {"lat": props.get("lat"), "lon": props.get("lon")},
38
+ "address": props.get("formatted")
 
39
  }
 
 
 
 
 
 
 
 
 
40
  out.append(item)
41
  return out
42
 
43
  def get_attractions_and_stays(lat: float, lon: float, radius=7000, limit=40):
44
+ attractions_categories = "tourism.sights,tourism.museum,historic"
45
+ stays_categories = "accommodation.hotel,accommodation.hostel,accommodation.guest_house,accommodation.apartment"
 
 
 
 
 
 
 
 
 
46
 
47
+ attractions = get_poi_radius(lat, lon, radius, attractions_categories, limit)
48
+ stays = get_poi_radius(lat, lon, radius, stays_categories, limit=30)
49
 
50
  return {
51
+ "attractions": enrich_pois(attractions),
52
+ "stays": enrich_pois(stays),
53
+ }