Skip to content

Commit

Permalink
improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Feb 4, 2024
1 parent da492e4 commit f98274f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
4 changes: 4 additions & 0 deletions tests/mixins/test_browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def test_get_album(self, yt, yt_auth, sample_album):
album = yt.get_album("MPREb_YuigcYm2erf") # album with track (#8) disabled/greyed out
assert album["tracks"][7]["trackNumber"] is None

def test_get_album_errors(self, yt):
with pytest.raises(Exception, match="Invalid album browseId"):
yt.get_album("asdf")

def test_get_album_other_versions(self, yt):
# Eminem - Curtain Call: The Hits (Explicit Variant)
album = yt.get_album("MPREb_LQCAymzbaKJ")
Expand Down
7 changes: 7 additions & 0 deletions tests/mixins/test_watch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


class TestWatch:
def test_get_watch_playlist(self, config, yt, yt_brand, yt_oauth):
playlist = yt_oauth.get_watch_playlist(
Expand All @@ -14,3 +17,7 @@ def test_get_watch_playlist(self, config, yt, yt_brand, yt_oauth):
assert len(playlist["tracks"]) == config.getint("albums", "album_track_length")
playlist = yt_brand.get_watch_playlist(playlistId=config["playlists"]["own"], shuffle=True)
assert len(playlist["tracks"]) == config.getint("playlists", "own_length")

def test_get_watch_playlist_errors(self, config, yt):
with pytest.raises(Exception, match="No content returned by the server"):
yt.get_watch_playlist(playlistId="PL_NOT_EXIST")
11 changes: 2 additions & 9 deletions ytmusicapi/parsers/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,12 @@ def get_fixed_column_item(item, index):
return item["fixedColumns"][index]["musicResponsiveListItemFixedColumnRenderer"]


def get_browse_id(item, index):
if "navigationEndpoint" not in item["text"]["runs"][index]:
return None
else:
return nav(item["text"]["runs"][index], NAVIGATION_BROWSE_ID)


def get_dot_separator_index(runs):
index = len(runs)
try:
index = runs.index({"text": " • "})
except ValueError:
len(runs)
index = len(runs)

return index


Expand Down
16 changes: 0 additions & 16 deletions ytmusicapi/parsers/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ def parse_chart_artist(data):
return parsed


def parse_chart_trending(data):
flex_0 = get_flex_column_item(data, 0)
artists = parse_song_artists(data, 1)
index = get_dot_separator_index(artists)
# last item is views for some reason
views = None if index == len(artists) else artists.pop()["name"].split(" ")[0]
return {
"title": nav(flex_0, TEXT_RUN_TEXT),
"videoId": nav(flex_0, TEXT_RUN + NAVIGATION_VIDEO_ID, True),
"playlistId": nav(flex_0, TEXT_RUN + NAVIGATION_PLAYLIST_ID, True),
"artists": artists,
"thumbnails": nav(data, THUMBNAILS),
"views": views,
}


def parse_ranking(data):
return {
"rank": nav(data, ["customIndexColumn", "musicCustomIndexColumnRenderer", *TEXT_RUN_TEXT]),
Expand Down
3 changes: 2 additions & 1 deletion ytmusicapi/parsers/songs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def parse_song_runs(runs):

def parse_song_album(data, index):
flex_item = get_flex_column_item(data, index)
return None if not flex_item else {"name": get_item_text(data, index), "id": get_browse_id(flex_item, 0)}
browse_id = nav(flex_item, TEXT_RUN + NAVIGATION_BROWSE_ID, True)
return None if not flex_item else {"name": get_item_text(data, index), "id": browse_id}


def parse_song_library_status(item) -> bool:
Expand Down

0 comments on commit f98274f

Please sign in to comment.