diff --git a/channels/channel.nos/nos2010/chn_nos2010.py b/channels/channel.nos/nos2010/chn_nos2010.py index 755d6627..a85485bd 100644 --- a/channels/channel.nos/nos2010/chn_nos2010.py +++ b/channels/channel.nos/nos2010/chn_nos2010.py @@ -2,7 +2,7 @@ import datetime import time -from typing import Optional, List +from typing import Optional, List, Union, Tuple, Dict import pytz @@ -71,7 +71,11 @@ def __init__(self, channel_info): self._add_data_parser("https://npo.nl/start/api/domain/guide-channels", name="Recent Items", label="recent", json=True, - parser=[], creator=self.create_api_recent_item) + # Should we have an EPG per day + preprocessor=self.create_api_epg_tree, + # Should we have an EPG per channel + # parser=[], creator=self.create_api_recent_item + ) self._add_data_parser("https://npo.nl/start/api/domain/guide-channel?guid=", name="EPG listing", json=True, @@ -693,6 +697,58 @@ def create_api_live_tv(self, result_set): item.isLive = True return item + def create_api_epg_tree(self, data: str) -> Tuple[JsonHelper, List[MediaItem]]: + data = JsonHelper(data) + # Keep a list of the days. + day_lookup: Dict[str, MediaItem] = {} + + for channel in data.get_value(): + guid = channel["guid"] + channel = channel["title"] + if channel not in ["NPO1", "NPO2", "NPO3"]: + continue + + # Fetch channel EPG + url = f"https://npo.nl/start/api/domain/guide-channel?guid={guid}" + channel_info = JsonHelper(UriHandler.open(url)) + + for day_info in channel_info.get_value("days"): + # Check if a day already exists + date = day_info["date"] + if date not in day_lookup: + # Create a Day media-item + day, month, year = date.split("-") + time_stamp = datetime.datetime(int(year), int(month), int(day)) + if time_stamp > datetime.datetime.now(): + continue + + days = LanguageHelper.get_days_list() + day_name = days[time_stamp.weekday()] + + if time_stamp.date() == datetime.datetime.now().date(): + day_name = LanguageHelper.get_localized_string(LanguageHelper.Today) + elif time_stamp.date() == datetime.datetime.now().date() - datetime.timedelta(days=1): + day_name = LanguageHelper.get_localized_string(LanguageHelper.Yesterday) + + day_item = FolderItem(f"{date} - {day_name}", "", content_type=contenttype.EPISODES) + day_item.set_date(year, month, day) + day_lookup[date] = day_item + + for program in day_info["scheduledPrograms"]: + item = self.create_api_epg_item(program, channel) + if not item: + continue + + # We should show all shows until 5:00 am the next day so link them up. + date_stamp = DateHelper.get_date_from_posix(program["programStart"], tz=pytz.UTC) + date_stamp -= datetime.timedelta(hours=4) + date_label = date_stamp.strftime("%d-%m-%Y") + if date_label in day_lookup: + day_item = day_lookup[date_label] + day_item.items.append(item) + + return data, list(day_lookup.values()) + def create_api_recent_item(self, result_set: dict) -> Optional[MediaItem]: name = result_set["title"] if name not in ["NPO1", "NPO2", "NPO3"]: @@ -733,34 +789,42 @@ def create_api_epg_day(self, result_set: dict) -> Optional[MediaItem]: # process the sub items for result in result_set["scheduledPrograms"]: - series_slug = result["series"].get("slug") - if not series_slug: - continue - program_guid = (result["program"] or {}).get("guid") - if not program_guid: - continue + sub_item = self.create_api_epg_item(result) + if sub_item: + day_item.items.append(sub_item) + return day_item - url = f"https://npo.nl/start/api/domain/series-seasons?slug={series_slug}" - name = result["title"] - season_slug = result["season"]["slug"] - start = result["programStart"] + def create_api_epg_item(self, result_set: dict, channel: Optional[str] = None) -> Optional[MediaItem]: + series_slug = result_set["series"].get("slug") + if not series_slug: + return None + program_guid = (result_set["program"] or {}).get("guid") + if not program_guid: + return None - date_stamp = DateHelper.get_date_from_posix(start, tz=pytz.UTC) - date_stamp = date_stamp.astimezone(self.__timezone) + url = f"https://npo.nl/start/api/domain/series-seasons?slug={series_slug}" + name = result_set["title"] + season_slug = result_set["season"]["slug"] + start = result_set["programStart"] - sub_item = MediaItem(f"{date_stamp.hour:02d}:{date_stamp.minute:02d} - {name}", url, media_type=mediatype.EPISODE) - sub_item.metaData = { - "season_slug": season_slug, - "program_guid": program_guid - } + date_stamp = DateHelper.get_date_from_posix(start, tz=pytz.UTC) + date_stamp = date_stamp.astimezone(self.__timezone) - if "images" in result and result["images"]: - image_data = result["images"][0] - sub_item.set_artwork(thumb=image_data["url"], fanart=image_data["url"]) - sub_item.description = image_data.get("description") + if channel: + item = MediaItem(f"{date_stamp.hour:02d}:{date_stamp.minute:02d} - {channel} - {name}", url, media_type=mediatype.EPISODE) + else: + item = MediaItem(f"{date_stamp.hour:02d}:{date_stamp.minute:02d} - {name}", url, media_type=mediatype.EPISODE) + item.metaData = { + "season_slug": season_slug, + "program_guid": program_guid + } + item.set_date(date_stamp.year, date_stamp.month, date_stamp.day, date_stamp.hour, date_stamp.minute, date_stamp.second) - day_item.items.append(sub_item) - return day_item + if "images" in result_set and result_set["images"]: + image_data = result_set["images"][0] + item.set_artwork(thumb=image_data["url"], fanart=image_data["url"]) + item.description = image_data.get("description") + return item def update_epg_series_item(self, item: MediaItem): # Go from season slug, show slug & program guid ->