Skip to content

Commit

Permalink
Fix get_history (#691)
Browse files Browse the repository at this point in the history
* fix get_history feedbackToken parsing

* remove broken playlist test

* skip broken tests for now
  • Loading branch information
sigma67 authored Dec 17, 2024
1 parent 160d266 commit 7ef703c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions tests/auth/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_setup_oauth(self, session_mock, json_mock, blank_code, config):
oauth_file.close()
Path(oauth_file.name).unlink()

@pytest.mark.skip(reason="this test needs to be fixed with the oauth update")
def test_oauth_tokens(self, oauth_filepath: str, yt_oauth: YTMusic):
# ensure instance initialized token
assert yt_oauth._token is not None
Expand Down
4 changes: 3 additions & 1 deletion tests/mixins/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def test_get_saved_episodes(self, yt_brand, yt_empty):
def test_get_history(self, yt_oauth):
songs = yt_oauth.get_history()
assert len(songs) > 0
assert all(song["feedbackToken"] is not None for song in songs)

def test_manipulate_history_items(self, yt_auth, sample_video):
song = yt_auth.get_song(sample_video)
Expand All @@ -113,9 +114,10 @@ def test_rate_song(self, yt_auth, sample_video):
response = yt_auth.rate_song(sample_video, "notexist")
assert not response

@pytest.mark.skip(reason="edit_song_library_status is currently broken due to server-side update")
def test_edit_song_library_status(self, yt_brand, sample_album):
album = yt_brand.get_album(sample_album)
response = yt_brand.edit_song_library_status(album["tracks"][0]["feedbackTokens"]["add"])
response = yt_brand.rate_playlist(album["tracks"][0]["feedbackTokens"]["add"])
album = yt_brand.get_album(sample_album)
assert album["tracks"][0]["inLibrary"]
assert response["feedbackResponses"][0]["isProcessed"]
Expand Down
1 change: 0 additions & 1 deletion tests/mixins/test_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def test_get_playlist_2024(self, yt, test_file, owned):
("RDCLAK5uy_nfjzC9YC1NVPPZHvdoAtKVBOILMDOuxOs", 200, 10),
("PLj4BSJLnVpNyIjbCWXWNAmybc97FXLlTk", 200, 0), # no related tracks
("PL6bPxvf5dW5clc3y9wAoslzqUrmkZ5c-u", 1000, 10), # very large
("PLZ6Ih9wLHQ2Hm2d3Cb0iV48Z2hQjGRyNz", 300, 10), # runs in subtitle, not title
("PL5ZNf-B8WWSZFIvpJWRjgt7iRqWT7_KF1", 10, 10), # track duration > 1k hours
],
)
Expand Down
3 changes: 2 additions & 1 deletion ytmusicapi/mixins/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ def get_history(self) -> list[dict]:
if not data:
error = nav(content, ["musicNotifierShelfRenderer", *TITLE], True)
raise YTMusicServerError(error)
songlist = parse_playlist_items(data)
menu_entries = [[*MENU_SERVICE, *FEEDBACK_TOKEN]]
songlist = parse_playlist_items(data, menu_entries)
for song in songlist:
song["played"] = nav(content["musicShelfRenderer"], TITLE_TEXT)
songs.extend(songlist)
Expand Down
7 changes: 6 additions & 1 deletion ytmusicapi/parsers/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,13 @@ def parse_playlist_item(
song["feedbackTokens"] = feedback_tokens

if menu_entries:
# sets the feedbackToken for get_history
menu_items = nav(data, MENU_ITEMS)
for menu_entry in menu_entries:
song[menu_entry[-1]] = nav(data, MENU_ITEMS + menu_entry)
items = find_objects_by_key(menu_items, menu_entry[0])
song[menu_entry[-1]] = next(
filter(lambda x: x is not None, (nav(itm, menu_entry, True) for itm in items)), None
)

return song

Expand Down

0 comments on commit 7ef703c

Please sign in to comment.