feat(schedule): add matchtv schedule
This commit is contained in:
72
gallery/painting/matchtv/api.py
Normal file
72
gallery/painting/matchtv/api.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
import aiohttp
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from gallery.sketch.schedule.api import ScheduleApi
|
||||
from gallery.sketch.schedule.model import Channel, Schedule, ScheduleValue
|
||||
|
||||
logger = logging.getLogger("matchtv")
|
||||
|
||||
|
||||
CHANNEL_LIST = [
|
||||
"matchtv",
|
||||
"igra",
|
||||
"arena",
|
||||
"futbol-1",
|
||||
"futbol-2",
|
||||
"futbol-2",
|
||||
"strana",
|
||||
"planeta",
|
||||
]
|
||||
|
||||
|
||||
class MatchTvApi(ScheduleApi):
|
||||
BASE_URL = "https://matchtv.ru"
|
||||
|
||||
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}"
|
||||
print(url)
|
||||
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 CHANNEL_LIST
|
||||
|
||||
async def get_channel_schedule(
|
||||
self, channel_id: str, date: datetime.date
|
||||
) -> Schedule:
|
||||
endpoint = f"channel/{channel_id}/tvguide?date={date:%d-%m-%Y}"
|
||||
data = await self._request(endpoint)
|
||||
soup = BeautifulSoup(data, features="html.parser")
|
||||
values = []
|
||||
channel_name = soup.select_one(".caption__heading").text.split("|")[0].strip()
|
||||
current_date = datetime.datetime.combine(
|
||||
date.today(), datetime.datetime.min.time()
|
||||
)
|
||||
for item in soup.select(".teleprogram-schedule .teleprogram-schedule__item"):
|
||||
title = item.select_one(".teleprogram-item__title").text.strip()
|
||||
time_str = item.select_one(".teleprogram-item__time").text.strip()
|
||||
hours, minutes = map(int, time_str.split(":"))
|
||||
item_date = datetime.datetime.combine(
|
||||
date, datetime.time(hour=hours, minute=minutes)
|
||||
)
|
||||
values.append(ScheduleValue(start=current_date, end=item_date, label=title))
|
||||
current_date = item_date
|
||||
return Schedule(
|
||||
channel=Channel(id=channel_id, name=channel_name), date=date, values=values
|
||||
)
|
||||
Reference in New Issue
Block a user