Spaces:
Sleeping
Sleeping
Create src/data_providers.py
Browse files- src/data_providers.py +87 -0
src/data_providers.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import os
|
| 3 |
+
from typing import Dict, Any, Optional, Tuple, List
|
| 4 |
+
import requests
|
| 5 |
+
from dateutil import tz
|
| 6 |
+
from retrying import retry
|
| 7 |
+
|
| 8 |
+
from .utils import get_env
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class GeoCoder:
|
| 12 |
+
"""Simple geocoding via Open-Meteo Nominatim proxy (no key)."""
|
| 13 |
+
@staticmethod
|
| 14 |
+
def geocode(place: str) -> Optional[Tuple[float, float, str]]:
|
| 15 |
+
try:
|
| 16 |
+
url = f"https://geocoding-api.open-meteo.com/v1/search?name={requests.utils.quote(place)}&count=1&language=en&format=json"
|
| 17 |
+
r = requests.get(url, timeout=15)
|
| 18 |
+
r.raise_for_status()
|
| 19 |
+
data = r.json()
|
| 20 |
+
if data.get("results"):
|
| 21 |
+
item = data["results"][0]
|
| 22 |
+
return float(item["latitude"]), float(item["longitude"]), item.get("name") + ", " + item.get("country", "")
|
| 23 |
+
except Exception:
|
| 24 |
+
return None
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class OpenMeteoProvider:
|
| 29 |
+
"""Free fallback provider for forecast and climate normals."""
|
| 30 |
+
|
| 31 |
+
@staticmethod
|
| 32 |
+
def forecast(lat: float, lon: float) -> Dict[str, Any]:
|
| 33 |
+
url = (
|
| 34 |
+
"https://api.open-meteo.com/v1/forecast?"
|
| 35 |
+
f"latitude={lat}&longitude={lon}&hourly=temperature_2m,relative_humidity_2m,precipitation,wind_speed_10m"
|
| 36 |
+
"&daily=temperature_2m_max,temperature_2m_min,precipitation_sum,wind_speed_10m_max&forecast_days=14&timezone=auto"
|
| 37 |
+
)
|
| 38 |
+
r = requests.get(url, timeout=20)
|
| 39 |
+
r.raise_for_status()
|
| 40 |
+
return r.json()
|
| 41 |
+
|
| 42 |
+
@staticmethod
|
| 43 |
+
def climate_normals(lat: float, lon: float) -> Dict[str, Any]:
|
| 44 |
+
# Monthly normals & warming continuation heuristic inputs
|
| 45 |
+
url = (
|
| 46 |
+
"https://climate-api.open-meteo.com/v1/climate?"
|
| 47 |
+
f"latitude={lat}&longitude={lon}&models=ERA5&month=1..12&daily=temperature_2m_mean,precipitation_sum"
|
| 48 |
+
)
|
| 49 |
+
r = requests.get(url, timeout=25)
|
| 50 |
+
r.raise_for_status()
|
| 51 |
+
return r.json()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class WeatherAPIProvider:
|
| 55 |
+
base = "http://api.weatherapi.com/v1"
|
| 56 |
+
|
| 57 |
+
@staticmethod
|
| 58 |
+
def enabled() -> bool:
|
| 59 |
+
return bool(get_env("WEATHERAPI_KEY"))
|
| 60 |
+
|
| 61 |
+
@staticmethod
|
| 62 |
+
def forecast(q: str, days: int = 14) -> Optional[Dict[str, Any]]:
|
| 63 |
+
if not WeatherAPIProvider.enabled():
|
| 64 |
+
return None
|
| 65 |
+
url = f"{WeatherAPIProvider.base}/forecast.json?key={get_env('WEATHERAPI_KEY')}&q={requests.utils.quote(q)}&days={min(days,14)}&aqi=yes&alerts=yes"
|
| 66 |
+
r = requests.get(url, timeout=20)
|
| 67 |
+
if r.status_code != 200:
|
| 68 |
+
return None
|
| 69 |
+
return r.json()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class OpenWeatherProvider:
|
| 73 |
+
base = "https://api.openweathermap.org/data/2.5/onecall"
|
| 74 |
+
|
| 75 |
+
@staticmethod
|
| 76 |
+
def enabled() -> bool:
|
| 77 |
+
return bool(get_env("OPENWEATHER_API_KEY"))
|
| 78 |
+
|
| 79 |
+
@staticmethod
|
| 80 |
+
def onecall(lat: float, lon: float) -> Optional[Dict[str, Any]]:
|
| 81 |
+
if not OpenWeatherProvider.enabled():
|
| 82 |
+
return None
|
| 83 |
+
url = f"{OpenWeatherProvider.base}?lat={lat}&lon={lon}&exclude=minutely&appid={get_env('OPENWEATHER_API_KEY')}&units=metric"
|
| 84 |
+
r = requests.get(url, timeout=20)
|
| 85 |
+
if r.status_code != 200:
|
| 86 |
+
return None
|
| 87 |
+
return r.json()
|