58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
import datetime
|
|
|
|
from aiocache import cached
|
|
|
|
from gallery.sketch.source import ApiSource
|
|
from gallery.sketch.weather.api import WeatherApi
|
|
from gallery.sketch.weather.model import (
|
|
Location,
|
|
WeatherResponse,
|
|
)
|
|
from gallery.sketch.weather.util import merge_weather_values
|
|
from gallery.util import TimeUnit
|
|
|
|
from . import PROVIDER
|
|
from .parser import YandexForecastParser, YandexLocationParser
|
|
|
|
|
|
class YandexWeatherApi(WeatherApi):
|
|
PROVIDER = PROVIDER
|
|
|
|
forecast_parser = YandexForecastParser()
|
|
location_parser = YandexLocationParser()
|
|
|
|
def _build_source(self) -> ApiSource:
|
|
return ApiSource("https://yandex.ru")
|
|
|
|
@cached(
|
|
key_builder=lambda fun, self, location_id: f"api.weather.{self.provider}.source.{location_id}.forecast",
|
|
alias="redis",
|
|
ttl=TimeUnit.HOUR,
|
|
)
|
|
async def _get_location_forecast(self, location_id: str) -> list[WeatherResponse]:
|
|
endpoint = f"pogoda/ru/{location_id}/details"
|
|
content = await self.source.request(endpoint, mode="browser")
|
|
return self.forecast_parser.parse(content)
|
|
|
|
async def find_locations(self, query: str) -> list[Location]:
|
|
endpoint = f"weather/api/suggest?part={query}&lang=ru&type=weather"
|
|
response = await self.source.request(endpoint)
|
|
return self.location_parser.parse(response)
|
|
|
|
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
|
|
forecast = await self._get_location_forecast(location_id)
|
|
for response in forecast:
|
|
if response.date == date:
|
|
return response
|
|
raise ValueError(f"Unsupported date: {date}")
|
|
|
|
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
|
|
forecast = await self._get_location_forecast(location_id)
|
|
values = [merge_weather_values(item.date, item.values) for item in forecast]
|
|
return WeatherResponse(
|
|
location=forecast[0].location,
|
|
date=datetime.date.today(),
|
|
period="days",
|
|
values=values,
|
|
)
|