diff --git a/tests/test.py b/tests/test.py index 42dd009..841b80b 100644 --- a/tests/test.py +++ b/tests/test.py @@ -41,6 +41,22 @@ class TestYTMusic(unittest.TestCase): # LIBRARY ############### + def test_get_playlists(self): + playlists = youtube_auth.get_playlists() + self.assertGreater(len(playlists), 0) + + def test_get_library_songs(self): + songs = youtube_auth.get_library_songs(); + self.assertGreater(len(songs), 0) + + def test_get_library_albums(self): + albums = youtube_auth.get_library_albums(); + self.assertGreater(len(albums), 0) + + def test_get_library_artists(self): + artists = youtube_auth.get_library_artists(); + self.assertGreater(len(artists), 0) + def test_get_liked_songs(self): songs = youtube_auth.get_liked_songs(100) self.assertGreater(len(songs), 0) @@ -69,10 +85,6 @@ class TestYTMusic(unittest.TestCase): # 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) diff --git a/ytmusicapi/parsers.py b/ytmusicapi/parsers.py index 6c0403a..0f97afc 100644 --- a/ytmusicapi/parsers.py +++ b/ytmusicapi/parsers.py @@ -11,6 +11,7 @@ FRAMEWORK_MUTATIONS = ['frameworkUpdates', 'entityBatchUpdate', 'mutations'] TITLE = ['title', 'runs', 0, 'text'] SUBTITLE = ['subtitle', 'runs', 0, 'text'] SUBTITLE2 = ['subtitle', 'runs', 2, 'text'] +SUBTITLE3 = ['subtitle', 'runs', 4, 'text'] def parse_playlist_items(results, owned=False): songs = [] diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index e220ac4..b96f981 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -390,6 +390,66 @@ class YTMusic: return playlists + def get_library_songs(self, limit=100): + self.__check_auth() + body = {'browseId': 'FEmusic_liked_videos'} + endpoint = 'browse' + response = self.__send_request(endpoint, body) + results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST), 'itemSectionRenderer') + results = nav(results, ITEM_SECTION)['musicShelfRenderer'] + songs = parse_playlist_items(results['contents'][1:]) + + if 'continuations' in results: + request_func = lambda additionalParams: self.__send_request(endpoint, body, additionalParams) + parse_func = lambda contents: parse_playlist_items(contents) + songs.extend(get_continuations(results, 'musicShelfContinuation', 100, limit, request_func, parse_func)) + + return songs + + def get_library_albums(self, upload=False): + self.__check_auth() + if not upload: + body = {'browseId': 'FEmusic_liked_albums'} + + endpoint = 'browse' + response = self.__send_request(endpoint, body) + results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST), 'itemSectionRenderer') + results = nav(results, ITEM_SECTION)['gridRenderer']['items'] + albums = [] + # skip first item ("New Playlist" button) + for result in results: + data = result['musicTwoRowItemRenderer'] + album = {} + album['browseId'] = data['title']['runs'][0]['navigationEndpoint']['browseEndpoint']['browseId'] + album['title'] = nav(data, TITLE) + album['type'] = nav(data, SUBTITLE) + album['artist'] = nav(data, SUBTITLE2) + album['year'] = nav(data, SUBTITLE3) + albums.append(album) + + #todo continuations + + return albums + + def get_library_artists(self): + self.__check_auth() + body = {'browseId': 'FEmusic_library_corpus_artists'} + endpoint = 'browse' + response = self.__send_request(endpoint, body) + results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST), 'itemSectionRenderer') + results = nav(results, ITEM_SECTION)['musicShelfRenderer'] + artists = [] + for result in results['contents']: + data = result['musicResponsiveListItemRenderer'] + browseId = data['navigationEndpoint']['browseEndpoint']['browseId'] + artist = get_item_text(data, 0) + subscribers = get_item_text(data, 1).split(' ')[0] + artists.append({"browseId": browseId, "artist": artist, "subscribers": subscribers}) + + #todo continuations + + return artists + def get_liked_songs(self, limit=1000): """ Gets playlist items for the 'Liked Songs' playlist @@ -683,6 +743,18 @@ class YTMusic: return songs + def get_uploaded_albums(self): + self.__check_auth() + body = {'browseId': 'FEmusic_library_privately_owned_albums'} + endpoint = 'browse' + response = self.__send_request(endpoint, body) + + def get_library_upload_artists(self): + self.__check_auth() + body = {'browseId': 'FEmusic_library_privately_owned_artists'} + endpoint = 'browse' + response = self.__send_request(endpoint, body) + def upload_song(self, filepath): """ Uploads a song to YouTube Music