import urllib.request import urllib.parse from typing import Optional from app.config import settings def get_current_weather() -> Optional[str]: """Fetch current weather report from wttr.in in Russian.""" city = urllib.parse.quote(settings.WEATHER_CITY) if settings.WEATHER_CITY else "" url = f"https://wttr.in/{city}?format=%C,+%t&lang=ru" try: req = urllib.request.Request( url, headers={"User-Agent": "curl/7.88.1"} ) with urllib.request.urlopen(req, timeout=3.5) as resp: text = resp.read().decode("utf-8").strip() # Basic validation if text and not text.startswith("<") and len(text) < 60: return text except Exception as e: print(f"[RadioWeather] Warning: unable to fetch weather: {e}") return None