fix: report weather in Fahrenheit by default
This commit is contained in:
+16
-11
@@ -24,14 +24,19 @@ WTTR_BASE = os.environ.get("WTTR_BASE", "https://wttr.in").rstrip("/")
|
|||||||
mcp = FastMCP("weather")
|
mcp = FastMCP("weather")
|
||||||
|
|
||||||
|
|
||||||
|
def _c_to_f(c: str | int) -> int:
|
||||||
|
"""Convert Celsius to Fahrenheit (rounds to whole degrees)."""
|
||||||
|
return round(int(c) * 9 / 5 + 32)
|
||||||
|
|
||||||
|
|
||||||
def _summarize(data: dict, location: str) -> str:
|
def _summarize(data: dict, location: str) -> str:
|
||||||
"""Build a short spoken-style weather summary from wttr.in j1 JSON."""
|
"""Build a short spoken-style weather summary from wttr.in j1 JSON."""
|
||||||
current = data.get("current_condition", [{}])[0] or {}
|
current = data.get("current_condition", [{}])[0] or {}
|
||||||
area = data.get("nearest_area", [{}])[0] or {}
|
area = data.get("nearest_area", [{}])[0] or {}
|
||||||
|
|
||||||
city = (area.get("areaName", [{}])[0].get("value") or location).strip()
|
city = (area.get("areaName", [{}])[0].get("value") or location).strip()
|
||||||
temp_c = current.get("temp_C")
|
temp_f = _c_to_f(current["temp_C"]) if current.get("temp_C") is not None else None
|
||||||
feels_c = current.get("FeelsLikeC")
|
feels_f = _c_to_f(current["FeelsLikeC"]) if current.get("FeelsLikeC") is not None else None
|
||||||
desc = (current.get("weatherDesc", [{}])[0].get("value") or "").strip().lower()
|
desc = (current.get("weatherDesc", [{}])[0].get("value") or "").strip().lower()
|
||||||
|
|
||||||
astronomy = data.get("astronomy", [{}])[0] or {}
|
astronomy = data.get("astronomy", [{}])[0] or {}
|
||||||
@@ -39,19 +44,19 @@ def _summarize(data: dict, location: str) -> str:
|
|||||||
sunset = astronomy.get("sunset", "")
|
sunset = astronomy.get("sunset", "")
|
||||||
|
|
||||||
today = (data.get("weather") or [None])[0] or {}
|
today = (data.get("weather") or [None])[0] or {}
|
||||||
high_c = today.get("maxtempC")
|
high_f = _c_to_f(today["maxtempC"]) if today.get("maxtempC") is not None else None
|
||||||
min_c = today.get("mintempC")
|
low_f = _c_to_f(today["mintempC"]) if today.get("mintempC") is not None else None
|
||||||
|
|
||||||
parts: list[str] = []
|
parts: list[str] = []
|
||||||
|
|
||||||
if temp_c is not None:
|
if temp_f is not None:
|
||||||
lead = f"It's {temp_c} degrees and {desc or 'clear'} in {city}"
|
lead = f"It's {temp_f} degrees and {desc or 'clear'} in {city}"
|
||||||
if feels_c is not None and feels_c != temp_c:
|
if feels_f is not None and feels_f != temp_f:
|
||||||
lead += f", feels like {feels_c}"
|
lead += f", feels like {feels_f}"
|
||||||
parts.append(lead + ".")
|
parts.append(lead + ".")
|
||||||
|
|
||||||
if high_c is not None and min_c is not None:
|
if high_f is not None and low_f is not None:
|
||||||
parts.append(f"Today's high is {high_c} and the low is {min_c}.")
|
parts.append(f"Today's high is {high_f} and the low is {low_f}.")
|
||||||
|
|
||||||
if sunrise and sunset:
|
if sunrise and sunset:
|
||||||
parts.append(f"Sunrise was at {sunrise} and sunset is at {sunset}.")
|
parts.append(f"Sunrise was at {sunrise} and sunset is at {sunset}.")
|
||||||
@@ -65,7 +70,7 @@ async def get_weather(location: str) -> str:
|
|||||||
|
|
||||||
Use this when the user asks about current conditions, temperature,
|
Use this when the user asks about current conditions, temperature,
|
||||||
or the forecast for a place. Returns a short conversational summary
|
or the forecast for a place. Returns a short conversational summary
|
||||||
suitable for reading aloud (1-2 sentences).
|
in Fahrenheit suitable for reading aloud (1-2 sentences).
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient(timeout=30) as client:
|
async with httpx.AsyncClient(timeout=30) as client:
|
||||||
|
|||||||
Reference in New Issue
Block a user