From 42b6c0a19a12e5d5fb56b3a8f0f4edc3240ec8a4 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Wed, 22 Apr 2020 18:59:51 +0200 Subject: [PATCH] Add rate_playlist, subscribe_artists, unsubscribe_artists --- docs/source/reference.rst | 3 ++ ytmusicapi/helpers.py | 11 ++++++ ytmusicapi/test.py | 70 ++++++++++++++++++++++++++----------- ytmusicapi/ytmusic.py | 72 ++++++++++++++++++++++++++++++++++----- 4 files changed, 129 insertions(+), 27 deletions(-) diff --git a/docs/source/reference.rst b/docs/source/reference.rst index aeae9cd..ed5e95a 100644 --- a/docs/source/reference.rst +++ b/docs/source/reference.rst @@ -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 --------- diff --git a/ytmusicapi/helpers.py b/ytmusicapi/helpers.py index 36625aa..6bf722c 100644 --- a/ytmusicapi/helpers.py +++ b/ytmusicapi/helpers.py @@ -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: diff --git a/ytmusicapi/test.py b/ytmusicapi/test.py index 94525fe..da29015 100644 --- a/ytmusicapi/test.py +++ b/ytmusicapi/test.py @@ -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) diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index 75c799d..22fd388 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -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