Skip to content

Commit 51dd2f7

Browse files
committed
PageCategoryV2: Minor cleanup. Fix missing Category type(s)
1 parent 6728663 commit 51dd2f7

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

tidalapi/page.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
from tidalapi.request import Requests
4343
from tidalapi.session import Session
4444

45-
from . import album, artist, media, mix, playlist
4645

4746
PageCategories = Union[
4847
"Album",
@@ -56,6 +55,15 @@
5655

5756
AllCategories = Union["Artist", PageCategories]
5857

58+
PageCategoriesV2 = Union[
59+
"TrackList",
60+
"ShortcutList",
61+
"HorizontalList",
62+
"HorizontalListWithContext",
63+
]
64+
65+
AllCategoriesV2 = Union[PageCategoriesV2]
66+
5967

6068
class Page:
6169
"""
@@ -180,7 +188,6 @@ def __init__(self, session: "Session"):
180188
def parse(self, json_obj: JsonObj) -> AllCategories:
181189
result = None
182190
category_type = json_obj["type"]
183-
print(category_type)
184191
if category_type in ("PAGE_LINKS_CLOUD", "PAGE_LINKS"):
185192
category: PageCategories = PageLinks(self.session)
186193
elif category_type in ("FEATURED_PROMOTIONS", "MULTIPLE_TOP_PROMOTIONS"):
@@ -237,24 +244,33 @@ class PageCategoryV2:
237244
def __init__(self, session: "Session"):
238245
self.session = session
239246
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+
}
240255

241-
def parse(self, json_obj: JsonObj) -> AllCategories:
256+
def parse(self, json_obj: JsonObj) -> AllCategoriesV2:
242257
category_type = json_obj["type"]
243-
print(category_type)
244258
if category_type == "TRACK_LIST":
245259
category = TrackList(self.session)
246260
elif category_type == "SHORTCUT_LIST":
247261
category = ShortcutList(self.session)
248262
elif category_type == "HORIZONTAL_LIST":
249263
category = HorizontalList(self.session)
264+
elif category_type == "HORIZONTAL_LIST_WITH_CONTEXT":
265+
category = HorizontalListWithContext(self.session)
250266
else:
251267
raise NotImplementedError(f"PageType {category_type} not implemented")
252268

253269
return category.parse(json_obj)
254270

255271

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."""
258274

259275
items: Optional[List[Any]] = None
260276

@@ -273,35 +289,23 @@ def parse(self, json_obj: JsonObj) -> "SimpleList":
273289

274290
def get_item(self, json_obj):
275291
item_type = json_obj["type"]
276-
# item_data = json_obj["data"]
277-
278-
print(item_type)
279292

280293
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): ...
297303

298304

299-
class HorizontalList(SimpleList):
300-
...
305+
class HorizontalListWithContext(HorizontalList): ...
301306

302307

303-
class ShortcutList(SimpleList):
304-
...
308+
class ShortcutList(SimpleList): ...
305309

306310

307311
class FeaturedItems(PageCategory):
@@ -383,7 +387,7 @@ def parse(self, json_obj: JsonObj) -> "ItemList":
383387

384388

385389
class TrackList(PageCategory):
386-
"""A list of track from TIDAL."""
390+
"""A list of tracks from TIDAL."""
387391

388392
items: Optional[List[Any]] = None
389393

@@ -457,7 +461,9 @@ def __init__(self, session: "Session", json_obj: JsonObj):
457461
self.text = json_obj["text"]
458462
self.featured = bool(json_obj["featured"])
459463

460-
def get(self) -> Union["Artist", "Playlist", "Track", "UserPlaylist", "Video"]:
464+
def get(
465+
self,
466+
) -> Union["Artist", "Playlist", "Track", "UserPlaylist", "Video", "Album"]:
461467
"""Retrieve the PageItem with the artifact_id matching the type.
462468
463469
:return: The fully parsed item, e.g. :class:`.Playlist`, :class:`.Video`, :class:`.Track`

0 commit comments

Comments
 (0)