Fix get_artist for artists without description or with few songs (closes #26)

song and view counts are now returned as int
This commit is contained in:
sigma67 2020-05-28 00:40:23 +02:00
commit 5bfa86a4d8
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
3 changed files with 26 additions and 18 deletions

View file

@ -46,3 +46,7 @@ def get_authorization(auth):
unix_timestamp = str(int(time.time())) unix_timestamp = str(int(time.time()))
sha_1.update((unix_timestamp + ' ' + auth).encode('utf-8')) sha_1.update((unix_timestamp + ' ' + auth).encode('utf-8'))
return "SAPISIDHASH " + unix_timestamp + "_" + sha_1.hexdigest() return "SAPISIDHASH " + unix_timestamp + "_" + sha_1.hexdigest()
def to_int(string):
return int(string.split(' ')[0].replace(',', ''))

View file

@ -7,7 +7,9 @@ ITEM_SECTION = ['itemSectionRenderer', 'contents', 0]
MUSIC_SHELF = [0, 'musicShelfRenderer'] MUSIC_SHELF = [0, 'musicShelfRenderer']
CONTINUATION = ['continuations', 0, 'nextContinuationData', 'continuation'] CONTINUATION = ['continuations', 0, 'nextContinuationData', 'continuation']
MENU_ITEMS = ['menu', 'menuRenderer', 'items'] MENU_ITEMS = ['menu', 'menuRenderer', 'items']
MENU_LIKE_STATUS = ['menu', 'menuRenderer', 'topLevelButtons', 0, 'likeButtonRenderer', 'likeStatus'] MENU_LIKE_STATUS = [
'menu', 'menuRenderer', 'topLevelButtons', 0, 'likeButtonRenderer', 'likeStatus'
]
MENU_SERVICE = ['menuServiceItemRenderer', 'serviceEndpoint'] MENU_SERVICE = ['menuServiceItemRenderer', 'serviceEndpoint']
PLAY_BUTTON = [ PLAY_BUTTON = [
'overlay', 'musicItemThumbnailOverlayRenderer', 'content', 'musicPlayButtonRenderer' 'overlay', 'musicItemThumbnailOverlayRenderer', 'content', 'musicPlayButtonRenderer'
@ -300,12 +302,12 @@ def nav(root, items):
return root return root
def find_object_by_key(object_list, key, nested=None): def find_object_by_key(object_list, key, nested=None, is_key=False):
for item in object_list: for item in object_list:
if nested: if nested:
item = item[nested] item = item[nested]
if key in item: if key in item:
return item return item[key] if is_key else item
return None return None

View file

@ -204,7 +204,7 @@ class YTMusic:
{ {
"name": "Oasis", "name": "Oasis",
"description": "Oasis were ...", "description": "Oasis were ...",
"views": "1,838,795,605", "views": "1838795605",
"songs": { "songs": {
"browseId": "VLPLMpM3Z0118S42R1npOhcjoakLIv1aqnS1", "browseId": "VLPLMpM3Z0118S42R1npOhcjoakLIv1aqnS1",
"results": [ "results": [
@ -256,16 +256,19 @@ class YTMusic:
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)
artist = {} artist = {'description': None, 'views': None}
artist['name'] = nav(response['header']['musicImmersiveHeaderRenderer'], TITLE_TEXT) artist['name'] = nav(response['header']['musicImmersiveHeaderRenderer'], TITLE_TEXT)
artist['description'] = results[-1]['musicDescriptionShelfRenderer']['description'][ descriptionShelf = find_object_by_key(results,
'runs'][0]['text'] 'musicDescriptionShelfRenderer',
artist['views'] = results[-1]['musicDescriptionShelfRenderer']['subheader']['runs'][0][ is_key=True)
'text'].split(' ')[0] if descriptionShelf:
artist['songs'] = {} artist['description'] = descriptionShelf['description']['runs'][0]['text']
artist['views'] = to_int(descriptionShelf['subheader']['runs'][0]['text'])
artist['songs'] = {'browseId': None}
if 'musicShelfRenderer' in results[0]: # API sometimes does not return songs if 'musicShelfRenderer' in results[0]: # API sometimes does not return songs
musicShelf = nav(results, MUSIC_SHELF) musicShelf = nav(results, MUSIC_SHELF)
artist['songs']['browseId'] = nav(musicShelf, TITLE + NAVIGATION_BROWSE_ID) if 'navigationEndpoint' in nav(musicShelf, TITLE):
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'])
categories = ['albums', 'singles', 'videos'] categories = ['albums', 'singles', 'videos']
@ -276,7 +279,7 @@ class YTMusic:
r['musicCarouselShelfRenderer'], CAROUSEL_TITLE)['text'].lower() == category r['musicCarouselShelfRenderer'], CAROUSEL_TITLE)['text'].lower() == category
] ]
if len(data) > 0: if len(data) > 0:
artist[category] = {"results": []} artist[category] = {'browseId': None, 'results': []}
if 'navigationEndpoint' in nav(data[0], CAROUSEL_TITLE): if 'navigationEndpoint' in nav(data[0], CAROUSEL_TITLE):
artist[category]['browseId'] = nav(data[0], artist[category]['browseId'] = nav(data[0],
CAROUSEL_TITLE + NAVIGATION_BROWSE_ID) CAROUSEL_TITLE + NAVIGATION_BROWSE_ID)
@ -388,14 +391,14 @@ class YTMusic:
response = self.__send_request(endpoint, body) response = self.__send_request(endpoint, body)
data = nav(response, FRAMEWORK_MUTATIONS) data = nav(response, FRAMEWORK_MUTATIONS)
album = {} album = {}
album_data = find_object_by_key(data, 'musicAlbumRelease', 'payload')['musicAlbumRelease'] album_data = find_object_by_key(data, 'musicAlbumRelease', 'payload', True)
album['title'] = album_data['title'] album['title'] = album_data['title']
album['trackCount'] = album_data['trackCount'] album['trackCount'] = album_data['trackCount']
album['durationMs'] = album_data['durationMs'] album['durationMs'] = album_data['durationMs']
album['playlistId'] = album_data['audioPlaylistId'] album['playlistId'] = album_data['audioPlaylistId']
album['releaseDate'] = album_data['releaseDate'] album['releaseDate'] = album_data['releaseDate']
album['description'] = find_object_by_key( album['description'] = find_object_by_key(data, 'musicAlbumReleaseDetail', 'payload',
data, 'musicAlbumReleaseDetail', 'payload')['musicAlbumReleaseDetail']['description'] True)['description']
album['artist'] = [] album['artist'] = []
artists_data = find_objects_by_key(data, 'musicArtist', 'payload') artists_data = find_objects_by_key(data, 'musicArtist', 'payload')
for artist in artists_data: for artist in artists_data:
@ -754,8 +757,7 @@ class YTMusic:
playlist['year'] = nav(header, SUBTITLE3) playlist['year'] = nav(header, SUBTITLE3)
if len(header['secondSubtitle']['runs']) > 1: if len(header['secondSubtitle']['runs']) > 1:
song_count = \ song_count = to_int(header['secondSubtitle']['runs'][0]['text'])
int(header['secondSubtitle']['runs'][0]['text'].split(' ')[0].replace(',', ''))
playlist['duration'] = header['secondSubtitle']['runs'][2]['text'] playlist['duration'] = header['secondSubtitle']['runs'][2]['text']
else: else:
playlist['duration'] = header['secondSubtitle']['runs'][0]['text'] playlist['duration'] = header['secondSubtitle']['runs'][0]['text']
@ -1092,7 +1094,7 @@ class YTMusic:
album['year'] = nav(header, SUBTITLE3) album['year'] = nav(header, SUBTITLE3)
if len(header['secondSubtitle']['runs']) > 1: if len(header['secondSubtitle']['runs']) > 1:
album['trackCount'] = int(header['secondSubtitle']['runs'][0]['text'].split(' ')[0]) album['trackCount'] = to_int(header['secondSubtitle']['runs'][0]['text'])
album['duration'] = header['secondSubtitle']['runs'][2]['text'] album['duration'] = header['secondSubtitle']['runs'][2]['text']
else: else:
album['duration'] = header['secondSubtitle']['runs'][0]['text'] album['duration'] = header['secondSubtitle']['runs'][0]['text']