refactor: add base parse module

This commit is contained in:
2026-09-17 00:47:01 +03:00
parent 65b6adbf16
commit 9ae9d1be14
8 changed files with 219 additions and 203 deletions
+7 -58
View File
@@ -2,17 +2,14 @@ import datetime
import json
import logging
from os import environ
from typing import Any
from bs4 import BeautifulSoup
from fastapi import HTTPException, status
from gallery.sketch.source import ApiSource
from gallery.sketch.weather.api import WeatherApi
from gallery.sketch.weather.model import Location, WeatherResponse, WeatherValue
from gallery.sketch.weather.util import merge_weather_values
from gallery.sketch.weather.model import Location, WeatherResponse
from .parser import DAYS_PARSER, LOCATION_PARSER, ONE_DAY_PARSER, ROW_PARSERS
from .parser import Context, GismeteoParser
logger = logging.getLogger("gismeteo")
@@ -28,17 +25,9 @@ def format_date(date: datetime.date) -> str:
return f"{days + 1}-day"
def set_dict_nested_value(target: dict, key: str, value: Any):
parts = key.split(".")
for part in parts[:-1]:
if part not in target:
target[part] = {}
target = target[part]
target[parts[-1]] = value
class GismeteoApi(WeatherApi):
PROVIDER = "gismeteo"
parser = GismeteoParser()
def _build_source(self) -> ApiSource:
return ApiSource(
@@ -46,48 +35,6 @@ class GismeteoApi(WeatherApi):
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]] = []
soup = BeautifulSoup(data, features="html.parser")
location = LOCATION_PARSER.parse_location(data)
widget = ONE_DAY_PARSER.parse_widget(soup)
for parser in ROW_PARSERS:
for index, value in enumerate(parser.parse_row(widget)):
while len(result) < index + 1:
result.append({})
set_dict_nested_value(result[index], parser.KEY, value)
values = [WeatherValue(**item) for item in result]
# group values by 3 hours
if len(values) > 8:
n = 3
values = [
merge_weather_values(values[i].date, values[i : i + n], "average") for i in range(0, len(values), n)
]
return WeatherResponse(
location=location or "n/a",
date=date,
period="day",
values=values,
)
def _parse_manydays(self, data: str) -> WeatherResponse:
result: list[dict[str, Any]] = []
soup = BeautifulSoup(data, features="html.parser")
location = LOCATION_PARSER.parse_location(data)
widget = DAYS_PARSER.parse_widget(soup)
for parser in ROW_PARSERS:
for index, value in enumerate(parser.parse_row(widget)):
while len(result) < index + 1:
result.append({})
set_dict_nested_value(result[index], parser.KEY, value)
values = [WeatherValue(**item) for item in result]
return WeatherResponse(
location=location or "n/a",
date=datetime.date.today(),
period="days",
values=values,
)
async def find_locations(self, query: str) -> list[Location]:
geo = "ru"
latitude = 52.968498
@@ -127,8 +74,10 @@ class GismeteoApi(WeatherApi):
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}/{format_date(date)}")
return self._parse_oneday(date, data)
context = Context(date, "day", data, 8)
return self.parser.parse(context)
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
data = await self.source.request(f"weather-{location_id}/{days}-days")
return self._parse_manydays(data)
context = Context(datetime.date.today(), "days", data)
return self.parser.parse(context)