From 5457cd8e11e05984e9d86ff0becb48022072611b Mon Sep 17 00:00:00 2001 From: mikefreemanwd Date: Mon, 22 Mar 2021 12:00:22 -0400 Subject: [PATCH 1/3] Feature: add related artist section to BrowsingMixin get_artist method --- tests/test.py | 25 +++++++++++++++++++++++-- ytmusicapi/__init__.py | 2 +- ytmusicapi/helpers.py | 8 ++++++++ ytmusicapi/mixins/browsing.py | 15 ++++++++++++++- ytmusicapi/parsers/browsing.py | 9 +++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/tests/test.py b/tests/test.py index b8ab593..963ea49 100644 --- a/tests/test.py +++ b/tests/test.py @@ -2,7 +2,7 @@ import unittest import configparser import sys sys.path.insert(0, '..') -from ytmusicapi.ytmusic import YTMusic # noqa: E402 +from ytmusicapi.ytmusic import YTMusic, YouTubeChannelIDIsNotArtistException # noqa: E402 config = configparser.RawConfigParser() config.read('./test.cfg', 'utf-8') @@ -50,13 +50,34 @@ class TestYTMusic(unittest.TestCase): def test_get_artist(self): results = youtube.get_artist("MPLAUCmMUZbaYdNH0bEd1PAlAqsA") - self.assertEqual(len(results), 13) + self.assertEqual(len(results), 14) + + # test correctness of related artists + related = results['related']['results'] + + # all should have 3 keys per item + self.assertEqual( + len([x for x in related if set(x.keys()) == {"browseId", "subscribers", "title"}]), + len(related)) + + # test names of some related artists + related_artist_names = {x['title'] for x in related} + self.assertTrue( + {"The Verve", "Liam Gallagher", "Noel Gallagher", "Beady Eye", + "The Stone Roses"}.issubset(related_artist_names)) + results = youtube.get_artist("UCLZ7tlKC06ResyDmEStSrOw") # no album year self.assertGreaterEqual(len(results), 11) results = youtube.get_artist( "UCDAPd3S5CBIEKXn-tvy57Lg") # no thumbnail, albums, subscribe count self.assertGreaterEqual(len(results), 11) + def test_get_artist_for_non_youtube_music_channel(self): + # all YouTube channel IDs can be looked up in YouTube Music, but the page they return will not necessarily return any music content + non_music_channel_id = "UCUcpVoi5KkJmnE3bvEhHR0Q" # e.g. https://music.youtube.com/channel/UCUcpVoi5KkJmnE3bvEhHR0Q + self.assertRaises(YouTubeChannelIDIsNotArtistException, youtube.get_artist, + non_music_channel_id) + def test_get_artist_albums(self): artist = youtube.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg") results = youtube.get_artist_albums(artist['albums']['browseId'], diff --git a/ytmusicapi/__init__.py b/ytmusicapi/__init__.py index 6cd910c..59567c3 100644 --- a/ytmusicapi/__init__.py +++ b/ytmusicapi/__init__.py @@ -1,5 +1,5 @@ from ytmusicapi._version import __version__ -from ytmusicapi.ytmusic import YTMusic +from ytmusicapi.ytmusic import YTMusic, YouTubeChannelIDIsNotArtistException __copyright__ = 'Copyright 2020 sigma67' __license__ = 'MIT' diff --git a/ytmusicapi/helpers.py b/ytmusicapi/helpers.py index 786a4e9..82085a6 100644 --- a/ytmusicapi/helpers.py +++ b/ytmusicapi/helpers.py @@ -83,3 +83,11 @@ def i18n(method): return method(self, *method_args, **method_kwargs) return _impl + + +class YouTubeChannelIDIsNotArtistException(Exception): + """ + Not all YouTube channel IDs have YouTube Music Content, this exception thrown when typical YT Channel ID given + to Browser Mixin get_artist method + """ + pass diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 241f799..45307c0 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -265,6 +265,11 @@ class BrowsingMixin: response = self._send_request(endpoint, body) results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST) + if len(results) == 1: + # not a YouTube Music Channel, a standard YouTube Channel ID with no music content was given + raise YouTubeChannelIDIsNotArtistException( + f"The YouTube Channel ID: {channelId} has no music content.") + artist = {'description': None, 'views': None} header = response['header']['musicImmersiveHeaderRenderer'] artist['name'] = nav(header, TITLE_TEXT) @@ -278,7 +283,8 @@ class BrowsingMixin: subscription_button = header['subscriptionButton']['subscribeButtonRenderer'] artist['channelId'] = subscription_button['channelId'] artist['shuffleId'] = nav(header, - ['playButton', 'buttonRenderer'] + NAVIGATION_WATCH_PLAYLIST_ID, True) + ['playButton', 'buttonRenderer'] + NAVIGATION_WATCH_PLAYLIST_ID, + True) artist['radioId'] = nav(header, ['startRadioButton', 'buttonRenderer'] + NAVIGATION_WATCH_PLAYLIST_ID, True) artist['subscribers'] = nav(subscription_button, @@ -292,6 +298,13 @@ class BrowsingMixin: artist['songs']['browseId'] = nav(musicShelf, TITLE + NAVIGATION_BROWSE_ID) artist['songs']['results'] = parse_playlist_items(musicShelf['contents']) + artist['related'] = {} + if len(results) > 5 and 'musicCarouselShelfRenderer' in results[5]: + related_tab = nav(results[5], ['musicCarouselShelfRenderer']) + artist['related']['results'] = [ + parse_related_artist(x) for x in related_tab['contents'] + ] + artist.update(self.parser.parse_artist_contents(results)) return artist diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 99ad040..e8a708e 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -198,3 +198,12 @@ def parse_playlist(data): if len(data['subtitle']['runs']) == 3: playlist['count'] = nav(data, SUBTITLE2).split(' ')[0] return playlist + + +def parse_related_artist(data): + item = data['musicTwoRowItemRenderer'] + return { + 'title': nav(item, TITLE_TEXT), + 'browseId': nav(item, TITLE + NAVIGATION_BROWSE_ID), + 'subscribers': nav(item, SUBTITLE) + } From 3272e1f5230d1e9d8cfad00b680ea247b34f7e0e Mon Sep 17 00:00:00 2001 From: mikefreemanwd Date: Mon, 22 Mar 2021 12:13:40 -0400 Subject: [PATCH 2/3] added documentation for related artists feature --- README.rst | 2 +- ytmusicapi/mixins/browsing.py | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 65a635e..5511da0 100644 --- a/README.rst +++ b/README.rst @@ -32,7 +32,7 @@ Features | **Browsing**: * search (including all filters) -* get artist information and releases (songs, videos, albums, singles) +* get artist information and releases (songs, videos, albums, singles, related artists) * get user information (videos, playlists) * get albums * get song metadata diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 45307c0..96a4c77 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -190,10 +190,11 @@ class BrowsingMixin: def get_artist(self, channelId: str) -> Dict: """ 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`. For albums/singles, - pass browseId and params to :py:func:`get_artist_albums`. + albums, singles, videos, and related artists). The top lists + contain pointers for getting the full list of releases. For + songs/videos, pass the browseId to :py:func:`get_playlist`. + For albums/singles, pass browseId and params to :py:func: + `get_artist_albums`. :param channelId: channel id of the artist :return: Dictionary with requested information. @@ -255,6 +256,21 @@ class BrowsingMixin: } ], "browseId": "VLPLMpM3Z0118S5xuNckw1HUcj1D021AnMEB" + }, + "related": { + "results": [ + { + "browseId": "UCt2KxZpY5D__kapeQ8cauQw", + "subscribers": "450K subscribers", + "title": "The Verve" + }, + { + "browseId": "UCwK2Grm574W1u-sBzLikldQ", + "subscribers": "341K subscribers", + "title": "Liam Gallagher" + }, + ... + ] } } """ From b1ecae3d9feb1b4d4b6e54599114a50c5c92deb5 Mon Sep 17 00:00:00 2001 From: mikefreemanwd Date: Tue, 23 Mar 2021 11:57:10 -0400 Subject: [PATCH 3/3] removed named exception in favor of ValueError when non-music YouTube channel id given to get_artist. Minor changes to structure of related artists property in get_artist, added thumbnails. --- tests/test.py | 18 ++++++------------ ytmusicapi/__init__.py | 2 +- ytmusicapi/helpers.py | 8 -------- ytmusicapi/mixins/browsing.py | 3 +-- ytmusicapi/parsers/browsing.py | 3 ++- 5 files changed, 10 insertions(+), 24 deletions(-) diff --git a/tests/test.py b/tests/test.py index 963ea49..d74db66 100644 --- a/tests/test.py +++ b/tests/test.py @@ -2,7 +2,7 @@ import unittest import configparser import sys sys.path.insert(0, '..') -from ytmusicapi.ytmusic import YTMusic, YouTubeChannelIDIsNotArtistException # noqa: E402 +from ytmusicapi.ytmusic import YTMusic # noqa: E402 config = configparser.RawConfigParser() config.read('./test.cfg', 'utf-8') @@ -55,16 +55,11 @@ class TestYTMusic(unittest.TestCase): # test correctness of related artists related = results['related']['results'] - # all should have 3 keys per item self.assertEqual( - len([x for x in related if set(x.keys()) == {"browseId", "subscribers", "title"}]), - len(related)) - - # test names of some related artists - related_artist_names = {x['title'] for x in related} - self.assertTrue( - {"The Verve", "Liam Gallagher", "Noel Gallagher", "Beady Eye", - "The Stone Roses"}.issubset(related_artist_names)) + len([ + x for x in related + if set(x.keys()) == {"browseId", "subscribers", "title", "thumbnails"} + ]), len(related)) results = youtube.get_artist("UCLZ7tlKC06ResyDmEStSrOw") # no album year self.assertGreaterEqual(len(results), 11) @@ -75,8 +70,7 @@ class TestYTMusic(unittest.TestCase): def test_get_artist_for_non_youtube_music_channel(self): # all YouTube channel IDs can be looked up in YouTube Music, but the page they return will not necessarily return any music content non_music_channel_id = "UCUcpVoi5KkJmnE3bvEhHR0Q" # e.g. https://music.youtube.com/channel/UCUcpVoi5KkJmnE3bvEhHR0Q - self.assertRaises(YouTubeChannelIDIsNotArtistException, youtube.get_artist, - non_music_channel_id) + self.assertRaises(ValueError, youtube.get_artist, non_music_channel_id) def test_get_artist_albums(self): artist = youtube.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg") diff --git a/ytmusicapi/__init__.py b/ytmusicapi/__init__.py index 59567c3..6cd910c 100644 --- a/ytmusicapi/__init__.py +++ b/ytmusicapi/__init__.py @@ -1,5 +1,5 @@ from ytmusicapi._version import __version__ -from ytmusicapi.ytmusic import YTMusic, YouTubeChannelIDIsNotArtistException +from ytmusicapi.ytmusic import YTMusic __copyright__ = 'Copyright 2020 sigma67' __license__ = 'MIT' diff --git a/ytmusicapi/helpers.py b/ytmusicapi/helpers.py index 82085a6..786a4e9 100644 --- a/ytmusicapi/helpers.py +++ b/ytmusicapi/helpers.py @@ -83,11 +83,3 @@ def i18n(method): return method(self, *method_args, **method_kwargs) return _impl - - -class YouTubeChannelIDIsNotArtistException(Exception): - """ - Not all YouTube channel IDs have YouTube Music Content, this exception thrown when typical YT Channel ID given - to Browser Mixin get_artist method - """ - pass diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 96a4c77..73e1f0d 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -283,8 +283,7 @@ class BrowsingMixin: if len(results) == 1: # not a YouTube Music Channel, a standard YouTube Channel ID with no music content was given - raise YouTubeChannelIDIsNotArtistException( - f"The YouTube Channel ID: {channelId} has no music content.") + raise ValueError(f"The YouTube Channel ID: {channelId} has no music content.") artist = {'description': None, 'views': None} header = response['header']['musicImmersiveHeaderRenderer'] diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index e8a708e..ce75aa7 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -205,5 +205,6 @@ def parse_related_artist(data): return { 'title': nav(item, TITLE_TEXT), 'browseId': nav(item, TITLE + NAVIGATION_BROWSE_ID), - 'subscribers': nav(item, SUBTITLE) + 'subscribers': nav(item, SUBTITLE).split(' ')[0], + 'thumbnails': nav(item, THUMBNAIL_RENDERER), }