42
42
from tidalapi .request import Requests
43
43
from tidalapi .session import Session
44
44
45
- from . import album , artist , media , mix , playlist
46
45
47
46
PageCategories = Union [
48
47
"Album" ,
56
55
57
56
AllCategories = Union ["Artist" , PageCategories ]
58
57
58
+ PageCategoriesV2 = Union [
59
+ "TrackList" ,
60
+ "ShortcutList" ,
61
+ "HorizontalList" ,
62
+ "HorizontalListWithContext" ,
63
+ ]
64
+
65
+ AllCategoriesV2 = Union [PageCategoriesV2 ]
66
+
59
67
60
68
class Page :
61
69
"""
@@ -180,7 +188,6 @@ def __init__(self, session: "Session"):
180
188
def parse (self , json_obj : JsonObj ) -> AllCategories :
181
189
result = None
182
190
category_type = json_obj ["type" ]
183
- print (category_type )
184
191
if category_type in ("PAGE_LINKS_CLOUD" , "PAGE_LINKS" ):
185
192
category : PageCategories = PageLinks (self .session )
186
193
elif category_type in ("FEATURED_PROMOTIONS" , "MULTIPLE_TOP_PROMOTIONS" ):
@@ -237,24 +244,33 @@ class PageCategoryV2:
237
244
def __init__ (self , session : "Session" ):
238
245
self .session = session
239
246
self .request = session .request
247
+ self .item_type_parser : Dict [str , Callable [..., Any ]] = {
248
+ "PLAYLIST" : self .session .parse_playlist ,
249
+ "VIDEO" : self .session .parse_video ,
250
+ "TRACK" : self .session .parse_track ,
251
+ "ARTIST" : self .session .parse_artist ,
252
+ "ALBUM" : self .session .parse_album ,
253
+ "MIX" : self .session .parse_v2_mix ,
254
+ }
240
255
241
- def parse (self , json_obj : JsonObj ) -> AllCategories :
256
+ def parse (self , json_obj : JsonObj ) -> AllCategoriesV2 :
242
257
category_type = json_obj ["type" ]
243
- print (category_type )
244
258
if category_type == "TRACK_LIST" :
245
259
category = TrackList (self .session )
246
260
elif category_type == "SHORTCUT_LIST" :
247
261
category = ShortcutList (self .session )
248
262
elif category_type == "HORIZONTAL_LIST" :
249
263
category = HorizontalList (self .session )
264
+ elif category_type == "HORIZONTAL_LIST_WITH_CONTEXT" :
265
+ category = HorizontalListWithContext (self .session )
250
266
else :
251
267
raise NotImplementedError (f"PageType { category_type } not implemented" )
252
268
253
269
return category .parse (json_obj )
254
270
255
271
256
- class SimpleList (PageCategory ):
257
- """A simple list of different items for the home page V2"""
272
+ class SimpleList (PageCategoryV2 ):
273
+ """A simple list of different items for the home page V2. """
258
274
259
275
items : Optional [List [Any ]] = None
260
276
@@ -273,35 +289,23 @@ def parse(self, json_obj: JsonObj) -> "SimpleList":
273
289
274
290
def get_item (self , json_obj ):
275
291
item_type = json_obj ["type" ]
276
- # item_data = json_obj["data"]
277
-
278
- print (item_type )
279
292
280
293
try :
281
- if item_type == "PLAYLIST" :
282
- return self .session .parse_playlist (json_obj )
283
- elif item_type == "VIDEO" :
284
- return self .session .parse_video (json_obj ["data" ])
285
- elif item_type == "TRACK" :
286
- return self .session .parse_track (json_obj ["data" ])
287
- elif item_type == "ARTIST" :
288
- return self .session .parse_artist (json_obj ["data" ])
289
- elif item_type == "ALBUM" :
290
- return self .session .parse_album (json_obj ["data" ])
291
- # elif item_type == "MIX":
292
- # return self.session.mix(json_obj["data"]["id"])
293
- except Exception as e :
294
- print (e )
295
- # raise NotImplementedError
296
- return None
294
+ if item_type in self .item_type_parser .keys ():
295
+ return self .item_type_parser [item_type ](json_obj ["data" ])
296
+ else :
297
+ raise NotImplementedError (f"PageItemType { item_type } not implemented" )
298
+ except TypeError as e :
299
+ print (f"Exception { e } while parsing SimpleList object." )
300
+
301
+
302
+ class HorizontalList (SimpleList ): ...
297
303
298
304
299
- class HorizontalList (SimpleList ):
300
- ...
305
+ class HorizontalListWithContext (HorizontalList ): ...
301
306
302
307
303
- class ShortcutList (SimpleList ):
304
- ...
308
+ class ShortcutList (SimpleList ): ...
305
309
306
310
307
311
class FeaturedItems (PageCategory ):
@@ -383,7 +387,7 @@ def parse(self, json_obj: JsonObj) -> "ItemList":
383
387
384
388
385
389
class TrackList (PageCategory ):
386
- """A list of track from TIDAL."""
390
+ """A list of tracks from TIDAL."""
387
391
388
392
items : Optional [List [Any ]] = None
389
393
@@ -457,7 +461,9 @@ def __init__(self, session: "Session", json_obj: JsonObj):
457
461
self .text = json_obj ["text" ]
458
462
self .featured = bool (json_obj ["featured" ])
459
463
460
- def get (self ) -> Union ["Artist" , "Playlist" , "Track" , "UserPlaylist" , "Video" ]:
464
+ def get (
465
+ self ,
466
+ ) -> Union ["Artist" , "Playlist" , "Track" , "UserPlaylist" , "Video" , "Album" ]:
461
467
"""Retrieve the PageItem with the artifact_id matching the type.
462
468
463
469
:return: The fully parsed item, e.g. :class:`.Playlist`, :class:`.Video`, :class:`.Track`
0 commit comments