Add rate_playlist, subscribe_artists, unsubscribe_artists

This commit is contained in:
sigma67 2020-04-22 18:59:51 +02:00
commit 42b6c0a19a
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
4 changed files with 129 additions and 27 deletions

View file

@ -30,6 +30,9 @@ Library
.. automethod:: YTMusic.get_liked_songs
.. automethod:: YTMusic.get_history
.. automethod:: YTMusic.rate_song
.. automethod:: YTMusic.rate_playlist
.. automethod:: YTMusic.subscribe_artists
.. automethod:: YTMusic.unsubscribe_artists
Playlists
---------

View file

@ -17,6 +17,17 @@ def prepare_browse_endpoint(type, browseId):
}
def prepare_like_endpoint(rating):
if rating == 'LIKE':
return 'like/like'
elif rating == 'DISLIKE':
return 'like/dislike'
elif rating == 'INDIFFERENT':
return 'like/removelike'
else:
return None
def html_to_txt(html_text):
tags = re.findall("<[^>]+>",html_text)
for tag in tags:

View file

@ -9,25 +9,9 @@ class TestYTMusic(unittest.TestCase):
def test_setup(self):
YTMusic.setup()
def test_get_playlists(self):
playlists = youtube_auth.get_playlists()
self.assertGreater(len(playlists), 0)
def test_get_foreign_playlist(self):
songs = youtube.get_playlist_items("PLw-VjHDlEOgs658kAHR_LAaILBXb-s6Q5")
self.assertEqual(len(songs), 200)
def test_get_owned_playlist(self):
songs = youtube_auth.get_playlist_items('PL528pVfw3ao2VzfY6zE1TOZm1cBSdk7Q0')
self.assertEqual(len(songs), 287)
def test_get_liked_songs(self):
songs = youtube_auth.get_liked_songs(100)
self.assertGreater(len(songs), 0)
def test_get_history(self):
songs = youtube_auth.get_history()
self.assertGreater(len(songs), 0)
###############
# BROWSING
###############
def test_search(self):
results = youtube_auth.search("Oasis Wonderwall")
@ -52,6 +36,54 @@ class TestYTMusic(unittest.TestCase):
self.assertEqual(len(results), 4)
self.assertEqual(len(results['tracks']), 7)
###############
# LIBRARY
###############
def test_get_liked_songs(self):
songs = youtube_auth.get_liked_songs(100)
self.assertGreater(len(songs), 0)
def test_get_history(self):
songs = youtube_auth.get_history()
self.assertGreater(len(songs), 0)
def test_rate_song(self):
response = youtube_auth.rate_song('ZrOKjDZOtkA', 'LIKE')
self.assertIn('actions', response)
response = youtube_auth.rate_song('ZrOKjDZOtkA', 'INDIFFERENT')
self.assertIn('actions', response)
def test_rate_playlist(self):
response = youtube_auth.rate_playlist('OLAK5uy_l3g4WcHZsEx_QuEDZzWEiyFzZl6pL0xZ4', 'DISLIKE')
self.assertIn('actions', response)
response = youtube_auth.rate_playlist('OLAK5uy_l3g4WcHZsEx_QuEDZzWEiyFzZl6pL0xZ4', 'INDIFFERENT')
self.assertIn('actions', response)
def test_subscribe_artists(self):
youtube_auth.subscribe_artists(['UCmMUZbaYdNH0bEd1PAlAqsA', 'UCEPMVbUzImPl4p8k4LkGevA'])
youtube_auth.unsubscribe_artists(['UCmMUZbaYdNH0bEd1PAlAqsA', 'UCEPMVbUzImPl4p8k4LkGevA'])
###############
# PLAYLISTS
###############
def test_get_playlists(self):
playlists = youtube_auth.get_playlists()
self.assertGreater(len(playlists), 0)
def test_get_foreign_playlist(self):
songs = youtube.get_playlist_items("PLw-VjHDlEOgs658kAHR_LAaILBXb-s6Q5")
self.assertEqual(len(songs), 200)
def test_get_owned_playlist(self):
songs = youtube_auth.get_playlist_items('PL528pVfw3ao2VzfY6zE1TOZm1cBSdk7Q0')
self.assertEqual(len(songs), 287)
###############
# UPLOADS
###############
def test_get_uploaded_songs(self):
results = youtube_auth.get_uploaded_songs(126)
self.assertEqual(len(results), 126)

View file

@ -73,6 +73,10 @@ class YTMusic:
"""
return setup(filepath)
###############
# BROWSING
###############
def search(self, query, filter=None):
"""
Search YouTube music
@ -346,6 +350,10 @@ class YTMusic:
return album
###############
# LIBRARY
###############
def get_playlists(self):
"""
Retrieves the content of the 'Library' page
@ -421,20 +429,64 @@ class YTMusic:
:param rating: One of 'LIKE', 'DISLIKE', 'INDIFFERENT'
| 'INDIFFERENT' removes the previous rating and assigns no rating
:return: Full response
"""
self.__check_auth()
body = {'target': {'videoId': videoId}}
if rating == 'LIKE':
endpoint = 'like/like'
elif rating == 'DISLIKE':
endpoint = 'like/dislike'
elif rating == 'INDIFFERENT':
endpoint = 'like/removelike'
else:
endpoint = prepare_like_endpoint(rating)
if endpoint is None:
return
self.__send_request(endpoint, body)
return self.__send_request(endpoint, body)
def rate_playlist(self, playlistId, rating='INDIFFERENT'):
"""
Rates a playlist/album ("Add to library"/"Remove from library" interactions on YouTube Music)
You can also dislike a playlist/album, which has an effect on your recommendations
:param videoId: Video id
:param rating: One of 'LIKE', 'DISLIKE', 'INDIFFERENT'
| 'INDIFFERENT' removes the playlist/album from the library
:return: Full response
"""
self.__check_auth()
body = {'target': {'playlistId': playlistId}}
endpoint = prepare_like_endpoint(rating)
if endpoint is None:
return
return self.__send_request(endpoint, body)
def subscribe_artists(self, channelIds: list) -> dict:
"""
Subscribe to artists. Adds the artists to your libary
:param channelIds: Artist channel ids
:return: Full response
"""
self.__check_auth()
body = {'channelIds': channelIds}
endpoint = 'subscription/subscribe'
return self.__send_request(endpoint, body)
def unsubscribe_artists(self, channelIds: list) -> dict:
"""
Unsubscribe from artists. Removes the artists from your libary
:param channelIds: Artist channel ids
:return: Full response
"""
self.__check_auth()
body = {'channelIds': channelIds}
endpoint = 'subscription/unsubscribe'
return self.__send_request(endpoint, body)
###############
# PLAYLISTS
###############
def get_playlist_items(self, playlistId, limit=1000):
"""
@ -589,6 +641,10 @@ class YTMusic:
response = self.__send_request(endpoint, body)
return response['status'] if 'status' in response else response
###############
# UPLOADS
###############
def get_uploaded_songs(self, limit=25):
"""
Returns a list of uploaded songs