Feature: remove_history_items (closes #31)

This commit is contained in:
sigma67 2020-08-27 12:28:17 +02:00
commit dad63116df
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
4 changed files with 31 additions and 3 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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: