Skip to content

Commit

Permalink
get_album (via parse_playlist_items): add track_position key + tests …
Browse files Browse the repository at this point in the history
…for the badly indexed album
  • Loading branch information
jcbirdwell committed Jan 9, 2024
1 parent 9753528 commit 8e2ab98
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tests/mixins/test_browsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,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_position"] == 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_position"] == 3
assert results["tracks"][13]["track_position"] == 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
8 changes: 8 additions & 0 deletions ytmusicapi/parsers/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def parse_playlist_items(results, menu_entries: Optional[List[List]] = None):
True,
)

track_position = None
if "index" in data:
# similar item nesting to duration above -> both could be refactored into
# a function similar to nav (?)
track_position = int(data["index"]["runs"][0]["text"])

song = {
"videoId": videoId,
"title": title,
Expand All @@ -91,7 +97,9 @@ def parse_playlist_items(results, menu_entries: Optional[List[List]] = None):
"isExplicit": isExplicit,
"videoType": videoType,
"views": views,
"track_position": track_position,
}

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

0 comments on commit 8e2ab98

Please sign in to comment.