Feature: add related artist section to BrowsingMixin get_artist method
This commit is contained in:
parent
3c1eb8519f
commit
5457cd8e11
5 changed files with 55 additions and 4 deletions
|
|
@ -2,7 +2,7 @@ import unittest
|
||||||
import configparser
|
import configparser
|
||||||
import sys
|
import sys
|
||||||
sys.path.insert(0, '..')
|
sys.path.insert(0, '..')
|
||||||
from ytmusicapi.ytmusic import YTMusic # noqa: E402
|
from ytmusicapi.ytmusic import YTMusic, YouTubeChannelIDIsNotArtistException # noqa: E402
|
||||||
|
|
||||||
config = configparser.RawConfigParser()
|
config = configparser.RawConfigParser()
|
||||||
config.read('./test.cfg', 'utf-8')
|
config.read('./test.cfg', 'utf-8')
|
||||||
|
|
@ -50,13 +50,34 @@ class TestYTMusic(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_artist(self):
|
def test_get_artist(self):
|
||||||
results = youtube.get_artist("MPLAUCmMUZbaYdNH0bEd1PAlAqsA")
|
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
|
results = youtube.get_artist("UCLZ7tlKC06ResyDmEStSrOw") # no album year
|
||||||
self.assertGreaterEqual(len(results), 11)
|
self.assertGreaterEqual(len(results), 11)
|
||||||
results = youtube.get_artist(
|
results = youtube.get_artist(
|
||||||
"UCDAPd3S5CBIEKXn-tvy57Lg") # no thumbnail, albums, subscribe count
|
"UCDAPd3S5CBIEKXn-tvy57Lg") # no thumbnail, albums, subscribe count
|
||||||
self.assertGreaterEqual(len(results), 11)
|
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):
|
def test_get_artist_albums(self):
|
||||||
artist = youtube.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg")
|
artist = youtube.get_artist("UCAeLFBCQS7FvI8PvBrWvSBg")
|
||||||
results = youtube.get_artist_albums(artist['albums']['browseId'],
|
results = youtube.get_artist_albums(artist['albums']['browseId'],
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from ytmusicapi._version import __version__
|
from ytmusicapi._version import __version__
|
||||||
from ytmusicapi.ytmusic import YTMusic
|
from ytmusicapi.ytmusic import YTMusic, YouTubeChannelIDIsNotArtistException
|
||||||
|
|
||||||
__copyright__ = 'Copyright 2020 sigma67'
|
__copyright__ = 'Copyright 2020 sigma67'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|
|
||||||
|
|
@ -83,3 +83,11 @@ def i18n(method):
|
||||||
return method(self, *method_args, **method_kwargs)
|
return method(self, *method_args, **method_kwargs)
|
||||||
|
|
||||||
return _impl
|
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
|
||||||
|
|
|
||||||
|
|
@ -265,6 +265,11 @@ class BrowsingMixin:
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)
|
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}
|
artist = {'description': None, 'views': None}
|
||||||
header = response['header']['musicImmersiveHeaderRenderer']
|
header = response['header']['musicImmersiveHeaderRenderer']
|
||||||
artist['name'] = nav(header, TITLE_TEXT)
|
artist['name'] = nav(header, TITLE_TEXT)
|
||||||
|
|
@ -278,7 +283,8 @@ class BrowsingMixin:
|
||||||
subscription_button = header['subscriptionButton']['subscribeButtonRenderer']
|
subscription_button = header['subscriptionButton']['subscribeButtonRenderer']
|
||||||
artist['channelId'] = subscription_button['channelId']
|
artist['channelId'] = subscription_button['channelId']
|
||||||
artist['shuffleId'] = nav(header,
|
artist['shuffleId'] = nav(header,
|
||||||
['playButton', 'buttonRenderer'] + NAVIGATION_WATCH_PLAYLIST_ID, True)
|
['playButton', 'buttonRenderer'] + NAVIGATION_WATCH_PLAYLIST_ID,
|
||||||
|
True)
|
||||||
artist['radioId'] = nav(header, ['startRadioButton', 'buttonRenderer']
|
artist['radioId'] = nav(header, ['startRadioButton', 'buttonRenderer']
|
||||||
+ NAVIGATION_WATCH_PLAYLIST_ID, True)
|
+ NAVIGATION_WATCH_PLAYLIST_ID, True)
|
||||||
artist['subscribers'] = nav(subscription_button,
|
artist['subscribers'] = nav(subscription_button,
|
||||||
|
|
@ -292,6 +298,13 @@ class BrowsingMixin:
|
||||||
artist['songs']['browseId'] = nav(musicShelf, TITLE + NAVIGATION_BROWSE_ID)
|
artist['songs']['browseId'] = nav(musicShelf, TITLE + NAVIGATION_BROWSE_ID)
|
||||||
artist['songs']['results'] = parse_playlist_items(musicShelf['contents'])
|
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))
|
artist.update(self.parser.parse_artist_contents(results))
|
||||||
return artist
|
return artist
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -198,3 +198,12 @@ def parse_playlist(data):
|
||||||
if len(data['subtitle']['runs']) == 3:
|
if len(data['subtitle']['runs']) == 3:
|
||||||
playlist['count'] = nav(data, SUBTITLE2).split(' ')[0]
|
playlist['count'] = nav(data, SUBTITLE2).split(' ')[0]
|
||||||
return playlist
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue