feat(api): add ApiSource

This commit is contained in:
2024-08-21 19:42:53 +03:00
parent b3043c3c8c
commit 0638fb8d50
3 changed files with 61 additions and 49 deletions

View File

@@ -1,39 +1,21 @@
import datetime
import logging
import aiohttp
from aiocache import cached
from bs4 import BeautifulSoup
from gallery.sketch.schedule.api import ScheduleApi
from gallery.sketch.schedule.catalog import ChannelId
from gallery.sketch.schedule.model import Channel, Schedule, ScheduleValue
from gallery.sketch.source import ApiSource
logger = logging.getLogger("matchtv")
class MatchTvApi(ScheduleApi):
BASE_URL = "https://matchtv.ru"
SOURCE = ApiSource("https://matchtv.ru")
CACHE_TTL = 30 * 60
USER_AGENT = (
"Mozilla/5.0 (X11; Linux x86_64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/126.0.0.0 Safari/537.36"
)
async def _request(self, endpoint: str) -> str:
url = f"{self.BASE_URL}/{endpoint}"
logger.info(url)
async with aiohttp.ClientSession(
headers={
"User-Agent": self.USER_AGENT,
},
raise_for_status=True,
) as session:
async with session.request("GET", url) as response:
return await response.text()
async def get_channels(self) -> list[str]:
return [
ChannelId.MATCH_TV,
@@ -50,7 +32,7 @@ class MatchTvApi(ScheduleApi):
self, channel_id: str, date: datetime.date
) -> Schedule:
endpoint = f"channel/{channel_id}/tvguide?date={date:%d-%m-%Y}"
data = await self._request(endpoint)
data = await self.SOURCE.request(endpoint)
soup = BeautifulSoup(data, features="html.parser")
values = []
channel_name = soup.select_one(".caption__heading").text.split("|")[0].strip()