Skip to content

Commit

Permalink
get_charts: removed trending category (no longer available on YTM)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Feb 4, 2024
1 parent cc26a63 commit da492e4
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion tests/mixins/test_browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class TestBrowsing:
def test_get_home(self, yt, yt_auth):
result = yt.get_home()
assert len(result) == 2
assert len(result) >= 2
result = yt_auth.get_home(limit=15)
assert len(result) >= 15

Expand Down
7 changes: 4 additions & 3 deletions tests/mixins/test_explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ def test_get_mood_playlists(self, yt):

def test_get_charts(self, yt, yt_oauth):
charts = yt_oauth.get_charts()
# songs section appears to be removed currently (US)
assert len(charts) >= 3
assert len(charts) == 4
charts = yt.get_charts(country="US")
assert len(charts) == 4
charts = yt_oauth.get_charts(country="US")
assert len(charts) == 5
charts = yt.get_charts(country="BE")
assert len(charts) == 4
assert len(charts) == 3
2 changes: 1 addition & 1 deletion tests/mixins/test_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_end2end(self, config, yt_brand, sample_video):
)
assert response["status"] == "STATUS_SUCCEEDED", "Adding playlist item failed"
assert len(response["playlistEditResults"]) > 0, "Adding playlist item failed"
time.sleep(3)
time.sleep(5)
yt_brand.edit_playlist(playlist_id, addToTop=False)
playlist = yt_brand.get_playlist(playlist_id, related=True)
assert len(playlist["tracks"]) == 46, "Getting playlist items failed"
Expand Down
2 changes: 1 addition & 1 deletion tests/setup/setup_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

def populate_account():
"""idempotent requests to populate an account"""
# library
# subscribe to some artists
playlist_id = "RDCLAK5uy_l9ex2d91-Qb1i-W7d0MLCEl_ZjRXss0Dk" # fixed playlist with many artists
yt_playlist = yt_brand.get_playlist(playlist_id)
artists = [track["artists"] for track in yt_playlist["tracks"]]
Expand Down
4 changes: 3 additions & 1 deletion ytmusicapi/mixins/_protocol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""protocol that defines the functions available to mixins"""
from typing import Dict, Optional, Protocol
from typing import Dict, Optional, Protocol, Union

from requests import Response

Expand All @@ -10,6 +10,8 @@
class MixinProtocol(Protocol):
"""protocol that defines the functions available to mixins"""

auth: Optional[Union[str, Dict]]

auth_type: AuthType

parser: Parser
Expand Down
4 changes: 3 additions & 1 deletion ytmusicapi/mixins/browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ def get_album_browse_id(self, audioPlaylistId: str) -> Optional[str]:
params = {"list": audioPlaylistId}
response = self._send_get_request(YTM_DOMAIN + "/playlist", params)

with warnings.catch_warnings(action="ignore", category=DeprecationWarning):
with warnings.catch_warnings():
# merge this with statement with catch_warnings on Python>=3.11
warnings.simplefilter(action="ignore", category=DeprecationWarning)
decoded = response.text.encode("utf8").decode("unicode_escape")

matches = re.search(r"\"MPRE.+?\"", decoded)
Expand Down
8 changes: 1 addition & 7 deletions ytmusicapi/mixins/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,15 @@ def get_charts(self, country: str = "ZZ") -> Dict:
charts_categories = ["videos", "artists"]

has_genres = country == "US"
has_trending = country != "ZZ"

# use result length to determine if songs category is present
# could also be done via an is_premium attribute on YTMusic instance
has_songs = (len(results) - 1) > (len(charts_categories) + has_genres + has_trending)
has_songs = (len(results) - 1) > (len(charts_categories) + has_genres)

if has_songs:
charts_categories.insert(0, "songs")
if has_genres:
charts_categories.append("genres")
if has_trending:
charts_categories.append("trending")

parse_chart = lambda i, parse_func, key: parse_content_list(
nav(results[i + has_songs], CAROUSEL_CONTENTS), parse_func, key
Expand All @@ -250,7 +247,4 @@ def get_charts(self, country: str = "ZZ") -> Dict:
if has_genres:
charts["genres"] = parse_chart(3, parse_playlist, MTRIR)

if has_trending:
charts["trending"]["items"] = parse_chart(3 + has_genres, parse_chart_trending, MRLIR)

return charts

0 comments on commit da492e4

Please sign in to comment.