feat(app): add html weather view

This commit is contained in:
2024-07-25 15:51:27 +03:00
parent 234a2b7b0e
commit b3d88997eb
22 changed files with 393 additions and 34 deletions

View File

@@ -3,9 +3,10 @@ from typing import Any, Dict, List, NamedTuple
import aiohttp
from bs4 import BeautifulSoup
from . import dateutil
from .location import LOCATION_BUNDLE
from .parser import ROW_PARSERS, OneDayParser
from .parser import ROW_PARSERS, ONE_DAY_PARSER
class WeatherValue(NamedTuple):
@@ -32,7 +33,7 @@ class GismeteoApi:
def _parse_oneday(self, data: str) -> List[WeatherValue]:
result: List[Dict[str, Any]] = []
soup = BeautifulSoup(data, features="html.parser")
widget = OneDayParser().parse_widget(soup)
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:
@@ -40,12 +41,7 @@ class GismeteoApi:
result[index][parser.KEY] = value
return [WeatherValue(**item) for item in result]
async def today(self, location_id: str) -> List[WeatherValue]:
async def get_day(self, location_id: str, date: datetime.date) -> List[WeatherValue]:
location = LOCATION_BUNDLE.parse(location_id)
data = await self._request(f"weather-{location}/today")
return self._parse_oneday(data)
async def tomorrow(self, location_id: str) -> List[WeatherValue]:
location = LOCATION_BUNDLE.parse(location_id)
data = await self._request(f"weather-{location}/tomorrow")
data = await self._request(f"weather-{location}/{dateutil.dump(date)}")
return self._parse_oneday(data)