fix: report weather in Fahrenheit by default

This commit is contained in:
Shane
2026-08-22 16:23:09 -04:00
parent c620867854
commit f882b694b9
+16 -11
View File
@@ -24,14 +24,19 @@ WTTR_BASE = os.environ.get("WTTR_BASE", "https://wttr.in").rstrip("/")
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:
"""Build a short spoken-style weather summary from wttr.in j1 JSON."""
current = data.get("current_condition", [{}])[0] or {}
area = data.get("nearest_area", [{}])[0] or {}
city = (area.get("areaName", [{}])[0].get("value") or location).strip()
temp_c = current.get("temp_C")
feels_c = current.get("FeelsLikeC")
temp_f = _c_to_f(current["temp_C"]) if current.get("temp_C") is not None else None
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()
astronomy = data.get("astronomy", [{}])[0] or {}
@@ -39,19 +44,19 @@ def _summarize(data: dict, location: str) -> str:
sunset = astronomy.get("sunset", "")
today = (data.get("weather") or [None])[0] or {}
high_c = today.get("maxtempC")
min_c = today.get("mintempC")
high_f = _c_to_f(today["maxtempC"]) if today.get("maxtempC") is not None else None
low_f = _c_to_f(today["mintempC"]) if today.get("mintempC") is not None else None
parts: list[str] = []
if temp_c is not None:
lead = f"It's {temp_c} degrees and {desc or 'clear'} in {city}"
if feels_c is not None and feels_c != temp_c:
lead += f", feels like {feels_c}"
if temp_f is not None:
lead = f"It's {temp_f} degrees and {desc or 'clear'} in {city}"
if feels_f is not None and feels_f != temp_f:
lead += f", feels like {feels_f}"
parts.append(lead + ".")
if high_c is not None and min_c is not None:
parts.append(f"Today's high is {high_c} and the low is {min_c}.")
if high_f is not None and low_f is not None:
parts.append(f"Today's high is {high_f} and the low is {low_f}.")
if sunrise and 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,
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:
async with httpx.AsyncClient(timeout=30) as client: