From dad63116df6fa8d8d772f2590e58dc8f8ec20e9d Mon Sep 17 00:00:00 2001 From: sigma67 Date: Thu, 27 Aug 2020 12:28:17 +0200 Subject: [PATCH] Feature: remove_history_items (closes #31) --- docs/source/reference.rst | 1 + tests/test.py | 6 ++++++ ytmusicapi/mixins/library.py | 20 ++++++++++++++++++-- ytmusicapi/parsers/playlists.py | 7 ++++++- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/docs/source/reference.rst b/docs/source/reference.rst index 2ce53f3..9d9d26e 100644 --- a/docs/source/reference.rst +++ b/docs/source/reference.rst @@ -41,6 +41,7 @@ Library .. automethod:: YTMusic.get_library_albums .. automethod:: YTMusic.get_liked_songs .. automethod:: YTMusic.get_history +.. automethod:: YTMusic.remove_history_items .. automethod:: YTMusic.rate_song .. automethod:: YTMusic.rate_playlist .. automethod:: YTMusic.subscribe_artists diff --git a/tests/test.py b/tests/test.py index 41db66c..af84d30 100644 --- a/tests/test.py +++ b/tests/test.py @@ -129,6 +129,12 @@ class TestYTMusic(unittest.TestCase): songs = youtube_auth.get_history() self.assertGreater(len(songs), 0) + @unittest.skip + def test_remove_history_items(self): + songs = youtube_auth.get_history() + response = youtube_auth.remove_history_items([songs[0]['feedbackToken'], songs[1]['feedbackToken']]) + self.assertIn('feedbackResponses', response) + def test_rate_song(self): response = youtube_auth.rate_song('ZrOKjDZOtkA', 'LIKE') self.assertIn('actions', response) diff --git a/ytmusicapi/mixins/library.py b/ytmusicapi/mixins/library.py index 09e81b2..0ce52e1 100644 --- a/ytmusicapi/mixins/library.py +++ b/ytmusicapi/mixins/library.py @@ -154,7 +154,8 @@ class LibraryMixin: Gets your play history in reverse chronological order :return: List of playlistItems, see :py:func:`get_playlist` - The additional property 'played' indicates when the playlistItem was played + The additional property ``played`` indicates when the playlistItem was played + The additional property ``feedbackToken`` can be used to remove items with :py:func:`remove_history_item` """ self._check_auth() body = {'browseId': 'FEmusic_history'} @@ -164,13 +165,28 @@ class LibraryMixin: songs = [] for content in results: data = content['musicShelfRenderer']['contents'] - songlist = parse_playlist_items(data) + menu_entries = [[-1] + MENU_SERVICE + ['feedbackEndpoint', 'feedbackToken']] + songlist = parse_playlist_items(data, menu_entries) for song in songlist: song['played'] = nav(content['musicShelfRenderer'], TITLE_TEXT) songs.extend(songlist) return songs + def remove_history_items(self, feedbackTokens: List[str]) -> Dict: # pragma: no cover + """ + Remove an item from the account's history. This method does currently not work with brand accounts + + :param feedbackTokens: Token to identify the item to remove, obtained from :py:func:`get_history` + :return: Full response + """ + self._check_auth() + body = {'feedbackTokens': feedbackTokens} + endpoint = 'feedback' + response = self._send_request(endpoint, body) + + return response + def rate_song(self, videoId: str, rating: str = 'INDIFFERENT') -> Dict: """ Rates a song ("thumbs up"/"thumbs down" interactions on YouTube Music) diff --git a/ytmusicapi/parsers/playlists.py b/ytmusicapi/parsers/playlists.py index 2afefff..5d0426f 100644 --- a/ytmusicapi/parsers/playlists.py +++ b/ytmusicapi/parsers/playlists.py @@ -1,7 +1,8 @@ from .utils import * +from typing import List -def parse_playlist_items(results): +def parse_playlist_items(results, menu_entries: List[List] = None): songs = [] count = 1 for result in results: @@ -65,6 +66,10 @@ def parse_playlist_items(results): if setVideoId: song['setVideoId'] = setVideoId + if menu_entries: + for menu_entry in menu_entries: + song[menu_entry[-1]] = nav(data, MENU_ITEMS + menu_entry) + songs.append(song) except Exception as e: