Add get_artist_albums (closes #4)
This commit also circumvents an issue with get_artist, where the API sometimes fails to return songs.
This commit is contained in:
parent
e96c4f1ca8
commit
4a653c5285
5 changed files with 64 additions and 17 deletions
|
|
@ -1,10 +1,5 @@
|
||||||
{
|
{
|
||||||
"context": {
|
"context": {
|
||||||
"activePlayers": [
|
|
||||||
{
|
|
||||||
"playerContextParams": "Q0FFU0FnZ0I="
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"capabilities": {},
|
"capabilities": {},
|
||||||
"client": {
|
"client": {
|
||||||
"clientName": "WEB_REMIX",
|
"clientName": "WEB_REMIX",
|
||||||
|
|
|
||||||
|
|
@ -4,5 +4,6 @@
|
||||||
"Accept-Language": "en-US,en;q=0.5",
|
"Accept-Language": "en-US,en;q=0.5",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Goog-AuthUser": "0",
|
"X-Goog-AuthUser": "0",
|
||||||
"x-origin": "https://music.youtube.com"
|
"x-origin": "https://music.youtube.com",
|
||||||
|
"X-Goog-Visitor-Id": "CgtvVTcxa1EtbV9hayiMu-P0BQ%3D%3D"
|
||||||
}
|
}
|
||||||
|
|
@ -131,11 +131,11 @@ def parse_search_result(data, resultType = None):
|
||||||
return search_result
|
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']:
|
if 'runs' not in item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']['text']:
|
||||||
return None
|
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):
|
def prepare_browse_endpoint(type, browseId):
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,10 @@ class TestYTMusic(unittest.TestCase):
|
||||||
songs = youtube_auth.get_playlist_items('PLQwVIlKxHM6oCeKLTsUx_w9CmWjHKdg68')
|
songs = youtube_auth.get_playlist_items('PLQwVIlKxHM6oCeKLTsUx_w9CmWjHKdg68')
|
||||||
self.assertEqual(len(songs), 64)
|
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):
|
def test_get_history(self):
|
||||||
songs = youtube_auth.get_history()
|
songs = youtube_auth.get_history()
|
||||||
self.assertGreater(len(songs), 0)
|
self.assertGreater(len(songs), 0)
|
||||||
|
|
@ -31,7 +35,17 @@ class TestYTMusic(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_artist(self):
|
def test_get_artist(self):
|
||||||
results = youtube.get_artist("UCmMUZbaYdNH0bEd1PAlAqsA")
|
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):
|
def test_get_album(self):
|
||||||
results = youtube.get_album("MPREb_BQZvl3BFGay")
|
results = youtube.get_album("MPREb_BQZvl3BFGay")
|
||||||
|
|
|
||||||
|
|
@ -165,11 +165,14 @@ class YTMusic:
|
||||||
|
|
||||||
def get_artist(self, channelId):
|
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
|
:param channelId: channel id of the artist
|
||||||
:return: Detailed information about the artist and
|
:return: Dictionary with requested information. Example::
|
||||||
their songs, albums, singles and videos. Example::
|
|
||||||
|
|
||||||
{
|
{
|
||||||
"name": "Oasis",
|
"name": "Oasis",
|
||||||
|
|
@ -230,8 +233,9 @@ class YTMusic:
|
||||||
artist['description'] = results[-1]['musicDescriptionShelfRenderer']['description']['runs'][0]['text']
|
artist['description'] = results[-1]['musicDescriptionShelfRenderer']['description']['runs'][0]['text']
|
||||||
artist['views'] = results[-1]['musicDescriptionShelfRenderer']['subheader']['runs'][0]['text'].split(' ')[0]
|
artist['views'] = results[-1]['musicDescriptionShelfRenderer']['subheader']['runs'][0]['text'].split(' ')[0]
|
||||||
artist['songs'] = {}
|
artist['songs'] = {}
|
||||||
artist['songs']['browseId'] = results[0]['musicShelfRenderer']['title']['runs'][0]['navigationEndpoint']['browseEndpoint']['browseId']
|
if 'musicShelfRenderer' in results[0]: # API sometimes does not return songs
|
||||||
artist['songs']['results'] = parse_playlist_items(results[0]['musicShelfRenderer']['contents'])
|
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']
|
categories = ['albums', 'singles', 'videos']
|
||||||
for category in categories:
|
for category in categories:
|
||||||
|
|
@ -261,10 +265,41 @@ class YTMusic:
|
||||||
return artist
|
return artist
|
||||||
|
|
||||||
def get_artist_albums(self, channelId, params):
|
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}
|
body = {"browseId": channelId, "params": params}
|
||||||
endpoint = 'browse'
|
endpoint = 'browse'
|
||||||
response = self.__send_request(endpoint, body)
|
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):
|
def get_album(self, browseId):
|
||||||
"""
|
"""
|
||||||
|
|
@ -299,7 +334,8 @@ class YTMusic:
|
||||||
track['index'] = item['payload']['musicTrack']['albumTrackIndex']
|
track['index'] = item['payload']['musicTrack']['albumTrackIndex']
|
||||||
track['title'] = item['payload']['musicTrack']['title']
|
track['title'] = item['payload']['musicTrack']['title']
|
||||||
track['artists'] = item['payload']['musicTrack']['artistNames']
|
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']
|
track['lengthMs'] = item['payload']['musicTrack']['lengthMs']
|
||||||
album['tracks'].append(track)
|
album['tracks'].append(track)
|
||||||
|
|
||||||
|
|
@ -411,7 +447,8 @@ class YTMusic:
|
||||||
needed for moving/removing playlist items
|
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'
|
endpoint = 'browse'
|
||||||
response = self.__send_request(endpoint, body)
|
response = self.__send_request(endpoint, body)
|
||||||
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)[0]['musicPlaylistShelfRenderer']
|
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)[0]['musicPlaylistShelfRenderer']
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue