Skip to content

Commit

Permalink
Changed: Create a more nested EPG experience like we had before.
Browse files Browse the repository at this point in the history
  • Loading branch information
basrieter committed Nov 16, 2023
1 parent 66df886 commit 411e791
Showing 1 changed file with 104 additions and 40 deletions.
144 changes: 104 additions & 40 deletions channels/channel.nos/nos2010/chn_nos2010.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import datetime
import time
from typing import Optional, List
from typing import Optional, List, Tuple, Dict

import pytz

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -151,21 +155,21 @@ def __init__(self, channel_info):

# Alpha listing based on JSON API
# self._add_data_parser("https://start-api.npo.nl/page/catalogue", json=True,
self._add_data_parser("https://start-api.npo.nl/media/series", json=True,
parser=["items"],
creator=self.create_json_episode_item,
preprocessor=self.extract_old_api_pages)

self._add_data_parser("https://start-api.npo.nl/media/series/", json=True,
name="API based video items",
parser=["items", ], creator=self.create_old_api_video_item,
preprocessor=self.extract_old_api_pages)

self._add_data_parser("https://start-api.npo.nl/page/franchise", json=True,
name="API based video items for a franchise",
parser=["items"],
creator=self.create_old_api_video_item,
preprocessor=self.process_old_franchise_page)
# self._add_data_parser("https://start-api.npo.nl/media/series", json=True,
# parser=["items"],
# creator=self.create_json_episode_item,
# preprocessor=self.extract_old_api_pages)
#
# self._add_data_parser("https://start-api.npo.nl/media/series/", json=True,
# name="API based video items",
# parser=["items", ], creator=self.create_old_api_video_item,
# preprocessor=self.extract_old_api_pages)
#
# self._add_data_parser("https://start-api.npo.nl/page/franchise", json=True,
# name="API based video items for a franchise",
# parser=["items"],
# creator=self.create_old_api_video_item,
# preprocessor=self.process_old_franchise_page)

self.__ignore_cookie_law()

Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit 411e791

Please sign in to comment.