Skip to content

Commit

Permalink
get_album: track positions (#510)
Browse files Browse the repository at this point in the history
* get_album (via parse_playlist_items): add track_position key + tests for the badly indexed album

* get_album & parse_playlist_items: nav changes

* parse_playlist_items: only add track_position for albums

* parse_playlist_items & related test_browsing: track_position > track_number

---------

Co-authored-by: sigma67 <[email protected]>
  • Loading branch information
jcbirdwell and sigma67 authored Jan 11, 2024
1 parent b60bcd7 commit ba71fc9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def fixture_sample_album() -> str:
return "MPREb_4pL8gzRtw1p"


@pytest.fixture(name="badly_indexed_album")
def fixture_badly_indexed_album() -> str:
"""Sean Paul - Dutty Classics Collection"""
return "MPREb_TPH4WqN5pUo"


@pytest.fixture(name="sample_video")
def fixture_sample_video() -> str:
"""Oasis - Wonderwall"""
Expand Down
6 changes: 5 additions & 1 deletion tests/mixins/test_browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@ def test_get_album_browse_id_issue_470(self, yt):
escaped_browse_id = yt.get_album_browse_id("OLAK5uy_nbMYyrfeg5ZgknoOsOGBL268hGxtcbnDM")
assert escaped_browse_id == "MPREb_scJdtUCpPE2"

def test_get_album(self, yt, yt_auth, sample_album):
def test_get_album(self, yt, yt_auth, sample_album, badly_indexed_album):
results = yt_auth.get_album(sample_album)
assert len(results) >= 9
assert results["tracks"][0]["isExplicit"]
assert all(item["views"] is not None for item in results["tracks"])
assert all(item["album"] is not None for item in results["tracks"])
assert results["tracks"][0]["track_number"] == 1
assert "feedbackTokens" in results["tracks"][0]
assert len(results["other_versions"]) >= 1 # appears to be regional
results = yt.get_album("MPREb_BQZvl3BFGay")
assert len(results["tracks"]) == 7
assert len(results["tracks"][0]["artists"]) == 1
results = yt.get_album("MPREb_rqH94Zr3NN0")
assert len(results["tracks"][0]["artists"]) == 2
results = yt.get_album(badly_indexed_album) # album with non-standard indexing
assert results["tracks"][0]["track_number"] == 3
assert results["tracks"][13]["track_number"] == 18

def test_get_song(self, config, yt, yt_oauth, sample_video):
song = yt_oauth.get_song(config["uploads"]["private_upload_id"]) # private upload
Expand Down
2 changes: 1 addition & 1 deletion ytmusicapi/mixins/browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def get_album(self, browseId: str) -> Dict:
response = self._send_request(endpoint, body)
album = parse_album_header(response)
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST_ITEM + MUSIC_SHELF)
album["tracks"] = parse_playlist_items(results["contents"])
album["tracks"] = parse_playlist_items(results["contents"], is_album=True)
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST + [1] + CAROUSEL, True)
if results is not None:
album["other_versions"] = parse_content_list(results["contents"], parse_album)
Expand Down
7 changes: 6 additions & 1 deletion ytmusicapi/parsers/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .songs import *


def parse_playlist_items(results, menu_entries: Optional[List[List]] = None):
def parse_playlist_items(results, menu_entries: Optional[List[List]] = None, is_album=False):
songs = []
for result in results:
if MRLIR not in result:
Expand Down Expand Up @@ -92,6 +92,11 @@ def parse_playlist_items(results, menu_entries: Optional[List[List]] = None):
"videoType": videoType,
"views": views,
}

if is_album:
track_idx_found = nav(data, ["index", "runs", 0, "text"], True)
song["track_number"] = track_idx_found if track_idx_found is None else int(track_idx_found)

if duration:
song["duration"] = duration
song["duration_seconds"] = parse_duration(duration)
Expand Down

0 comments on commit ba71fc9

Please sign in to comment.