Skip to content

Commit d209ab7

Browse files
committed
Support new home page endpoint
1 parent 8cbedaf commit d209ab7

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

tidalapi/page.py

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

45+
from . import album, artist, media, mix, playlist
46+
4547
PageCategories = Union[
4648
"Album",
4749
"PageLinks",
@@ -114,6 +116,17 @@ def parse(self, json_obj: JsonObj) -> "Page":
114116

115117
return copy.copy(self)
116118

119+
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."""
123+
self.categories = []
124+
for item in json_obj["items"]:
125+
page_item = self.page_category.parse(item)
126+
self.categories.append(page_item)
127+
128+
return copy.copy(self)
129+
117130
def get(self, endpoint: str, params: Optional[Dict[str, Any]] = None) -> "Page":
118131
"""Retrieve a page from the specified endpoint, overwrites the calling page.
119132
@@ -196,6 +209,10 @@ def parse(self, json_obj: JsonObj) -> AllCategories:
196209
elif category_type == "SOCIAL":
197210
json_obj["items"] = json_obj["socialProfiles"]
198211
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)
199216
else:
200217
raise NotImplementedError(f"PageType {category_type} not implemented")
201218

@@ -215,6 +232,51 @@ def show_more(self) -> Optional[Page]:
215232
)
216233

217234

235+
class SimpleList(PageCategory):
236+
"""A simple list of different items for the home page V2"""
237+
238+
items: Optional[List[Any]] = None
239+
240+
def __init__(self, session: "Session"):
241+
super().__init__(session)
242+
self.session = session
243+
244+
def parse(self, json_obj: JsonObj) -> "SimpleList":
245+
self.items = []
246+
self.title = json_obj["title"]
247+
248+
for item in json_obj["items"]:
249+
self.items.append(self.get_item(item))
250+
251+
return self
252+
253+
def get_item(self, json_obj):
254+
item_type = json_obj["type"]
255+
item_data = json_obj["data"]
256+
257+
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
270+
271+
272+
class HorizontalList(SimpleList):
273+
...
274+
275+
276+
class ShortcutList(SimpleList):
277+
...
278+
279+
218280
class FeaturedItems(PageCategory):
219281
"""Items that have been featured by TIDAL."""
220282

tidalapi/session.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,19 @@ def home(self) -> page.Page:
10941094
10951095
:return: A :class:`.Page` object with the :class:`.PageCategory` list from the home page
10961096
"""
1097-
return self.page.get("pages/home")
1097+
params = {}
1098+
params["deviceType"] = "BROWSER"
1099+
params["countryCode"] = "IT"
1100+
params["locale"] = "en_US"
1101+
params["platform"] = "WEB"
1102+
1103+
json_obj = self.request.request(
1104+
"GET",
1105+
"home/feed/static",
1106+
base_url=self.config.api_v2_location,
1107+
params=params,
1108+
).json()
1109+
return self.page.parseV2(json_obj)
10981110

10991111
def explore(self) -> page.Page:
11001112
"""

0 commit comments

Comments
 (0)