From 1862c84c3944363aaaaa6195326eec9b51fad7bf Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sun, 31 May 2020 22:11:21 +0200 Subject: [PATCH] Return thumbnails with songs, albums and artists (closes #27) --- tests/test.py | 4 ++-- ytmusicapi/parsers.py | 27 ++++++++++++++++++++------- ytmusicapi/ytmusic.py | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/tests/test.py b/tests/test.py index 866fb65..c9a4a9a 100644 --- a/tests/test.py +++ b/tests/test.py @@ -41,7 +41,7 @@ class TestYTMusic(unittest.TestCase): def test_get_artist(self): results = youtube.get_artist("UCmMUZbaYdNH0bEd1PAlAqsA") - self.assertEqual(len(results), 7) + self.assertEqual(len(results), 8) def test_get_artist_albums(self): artist = youtube.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg") @@ -57,7 +57,7 @@ class TestYTMusic(unittest.TestCase): def test_get_album(self): results = youtube_auth.get_album("MPREb_BQZvl3BFGay") - self.assertEqual(len(results), 8) + self.assertEqual(len(results), 9) self.assertEqual(len(results['tracks']), 7) ############### diff --git a/ytmusicapi/parsers.py b/ytmusicapi/parsers.py index e0995bf..67f14a1 100644 --- a/ytmusicapi/parsers.py +++ b/ytmusicapi/parsers.py @@ -6,10 +6,9 @@ SECTION_LIST = ['sectionListRenderer', 'contents'] ITEM_SECTION = ['itemSectionRenderer', 'contents', 0] MUSIC_SHELF = [0, 'musicShelfRenderer'] CONTINUATION = ['continuations', 0, 'nextContinuationData', 'continuation'] -MENU_ITEMS = ['menu', 'menuRenderer', 'items'] -MENU_LIKE_STATUS = [ - 'menu', 'menuRenderer', 'topLevelButtons', 0, 'likeButtonRenderer', 'likeStatus' -] +MENU = ['menu', 'menuRenderer'] +MENU_ITEMS = MENU + ['items'] +MENU_LIKE_STATUS = MENU + ['topLevelButtons', 0, 'likeButtonRenderer', 'likeStatus'] MENU_SERVICE = ['menuServiceItemRenderer', 'serviceEndpoint'] PLAY_BUTTON = [ 'overlay', 'musicItemThumbnailOverlayRenderer', 'content', 'musicPlayButtonRenderer' @@ -24,6 +23,9 @@ TITLE_TEXT = ['title', 'runs', 0, 'text'] SUBTITLE = ['subtitle', 'runs', 0, 'text'] SUBTITLE2 = ['subtitle', 'runs', 2, 'text'] SUBTITLE3 = ['subtitle', 'runs', 4, 'text'] +THUMBNAILS = ['thumbnail', 'musicThumbnailRenderer', 'thumbnail', 'thumbnails'] +THUMBNAIL_RENDERER = ['thumbnailRenderer', 'musicThumbnailRenderer', 'thumbnail', 'thumbnails'] +THUMBNAIL_CROPPED = ['thumbnail', 'croppedSquareThumbnailRenderer', 'thumbnail', 'thumbnails'] def parse_playlists(results): @@ -33,6 +35,7 @@ def parse_playlists(results): playlist = {} playlist['playlistId'] = nav(data, TITLE + NAVIGATION_BROWSE_ID)[2:] playlist['title'] = nav(data, TITLE_TEXT) + playlist['thumbnails'] = nav(data, THUMBNAIL_RENDERER) if len(data['subtitle']['runs']) == 3: playlist['count'] = nav(data, SUBTITLE2).split(' ')[0] @@ -54,6 +57,7 @@ def parse_artists(results, uploaded=False): subtitle = get_item_text(data, 1) if subtitle: artist['subscribers'] = subtitle.split(' ')[0] + artist['thumbnails'] = nav(data, THUMBNAILS) artists.append(artist) return artists @@ -67,6 +71,7 @@ def parse_albums(results): album['browseId'] = nav(data, TITLE + NAVIGATION_BROWSE_ID) album['title'] = nav(data, TITLE_TEXT) album['type'] = nav(data, SUBTITLE) + album['thumbnails'] = nav(data, THUMBNAIL_RENDERER) album['artists'] = [] run_count = len(data['subtitle']['runs']) has_artists = False @@ -137,15 +142,20 @@ def parse_playlist_items(results): 'musicResponsiveListItemFixedColumnRenderer']['text']['runs'][0]['text'] like = None - if 'menu' in data: + if 'menu' in data and 'topLevelButtons' in nav(data, MENU): like = nav(data, MENU_LIKE_STATUS) + thumbnails = None + if 'thumbnail' in data: + thumbnails = nav(data, THUMBNAILS) + song = { 'videoId': videoId, 'title': title, 'artists': artists, 'album': album, - 'likeStatus': like + 'likeStatus': like, + 'thumbnails': thumbnails } if duration: song['duration'] = duration @@ -175,13 +185,15 @@ def parse_uploaded_items(results): title = get_item_text(data, 0) like = nav(data, MENU_LIKE_STATUS) + thumbnails = nav(data, THUMBNAILS) if 'thumbnail' in data else None song = { 'entityId': entityId, 'videoId': videoId, 'title': title, 'artist': None, 'album': None, - 'likeStatus': like + 'likeStatus': like, + 'thumbnails': thumbnails } if get_flex_column_item(data, 1): song['artist'] = parse_song_artists(data, 1) @@ -235,6 +247,7 @@ def parse_search_result(data, resultType=None): search_result['views'] = get_item_text(data, 2 + default).split(' ')[0] search_result['duration'] = get_item_text(data, 3 + default) + search_result['thumbnails'] = nav(data, THUMBNAILS) search_result['resultType'] = resultType return search_result diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index b03aefe..aacf213 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -124,6 +124,7 @@ class YTMusic: "id": "MPREb_9nqEki4ZDpp" }, "duration": "4:19", + "thumbnails": [...], "resultType": "song" } ] @@ -205,12 +206,14 @@ class YTMusic: "name": "Oasis", "description": "Oasis were ...", "views": "1838795605", + "thumbnails": [...], "songs": { "browseId": "VLPLMpM3Z0118S42R1npOhcjoakLIv1aqnS1", "results": [ { "videoId": "ZrOKjDZOtkA", "title": "Wonderwall (Remastered)", + "thumbnails": [...], "artist": "Oasis", "album": "(What's The Story) Morning Glory? (Remastered)" } @@ -220,6 +223,7 @@ class YTMusic: "results": [ { "title": "Familiar To Millions", + "thumbnails": [...], "year": "2018", "browseId": "MPREb_AYetWMZunqA" } @@ -231,6 +235,7 @@ class YTMusic: "results": [ { "title": "Stand By Me (Mustique Demo)", + "thumbnails": [...], "year": "2016", "browseId": "MPREb_7MPKLhibN5G" } @@ -242,6 +247,7 @@ class YTMusic: "results": [ { "title": "Wonderwall", + "thumbnails": [...], "views": "358M", "videoId": "bx1Bh8ZvH84", "playlistId": "PLMpM3Z0118S5xuNckw1HUcj1D021AnMEB" @@ -264,6 +270,7 @@ class YTMusic: if descriptionShelf: artist['description'] = descriptionShelf['description']['runs'][0]['text'] artist['views'] = to_int(descriptionShelf['subheader']['runs'][0]['text']) + artist['thumbnails'] = nav(response['header']['musicImmersiveHeaderRenderer'], THUMBNAILS) artist['songs'] = {'browseId': None} if 'musicShelfRenderer' in results[0]: # API sometimes does not return songs musicShelf = nav(results, MUSIC_SHELF) @@ -291,6 +298,7 @@ class YTMusic: for item in data[0]['contents']: item = item['musicTwoRowItemRenderer'] result = {'title': nav(item, TITLE_TEXT)} + result['thumbnails'] = nav(item, THUMBNAIL_RENDERER) if category == 'albums': result['year'] = nav(item, SUBTITLE2) result['browseId'] = nav(item, TITLE + NAVIGATION_BROWSE_ID) @@ -319,6 +327,7 @@ class YTMusic: "browseId": "MPREb_0rtvKhqeCY0", "artist": "Armin van Buuren", "title": "This I Vow (feat. Mila Josef)", + "thumbnails": [...], "type": "EP", "year": "2020" } @@ -334,6 +343,7 @@ class YTMusic: data = result['musicResponsiveListItemRenderer'] browseId = nav(data, NAVIGATION_BROWSE_ID) title = get_item_text(data, 0) + thumbnails = nav(data, THUMBNAILS) 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) @@ -341,6 +351,7 @@ class YTMusic: "browseId": browseId, "artist": artist, "title": title, + "thumbnails": thumbnails, "type": album_type, "year": year }) @@ -368,6 +379,7 @@ class YTMusic: "day": 28 }, "description": "Seven is ...", + "thumbnails": [...], "artist": [ { "name": "Martin Garrix", @@ -399,6 +411,7 @@ class YTMusic: album['releaseDate'] = album_data['releaseDate'] album['description'] = find_object_by_key(data, 'musicAlbumReleaseDetail', 'payload', True)['description'] + album['thumbnails'] = album_data['thumbnailDetails']['thumbnails'] album['artist'] = [] artists_data = find_objects_by_key(data, 'musicArtist', 'payload') for artist in artists_data: @@ -423,6 +436,8 @@ class YTMusic: track = {} track['index'] = item['payload']['musicTrack']['albumTrackIndex'] track['title'] = item['payload']['musicTrack']['title'] + track['thumbnails'] = item['payload']['musicTrack']['thumbnailDetails'][ + 'thumbnails'] track['artists'] = item['payload']['musicTrack']['artistNames'] # in case the song is unavailable, there is no videoId track['videoId'] = item['payload']['musicTrack']['videoId'] if 'videoId' in item[ @@ -449,6 +464,7 @@ class YTMusic: { 'playlistId': 'PLQwVIlKxHM6rz0fDJVv_0UlXGEWf-bFys', 'title': 'Playlist title', + 'thumbnails: [...], 'count': 5 } """ @@ -542,7 +558,8 @@ class YTMusic: { "browseId": "UCxEqaQWosMHaTih-tgzDqug", "artist": "WildVibes", - "subscribers": "2.91K" + "subscribers": "2.91K", + "thumbnails": [...] } """ self.__check_auth() @@ -699,6 +716,7 @@ class YTMusic: "id": "PLQwVIlKxHM6qv-o99iX9R85og7IzF9YS_", "privacy": "PUBLIC", "title": "New EDM This Week 03/13/2020", + "thumbnails": [...] "description": "Weekly r/EDM new release roundup. Created with github.com/sigma67/spotifyplaylist_to_gmusic", "author": "sigmatics", "year": "2020", @@ -723,7 +741,8 @@ class YTMusic: "id": "MPREb_PxmzvDuqOnC" }, "duration": "2:58", - "likeStatus": "INDIFFERENT" + "likeStatus": "INDIFFERENT", + "thumbnails": [...] } ] } @@ -748,6 +767,7 @@ class YTMusic: header = header['header']['musicDetailHeaderRenderer'] playlist['title'] = nav(header, TITLE_TEXT) + playlist['thumbnails'] = nav(header, THUMBNAIL_CROPPED) if "description" in header: playlist["description"] = header["description"]["runs"][0]["text"] run_count = len(header['subtitle']['runs']) @@ -938,7 +958,9 @@ class YTMusic: "videoId": "Uise6RPKoek", "artist": "Coldplay", "title": "A Sky Full Of Stars", - "album": "Ghost Stories" + "album": "Ghost Stories", + "likeStatus": "LIKE", + "thumbnails": [...] } """ self.__check_auth() @@ -1037,7 +1059,9 @@ class YTMusic: "id": "FEmusic_library_privately_owned_artist_detaila_po_CICr2crg7OWpchIFamFra28" } ], - "album": null + "album": null, + "likeStatus": "LIKE", + "thumbnails": [...] } ] """ @@ -1062,6 +1086,7 @@ class YTMusic: { "title": "Hard To Stop - Single", + "thumbnails": [...] "year": "2013", "trackCount": 1, "duration": "4 minutes, 2 seconds", @@ -1069,7 +1094,8 @@ class YTMusic: { "entityId": "t_po_CICr2crg7OWpchDN6tnYBw", "videoId": "VBQVcjJM7ak", - "title": "Hard To Stop (Vicetone x Ne-Yo x Daft Punk)" + "title": "Hard To Stop (Vicetone x Ne-Yo x Daft Punk)", + "likeStatus": "LIKE" } ] } @@ -1080,6 +1106,7 @@ class YTMusic: response = self.__send_request(endpoint, body) header = response['header']['musicDetailHeaderRenderer'] album = {'title': nav(header, TITLE_TEXT)} + album['thumbnails'] = nav(header, THUMBNAIL_CROPPED) if "description" in header: album["description"] = header["description"]["runs"][0]["text"] run_count = len(header['subtitle']['runs'])