feat: add common view module

This commit is contained in:
2024-08-13 20:54:12 +03:00
parent bf51ddabc0
commit 05161749e8
19 changed files with 155 additions and 88 deletions

View File

@@ -2,6 +2,7 @@ import datetime
import logging
import aiohttp
from aiocache import cached
from bs4 import BeautifulSoup
from gallery.sketch.schedule.api import ScheduleApi
@@ -24,6 +25,7 @@ CHANNEL_LIST = [
class MatchTvApi(ScheduleApi):
BASE_URL = "https://matchtv.ru"
CACHE_TTL = 30 * 60
USER_AGENT = (
"Mozilla/5.0 (X11; Linux x86_64) "
@@ -47,6 +49,7 @@ class MatchTvApi(ScheduleApi):
async def get_channels(self) -> list[str]:
return CHANNEL_LIST
@cached(ttl=CACHE_TTL)
async def get_channel_schedule(
self, channel_id: str, date: datetime.date
) -> Schedule:
@@ -55,18 +58,25 @@ class MatchTvApi(ScheduleApi):
soup = BeautifulSoup(data, features="html.parser")
values = []
channel_name = soup.select_one(".caption__heading").text.split("|")[0].strip()
current_date = datetime.datetime.combine(
current_day = datetime.datetime.combine(
date.today(), datetime.datetime.min.time()
)
end = current_day + datetime.timedelta(days=1, hours=6)
prev_value: ScheduleValue | None = None
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
item_date = current_day.replace(hour=hours, minute=minutes)
if prev_value is not None and item_date.hour < prev_value.start.hour:
current_day += datetime.timedelta(days=1)
item_date += datetime.timedelta(days=1)
live = "Прямая трансляция" in title
value = ScheduleValue(start=item_date, end=end, label=title, live=live)
values.append(value)
if prev_value is not None:
prev_value.end = item_date
prev_value = value
return Schedule(
channel=Channel(id=channel_id, name=channel_name), date=date, values=values
)