115 lines
4.4 KiB
Python
115 lines
4.4 KiB
Python
"""
|
|
WeatherService - Integración meteorológica y de ubicación en tiempo real para UniCABA.
|
|
Suministra el estado del clima, temperatura y sensación térmica para la Sede Central en CABA
|
|
utilizando la API abierta de Open-Meteo, con caché en memoria de 15 minutos y fallback resiliente.
|
|
"""
|
|
import time
|
|
import requests
|
|
from typing import Dict, Any, Optional
|
|
|
|
WMO_WEATHER_CODES = {
|
|
0: "Despejado / Soleado",
|
|
1: "Mayormente despejado",
|
|
2: "Parcialmente nublado",
|
|
3: "Nublado",
|
|
45: "Niebla",
|
|
48: "Niebla con escarcha",
|
|
51: "Llovizna ligera",
|
|
53: "Llovizna moderada",
|
|
55: "Llovizna densa",
|
|
61: "Lluvia ligera",
|
|
63: "Lluvia moderada",
|
|
65: "Lluvia intensa",
|
|
71: "Nevada ligera",
|
|
73: "Nevada moderada",
|
|
75: "Nevada intensa",
|
|
80: "Chubascos leves",
|
|
81: "Chubascos moderados",
|
|
82: "Chubascos violentos",
|
|
95: "Tormenta eléctrica",
|
|
96: "Tormenta con granizo ligero",
|
|
99: "Tormenta con granizo fuerte"
|
|
}
|
|
|
|
|
|
class WeatherService:
|
|
# Coordenadas exactas de UniCABA Edificio Central: Tte. Gral. Juan Domingo Perón 802, CABA
|
|
LATITUDE = -34.6037
|
|
LONGITUDE = -58.3816
|
|
CAMPUS_NAME = "UniCABA - Edificio Central"
|
|
CAMPUS_ADDRESS = "Tte. Gral. Juan Domingo Perón 802, San Nicolás, CABA (C1038AAP)"
|
|
MAPS_DESTINATION_URL = "https://www.google.com/maps/dir/?api=1&destination=UniCABA,+Tte.+Gral.+Juan+Domingo+Per%C3%B3n+802,+CABA"
|
|
|
|
_cached_weather: Optional[Dict[str, Any]] = None
|
|
_last_fetched: float = 0.0
|
|
CACHE_DURATION: float = 900.0 # 15 minutos de caché
|
|
|
|
@classmethod
|
|
def get_university_weather(cls) -> Dict[str, Any]:
|
|
"""
|
|
Retorna el estado del clima actual en la universidad.
|
|
Si la llamada falla o está en caché, devuelve el último dato conocido o un fallback.
|
|
"""
|
|
now = time.time()
|
|
if cls._cached_weather and (now - cls._last_fetched < cls.CACHE_DURATION):
|
|
return cls._cached_weather
|
|
|
|
url = (
|
|
f"https://api.open-meteo.com/v1/forecast?"
|
|
f"latitude={cls.LATITUDE}&longitude={cls.LONGITUDE}&"
|
|
f"current=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,weather_code,wind_speed_10m"
|
|
)
|
|
|
|
try:
|
|
resp = requests.get(url, timeout=3.0)
|
|
if resp.status_code == 200:
|
|
data = resp.json().get("current", {})
|
|
temp = data.get("temperature_2m", 20.0)
|
|
app_temp = data.get("apparent_temperature", temp)
|
|
humidity = data.get("relative_humidity_2m", 60)
|
|
precip = data.get("precipitation", 0.0)
|
|
code = data.get("weather_code", 0)
|
|
wind = data.get("wind_speed_10m", 5.0)
|
|
|
|
condition = WMO_WEATHER_CODES.get(code, "Agradable")
|
|
if precip > 0:
|
|
condition += f" (Precipitación: {precip} mm)"
|
|
|
|
cls._cached_weather = {
|
|
"campus": cls.CAMPUS_NAME,
|
|
"address": cls.CAMPUS_ADDRESS,
|
|
"temperature": round(temp, 1),
|
|
"apparent_temperature": round(app_temp, 1),
|
|
"humidity": humidity,
|
|
"condition": condition,
|
|
"wind_speed_kmh": round(wind, 1),
|
|
"maps_url": cls.MAPS_DESTINATION_URL,
|
|
"is_live": True,
|
|
"summary": (
|
|
f"Temperatura actual en CABA: {round(temp, 1)}°C (Sensación térmica: {round(app_temp, 1)}°C) | "
|
|
f"Humedad: {humidity}% | Estado: {condition} | Viento: {round(wind, 1)} km/h."
|
|
)
|
|
}
|
|
cls._last_fetched = now
|
|
return cls._cached_weather
|
|
except Exception:
|
|
pass
|
|
|
|
# Fallback si no hay conexión externa
|
|
fallback = {
|
|
"campus": cls.CAMPUS_NAME,
|
|
"address": cls.CAMPUS_ADDRESS,
|
|
"temperature": 20.0,
|
|
"apparent_temperature": 20.0,
|
|
"humidity": 65,
|
|
"condition": "Templado / Parcialmente nublado",
|
|
"wind_speed_kmh": 10.0,
|
|
"maps_url": cls.MAPS_DESTINATION_URL,
|
|
"is_live": False,
|
|
"summary": (
|
|
f"Sede Central en CABA: Clima templado promedio 20°C. "
|
|
f"Ubicación: {cls.CAMPUS_ADDRESS}."
|
|
)
|
|
}
|
|
return cls._cached_weather or fallback
|