From 50c4221b8d7918d725a616a31926b1a72f5a5903 Mon Sep 17 00:00:00 2001 From: shmyga Date: Sat, 22 Aug 2026 16:41:12 +0300 Subject: [PATCH] feat(yandexweather): add yandexweather api --- .env-base | 2 +- .pm.toml | 2 + .pylintrc | 2 +- Dockerfile | 2 + gallery.code-workspace | 1 + .../route/view/weather/templates/weather.html | 3 +- gallery/main.py | 2 + gallery/painting/gismeteo/api.py | 16 ++- gallery/painting/gismeteo/parser.py | 18 +-- gallery/painting/matchtv/api.py | 8 +- gallery/painting/openweather/api.py | 12 +- gallery/painting/openweather/openweather.py | 10 +- gallery/painting/yandextv/api.py | 8 +- gallery/painting/yandexweather/__init__.py | 0 gallery/painting/yandexweather/api.py | 75 ++++++++++ gallery/painting/yandexweather/parser.py | 89 ++++++++++++ gallery/sketch/api.py | 17 ++- gallery/sketch/cached.py | 4 + gallery/sketch/schedule/api.py | 3 +- gallery/sketch/source.py | 31 +++- gallery/sketch/weather/api.py | 3 +- gallery/sketch/weather/parser.py | 24 ++++ gallery/sketch/weather/util.py | 2 +- poetry.lock | 135 +++++++++++++++++- pyproject.toml | 2 + scripts/setup | 1 + tests/common/mock.py | 17 ++- tests/data/openweather/__init__.py | 8 +- tests/data/yandexweather/suggest.json | 57 ++++++++ tests/test_gismeteo_api.py | 5 +- tests/test_matchtv_api.py | 5 +- tests/test_openweather_api.py | 11 +- tests/test_yandextv_api.py | 5 +- 33 files changed, 506 insertions(+), 74 deletions(-) create mode 100644 .pm.toml create mode 100644 gallery/painting/yandexweather/__init__.py create mode 100644 gallery/painting/yandexweather/api.py create mode 100644 gallery/painting/yandexweather/parser.py create mode 100644 gallery/sketch/weather/parser.py create mode 100644 tests/data/yandexweather/suggest.json diff --git a/.env-base b/.env-base index 3b5f5ec..a853d71 100644 --- a/.env-base +++ b/.env-base @@ -4,7 +4,7 @@ DOCKER_ROOT="$DOCKER_REPO/$DOCKER_GROUP" VERSION=$(grep -m 1 'version' ./pyproject.toml | grep -oP 'version\s*=\s*"\K[^"]+') PYTHON_VERSION=$(grep -m 1 'python' ./pyproject.toml | grep -oP 'python\s*=\s*"\^?\K[^"]+') DOCKER_PROJECTS=("gallery") -DOCKER_TAG=latest +DOCKER_TAG="${DOCKER_TAG:-latest}" OPENWEATHER_KEY="" GISMETEO_AUTH="" \ No newline at end of file diff --git a/.pm.toml b/.pm.toml new file mode 100644 index 0000000..f323a7e --- /dev/null +++ b/.pm.toml @@ -0,0 +1,2 @@ +[presets.python.tasks.setup.playwright] +commands = [".venv/bin/playwright install chromium --with-deps --only-shell"] \ No newline at end of file diff --git a/.pylintrc b/.pylintrc index 95a4507..df1e499 100644 --- a/.pylintrc +++ b/.pylintrc @@ -9,7 +9,7 @@ disable= duplicate-code, too-few-public-methods, too-many-locals, - no-else-return + no-else-return, [MISCELLANEOUS] notes=FIXME,TODO diff --git a/Dockerfile b/Dockerfile index f8ccdd1..375be50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,4 +32,6 @@ COPY --from=node-builder /app/dist ./static/dist COPY gallery gallery/ COPY --from=builder --parents locales/**/*.mo ./ +RUN .venv/bin/playwright install chromium --with-deps --only-shell + CMD ["uvicorn", "gallery.main:app", "--host", "0.0.0.0", "--port", "80", "--log-config", "gallery/logging.yaml"] diff --git a/gallery.code-workspace b/gallery.code-workspace index 458f5a2..bca4832 100644 --- a/gallery.code-workspace +++ b/gallery.code-workspace @@ -9,6 +9,7 @@ "python.testing.unittestEnabled": false, "python.testing.pytestEnabled": true, "python.terminal.useEnvFile": true, + "python.analysis.importFormat": "relative", "python-envs.pythonProjects": [ { "path": ".", diff --git a/gallery/easel/route/view/weather/templates/weather.html b/gallery/easel/route/view/weather/templates/weather.html index 509e39c..57f9e31 100644 --- a/gallery/easel/route/view/weather/templates/weather.html +++ b/gallery/easel/route/view/weather/templates/weather.html @@ -25,6 +25,7 @@ {% endif %} {% if response.period == 'days' %} + {{provider}} {{response.location}} | {{format_date(response.date, DATE_FORMAT, locale=request.state.language)}} - {{format_date(response.date + datetime.timedelta(days=(response.values | length - 1)), DATE_FORMAT, locale=request.state.language)}} @@ -121,7 +122,7 @@ {{value.wind_speed}} - {% if value.wind_gust != value.wind_speed %} + {% if value.wind_gust != value.wind_speed and value.wind_gust > 0 %} ({{value.wind_gust}}) diff --git a/gallery/main.py b/gallery/main.py index b72e48e..5d62dfb 100644 --- a/gallery/main.py +++ b/gallery/main.py @@ -8,6 +8,7 @@ from gallery.painting.gismeteo.api import GismeteoApi from gallery.painting.matchtv.api import MatchTvApi from gallery.painting.openweather.api import OpenWeatherApi from gallery.painting.yandextv.api import YandexTvApi +from gallery.painting.yandexweather.api import YandexWeatherApi from gallery.sketch.bundle import ApiBundle from gallery.sketch.schedule.cached import CachedScheduleApi from gallery.sketch.weather.cached import CachedWeatherApi @@ -18,6 +19,7 @@ api = ApiBundle( CachedScheduleApi(MatchTvApi()), CachedWeatherApi(GismeteoApi()), CachedWeatherApi(OpenWeatherApi()), + CachedWeatherApi(YandexWeatherApi()), ] ) app = build_app(api) diff --git a/gallery/painting/gismeteo/api.py b/gallery/painting/gismeteo/api.py index 1547a06..5e6c549 100644 --- a/gallery/painting/gismeteo/api.py +++ b/gallery/painting/gismeteo/api.py @@ -20,10 +20,12 @@ logger = logging.getLogger("gismeteo") class GismeteoApi(WeatherApi): PROVIDER = "gismeteo" - SOURCE = ApiSource( - "https://www.gismeteo.ru", - cookies={"xauth": auth} if (auth := environ.get("GISMETEO_AUTH")) else None, - ) + + def _build_source(self) -> ApiSource: + return ApiSource( + "https://www.gismeteo.ru", + cookies={"xauth": auth} if (auth := environ.get("GISMETEO_AUTH")) else None, + ) def _parse_oneday(self, date: datetime.date, data: str) -> WeatherResponse: result: list[dict[str, Any]] = [] @@ -70,7 +72,7 @@ class GismeteoApi(WeatherApi): latitude = 52.968498 longitude = 36.0695 data = json.loads( - await self.SOURCE.request( + await self.source.request( f"mq/city/q/?q={query}&geo={geo}&latitude={latitude}&longitude={longitude}&limit=10" ) ) @@ -103,9 +105,9 @@ class GismeteoApi(WeatherApi): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail={"max_date": max_date.strftime("%Y-%m-%d")} ) - data = await self.SOURCE.request(f"weather-{location_id}/{datehelp.dump(date)}") + data = await self.source.request(f"weather-{location_id}/{datehelp.dump(date)}") return self._parse_oneday(date, data) async def get_days(self, location_id: str, days: int) -> WeatherResponse: - data = await self.SOURCE.request(f"weather-{location_id}/{days}-days") + data = await self.source.request(f"weather-{location_id}/{days}-days") return self._parse_manydays(data) diff --git a/gallery/painting/gismeteo/parser.py b/gallery/painting/gismeteo/parser.py index 44a4a1b..28c31dd 100644 --- a/gallery/painting/gismeteo/parser.py +++ b/gallery/painting/gismeteo/parser.py @@ -11,8 +11,8 @@ from gallery.sketch.weather.model import ( Precipitation, Sky, WindDirection, - WindDirectionDeg, ) +from gallery.sketch.weather.parser import parse_wind_direction from .core import BaseWidgetParser, RowParser @@ -147,24 +147,10 @@ class WindGustParser(RowParser[int]): class WindDirectionParser(RowParser[WindDirection]): KEY = "wind_direction" - WIND_DIRECTION_MAP: dict[str, WindDirection] = { - "—": WindDirection.CALM, - "": WindDirection.CALM, - "штиль": WindDirection.CALM, - "с": WindDirection.N, - "св": WindDirection.NE, - "в": WindDirection.E, - "юв": WindDirection.SE, - "ю": WindDirection.S, - "юз": WindDirection.SW, - "з": WindDirection.W, - "сз": WindDirection.NW, - } - def parse_row(self, tag: Tag) -> Iterable[float]: for item in tag.select(".widget-row-wind > .row-item > .wind-speed > .wind-direction"): wind_direction_str = item.text.lower().strip() - yield WindDirectionDeg.from_direction(self.WIND_DIRECTION_MAP[wind_direction_str]).value + yield parse_wind_direction(wind_direction_str) class PrecipitationParser(RowParser[float]): diff --git a/gallery/painting/matchtv/api.py b/gallery/painting/matchtv/api.py index 0cf5dc7..dff5602 100644 --- a/gallery/painting/matchtv/api.py +++ b/gallery/painting/matchtv/api.py @@ -11,11 +11,13 @@ logger = logging.getLogger("matchtv") class MatchTvApi(ScheduleApi): PROVIDER = "matchtv" - SOURCE = ApiSource("https://matchtv.ru") + + def _build_source(self) -> ApiSource: + return ApiSource("https://matchtv.ru") async def find_channels(self, query: str) -> list[Channel]: endpoint = "api/v1/channels" - data = json.loads(await self.SOURCE.request(endpoint)) + data = json.loads(await self.source.request(endpoint)) result = [] query = query.lower() for item in data["result"]: @@ -27,7 +29,7 @@ class MatchTvApi(ScheduleApi): async def get_schedule(self, channel_id: str, date: datetime.date) -> Schedule: endpoint = f"api/v1/channels/{channel_id}/tv-schedule?date={date:%Y%m%d}" - data = json.loads(await self.SOURCE.request(endpoint)) + data = json.loads(await self.source.request(endpoint)) channel_data = data["result"]["channels"][0] channel = Channel(id=channel_data["alias"], name=channel_data["name"], provider=self.provider) values = [] diff --git a/gallery/painting/openweather/api.py b/gallery/painting/openweather/api.py index 2ba4e98..d7a1274 100644 --- a/gallery/painting/openweather/api.py +++ b/gallery/painting/openweather/api.py @@ -18,9 +18,11 @@ from .parser import FORECAST_ITEM_PARSER logger = logging.getLogger("openweather") -class OpenWeatherApi(WeatherApi): +class OpenWeatherApi(WeatherApi[OpenWeather]): PROVIDER = "openweather" - SOURCE = OpenWeather(environ["OPENWEATHER_KEY"]) + + def _build_source(self) -> OpenWeather: + return OpenWeather(environ["OPENWEATHER_KEY"]) @classmethod def _parse_location(cls, location_id: str) -> tuple[float, float]: @@ -32,7 +34,7 @@ class OpenWeatherApi(WeatherApi): ttl=TimeUnit.DAY, ) async def _get_location(self, location_id: str) -> OpenWeatherLocation: - return await self.SOURCE.get_location(*self._parse_location(location_id)) + return await self.source.get_location(*self._parse_location(location_id)) @cached( key_builder=lambda fun, self, location_id: f"api.weather.{self.provider}.source.{location_id}.forecast", @@ -40,10 +42,10 @@ class OpenWeatherApi(WeatherApi): ttl=TimeUnit.HOUR, ) async def _get_location_forecast(self, location_id: str) -> Forecast: - return await self.SOURCE.get_forecast(*self._parse_location(location_id)) + return await self.source.get_forecast(*self._parse_location(location_id)) async def find_locations(self, query: str) -> list[Location]: - result = await self.SOURCE.find_locations(query) + result = await self.source.find_locations(query) return [ Location( id=f"{item.lat}:{item.lon}", diff --git a/gallery/painting/openweather/openweather.py b/gallery/painting/openweather/openweather.py index 00fd68a..4566248 100644 --- a/gallery/painting/openweather/openweather.py +++ b/gallery/painting/openweather/openweather.py @@ -76,28 +76,28 @@ class Location(Model): local_names: dict[str, str] | None = None -class OpenWeather: +class OpenWeather(ApiSource): BASE_URL = "https://api.openweathermap.org" def __init__(self, api_key: str): self._api_key = api_key - self._source = ApiSource(self.BASE_URL) + super().__init__(self.BASE_URL) async def get_forecast(self, lat: float, lon: float) -> Forecast: endpoint = f"data/2.5/forecast?lat={lat}&lon={lon}&appid={self._api_key}&units=metric" - response = await self._source.request(endpoint) + response = await self.request(endpoint) response_data = json.loads(response) return Forecast.model_validate(response_data) async def find_locations(self, query: str, limit: int = 5) -> list[Location]: endpoint = f"geo/1.0/direct?q={query}&limit={limit}&appid={self._api_key}" - response = await self._source.request(endpoint) + response = await self.request(endpoint) response_data = json.loads(response) return [Location.model_validate(item) for item in response_data] async def get_location(self, lat: float, lon: float) -> Location: limit = 1 endpoint = f"geo/1.0/reverse?lat={lat}&lon={lon}&limit={limit}&appid={self._api_key}" - response = await self._source.request(endpoint) + response = await self.request(endpoint) response_data = json.loads(response) return Location.model_validate(response_data[0]) diff --git a/gallery/painting/yandextv/api.py b/gallery/painting/yandextv/api.py index 44a68cd..a1f7ae0 100644 --- a/gallery/painting/yandextv/api.py +++ b/gallery/painting/yandextv/api.py @@ -43,14 +43,16 @@ HEADERS: dict[str, str] = { class YandexTvApi(ScheduleApi): PROVIDER = "yandextv" - SOURCE = ApiSource("https://tv.yandex.ru", headers=HEADERS) + + def _build_source(self) -> ApiSource: + return ApiSource("https://tv.yandex.ru", headers=HEADERS) async def find_channels(self, query: str) -> list[Channel]: url = ( "https://suggest-multi.yandex.ru/suggest-tv2?" f"v=4&uil=ru&lr=10&count_channels=4&count_programs=4&sn=50&part={query}" ) - _, values = json.loads(await self.SOURCE.request(url)) + _, values = json.loads(await self.source.request(url)) result = [] for _, name, content in values: if content["label"] == "Каналы": @@ -60,7 +62,7 @@ class YandexTvApi(ScheduleApi): async def get_schedule(self, channel_id: str, date: datetime.date) -> Schedule: endpoint = f"channels/{channel_id}?date={date:%Y-%m-%d}" - data = await self.SOURCE.request(endpoint) + data = await self.source.request(endpoint) soup = BeautifulSoup(data, features="html.parser") if soup.select_one(".CheckboxCaptcha") is not None: raise RuntimeError("Captcha") diff --git a/gallery/painting/yandexweather/__init__.py b/gallery/painting/yandexweather/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gallery/painting/yandexweather/api.py b/gallery/painting/yandexweather/api.py new file mode 100644 index 0000000..c9e36af --- /dev/null +++ b/gallery/painting/yandexweather/api.py @@ -0,0 +1,75 @@ +import datetime +import json +import logging + +from aiocache import cached +from bs4 import BeautifulSoup + +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 .parser import parse_article + +logger = logging.getLogger("yandexweather") + + +class YandexWeatherApi(WeatherApi): + PROVIDER = "yandex" + + 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") + soup = BeautifulSoup(content, features="html.parser") + location = soup.select_one("[class^=Suggest_placeholder]").text.strip() + articles = soup.select("article[class^=AppForecastDay]") + return [parse_article(article, location) for article in articles] + + 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) + data = json.loads(response) + return [ + Location( + id=str(item["geoId"]), + name=item["title"], + provider=self.provider, + lat=item["coords"]["lat"], + lon=item["coords"]["lon"], + country="ru", + country_code="ru", + district=item["description"], + subdistrict="", + ) + for item in data + ] + + 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, + ) diff --git a/gallery/painting/yandexweather/parser.py b/gallery/painting/yandexweather/parser.py new file mode 100644 index 0000000..27f0720 --- /dev/null +++ b/gallery/painting/yandexweather/parser.py @@ -0,0 +1,89 @@ +import logging +from enum import Enum + +import dateparser +from bs4 import Tag + +from gallery.sketch.weather.model import Cloudness, Precipitation, Sky, WeatherResponse +from gallery.sketch.weather.parser import ParseError, parse_wind_direction +from gallery.sketch.weather.util import build_weather_value + +logger = logging.getLogger("yandexweather") + + +class DayPart(Enum): + MORNING = ("Утром", 6) + AFTERNOON = ("Днём", 12) + EVENING = ("Вечером", 18) + NIGHT = ("Ночью", 0) + + def __init__(self, title: str, hour: int): + super().__init__() + self.title = title + self.hour = hour + + +def parse_sky(sky: str) -> Sky: + cloudness = Cloudness.CLEAR + precipitation = Precipitation.NO + thunder = False + fog = False + if sky == "ясно": + pass + elif sky == "облачно с прояснениями": + cloudness = Cloudness.PARTLY_CLOUDY + elif sky == "малооблачно": + cloudness = Cloudness.CLOUDY + elif sky == "пасмурно": + cloudness = Cloudness.MAINLY_CLOUDY + elif sky == "небольшой дождь": + precipitation = Precipitation.RAIN + elif sky == "дождь": + precipitation = Precipitation.SMALL_RAIN + else: + logger.warning("unknown sky value: %s:", sky) + return Sky( + cloudness=cloudness, + precipitation=precipitation, + thunder=thunder, + fog=fog, + ) + + +def parse_article(article: Tag, location: str) -> WeatherResponse: + date_str = article.select_one("[class^=AppForecastDayHeader_dayTitle]").text.split(",")[-1].strip() + date = dateparser.parse(date_str, languages=["ru"]) + if date is None: + raise ParseError("Invalid date string", date_str) + values = [] + for day_part in DayPart: + value = build_weather_value(date.replace(hour=day_part.hour)) + day_part_tag = article.find(string=day_part.title) + temperature_tag = day_part_tag.next_element + temperature = int(temperature_tag.text.strip().rstrip("°")) + value.temperature.append(temperature) + sky_tag = temperature_tag.next_element.next_element.next_element + sky_str = sky_tag.text.strip() + value.sky = parse_sky(sky_str) + temperature_tag = sky_tag.next_element.next_element + temperature = int(temperature_tag.text.strip().rstrip("°")) + value.temperature.append(temperature) + wind_speed_tag = temperature_tag.next_element.next_element + wind_speed = int(wind_speed_tag.text.strip()) + value.wind_speed = wind_speed + wind_direction_tag = wind_speed_tag.next_element.next_element.next_element.next_element + wind_direction_str = wind_direction_tag.text.strip() + value.wind_direction = parse_wind_direction(wind_direction_str) + humidity_tag = wind_direction_tag.next_element.next_element.next_element.next_element + humidity = int(humidity_tag.text.strip().rstrip("%")) + value.humidity = humidity + pressure_tag = humidity_tag.next_element.next_element + pressure = int(pressure_tag.text.strip()) + value.pressure.append(pressure) + values.append(value) + return WeatherResponse( + location=location, + date=date, + period="day", + values=values, + ) diff --git a/gallery/sketch/api.py b/gallery/sketch/api.py index 4e4ecf9..6ae7857 100644 --- a/gallery/sketch/api.py +++ b/gallery/sketch/api.py @@ -1,10 +1,19 @@ from typing import TypeVar +from .source import ApiSource -class Api: +S = TypeVar("S", bound=ApiSource) + + +class Api[S = ApiSource]: TYPE: str PROVIDER: str + _source: S | None = None + + def _build_source(self) -> S: + raise NotImplementedError + @property def type(self) -> str: return self.TYPE @@ -13,5 +22,11 @@ class Api: def provider(self) -> str: return self.PROVIDER + @property + def source(self) -> S: + if self._source is None: + self._source = self._build_source() + return self._source + API = TypeVar("API", bound=Api) diff --git a/gallery/sketch/cached.py b/gallery/sketch/cached.py index 52f7aa4..d0a6691 100644 --- a/gallery/sketch/cached.py +++ b/gallery/sketch/cached.py @@ -1,5 +1,6 @@ from typing import Generic, NamedTuple +from gallery.sketch.source import ApiSource from gallery.util import TimeUnit from .api import API, Api @@ -19,6 +20,9 @@ class CachedApi(Api, Generic[API]): def __init__(self, api: API): self._api = api + def _build_source(self) -> ApiSource: + raise RuntimeError("Source not used in CachedApi") + @property def provider(self) -> str: return self._api.provider diff --git a/gallery/sketch/schedule/api.py b/gallery/sketch/schedule/api.py index b8d55d2..01fa6b5 100644 --- a/gallery/sketch/schedule/api.py +++ b/gallery/sketch/schedule/api.py @@ -1,10 +1,11 @@ import datetime from ..api import Api +from ..source import ApiSource from .model import Channel, Schedule -class ScheduleApi(Api): +class ScheduleApi[S = ApiSource](Api[S]): TYPE = "schedule" async def find_channels(self, query: str) -> list[Channel]: diff --git a/gallery/sketch/source.py b/gallery/sketch/source.py index 1b716c1..cf6b87b 100644 --- a/gallery/sketch/source.py +++ b/gallery/sketch/source.py @@ -1,9 +1,15 @@ import logging +from typing import Literal import aiohttp +from playwright.async_api import async_playwright logger = logging.getLogger("source") +RequestMode = Literal["base", "browser"] + +DEFAULT_REQUEST_MODE: RequestMode = "base" + class ApiSource: DEFAULT_USER_AGENT = ( @@ -25,12 +31,7 @@ class ApiSource: self._cookies = cookies self._headers = headers - async def request(self, endpoint: str) -> str: - if endpoint.startswith("https:"): - url = endpoint - else: - url = f"{self._base_url}/{endpoint}" - logger.info(url) + async def _base_request(self, url: str) -> str: headers = {"User-Agent": self._user_agent, **(self._headers or {})} async with aiohttp.ClientSession( headers=headers, @@ -39,3 +40,21 @@ class ApiSource: ) as session: async with session.request("GET", url, timeout=self._timeout) as response: return await response.text() + + async def _browser_request(self, url: str) -> str: + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page() + await page.goto(url, wait_until="networkidle") + content = await page.content() + await browser.close() + return content + + async def request(self, endpoint: str, /, mode: RequestMode = DEFAULT_REQUEST_MODE) -> str: + if endpoint.startswith("https:"): + url = endpoint + else: + url = f"{self._base_url}/{endpoint}" + logger.info(url) + method = getattr(self, f"_{mode}_request") + return await method(url) diff --git a/gallery/sketch/weather/api.py b/gallery/sketch/weather/api.py index 0196dd8..26cfc59 100644 --- a/gallery/sketch/weather/api.py +++ b/gallery/sketch/weather/api.py @@ -1,10 +1,11 @@ import datetime from ..api import Api +from ..source import ApiSource from .model import Location, WeatherResponse -class WeatherApi(Api): +class WeatherApi[S = ApiSource](Api[S]): TYPE = "weather" async def find_locations(self, query: str) -> list[Location]: diff --git a/gallery/sketch/weather/parser.py b/gallery/sketch/weather/parser.py new file mode 100644 index 0000000..3b601df --- /dev/null +++ b/gallery/sketch/weather/parser.py @@ -0,0 +1,24 @@ +from gallery.sketch.weather.model import WindDirection, WindDirectionDeg + + +class ParseError(Exception): + pass + + +WIND_DIRECTION_MAP: dict[str, WindDirection] = { + "—": WindDirection.CALM, + "": WindDirection.CALM, + "штиль": WindDirection.CALM, + "с": WindDirection.N, + "св": WindDirection.NE, + "в": WindDirection.E, + "юв": WindDirection.SE, + "ю": WindDirection.S, + "юз": WindDirection.SW, + "з": WindDirection.W, + "сз": WindDirection.NW, +} + + +def parse_wind_direction(value: str) -> WindDirectionDeg: + return WindDirectionDeg.from_direction(WIND_DIRECTION_MAP[value.lower()]) diff --git a/gallery/sketch/weather/util.py b/gallery/sketch/weather/util.py index 6c4f7c9..43afbc3 100644 --- a/gallery/sketch/weather/util.py +++ b/gallery/sketch/weather/util.py @@ -19,7 +19,7 @@ def build_weather_value(date: datetime.datetime) -> WeatherValue: wind_direction=WindDirectionDeg(-1), precipitation=0, pressure=[], - humidity=0, + humidity=-1, ) diff --git a/poetry.lock b/poetry.lock index 7e0a9fb..88ce30e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -853,6 +853,99 @@ files = [ {file = "frozenlist-1.8.0.tar.gz", hash = "sha256:3ede829ed8d842f6cd48fc7081d7a41001a56f1f38603f9d49bf3020d59a31ad"}, ] +[[package]] +name = "greenlet" +version = "3.5.5" +description = "Lightweight in-process concurrent programming" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "greenlet-3.5.5-cp310-cp310-macosx_11_0_universal2.whl", hash = "sha256:816230f469381ad0a43abc9fa8dda5a699e32fb78958dde32ded93213b70a667"}, + {file = "greenlet-3.5.5-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:a5433cf291e0ef9114bd14d0d824db6e5e4a43033234bca48181a9597acca07b"}, + {file = "greenlet-3.5.5-cp310-cp310-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:19d59f068887d8c5907fc177f27683413ace3011b6ed646c0b309266e74a6502"}, + {file = "greenlet-3.5.5-cp310-cp310-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:86c5113d698cb8d927b2750bb1f1d59eefe3a37e0e0217491aee29a7f84ef52c"}, + {file = "greenlet-3.5.5-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9ff00e12102358292087274dfb1669132387ff6e7920ebf9d85f4826ce0d3a56"}, + {file = "greenlet-3.5.5-cp310-cp310-manylinux_2_39_riscv64.whl", hash = "sha256:c69bed34470abfcd456984fdadaa18e62169af4480335c45f3c32d1d9c12e638"}, + {file = "greenlet-3.5.5-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:523bb8e27614d77101ea7a8cf59f8d91219b72d5c29f6a038c92b50828bfa8d0"}, + {file = "greenlet-3.5.5-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:f1e2db190db51c17433eee424803818cf0670bf049d9cfe0dd07be111d1aa7c4"}, + {file = "greenlet-3.5.5-cp310-cp310-win_amd64.whl", hash = "sha256:740e544169527b82695ce76af2f7ad6f030904658f2f3921a1d245771fb88cfc"}, + {file = "greenlet-3.5.5-cp311-cp311-macosx_11_0_universal2.whl", hash = "sha256:be63afcbbccfad3dd95a1ba12ada84dab2ef32031973d80b5b92df67fa763a61"}, + {file = "greenlet-3.5.5-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:8a268024ce2d7d2b04694bf1594058981a9fa663d1df4b762dee499211ed7c1c"}, + {file = "greenlet-3.5.5-cp311-cp311-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:35cbb8bf55ace57fbccb4fb8622c4521713acd8691e77f4696d416ea7ca527da"}, + {file = "greenlet-3.5.5-cp311-cp311-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:abc8bc8d9f935cd685457545b6a53863a877fdc12c2c0f5ee9beee18d9db139c"}, + {file = "greenlet-3.5.5-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:74cc6df89ec5302337adc9cf096221cbed2510fd444b0e0f1586cf0470740864"}, + {file = "greenlet-3.5.5-cp311-cp311-manylinux_2_39_riscv64.whl", hash = "sha256:3134291427bb0f3526e9d90311988caf336eb43730e95244997a4fb15f45144f"}, + {file = "greenlet-3.5.5-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:6d9b454c5fc48aeaa7c4337813dbf513a6870468e426438a04d922c6d0fe63db"}, + {file = "greenlet-3.5.5-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:03551ed792cb1b4fc0277a0c60dfd8c343894a0ba06fe60dcd22f568b433da39"}, + {file = "greenlet-3.5.5-cp311-cp311-win_amd64.whl", hash = "sha256:ab3df3dffb58bf70564e93a5cec7941e4d9faa5a36cc4234a10d3131afe04f53"}, + {file = "greenlet-3.5.5-cp311-cp311-win_arm64.whl", hash = "sha256:2b70a766135540c472ac1393d57c2e1b4a2eb85bf526a1e41e6d096173a8cee5"}, + {file = "greenlet-3.5.5-cp312-cp312-macosx_11_0_universal2.whl", hash = "sha256:49520f0c95a48b42cf55414b8e8479beb274ea70431afc33e3f79903c71f4380"}, + {file = "greenlet-3.5.5-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:55272212cbc5f43d1d723725ab931f1939969b7e9523882ca58b55061769d053"}, + {file = "greenlet-3.5.5-cp312-cp312-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:655bca754a2ef4efcb0eb48a94d3f4593536d0f3d48f8ed44343c01d16a92f95"}, + {file = "greenlet-3.5.5-cp312-cp312-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:6ca5d6ae0739e5764f2cfcfaa562ac5a990cbdaedca93251c5e3cf07c362371f"}, + {file = "greenlet-3.5.5-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:147b25a42e5ca5be3d42356e8f608b37af715a1c196e9bf9d1627f3341adfe1d"}, + {file = "greenlet-3.5.5-cp312-cp312-manylinux_2_39_riscv64.whl", hash = "sha256:1b5ed9162c0c098e0bbc2cf88a94f433c1b8926f831745252e099e5d83e17759"}, + {file = "greenlet-3.5.5-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:27493374cff1d1b7919dc8126547f2aea582737e3046147b434b1e12de56389b"}, + {file = "greenlet-3.5.5-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:12e2ee66c2aba86133f10fd99d6a8856c6d351ffb7be0e4d52ef2cc5fbb705b2"}, + {file = "greenlet-3.5.5-cp312-cp312-win_amd64.whl", hash = "sha256:49ddacd36af37735fab103846f4ee4d18a492dde72730d1699c0c8ebe30d9f18"}, + {file = "greenlet-3.5.5-cp312-cp312-win_arm64.whl", hash = "sha256:5f1b1ff4828cdc1aba4266aff814085d04a1d07959287219af021b838b265d52"}, + {file = "greenlet-3.5.5-cp313-cp313-macosx_11_0_universal2.whl", hash = "sha256:b79fd2a5bc099b5e744f34c4c9a58954a5f4cb7529fb4b6e8446057d61b6edaa"}, + {file = "greenlet-3.5.5-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:634cf15a233a949136879dd388e25d3296e16f3f1e217d2456797b8579ebc6ed"}, + {file = "greenlet-3.5.5-cp313-cp313-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:499adea519f748407fc6806d20eedabac2884fd73b9f38d81236e190ba20dfef"}, + {file = "greenlet-3.5.5-cp313-cp313-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:f7278591501941bb2456af102bb9cd59aab48c6cfd6e2dd68fa1290bb0c49a42"}, + {file = "greenlet-3.5.5-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:2eabb980975cba5b93a95f6f69287d05fc05ac955bfd6a320a7c083eeb52c0b0"}, + {file = "greenlet-3.5.5-cp313-cp313-manylinux_2_39_riscv64.whl", hash = "sha256:4dfc7c4470354e7b09184d1a3a985761053a2fd694ddb5b5c80242afc2c8c90b"}, + {file = "greenlet-3.5.5-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:03115c2e0a371999bf8ae616aa8d653f96641d4705c457aebaa187276e9f7537"}, + {file = "greenlet-3.5.5-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:4441153ffba21b90d3ca89fe3d31f5c093ae6c0bf0cfdfc98f54cde22f95b62e"}, + {file = "greenlet-3.5.5-cp313-cp313-win_amd64.whl", hash = "sha256:95c5b1f4b3a193f8a0c2de4bfdcb48d119f7f1063941f1de1f2168051b3e52dd"}, + {file = "greenlet-3.5.5-cp313-cp313-win_arm64.whl", hash = "sha256:1af90aa4bc129883b340cdd6957a3bc74f60528a4993bbd1f53aaebe1d9981cc"}, + {file = "greenlet-3.5.5-cp314-cp314-macosx_11_0_universal2.whl", hash = "sha256:d4a389a852e392a6366058651a20fa5ba40d979865aa81bea2ccbdc44805070d"}, + {file = "greenlet-3.5.5-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:70b157cd319873e8b544ddc2de158f55bbd0a9b0218c8ce9332039801518e328"}, + {file = "greenlet-3.5.5-cp314-cp314-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:8bdfd1424abcf26832961e766570cae79efdb9599d709088c9cb6ef82b194926"}, + {file = "greenlet-3.5.5-cp314-cp314-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:d98ef6f92e67c6dbf299dbfd8facc1b0d2d9cedf91e325e73b3d0373fe4309d8"}, + {file = "greenlet-3.5.5-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:182de51c6b572a705f2fafaab2e783bcf7d2760940229dfe73086cbae037af3e"}, + {file = "greenlet-3.5.5-cp314-cp314-manylinux_2_39_riscv64.whl", hash = "sha256:159df1942d88e8f784cbb38d6f18bdb365cd11319cfbb3e89623de2b97892d53"}, + {file = "greenlet-3.5.5-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:8fec3f165dfe332e490c3247c0f6c23b0bfc45f06496ad7f00ddb00e3d35e4dc"}, + {file = "greenlet-3.5.5-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:c6ce25fee6cabc8bf22cb8b52e642cbb821be5b9aec8094d07ff03378141b8e9"}, + {file = "greenlet-3.5.5-cp314-cp314-win_amd64.whl", hash = "sha256:7dffc5c859fe6059974df1e37d7923d654a83e2ae18fdd616994270e001115e1"}, + {file = "greenlet-3.5.5-cp314-cp314-win_arm64.whl", hash = "sha256:5e2afcfc4d4305dd715809b03da5cbe437c8984f61d8917751eb5fe4aefa3e07"}, + {file = "greenlet-3.5.5-cp314-cp314t-macosx_11_0_universal2.whl", hash = "sha256:0e5a7de979d764aea1f5b6e95cf92b5b37741b9823702041f34b126e7f690277"}, + {file = "greenlet-3.5.5-cp314-cp314t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:fef01bd457f11fc158b130ca0027a3c365693280e8e231b65bdaf57999f39f5b"}, + {file = "greenlet-3.5.5-cp314-cp314t-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:5173a72310725a74afc82c164f0e52cb8ad0de62f2bb623f24f6c0cc07d80272"}, + {file = "greenlet-3.5.5-cp314-cp314t-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:5e9ec2e7c98e895fcea0c5cc57b2606cf86ece6d0a56578f3eb225e2af4f0387"}, + {file = "greenlet-3.5.5-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:44f08341873200ba8a60a8bc14ace3d91f1754f7fa7bc66157714a8cd420a476"}, + {file = "greenlet-3.5.5-cp314-cp314t-manylinux_2_39_riscv64.whl", hash = "sha256:102817506f6090b5176c746a82603341a549b40e5c3d5b72a4c672228a918c41"}, + {file = "greenlet-3.5.5-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:d246c0db9a2513cd45f019ba178ea4d4d4705bd210ee465e2c15d76a1ab13874"}, + {file = "greenlet-3.5.5-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:72507285b5caa1d17904a3f7c322ca780823a54170a0e04ec3f37bcc60d4db71"}, + {file = "greenlet-3.5.5-cp314-cp314t-win_amd64.whl", hash = "sha256:7805655781fb8f28a55d05fe57ed61f5f10f1892fb587673e3bb5264f28041f0"}, + {file = "greenlet-3.5.5-cp315-cp315-macosx_11_0_universal2.whl", hash = "sha256:c0db80fcd5b8aece93f66c64f78a786bbb6b96c5fe63ef5a5a4581ecf8bab206"}, + {file = "greenlet-3.5.5-cp315-cp315-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6b241c32f912ada659808d68e308c568baf577eebf757d15471472de0c18cfad"}, + {file = "greenlet-3.5.5-cp315-cp315-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:ef6a08349401d8eaf3cb12688ac8557de95788556b8631ef17555a4a173022c0"}, + {file = "greenlet-3.5.5-cp315-cp315-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:37faa97daccb6d9f4c2141ce3118d023c3c5506864a7d8bdf726f665018c1f76"}, + {file = "greenlet-3.5.5-cp315-cp315-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:f2e3d061b8e13aec2f0441689b3c71b244a20e5d274a52cb0f7e31bd1d139552"}, + {file = "greenlet-3.5.5-cp315-cp315-manylinux_2_39_riscv64.whl", hash = "sha256:b18007dc2473a7942fd157366b55f01da6fed7ce85318591005b419e0a439474"}, + {file = "greenlet-3.5.5-cp315-cp315-musllinux_1_2_aarch64.whl", hash = "sha256:9ab5f5b93655e77fe0d6c2dfd22b5eac751bb1f876d8ec21761b7c1fb9266007"}, + {file = "greenlet-3.5.5-cp315-cp315-musllinux_1_2_x86_64.whl", hash = "sha256:f0e5a21bd4452a88cf032fc43c4a5b307ab1380eacb63b5988f9c0317885e773"}, + {file = "greenlet-3.5.5-cp315-cp315-win_amd64.whl", hash = "sha256:469dbb0a78625642f4a626cfd0c6e8bccc0385b5e49189b6308bbe849ec88a8e"}, + {file = "greenlet-3.5.5-cp315-cp315-win_arm64.whl", hash = "sha256:2d57406c3efd32d7a81e17a674314e8bd00792cdab49ea3228a49aa1bfb2e769"}, + {file = "greenlet-3.5.5-cp315-cp315t-macosx_11_0_universal2.whl", hash = "sha256:68184dfcf50ccaa8e864770fe0633a7e27250ea9329f8192ef47ee9ecfd78e1c"}, + {file = "greenlet-3.5.5-cp315-cp315t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:9ec0dc0e59dc9c61af5c47348365ccbbd7addfafe0a93b00336ff3da2907bdc6"}, + {file = "greenlet-3.5.5-cp315-cp315t-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:e604f58e35833fc46ef20302bcb314dddbfd3fcf33a4f936216d51dd678d63ae"}, + {file = "greenlet-3.5.5-cp315-cp315t-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:2888a3a38bc5ee5bb6c438372197152e815837e4fab7ed7a1f86ef18ffd58ad1"}, + {file = "greenlet-3.5.5-cp315-cp315t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:40239b5384f96da3963585cc6d7eaa9b56f8ae67e8d92cc82dd9e202fc847de3"}, + {file = "greenlet-3.5.5-cp315-cp315t-manylinux_2_39_riscv64.whl", hash = "sha256:1e8d9391fe77f15649589a907cef972dbbd6352ef7ff7dc0492f658c0c26495f"}, + {file = "greenlet-3.5.5-cp315-cp315t-musllinux_1_2_aarch64.whl", hash = "sha256:a1eaccf5c3a1d3e46dead602c72e6836731e8e245c9de6a27764567b6b62d4c0"}, + {file = "greenlet-3.5.5-cp315-cp315t-musllinux_1_2_x86_64.whl", hash = "sha256:19e4e026fe20691f333b8eb1a3bc9625eceba8c3f9d62ec5a6f8581afbc6b5a5"}, + {file = "greenlet-3.5.5-cp315-cp315t-win_amd64.whl", hash = "sha256:712aee154f648bde84634654bb38bb78c69ac640c37a45c9effed800735049d8"}, + {file = "greenlet-3.5.5-cp315-cp315t-win_arm64.whl", hash = "sha256:7f049911ee81a16a03c33d5450d8d5867d27f596ca5fb201b86f4524e874468b"}, + {file = "greenlet-3.5.5.tar.gz", hash = "sha256:adb4bae02e91a8e863e48b177e4014bdcac8a6b5e047ea1df687a61534b85e6c"}, +] + +[package.extras] +docs = ["Sphinx", "furo"] +test = ["objgraph", "psutil", "setuptools"] + [[package]] name = "h11" version = "0.16.0" @@ -1396,6 +1489,28 @@ files = [ {file = "platformdirs-4.10.0.tar.gz", hash = "sha256:31e761a6a0ca04faf7353ea759bdba55652be214725111e5aac52dfa29d4bef7"}, ] +[[package]] +name = "playwright" +version = "1.62.0" +description = "A high-level API to automate web browsers" +optional = false +python-versions = ">=3.10" +groups = ["main"] +files = [ + {file = "playwright-1.62.0-py3-none-macosx_10_13_x86_64.whl", hash = "sha256:d8da938f3748841a8754f2e1f0216902c1c8f8ae3720de8b32ccf8e6913a7c4f"}, + {file = "playwright-1.62.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:db755ab27db21a04186f1fe8169888e42356086e439b1059b923ef417f0b6034"}, + {file = "playwright-1.62.0-py3-none-macosx_11_0_universal2.whl", hash = "sha256:5108bd5b3e87169ddf269feee097da5893af7f8aea4634dfc840518d64c1f1da"}, + {file = "playwright-1.62.0-py3-none-manylinux1_x86_64.whl", hash = "sha256:ba33bae6a13b3d9d354c751cb618af357d20fe1d57767cbcce52079bbef17ad3"}, + {file = "playwright-1.62.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:db2d76613a57ad844362ce42f7d0c2fa26b19a4f7a46d4f76b891c631e6e5aff"}, + {file = "playwright-1.62.0-py3-none-win32.whl", hash = "sha256:e5614fa89355d7081457680324bb219f79f69c423c5cb6fa250e30b0d8aebf1c"}, + {file = "playwright-1.62.0-py3-none-win_amd64.whl", hash = "sha256:92c0d98ed04eb35af557b709875edba415b1f548bdb22ddb5bb3e1e6c835c2f1"}, + {file = "playwright-1.62.0-py3-none-win_arm64.whl", hash = "sha256:ea8d3055aa9d5a9f1832ac82517bd8b42c78fac7ebcbebb0107116735c8cb6a1"}, +] + +[package.dependencies] +greenlet = ">=3.1.1,<4.0.0" +pyee = ">=13,<14" + [[package]] name = "pluggy" version = "1.6.0" @@ -1751,6 +1866,24 @@ gcp-secret-manager = ["google-cloud-secret-manager (>=2.23.1)"] toml = ["tomli (>=2.0.1)"] yaml = ["pyyaml (>=6.0.1)"] +[[package]] +name = "pyee" +version = "13.0.1" +description = "A rough port of Node.js's EventEmitter to Python with a few tricks of its own" +optional = false +python-versions = ">=3.8" +groups = ["main"] +files = [ + {file = "pyee-13.0.1-py3-none-any.whl", hash = "sha256:af2f8fede4171ef667dfded53f96e2ed0d6e6bd7ee3bb46437f77e3b57689228"}, + {file = "pyee-13.0.1.tar.gz", hash = "sha256:0b931f7c14535667ed4c7e0d531716368715e860b988770fc7eb8578d1f67fc8"}, +] + +[package.dependencies] +typing-extensions = "*" + +[package.extras] +dev = ["black", "build", "flake8", "flake8-black", "isort", "jupyter-console", "mkdocs", "mkdocs-include-markdown-plugin", "mkdocstrings[python]", "mypy", "pytest", "pytest-asyncio ; python_version >= \"3.4\"", "pytest-trio ; python_version >= \"3.7\"", "sphinx", "toml", "tox", "trio", "trio ; python_version > \"3.6\"", "trio-typing ; python_version > \"3.6\"", "twine", "twisted", "validate-pyproject[all]"] + [[package]] name = "pygments" version = "2.20.0" @@ -3015,4 +3148,4 @@ app = ["babel", "fastapi", "jinja2"] [metadata] lock-version = "2.1" python-versions = ">=3.14,<4.0" -content-hash = "7f8a3d007747f68e9735e957a0c06459044c8006b89cf3f5d37512f36638abd5" +content-hash = "b0c7f9c6c9fbb20ada45e7d27601920b0ba42716a1a70b4e317e7295de230376" diff --git a/pyproject.toml b/pyproject.toml index 3e6dfd9..bf4abeb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ dependencies = [ 'dateparser (>=1.4,<2.0)', 'pydantic (>=2.13,<3.0)', 'aiocache[redis] (>=0.12,<0.13)', + "playwright (>=1.62.0,<2.0.0)", ] [project.optional-dependencies] @@ -37,6 +38,7 @@ build-backend = "poetry.core.masonry.api" [tool.black] line-length = 120 +target-version = ["py314"] [tool.isort] profile = "black" diff --git a/scripts/setup b/scripts/setup index c76ec3d..8affb13 100755 --- a/scripts/setup +++ b/scripts/setup @@ -11,6 +11,7 @@ source .env # python poetry env use ${PYTHON_VERSION} poetry install +.venv/bin/playwright install chromium --with-deps --only-shell # static cd static || exit diff --git a/tests/common/mock.py b/tests/common/mock.py index db96716..a3b0c14 100644 --- a/tests/common/mock.py +++ b/tests/common/mock.py @@ -1,6 +1,8 @@ from pathlib import Path +from typing import TypeVar -from gallery.sketch.source import ApiSource +from gallery.sketch.api import Api, S +from gallery.sketch.source import DEFAULT_REQUEST_MODE, ApiSource, RequestMode class MockSource(ApiSource): @@ -10,8 +12,19 @@ class MockSource(ApiSource): self._path = path self._mapping = mapping - async def request(self, endpoint: str) -> str: + async def request(self, endpoint: str, /, mode: RequestMode = DEFAULT_REQUEST_MODE) -> str: for pattern, filename in self._mapping.items(): if pattern in endpoint: return (self._path / filename).read_text() raise ValueError(endpoint) + + +API = Api[S] +A = TypeVar("A", bound=API) + + +def create_mock_api(api_type: type[A], mock_source: MockSource) -> A: + api = api_type() + # pylint: disable=protected-access + api._build_source = lambda: mock_source + return api diff --git a/tests/data/openweather/__init__.py b/tests/data/openweather/__init__.py index 49b1ade..f045bf6 100644 --- a/tests/data/openweather/__init__.py +++ b/tests/data/openweather/__init__.py @@ -1,8 +1,14 @@ from pathlib import Path +from gallery.painting.openweather.openweather import OpenWeather from tests.common.mock import MockSource -OPENWEATHER_MOCK_SOURCE = MockSource( + +class OpenWeatherMockSource(MockSource, OpenWeather): + pass + + +OPENWEATHER_MOCK_SOURCE = OpenWeatherMockSource( Path(__file__).parent, { "forecast": "forecast.json", diff --git a/tests/data/yandexweather/suggest.json b/tests/data/yandexweather/suggest.json new file mode 100644 index 0000000..f340678 --- /dev/null +++ b/tests/data/yandexweather/suggest.json @@ -0,0 +1,57 @@ +[ + { + "title": "Орёл", + "description": "Орёл", + "coords": { "lat": 52.97075653, "lon": 36.06435776 }, + "uri": "//yandex.ru/pogoda?name=%D0%9E%D1%80%D1%91%D0%BB&kind=locality", + "geoId": 10, + "slug": "10", + "kind": "locality", + "distance": { "value": null, "text": "" }, + "url": "//yandex.ru/pogoda?name=%D0%9E%D1%80%D1%91%D0%BB&kind=locality" + }, + { + "title": "село Орёл-Изумруд", + "description": "Краснодарский край", + "coords": { "lat": 43.46733475, "lon": 39.92550659 }, + "uri": "//yandex.ru/pogoda?name=%D0%9A%D1%80%D0%B0%D1%81%D0%BD%D0%BE%D0%B4%D0%B0%D1%80%D1%81%D0%BA%D0%B8%D0%B9+%D0%BA%D1%80%D0%B0%D0%B9%2C+%D0%B3%D0%BE%D1%80%D0%BE%D0%B4%D1%81%D0%BA%D0%BE%D0%B9+%D0%BE%D0%BA%D1%80%D1%83%D0%B3+%D0%A1%D0%BE%D1%87%D0%B8%2C+%D1%81%D0%B5%D0%BB%D0%BE+%D0%9E%D1%80%D1%91%D0%BB-%D0%98%D0%B7%D1%83%D0%BC%D1%80%D1%83%D0%B4&kind=locality", + "geoId": 211278, + "slug": "211278", + "kind": "locality", + "distance": { "value": null, "text": "" }, + "url": "//yandex.ru/pogoda?name=%D0%9A%D1%80%D0%B0%D1%81%D0%BD%D0%BE%D0%B4%D0%B0%D1%80%D1%81%D0%BA%D0%B8%D0%B9+%D0%BA%D1%80%D0%B0%D0%B9%2C+%D0%B3%D0%BE%D1%80%D0%BE%D0%B4%D1%81%D0%BA%D0%BE%D0%B9+%D0%BE%D0%BA%D1%80%D1%83%D0%B3+%D0%A1%D0%BE%D1%87%D0%B8%2C+%D1%81%D0%B5%D0%BB%D0%BE+%D0%9E%D1%80%D1%91%D0%BB-%D0%98%D0%B7%D1%83%D0%BC%D1%80%D1%83%D0%B4&kind=locality" + }, + { + "title": "деревня Орёл", + "description": "Республика Татарстан (Татарстан)", + "coords": { "lat": 55.56605148, "lon": 49.06790161 }, + "uri": "//yandex.ru/pogoda?name=%D0%A0%D0%B5%D1%81%D0%BF%D1%83%D0%B1%D0%BB%D0%B8%D0%BA%D0%B0+%D0%A2%D0%B0%D1%82%D0%B0%D1%80%D1%81%D1%82%D0%B0%D0%BD+%28%D0%A2%D0%B0%D1%82%D0%B0%D1%80%D1%81%D1%82%D0%B0%D0%BD%29%2C+%D0%9B%D0%B0%D0%B8%D1%88%D0%B5%D0%B2%D1%81%D0%BA%D0%B8%D0%B9+%D1%80%D0%B0%D0%B9%D0%BE%D0%BD%2C+%D0%9E%D1%80%D0%BB%D0%BE%D0%B2%D1%81%D0%BA%D0%BE%D0%B5+%D1%81%D0%B5%D0%BB%D1%8C%D1%81%D0%BA%D0%BE%D0%B5+%D0%BF%D0%BE%D1%81%D0%B5%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5%2C+%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%BD%D1%8F+%D0%9E%D1%80%D1%91%D0%BB&kind=locality", + "geoId": 140917, + "slug": "140917", + "kind": "locality", + "distance": { "value": null, "text": "" }, + "url": "//yandex.ru/pogoda?name=%D0%A0%D0%B5%D1%81%D0%BF%D1%83%D0%B1%D0%BB%D0%B8%D0%BA%D0%B0+%D0%A2%D0%B0%D1%82%D0%B0%D1%80%D1%81%D1%82%D0%B0%D0%BD+%28%D0%A2%D0%B0%D1%82%D0%B0%D1%80%D1%81%D1%82%D0%B0%D0%BD%29%2C+%D0%9B%D0%B0%D0%B8%D1%88%D0%B5%D0%B2%D1%81%D0%BA%D0%B8%D0%B9+%D1%80%D0%B0%D0%B9%D0%BE%D0%BD%2C+%D0%9E%D1%80%D0%BB%D0%BE%D0%B2%D1%81%D0%BA%D0%BE%D0%B5+%D1%81%D0%B5%D0%BB%D1%8C%D1%81%D0%BA%D0%BE%D0%B5+%D0%BF%D0%BE%D1%81%D0%B5%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5%2C+%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%BD%D1%8F+%D0%9E%D1%80%D1%91%D0%BB&kind=locality" + }, + { + "title": "посёлок Орёл", + "description": "Пермский край", + "coords": { "lat": 59.33684921, "lon": 56.58647919 }, + "uri": "//yandex.ru/pogoda?name=%D0%9F%D0%B5%D1%80%D0%BC%D1%81%D0%BA%D0%B8%D0%B9+%D0%BA%D1%80%D0%B0%D0%B9%2C+%D0%BC%D1%83%D0%BD%D0%B8%D1%86%D0%B8%D0%BF%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B9+%D0%BE%D0%BA%D1%80%D1%83%D0%B3+%D0%91%D0%B5%D1%80%D0%B5%D0%B7%D0%BD%D0%B8%D0%BA%D0%B8%2C+%D0%BF%D0%BE%D1%81%D1%91%D0%BB%D0%BE%D0%BA+%D0%9E%D1%80%D1%91%D0%BB&kind=locality", + "geoId": 135862, + "slug": "135862", + "kind": "locality", + "distance": { "value": null, "text": "" }, + "url": "//yandex.ru/pogoda?name=%D0%9F%D0%B5%D1%80%D0%BC%D1%81%D0%BA%D0%B8%D0%B9+%D0%BA%D1%80%D0%B0%D0%B9%2C+%D0%BC%D1%83%D0%BD%D0%B8%D1%86%D0%B8%D0%BF%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B9+%D0%BE%D0%BA%D1%80%D1%83%D0%B3+%D0%91%D0%B5%D1%80%D0%B5%D0%B7%D0%BD%D0%B8%D0%BA%D0%B8%2C+%D0%BF%D0%BE%D1%81%D1%91%D0%BB%D0%BE%D0%BA+%D0%9E%D1%80%D1%91%D0%BB&kind=locality" + }, + { + "title": "Пуэрто-Франсиско-де-Орельяна", + "description": "Провинция Орельяна", + "coords": { "lat": -0.4602830112, "lon": -76.99185944 }, + "uri": "//yandex.ru/pogoda?name=%D0%9F%D1%80%D0%BE%D0%B2%D0%B8%D0%BD%D1%86%D0%B8%D1%8F+%D0%9E%D1%80%D0%B5%D0%BB%D1%8C%D1%8F%D0%BD%D0%B0%2C+%D0%9F%D1%83%D1%8D%D1%80%D1%82%D0%BE-%D0%A4%D1%80%D0%B0%D0%BD%D1%81%D0%B8%D1%81%D0%BA%D0%BE-%D0%B4%D0%B5-%D0%9E%D1%80%D0%B5%D0%BB%D1%8C%D1%8F%D0%BD%D0%B0&kind=weather", + "geoId": 111891, + "slug": "111891", + "kind": "weather", + "distance": { "value": null, "text": "" }, + "url": "//yandex.ru/pogoda?name=%D0%9F%D1%80%D0%BE%D0%B2%D0%B8%D0%BD%D1%86%D0%B8%D1%8F+%D0%9E%D1%80%D0%B5%D0%BB%D1%8C%D1%8F%D0%BD%D0%B0%2C+%D0%9F%D1%83%D1%8D%D1%80%D1%82%D0%BE-%D0%A4%D1%80%D0%B0%D0%BD%D1%81%D0%B8%D1%81%D0%BA%D0%BE-%D0%B4%D0%B5-%D0%9E%D1%80%D0%B5%D0%BB%D1%8C%D1%8F%D0%BD%D0%B0&kind=weather" + } +] diff --git a/tests/test_gismeteo_api.py b/tests/test_gismeteo_api.py index ceacf5c..a2e8a6d 100644 --- a/tests/test_gismeteo_api.py +++ b/tests/test_gismeteo_api.py @@ -3,14 +3,13 @@ import datetime import pytest from gallery.painting.gismeteo.api import GismeteoApi +from tests.common.mock import create_mock_api from tests.data.gismeteo import GISMETEO_MOCK_SOURCE @pytest.fixture(name="gismeteo_api", scope="module") def gismeteo_api_fixture() -> GismeteoApi: - api = GismeteoApi() - api.SOURCE = GISMETEO_MOCK_SOURCE - return api + return create_mock_api(GismeteoApi, GISMETEO_MOCK_SOURCE) async def test_search(gismeteo_api: GismeteoApi): diff --git a/tests/test_matchtv_api.py b/tests/test_matchtv_api.py index dc8e576..c236ef8 100644 --- a/tests/test_matchtv_api.py +++ b/tests/test_matchtv_api.py @@ -3,14 +3,13 @@ import datetime import pytest from gallery.painting.matchtv.api import MatchTvApi +from tests.common.mock import create_mock_api from tests.data.matchtv import MATCHTV_MOCK_SOURCE @pytest.fixture(name="matchtv_api", scope="module") def matchtv_api_fixture() -> MatchTvApi: - api = MatchTvApi() - api.SOURCE = MATCHTV_MOCK_SOURCE - return api + return create_mock_api(MatchTvApi, MATCHTV_MOCK_SOURCE) async def test_search(matchtv_api: MatchTvApi): diff --git a/tests/test_openweather_api.py b/tests/test_openweather_api.py index 9041633..6c9778b 100644 --- a/tests/test_openweather_api.py +++ b/tests/test_openweather_api.py @@ -3,20 +3,13 @@ import datetime import pytest from gallery.painting.openweather.api import OpenWeatherApi -from gallery.painting.openweather.openweather import OpenWeather +from tests.common.mock import create_mock_api from tests.data.openweather import OPENWEATHER_MOCK_SOURCE @pytest.fixture(name="openweather_api", scope="module") def openweather_api_fixture() -> OpenWeatherApi: - class MockOpenWeather(OpenWeather): - def __init__(self): - super().__init__("") - self._source = OPENWEATHER_MOCK_SOURCE - - api = OpenWeatherApi() - api.SOURCE = MockOpenWeather() - return api + return create_mock_api(OpenWeatherApi, OPENWEATHER_MOCK_SOURCE) async def test_search(openweather_api: OpenWeatherApi): diff --git a/tests/test_yandextv_api.py b/tests/test_yandextv_api.py index 0f7a64f..ea48380 100644 --- a/tests/test_yandextv_api.py +++ b/tests/test_yandextv_api.py @@ -3,14 +3,13 @@ import datetime import pytest from gallery.painting.yandextv.api import YandexTvApi +from tests.common.mock import create_mock_api from tests.data.yandextv import YANDEXTV_MOCK_SOURCE @pytest.fixture(name="yandextv_api", scope="module") def yandextv_api_fixture() -> YandexTvApi: - api = YandexTvApi() - api.SOURCE = YANDEXTV_MOCK_SOURCE - return api + return create_mock_api(YandexTvApi, YANDEXTV_MOCK_SOURCE) async def test_search(yandextv_api: YandexTvApi):