Add rate_playlist, subscribe_artists, unsubscribe_artists
This commit is contained in:
parent
3692d67df8
commit
42b6c0a19a
4 changed files with 129 additions and 27 deletions
|
|
@ -30,6 +30,9 @@ Library
|
||||||
.. automethod:: YTMusic.get_liked_songs
|
.. automethod:: YTMusic.get_liked_songs
|
||||||
.. automethod:: YTMusic.get_history
|
.. automethod:: YTMusic.get_history
|
||||||
.. automethod:: YTMusic.rate_song
|
.. automethod:: YTMusic.rate_song
|
||||||
|
.. automethod:: YTMusic.rate_playlist
|
||||||
|
.. automethod:: YTMusic.subscribe_artists
|
||||||
|
.. automethod:: YTMusic.unsubscribe_artists
|
||||||
|
|
||||||
Playlists
|
Playlists
|
||||||
---------
|
---------
|
||||||
|
|
|
||||||
|
|
@ -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):
|
def html_to_txt(html_text):
|
||||||
tags = re.findall("<[^>]+>",html_text)
|
tags = re.findall("<[^>]+>",html_text)
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
|
|
|
||||||
|
|
@ -9,25 +9,9 @@ class TestYTMusic(unittest.TestCase):
|
||||||
def test_setup(self):
|
def test_setup(self):
|
||||||
YTMusic.setup()
|
YTMusic.setup()
|
||||||
|
|
||||||
def test_get_playlists(self):
|
###############
|
||||||
playlists = youtube_auth.get_playlists()
|
# BROWSING
|
||||||
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)
|
|
||||||
|
|
||||||
def test_search(self):
|
def test_search(self):
|
||||||
results = youtube_auth.search("Oasis Wonderwall")
|
results = youtube_auth.search("Oasis Wonderwall")
|
||||||
|
|
@ -52,6 +36,54 @@ class TestYTMusic(unittest.TestCase):
|
||||||
self.assertEqual(len(results), 4)
|
self.assertEqual(len(results), 4)
|
||||||
self.assertEqual(len(results['tracks']), 7)
|
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):
|
def test_get_uploaded_songs(self):
|
||||||
results = youtube_auth.get_uploaded_songs(126)
|
results = youtube_auth.get_uploaded_songs(126)
|
||||||
self.assertEqual(len(results), 126)
|
self.assertEqual(len(results), 126)
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,10 @@ class YTMusic:
|
||||||
"""
|
"""
|
||||||
return setup(filepath)
|
return setup(filepath)
|
||||||
|
|
||||||
|
###############
|
||||||
|
# BROWSING
|
||||||
|
###############
|
||||||
|
|
||||||
def search(self, query, filter=None):
|
def search(self, query, filter=None):
|
||||||
"""
|
"""
|
||||||
Search YouTube music
|
Search YouTube music
|
||||||
|
|
@ -346,6 +350,10 @@ class YTMusic:
|
||||||
|
|
||||||
return album
|
return album
|
||||||
|
|
||||||
|
###############
|
||||||
|
# LIBRARY
|
||||||
|
###############
|
||||||
|
|
||||||
def get_playlists(self):
|
def get_playlists(self):
|
||||||
"""
|
"""
|
||||||
Retrieves the content of the 'Library' page
|
Retrieves the content of the 'Library' page
|
||||||
|
|
@ -421,20 +429,64 @@ class YTMusic:
|
||||||
:param rating: One of 'LIKE', 'DISLIKE', 'INDIFFERENT'
|
:param rating: One of 'LIKE', 'DISLIKE', 'INDIFFERENT'
|
||||||
|
|
||||||
| 'INDIFFERENT' removes the previous rating and assigns no rating
|
| 'INDIFFERENT' removes the previous rating and assigns no rating
|
||||||
|
|
||||||
|
:return: Full response
|
||||||
"""
|
"""
|
||||||
self.__check_auth()
|
self.__check_auth()
|
||||||
body = {'target': {'videoId': videoId}}
|
body = {'target': {'videoId': videoId}}
|
||||||
if rating == 'LIKE':
|
endpoint = prepare_like_endpoint(rating)
|
||||||
endpoint = 'like/like'
|
if endpoint is None:
|
||||||
elif rating == 'DISLIKE':
|
|
||||||
endpoint = 'like/dislike'
|
|
||||||
elif rating == 'INDIFFERENT':
|
|
||||||
endpoint = 'like/removelike'
|
|
||||||
else:
|
|
||||||
return
|
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):
|
def get_playlist_items(self, playlistId, limit=1000):
|
||||||
"""
|
"""
|
||||||
|
|
@ -589,6 +641,10 @@ class YTMusic:
|
||||||
response = self.__send_request(endpoint, body)
|
response = self.__send_request(endpoint, body)
|
||||||
return response['status'] if 'status' in response else response
|
return response['status'] if 'status' in response else response
|
||||||
|
|
||||||
|
###############
|
||||||
|
# UPLOADS
|
||||||
|
###############
|
||||||
|
|
||||||
def get_uploaded_songs(self, limit=25):
|
def get_uploaded_songs(self, limit=25):
|
||||||
"""
|
"""
|
||||||
Returns a list of uploaded songs
|
Returns a list of uploaded songs
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue