77
88from resources .lib import kodiutils
99from resources .lib .kodiutils import TitleItem
10+ from resources .lib .modules .menu import Menu
1011from resources .lib .viervijfzes import CHANNELS , STREAM_DICT
12+ from resources .lib .viervijfzes .auth import AuthApi
13+ from resources .lib .viervijfzes .content import ContentApi , CACHE_ONLY , CACHE_AUTO
1114
1215_LOGGER = logging .getLogger ('channels' )
1316
@@ -17,6 +20,8 @@ class Channels:
1720
1821 def __init__ (self ):
1922 """ Initialise object """
23+ auth = AuthApi (kodiutils .get_setting ('username' ), kodiutils .get_setting ('password' ), kodiutils .get_tokens_path ())
24+ self ._api = ContentApi (auth , cache_path = kodiutils .get_cache_path ())
2025
2126 @staticmethod
2227 def show_channels ():
@@ -33,7 +38,7 @@ def show_channels():
3338 (
3439 kodiutils .localize (30053 , channel = channel .get ('name' )), # TV Guide for {channel}
3540 'Container.Update(%s)' %
36- kodiutils .url_for ('show_tvguide_channel ' , channel = channel .get ('epg' ))
41+ kodiutils .url_for ('show_channel_tvguide ' , channel = channel .get ('epg' ))
3742 )
3843 ]
3944
@@ -60,43 +65,54 @@ def show_channels():
6065 kodiutils .show_listing (listing , 30007 )
6166
6267 @staticmethod
63- def show_channel_menu (key ):
68+ def show_channel_menu (channel ):
6469 """ Shows a TV channel
65- :type key : str
70+ :type channel : str
6671 """
67- channel = CHANNELS [key ]
72+ channel_info = CHANNELS [channel ]
6873
6974 # Lookup the high resolution logo based on the channel name
70- fanart = '{path}/resources/logos/{logo}' .format (path = kodiutils .addon_path (), logo = channel .get ('background' ))
75+ fanart = '{path}/resources/logos/{logo}' .format (path = kodiutils .addon_path (), logo = channel_info .get ('background' ))
7176
7277 listing = [
7378 TitleItem (
74- title = kodiutils .localize (30053 , channel = channel .get ('name' )), # TV Guide for {channel}
75- path = kodiutils .url_for ('show_tvguide_channel ' , channel = key ),
79+ title = kodiutils .localize (30053 , channel = channel_info .get ('name' )), # TV Guide for {channel}
80+ path = kodiutils .url_for ('show_channel_tvguide ' , channel = channel ),
7681 art_dict = {
7782 'icon' : 'DefaultAddonTvInfo.png' ,
7883 'fanart' : fanart ,
7984 },
8085 info_dict = {
81- 'plot' : kodiutils .localize (30054 , channel = channel .get ('name' )), # Browse the TV Guide for {channel}
86+ 'plot' : kodiutils .localize (30054 , channel = channel_info .get ('name' )), # Browse the TV Guide for {channel}
8287 }
8388 ),
8489 TitleItem (
85- title = kodiutils .localize (30055 , channel = channel .get ('name' )), # Catalog for {channel}
86- path = kodiutils .url_for ('show_catalog_channel ' , channel = key ),
90+ title = kodiutils .localize (30055 , channel = channel_info .get ('name' )), # Catalog for {channel}
91+ path = kodiutils .url_for ('show_channel_catalog ' , channel = channel ),
8792 art_dict = {
8893 'icon' : 'DefaultMovieTitle.png' ,
8994 'fanart' : fanart ,
9095 },
9196 info_dict = {
92- 'plot' : kodiutils .localize (30056 , channel = channel .get ('name' )), # Browse the Catalog for {channel}
97+ 'plot' : kodiutils .localize (30056 , channel = channel_info .get ('name' )), # Browse the Catalog for {channel}
9398 }
94- )
99+ ),
100+ TitleItem (
101+ title = kodiutils .localize (30057 , channel = channel_info .get ('name' )), # Categories for {channel}
102+ path = kodiutils .url_for ('show_channel_categories' , channel = channel ),
103+ art_dict = {
104+ 'icon' : 'DefaultGenre.png' ,
105+ 'fanart' : fanart ,
106+ },
107+ info_dict = {
108+ 'plot' : kodiutils .localize (30058 , channel = channel_info .get ('name' )), # Browse the Categories for {channel}
109+ }
110+ ),
95111 ]
96112
97113 # Add YouTube channels
98114 if kodiutils .get_cond_visibility ('System.HasAddon(plugin.video.youtube)' ) != 0 :
99- for youtube in channel .get ('youtube' , []):
115+ for youtube in channel_info .get ('youtube' , []):
100116 listing .append (
101117 TitleItem (
102118 title = kodiutils .localize (30206 , label = youtube .get ('label' )), # Watch {label} on YouTube
@@ -108,3 +124,58 @@ def show_channel_menu(key):
108124 )
109125
110126 kodiutils .show_listing (listing , 30007 , sort = ['unsorted' ])
127+
128+ def show_channel_categories (self , channel ):
129+ """ Shows the categories of a channel
130+ :type channel: str
131+ """
132+ categories = self ._api .get_categories (channel )
133+
134+ listing = [
135+ TitleItem (
136+ title = category .title ,
137+ path = kodiutils .url_for ('show_channel_category' , channel = category .channel , category = category .uuid ),
138+ art_dict = {
139+ 'icon' : 'DefaultGenre.png' ,
140+ },
141+ ) for category in categories
142+ ]
143+
144+ kodiutils .show_listing (listing , 30007 , sort = ['unsorted' ])
145+
146+ def show_channel_category (self , channel , category_id ):
147+ """ Shows a selected category of a channel
148+ :type channel: str
149+ :type category_id: str
150+ """
151+ categories = self ._api .get_categories (channel )
152+
153+ # Extract selected category
154+ category = next (category for category in categories if category .uuid == category_id )
155+ if not category :
156+ raise Exception ('Unknown category' )
157+
158+ # Add programs
159+ listing_programs = []
160+ for item in category .programs :
161+ program = self ._api .get_program (channel , item .path , CACHE_ONLY ) # Get program details, but from cache only
162+
163+ if program :
164+ listing_programs .append (Menu .generate_titleitem (program ))
165+ else :
166+ listing_programs .append (Menu .generate_titleitem (item ))
167+
168+ # Add episodes
169+ listing_episodes = []
170+ for item in category .episodes :
171+ # We don't have the Program Name without making a request to the page, so we use CACHE_AUTO instead of CACHE_ONLY.
172+ # This will make a request for each item in this view (about 12 items), but it goes quite fast.
173+ # Results are cached, so this will only happen once.
174+ episode = self ._api .get_episode (channel , item .path , CACHE_AUTO )
175+
176+ if episode :
177+ listing_episodes .append (Menu .generate_titleitem (episode ))
178+ else :
179+ listing_episodes .append (Menu .generate_titleitem (item ))
180+
181+ kodiutils .show_listing (listing_programs + listing_episodes , 30007 , content = 'tvshows' , sort = ['unsorted' ])
0 commit comments