From 4a653c52850bca2fa4c4c7413a2a63d7c9843055 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Fri, 17 Apr 2020 00:42:39 +0200 Subject: [PATCH] Add get_artist_albums (closes #4) This commit also circumvents an issue with get_artist, where the API sometimes fails to return songs. --- ytmusicapi/context.json | 5 ---- ytmusicapi/headers.json | 3 ++- ytmusicapi/helpers.py | 4 ++-- ytmusicapi/test.py | 16 ++++++++++++- ytmusicapi/ytmusic.py | 53 ++++++++++++++++++++++++++++++++++------- 5 files changed, 64 insertions(+), 17 deletions(-) diff --git a/ytmusicapi/context.json b/ytmusicapi/context.json index ff02c9f..b2ddc7b 100644 --- a/ytmusicapi/context.json +++ b/ytmusicapi/context.json @@ -1,10 +1,5 @@ { "context": { - "activePlayers": [ - { - "playerContextParams": "Q0FFU0FnZ0I=" - } - ], "capabilities": {}, "client": { "clientName": "WEB_REMIX", diff --git a/ytmusicapi/headers.json b/ytmusicapi/headers.json index a49c6ec..c464706 100644 --- a/ytmusicapi/headers.json +++ b/ytmusicapi/headers.json @@ -4,5 +4,6 @@ "Accept-Language": "en-US,en;q=0.5", "Content-Type": "application/json", "X-Goog-AuthUser": "0", - "x-origin": "https://music.youtube.com" + "x-origin": "https://music.youtube.com", + "X-Goog-Visitor-Id": "CgtvVTcxa1EtbV9hayiMu-P0BQ%3D%3D" } \ No newline at end of file diff --git a/ytmusicapi/helpers.py b/ytmusicapi/helpers.py index 26f899d..d6b05b4 100644 --- a/ytmusicapi/helpers.py +++ b/ytmusicapi/helpers.py @@ -131,11 +131,11 @@ def parse_search_result(data, resultType = None): return search_result -def get_item_text(item, index): +def get_item_text(item, index, run_index=0): if 'runs' not in item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']['text']: return None - return item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text'] + return item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][run_index]['text'] def prepare_browse_endpoint(type, browseId): diff --git a/ytmusicapi/test.py b/ytmusicapi/test.py index e1cd047..d836fe1 100644 --- a/ytmusicapi/test.py +++ b/ytmusicapi/test.py @@ -21,6 +21,10 @@ class TestYTMusic(unittest.TestCase): songs = youtube_auth.get_playlist_items('PLQwVIlKxHM6oCeKLTsUx_w9CmWjHKdg68') self.assertEqual(len(songs), 64) + 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) @@ -31,7 +35,17 @@ class TestYTMusic(unittest.TestCase): def test_get_artist(self): results = youtube.get_artist("UCmMUZbaYdNH0bEd1PAlAqsA") - self.assertEqual(len(results), 4) + self.assertEqual(len(results), 7) + + def test_get_artist_albums(self): + artist = youtube.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg") + results = youtube.get_artist_albums(artist['albums']['browseId'], artist['albums']['params']) + self.assertGreater(len(results), 0) + + def test_get_artist_singles(self): + artist = youtube_auth.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg") + results = youtube_auth.get_artist_albums(artist['singles']['browseId'], artist['singles']['params']) + self.assertGreater(len(results), 0) def test_get_album(self): results = youtube.get_album("MPREb_BQZvl3BFGay") diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index 1ba8e52..daaa059 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -165,11 +165,14 @@ class YTMusic: def get_artist(self, channelId): """ - Get data about an artist + Get information about an artist and their top releases (songs, + albums, singles and videos). The top lists contain pointers + for getting the full list of releases. For songs/videos, pass + the browseId to :py:func:`get_playlist_items`. For albums/singles, + pass browseId and params to :py:func:`get_artist_albums`. :param channelId: channel id of the artist - :return: Detailed information about the artist and - their songs, albums, singles and videos. Example:: + :return: Dictionary with requested information. Example:: { "name": "Oasis", @@ -230,8 +233,9 @@ class YTMusic: artist['description'] = results[-1]['musicDescriptionShelfRenderer']['description']['runs'][0]['text'] artist['views'] = results[-1]['musicDescriptionShelfRenderer']['subheader']['runs'][0]['text'].split(' ')[0] artist['songs'] = {} - artist['songs']['browseId'] = results[0]['musicShelfRenderer']['title']['runs'][0]['navigationEndpoint']['browseEndpoint']['browseId'] - artist['songs']['results'] = parse_playlist_items(results[0]['musicShelfRenderer']['contents']) + if 'musicShelfRenderer' in results[0]: # API sometimes does not return songs + artist['songs']['browseId'] = results[0]['musicShelfRenderer']['title']['runs'][0]['navigationEndpoint']['browseEndpoint']['browseId'] + artist['songs']['results'] = parse_playlist_items(results[0]['musicShelfRenderer']['contents']) categories = ['albums', 'singles', 'videos'] for category in categories: @@ -261,10 +265,41 @@ class YTMusic: return artist def get_artist_albums(self, channelId, params): + """ + Get the full list of an artist's albums or singles + + :param channelId: channel Id of the artist + :param params: params obtained by :py:func:`get_artist` + :return: List of albums or singles + + Example:: + + { + "browseId": "MPREb_0rtvKhqeCY0", + "artist": "Armin van Buuren", + "title": "This I Vow (feat. Mila Josef)", + "type": "EP", + "year": "2020" + } + + """ body = {"browseId": channelId, "params": params} endpoint = 'browse' response = self.__send_request(endpoint, body) - pass + artist = response['header']['musicHeaderRenderer']['title']['runs'][0]['text'] + results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST) + albums = [] + musicShelf = results[0]['musicShelfRenderer'] + release_type = musicShelf['title']['runs'][0]['text'].lower() + for result in musicShelf['contents']: + data = result['musicResponsiveListItemRenderer'] + browseId = data['navigationEndpoint']['browseEndpoint']['browseId'] + title = get_item_text(data, 0) + album_type = get_item_text(data, 1) if release_type == "albums" else "Single" + year = get_item_text(data, 1, 2) if release_type == "albums" else get_item_text(data, 1) + albums.append({"browseId": browseId, "artist": artist, "title": title, "type": album_type, "year": year}) + + return albums def get_album(self, browseId): """ @@ -299,7 +334,8 @@ class YTMusic: track['index'] = item['payload']['musicTrack']['albumTrackIndex'] track['title'] = item['payload']['musicTrack']['title'] track['artists'] = item['payload']['musicTrack']['artistNames'] - track['videoId'] = item['payload']['musicTrack']['videoId'] + # in case the song is unavailable, there is no videoId + track['videoId'] = item['payload']['musicTrack']['videoId'] if 'videoId' in item['payload']['musicTrack'] else None track['lengthMs'] = item['payload']['musicTrack']['lengthMs'] album['tracks'].append(track) @@ -411,7 +447,8 @@ class YTMusic: needed for moving/removing playlist items """ - body = prepare_browse_endpoint("PLAYLIST", "VL" + playlistId) + browseId = "VL" + playlistId if not playlistId.startswith("VL") else playlistId + body = prepare_browse_endpoint("PLAYLIST", browseId) endpoint = 'browse' response = self.__send_request(endpoint, body) results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)[0]['musicPlaylistShelfRenderer']