27 lines
671 B
Python
27 lines
671 B
Python
import requests
|
|
|
|
|
|
class WeatherAPI:
|
|
BASE_URL = "https://api.weatherapi.com/v1"
|
|
|
|
def __init__(self, api_key):
|
|
self._api_key = api_key
|
|
|
|
def _get(self, endpoint, params):
|
|
params["key"] = self._api_key
|
|
return requests.get(f"{self.BASE_URL}{endpoint}", params=params)
|
|
|
|
def get_hourly_conditions(self, query, days=1):
|
|
r = self._get("/forecast.json", {
|
|
"q": query,
|
|
"days": days,
|
|
"aqi": "no",
|
|
"alert": "no",
|
|
})
|
|
|
|
if r.status_code != 200:
|
|
return None
|
|
|
|
data = r.json()
|
|
return sum([obj["hour"] for obj in data["forecast"]["forecastday"]], [])
|