Skip to content

Commit 989d63d

Browse files
committed
Hack it to make it work
1 parent d209ab7 commit 989d63d

File tree

1 file changed

+80
-21
lines changed

1 file changed

+80
-21
lines changed

tidalapi/page.py

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ class Page:
7070
_categories_iter: Optional[Iterator["AllCategories"]] = None
7171
_items_iter: Optional[Iterator[Callable[..., Any]]] = None
7272
page_category: "PageCategory"
73+
page_category_v2: "PageCategoryV2"
7374
request: "Requests"
7475

7576
def __init__(self, session: "Session", title: str):
7677
self.request = session.request
7778
self.categories = None
7879
self.title = title
7980
self.page_category = PageCategory(session)
81+
self.page_category_v2 = PageCategoryV2(session)
8082

8183
def __iter__(self) -> "Page":
8284
if self.categories is None:
@@ -117,12 +119,9 @@ def parse(self, json_obj: JsonObj) -> "Page":
117119
return copy.copy(self)
118120

119121
def parseV2(self, json_obj: JsonObj) -> "Page":
120-
"""Goes through everything in the page, and gets the title and adds all the rows
121-
to the categories field :param json_obj: The json to be parsed :return: A copy
122-
of the Page that you can use to browse all the items."""
123122
self.categories = []
124123
for item in json_obj["items"]:
125-
page_item = self.page_category.parse(item)
124+
page_item = self.page_category_v2.parse(item)
126125
self.categories.append(page_item)
127126

128127
return copy.copy(self)
@@ -181,6 +180,7 @@ def __init__(self, session: "Session"):
181180
def parse(self, json_obj: JsonObj) -> AllCategories:
182181
result = None
183182
category_type = json_obj["type"]
183+
print(category_type)
184184
if category_type in ("PAGE_LINKS_CLOUD", "PAGE_LINKS"):
185185
category: PageCategories = PageLinks(self.session)
186186
elif category_type in ("FEATURED_PROMOTIONS", "MULTIPLE_TOP_PROMOTIONS"):
@@ -209,10 +209,6 @@ def parse(self, json_obj: JsonObj) -> AllCategories:
209209
elif category_type == "SOCIAL":
210210
json_obj["items"] = json_obj["socialProfiles"]
211211
category = LinkList(self.session)
212-
elif category_type == "SHORTCUT_LIST":
213-
category = ShortcutList(self.session)
214-
elif category_type == "HORIZONTAL_LIST":
215-
category = HorizontalList(self.session)
216212
else:
217213
raise NotImplementedError(f"PageType {category_type} not implemented")
218214

@@ -232,6 +228,41 @@ def show_more(self) -> Optional[Page]:
232228
)
233229

234230

231+
class PageCategoryV2:
232+
type = None
233+
title: Optional[str] = None
234+
description: Optional[str] = ""
235+
request: "Requests"
236+
237+
def __init__(self, session: "Session"):
238+
self.session = session
239+
self.request = session.request
240+
self.item_types: Dict[str, Callable[..., Any]] = {
241+
"ALBUM_LIST": self.session.parse_album,
242+
"ARTIST_LIST": self.session.parse_artist,
243+
"TRACK_LIST": self.session.parse_track,
244+
"PLAYLIST_LIST": self.session.parse_playlist,
245+
"VIDEO_LIST": self.session.parse_video,
246+
"MIX_LIST": self.session.parse_mix,
247+
}
248+
249+
def parse(self, json_obj: JsonObj) -> AllCategories:
250+
category_type = json_obj["type"]
251+
print(category_type)
252+
# if category_type in self.item_types.keys():
253+
# category = ItemListV2(self.session)
254+
# el
255+
if category_type == "SHORTCUT_LIST":
256+
category = ShortcutList(self.session)
257+
elif category_type == "HORIZONTAL_LIST":
258+
category = HorizontalList(self.session)
259+
else:
260+
return None
261+
# raise NotImplementedError(f"PageType {category_type} not implemented")
262+
263+
return category.parse(json_obj)
264+
265+
235266
class SimpleList(PageCategory):
236267
"""A simple list of different items for the home page V2"""
237268

@@ -252,21 +283,22 @@ def parse(self, json_obj: JsonObj) -> "SimpleList":
252283

253284
def get_item(self, json_obj):
254285
item_type = json_obj["type"]
255-
item_data = json_obj["data"]
286+
# item_data = json_obj["data"]
256287

257288
if item_type == "PLAYLIST":
258-
return self.session.parse_playlist(item_data)
259-
elif item_type == "VIDEO":
260-
return self.session.parse_video(item_data)
261-
elif item_type == "TRACK":
262-
return self.session.parse_track(item_data)
263-
elif item_type == "ARTIST":
264-
return self.session.parse_artist(item_data)
265-
elif item_type == "ALBUM":
266-
return self.session.parse_album(item_data)
267-
elif item_type == "MIX":
268-
return self.session.parse_mix(item_data)
269-
raise NotImplementedError
289+
return self.session.parse_playlist(json_obj)
290+
# elif item_type == "VIDEO":
291+
# return self.session.parse_video(item_data)
292+
# elif item_type == "TRACK":
293+
# return self.session.parse_track(item_data)
294+
# elif item_type == "ARTIST":
295+
# return self.session.parse_artist(item_data)
296+
# elif item_type == "ALBUM":
297+
# return self.session.parse_album(item_data)
298+
# elif item_type == "MIX":
299+
# return self.session.parse_v2_mix(json_obj)
300+
# raise NotImplementedError
301+
return None
270302

271303

272304
class HorizontalList(SimpleList):
@@ -355,6 +387,33 @@ def parse(self, json_obj: JsonObj) -> "ItemList":
355387
return copy.copy(self)
356388

357389

390+
class ItemListV2(PageCategory):
391+
"""A list of items from TIDAL, can be a list of mixes, for example, or a list of
392+
playlists and mixes in some cases."""
393+
394+
items: Optional[List[Any]] = None
395+
396+
def parse(self, json_obj: JsonObj) -> "ItemListV2":
397+
"""Parse a list of items on TIDAL from the pages endpoints.
398+
399+
:param json_obj: The json from TIDAL to be parsed
400+
:return: A copy of the ItemListV2 with a list of items
401+
"""
402+
self.title = json_obj["title"]
403+
item_type = json_obj["type"]
404+
session: Optional["Session"] = None
405+
parse: Optional[Callable[..., Any]] = None
406+
407+
if item_type in self.item_types.keys():
408+
parse = self.item_types[item_type]
409+
else:
410+
raise NotImplementedError("PageType {} not implemented".format(item_type))
411+
412+
self.items = self.request.map_json(json_obj["items"], parse, session)
413+
414+
return copy.copy(self)
415+
416+
358417
class PageLink:
359418
"""A Link to another :class:`.Page` on TIDAL, Call get() to retrieve the Page."""
360419

0 commit comments

Comments
 (0)