Add artists, album and feedbackTokens to get_watch_playlist
This commit is contained in:
parent
8cff17e6b4
commit
3de9b5e4e8
4 changed files with 49 additions and 20 deletions
|
|
@ -112,8 +112,10 @@ class TestYTMusic(unittest.TestCase):
|
|||
###############
|
||||
|
||||
def test_get_watch_playlist(self):
|
||||
playlist = youtube.get_watch_playlist("4y33h81phKU", limit=50)
|
||||
playlist = youtube_auth.get_watch_playlist("9mWr4c_ig54", limit=50)
|
||||
self.assertGreater(len(playlist['tracks']), 50)
|
||||
playlist = youtube_auth.get_watch_playlist("UoAf_y9Ok4k") # private track
|
||||
self.assertEqual(len(playlist['tracks']), 25)
|
||||
|
||||
def test_get_watch_playlist_shuffle(self):
|
||||
playlist = youtube.get_watch_playlist_shuffle(
|
||||
|
|
@ -331,7 +333,8 @@ class TestYTMusic(unittest.TestCase):
|
|||
self.assertGreater(len(album['tracks']), 0)
|
||||
|
||||
def test_get_library_upload_artist(self):
|
||||
tracks = youtube_auth.get_library_upload_artist(config['uploads']['private_artist_id'], 100)
|
||||
tracks = youtube_auth.get_library_upload_artist(config['uploads']['private_artist_id'],
|
||||
100)
|
||||
self.assertGreater(len(tracks), 0)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -35,14 +35,7 @@ class Parser:
|
|||
search_result['title'] = get_item_text(data, 0)
|
||||
|
||||
runs = get_flex_column_item(data, 1)['text']['runs']
|
||||
# determine the number of artists
|
||||
try:
|
||||
last_artist_index = next(
|
||||
len(runs) - i - 1 for i, run in enumerate(reversed(runs))
|
||||
if 'navigationEndpoint' in run
|
||||
and nav(run, NAVIGATION_BROWSE_ID).startswith('UC'))
|
||||
except StopIteration: # if single artist is missing the browseId
|
||||
pass
|
||||
last_artist_index = get_last_artist_index(runs)
|
||||
|
||||
search_result['artists'] = parse_song_artists_runs(
|
||||
runs[default_offset:last_artist_index + 1])
|
||||
|
|
@ -68,13 +61,7 @@ class Parser:
|
|||
default_offset + 2).split(' ')[0]
|
||||
|
||||
elif resultType in ['song']:
|
||||
search_result['album'] = None
|
||||
if len(runs) - last_artist_index == 5: # has album
|
||||
search_result['album'] = {
|
||||
'name': runs[last_artist_index + 2]['text'],
|
||||
'id': nav(runs[last_artist_index + 2], NAVIGATION_BROWSE_ID, True)
|
||||
}
|
||||
|
||||
search_result['album'] = parse_song_album_runs(runs, last_artist_index)
|
||||
search_result['duration'] = runs[-1]['text']
|
||||
if 'menu' in data:
|
||||
toggle_menu = find_object_by_key(nav(data, MENU_ITEMS),
|
||||
|
|
|
|||
|
|
@ -20,6 +20,20 @@ def parse_song_artists_runs(runs):
|
|||
return artists
|
||||
|
||||
|
||||
def get_last_artist_index(runs):
|
||||
try:
|
||||
return next(
|
||||
len(runs) - i - 1 for i, run in enumerate(reversed(runs))
|
||||
if 'navigationEndpoint' in run and
|
||||
(nav(run, NAVIGATION_BROWSE_ID).startswith('UC') or
|
||||
nav(run, NAVIGATION_BROWSE_ID).startswith('FEmusic_library_privately_owned_artist')))
|
||||
except StopIteration: # try to find album if no artist IDs available
|
||||
if 'navigationEndpoint' in runs[-3]: # has album
|
||||
return len(runs) - 5
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def parse_song_album(data, index):
|
||||
flex_item = get_flex_column_item(data, index)
|
||||
return None if not flex_item else {
|
||||
|
|
@ -28,6 +42,16 @@ def parse_song_album(data, index):
|
|||
}
|
||||
|
||||
|
||||
def parse_song_album_runs(runs, last_artist_index):
|
||||
if len(runs) - last_artist_index == 5: # has album
|
||||
return {
|
||||
'name': runs[last_artist_index + 2]['text'],
|
||||
'id': nav(runs[last_artist_index + 2], NAVIGATION_BROWSE_ID, True)
|
||||
}
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def parse_song_menu_tokens(item):
|
||||
library_add_token = library_remove_token = None
|
||||
toggle_menu = item['toggleMenuServiceItemRenderer']
|
||||
|
|
|
|||
|
|
@ -9,13 +9,28 @@ def parse_watch_playlist(results):
|
|||
data = result['playlistPanelVideoRenderer']
|
||||
if 'unplayableText' in data:
|
||||
continue
|
||||
|
||||
feedback_tokens = None
|
||||
for item in nav(data, MENU_ITEMS):
|
||||
if 'toggleMenuServiceItemRenderer' in item:
|
||||
feedback_tokens = parse_song_menu_tokens(item)
|
||||
break
|
||||
|
||||
runs = data['longBylineText']['runs']
|
||||
last_artist_index = get_last_artist_index(runs)
|
||||
artists = parse_song_artists_runs(runs[:last_artist_index + 1])
|
||||
album = parse_song_album_runs(runs, last_artist_index)
|
||||
|
||||
track = {
|
||||
'title': nav(data, TITLE_TEXT),
|
||||
'byline': nav(data, ['shortBylineText', 'runs', 0, 'text'], True),
|
||||
'length': nav(data, ['lengthText', 'runs', 0, 'text']),
|
||||
'videoId': data['videoId'],
|
||||
'title': nav(data, TITLE_TEXT),
|
||||
'artists': artists,
|
||||
'album': album,
|
||||
'length': nav(data, ['lengthText', 'runs', 0, 'text']),
|
||||
'playlistId': nav(data, NAVIGATION_PLAYLIST_ID),
|
||||
'thumbnail': nav(data, THUMBNAIL)
|
||||
'thumbnail': nav(data, THUMBNAIL),
|
||||
'feedbackTokens': feedback_tokens
|
||||
}
|
||||
tracks.append(track)
|
||||
|
||||
return tracks
|
||||
|
|
|
|||
Loading…
Reference in a new issue