From 8dc6ed17224c9c59cb52c2f398b3f2fab2129fda Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sun, 25 Feb 2024 09:51:51 -0500 Subject: [PATCH] improve US channel guide fetching --- src/dlhdhr/epg/zap2it.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/dlhdhr/epg/zap2it.py b/src/dlhdhr/epg/zap2it.py index 648b36d..278c8c5 100644 --- a/src/dlhdhr/epg/zap2it.py +++ b/src/dlhdhr/epg/zap2it.py @@ -59,17 +59,30 @@ class Zap2it: listings: dict[str, list[Program]] = {} now = datetime.datetime.now(datetime.UTC) async with self._get_client() as client: - res = await client.get("/grid", params=params) - res.raise_for_status() - - data = res.json() - - for ch_data in data["channels"]: - call_sign = ch_data["callSign"] + channels = set() + events = {} + + # Fetch up to 18 hours into the future + for i in range(1, 3, 1): + params["time"] = str(int(time.time()) + (21600 * i)) + res = await client.get("/grid", params=params) + res.raise_for_status() + + data = res.json() + for ch_data in data["channels"]: + call_sign = ch_data["callSign"] + channels.add(call_sign) + if call_sign not in events: + events[call_sign] = {} + + for evt in ch_data["events"]: + key = (evt["startTime"], evt["endTime"]) + if key not in events[call_sign]: + events[call_sign][key] = evt + + for call_sign in channels: programs = [] - listings[call_sign] = programs - - for evt_data in ch_data["events"]: + for evt_data in events[call_sign].values(): end_time = datetime.datetime.fromisoformat(evt_data["endTime"]) if end_time < now: continue @@ -92,6 +105,9 @@ class Zap2it: rating=rating, ) ) + + listings[call_sign] = sorted(programs, key=lambda p: p.start_time, reverse=True) + return listings async def _refresh_listings(self) -> dict[str, list[Program]]: