Logging!
Browse files- app.py +36 -33
- camptocamp_api.py +9 -2
app.py
CHANGED
|
@@ -1,59 +1,61 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from camptocamp_api import CamptocampAPI
|
| 3 |
from typing import Optional
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
c2c = CamptocampAPI(language="en")
|
| 6 |
|
| 7 |
-
def get_recent_outings_by_location(
|
| 8 |
-
location:
|
| 9 |
-
start_date: Optional[str] = None,
|
| 10 |
-
end_date: Optional[str] = None,
|
| 11 |
-
activity: Optional[str] = None,
|
| 12 |
-
limit: int = 10
|
| 13 |
-
) -> dict:
|
| 14 |
-
"""
|
| 15 |
-
Get recent outings for a location name using geocoded bounding box.
|
| 16 |
-
"""
|
| 17 |
bbox = c2c.get_bbox_from_location(location)
|
| 18 |
if not bbox:
|
|
|
|
| 19 |
return {"error": f"Could not resolve bounding box for location: {location}"}
|
|
|
|
| 20 |
date_range = (start_date, end_date) if start_date and end_date else None
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
def search_routes_by_location(
|
| 24 |
-
location:
|
| 25 |
-
activity: str,
|
| 26 |
-
limit: int = 10
|
| 27 |
-
) -> dict:
|
| 28 |
-
"""
|
| 29 |
-
Search for routes near a named location.
|
| 30 |
-
"""
|
| 31 |
bbox = c2c.get_bbox_from_location(location)
|
| 32 |
if not bbox:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def get_route_details(route_id: int) -> dict:
|
| 37 |
-
""
|
| 38 |
-
Retrieve detailed route information by route ID.
|
| 39 |
-
"""
|
| 40 |
return c2c.get_route_details(route_id)
|
| 41 |
|
| 42 |
def search_waypoints_by_location(location: str, limit: int = 10) -> dict:
|
| 43 |
-
""
|
| 44 |
-
Get known waypoints (e.g. huts, peaks) in a given region.
|
| 45 |
-
"""
|
| 46 |
bbox = c2c.get_bbox_from_location(location)
|
| 47 |
if not bbox:
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def lookup_bbox_from_location(location_name: str) -> Optional[dict]:
|
| 52 |
-
""
|
| 53 |
-
Look up the bounding box for a given place name.
|
| 54 |
-
"""
|
| 55 |
bbox = c2c.get_bbox_from_location(location_name)
|
| 56 |
if not bbox:
|
|
|
|
| 57 |
return {"error": f"No bounding box found for: {location_name}"}
|
| 58 |
return {
|
| 59 |
"west": bbox[0],
|
|
@@ -62,8 +64,9 @@ def lookup_bbox_from_location(location_name: str) -> Optional[dict]:
|
|
| 62 |
"north": bbox[3]
|
| 63 |
}
|
| 64 |
|
|
|
|
| 65 |
with gr.Blocks(title="Camptocamp MCP Server") as demo:
|
| 66 |
-
gr.Markdown("# ποΈ Camptocamp API
|
| 67 |
|
| 68 |
with gr.Tab("π Recent Outings"):
|
| 69 |
loc = gr.Textbox(label="Location (e.g. Chamonix, La Grave)")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import logging
|
| 3 |
from camptocamp_api import CamptocampAPI
|
| 4 |
from typing import Optional
|
| 5 |
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO,
|
| 9 |
+
format="%(asctime)s [%(levelname)s] %(message)s"
|
| 10 |
+
)
|
| 11 |
+
logger = logging.getLogger("CamptocampApp")
|
| 12 |
+
|
| 13 |
+
# Instantiate API client
|
| 14 |
c2c = CamptocampAPI(language="en")
|
| 15 |
|
| 16 |
+
def get_recent_outings_by_location(location: str, start_date: Optional[str] = None, end_date: Optional[str] = None, activity: Optional[str] = None, limit: int = 10) -> dict:
|
| 17 |
+
logger.info(f"[Outings] Resolving location: {location}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
bbox = c2c.get_bbox_from_location(location)
|
| 19 |
if not bbox:
|
| 20 |
+
logger.warning(f"No bounding box found for: {location}")
|
| 21 |
return {"error": f"Could not resolve bounding box for location: {location}"}
|
| 22 |
+
logger.info(f"BBox for '{location}': {bbox}")
|
| 23 |
date_range = (start_date, end_date) if start_date and end_date else None
|
| 24 |
+
result = c2c.get_recent_outings(bbox, date_range, activity, limit)
|
| 25 |
+
logger.info(f"Returned {len(result.get('documents', []))} outings.")
|
| 26 |
+
return result
|
| 27 |
|
| 28 |
+
def search_routes_by_location(location: str, activity: str, limit: int = 10) -> dict:
|
| 29 |
+
logger.info(f"[Routes] Resolving location: {location}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
bbox = c2c.get_bbox_from_location(location)
|
| 31 |
if not bbox:
|
| 32 |
+
logger.warning(f"No bounding box found for: {location}")
|
| 33 |
+
return {"error": f"Could not resolve bounding box for location: {location}"}
|
| 34 |
+
logger.info(f"BBox for '{location}': {bbox}")
|
| 35 |
+
result = c2c.search_routes_by_activity(bbox, activity, limit)
|
| 36 |
+
logger.info(f"Returned {len(result.get('documents', []))} routes.")
|
| 37 |
+
return result
|
| 38 |
|
| 39 |
def get_route_details(route_id: int) -> dict:
|
| 40 |
+
logger.info(f"[Details] Fetching route ID: {route_id}")
|
|
|
|
|
|
|
| 41 |
return c2c.get_route_details(route_id)
|
| 42 |
|
| 43 |
def search_waypoints_by_location(location: str, limit: int = 10) -> dict:
|
| 44 |
+
logger.info(f"[Waypoints] Resolving location: {location}")
|
|
|
|
|
|
|
| 45 |
bbox = c2c.get_bbox_from_location(location)
|
| 46 |
if not bbox:
|
| 47 |
+
logger.warning(f"No bounding box found for: {location}")
|
| 48 |
+
return {"error": f"Could not resolve bounding box for location: {location}"}
|
| 49 |
+
logger.info(f"BBox for '{location}': {bbox}")
|
| 50 |
+
result = c2c.search_waypoints(bbox, limit)
|
| 51 |
+
logger.info(f"Returned {len(result.get('documents', []))} waypoints.")
|
| 52 |
+
return result
|
| 53 |
|
| 54 |
def lookup_bbox_from_location(location_name: str) -> Optional[dict]:
|
| 55 |
+
logger.info(f"[Lookup] Resolving location: {location_name}")
|
|
|
|
|
|
|
| 56 |
bbox = c2c.get_bbox_from_location(location_name)
|
| 57 |
if not bbox:
|
| 58 |
+
logger.warning(f"No bounding box found for: {location_name}")
|
| 59 |
return {"error": f"No bounding box found for: {location_name}"}
|
| 60 |
return {
|
| 61 |
"west": bbox[0],
|
|
|
|
| 64 |
"north": bbox[3]
|
| 65 |
}
|
| 66 |
|
| 67 |
+
# Gradio UI
|
| 68 |
with gr.Blocks(title="Camptocamp MCP Server") as demo:
|
| 69 |
+
gr.Markdown("# ποΈ Camptocamp API MCP")
|
| 70 |
|
| 71 |
with gr.Tab("π Recent Outings"):
|
| 72 |
loc = gr.Textbox(label="Location (e.g. Chamonix, La Grave)")
|
camptocamp_api.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import requests
|
|
|
|
| 2 |
from typing import Tuple, Optional, Dict, Any
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
class CamptocampAPI:
|
| 6 |
"""
|
|
@@ -15,7 +17,9 @@ class CamptocampAPI:
|
|
| 15 |
|
| 16 |
def _request(self, endpoint: str, params: Dict[str, Any]) -> Dict[str, Any]:
|
| 17 |
params["pl"] = self.language
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
response.raise_for_status()
|
| 20 |
return response.json()
|
| 21 |
|
|
@@ -83,12 +87,15 @@ class CamptocampAPI:
|
|
| 83 |
"limit": 1
|
| 84 |
}
|
| 85 |
headers = {"User-Agent": "camptocamp-api-wrapper"}
|
|
|
|
| 86 |
response = requests.get(url, params=params, headers=headers)
|
| 87 |
response.raise_for_status()
|
| 88 |
results = response.json()
|
| 89 |
if not results:
|
|
|
|
| 90 |
return None
|
| 91 |
-
bbox = results[0]["boundingbox"]
|
|
|
|
| 92 |
return (
|
| 93 |
float(bbox[2]), # west
|
| 94 |
float(bbox[0]), # south
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import logging
|
| 3 |
from typing import Tuple, Optional, Dict, Any
|
| 4 |
|
| 5 |
+
logger = logging.getLogger("CamptocampAPI")
|
| 6 |
|
| 7 |
class CamptocampAPI:
|
| 8 |
"""
|
|
|
|
| 17 |
|
| 18 |
def _request(self, endpoint: str, params: Dict[str, Any]) -> Dict[str, Any]:
|
| 19 |
params["pl"] = self.language
|
| 20 |
+
url = f"{self.BASE_URL}{endpoint}"
|
| 21 |
+
logger.info(f"Requesting {url} with params: {params}")
|
| 22 |
+
response = requests.get(url, params=params)
|
| 23 |
response.raise_for_status()
|
| 24 |
return response.json()
|
| 25 |
|
|
|
|
| 87 |
"limit": 1
|
| 88 |
}
|
| 89 |
headers = {"User-Agent": "camptocamp-api-wrapper"}
|
| 90 |
+
logger.info(f"Geocoding location: {query}")
|
| 91 |
response = requests.get(url, params=params, headers=headers)
|
| 92 |
response.raise_for_status()
|
| 93 |
results = response.json()
|
| 94 |
if not results:
|
| 95 |
+
logger.warning(f"No results found for: {query}")
|
| 96 |
return None
|
| 97 |
+
bbox = results[0]["boundingbox"]
|
| 98 |
+
logger.info(f"BBox for '{query}': {bbox}")
|
| 99 |
return (
|
| 100 |
float(bbox[2]), # west
|
| 101 |
float(bbox[0]), # south
|