feat(yandexweather): add yandexweather api

This commit is contained in:
2026-08-28 10:01:49 +03:00
parent a92b63b855
commit 50c4221b8d
33 changed files with 506 additions and 74 deletions
+25 -6
View File
@@ -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)