From fba7f5b5d6f2a7dd6221f133766563526f46fdae Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 28 Feb 2024 11:40:47 -0500 Subject: [PATCH] xmltv: add subtitle, keep now - 3 hours of episode data --- src/dlhdhr/epg/program.py | 7 +++++++ src/dlhdhr/epg/zap2it.py | 7 +++++-- src/dlhdhr/epg/zaptv.py | 15 +++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/dlhdhr/epg/program.py b/src/dlhdhr/epg/program.py index eee73df..a60e831 100644 --- a/src/dlhdhr/epg/program.py +++ b/src/dlhdhr/epg/program.py @@ -18,6 +18,7 @@ class Program: title: str description: str tags: list[str] + subtitle: str | None thumbnail: str | None season: int | None episode: int | None @@ -34,6 +35,8 @@ class Program: programme = Element("programme", attrib={"start": start_time, "stop": end_time, "channel": channel.xmltv_id}) if self.title: SubElement(programme, "title", attrib={"lang": "en"}).text = self.title + if self.subtitle: + SubElement(programme, "sub-title", attrib={"lang": "en"}).text = self.subtitle if self.description: SubElement(programme, "desc", attrib={"lang": "en"}).text = self.description @@ -50,6 +53,10 @@ class Program: season_id = self.season or "" episode_id = self.episode or "" SubElement(programme, "episode-num", attrib={"system": "xmltv_ns"}).text = f"{season_id}.{episode_id}." + if self.season and self.episode: + SubElement( + programme, "episode-num", attrib={"system": "onscreen"} + ).text = f"S{self.season} E{self.episode}" if self.rating: rating = SubElement(programme, "rating", attrib={"system": self.rating.system}) diff --git a/src/dlhdhr/epg/zap2it.py b/src/dlhdhr/epg/zap2it.py index ac3a6e9..5d4b692 100644 --- a/src/dlhdhr/epg/zap2it.py +++ b/src/dlhdhr/epg/zap2it.py @@ -31,10 +31,11 @@ class Zap2it: def _cleanup_listings(self) -> None: now = datetime.datetime.now(datetime.UTC) + cutoff = now - datetime.timedelta(hours=3) updated: dict[str, list[Program]] = {} for call_sign, programs in self._listings.items(): - updated_programs = [p for p in programs if p.end_time > now] + updated_programs = [p for p in programs if p.end_time > cutoff] if updated_programs: updated[call_sign] = updated_programs self._listings = updated @@ -58,6 +59,7 @@ class Zap2it: listings: dict[str, list[Program]] = {} now = datetime.datetime.now(datetime.UTC) + cutoff = now - datetime.timedelta(hours=3) async with self._get_client() as client: channels = set() events = {} @@ -84,7 +86,7 @@ class Zap2it: programs = [] for evt_data in events[call_sign].values(): end_time = datetime.datetime.fromisoformat(evt_data["endTime"]) - if end_time < now: + if end_time < cutoff: continue rating = None @@ -96,6 +98,7 @@ class Zap2it: start_time=datetime.datetime.fromisoformat(evt_data["startTime"]), end_time=end_time, title=evt_data["program"]["title"], + subtitle=evt_data["program"].get("episodeTitle") or None, description=evt_data["program"]["shortDesc"], season=evt_data["program"]["season"], episode=evt_data["program"]["episode"], diff --git a/src/dlhdhr/epg/zaptv.py b/src/dlhdhr/epg/zaptv.py index 7141c6d..9cd4bf9 100644 --- a/src/dlhdhr/epg/zaptv.py +++ b/src/dlhdhr/epg/zaptv.py @@ -31,10 +31,11 @@ class ZapTV: def _cleanup_listings(self) -> None: now = datetime.datetime.now(datetime.UTC) + cutoff = now - datetime.timedelta(hours=3) updated: dict[str, list[Program]] = {} for call_sign, programs in self._listings.items(): - updated_programs = [p for p in programs if p.end_time > now] + updated_programs = [p for p in programs if p.end_time > cutoff] if updated_programs: updated[call_sign] = updated_programs self._listings = updated @@ -42,6 +43,7 @@ class ZapTV: async def _fetch_listings(self) -> dict[str, list[Program]]: listings: dict[str, list[Program]] = {} now = datetime.datetime.now(datetime.UTC) + cutoff = now - datetime.timedelta(hours=3) async with self._get_client() as client: channels = set() events = {} @@ -66,17 +68,21 @@ class ZapTV: programs = [] for evt_data in events[code].values(): end_time = datetime.datetime.fromisoformat(evt_data["endsAt"]) - if end_time < now: + if end_time < cutoff: continue ep_data = evt_data["metadata"].get("episode") or {} - season = ep_data.get("season") or None + season: str | None = ep_data.get("season") or None if season is not None: season = str(season) - episode = ep_data.get("number") or None + episode: str | None = ep_data.get("number") or None if episode is not None: episode = str(episode) + episode_title: str | None = ep_data.get("title") or None + if episode_title is not None: + episode_title = str(episode_title) + release_year = evt_data["metadata"].get("year") if release_year is not None: release_year = str(release_year) @@ -87,6 +93,7 @@ class ZapTV: end_time=end_time, title=evt_data["title"], description="", + subtitle=episode_title, season=season, episode=episode, tags=[],