Finalize 0.7.0 release (see release info for all changes)

This commit is contained in:
sigma67 2020-05-03 12:04:50 +02:00
commit 56f754367a
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
10 changed files with 517 additions and 193 deletions

View file

@ -6,13 +6,82 @@ CONTINUATION = ['continuations', 0, 'nextContinuationData', 'continuation']
MENU_ITEMS = ['menu', 'menuRenderer', 'items']
MENU_SERVICE = ['menuServiceItemRenderer', 'serviceEndpoint']
PLAY_BUTTON = ['overlay', 'musicItemThumbnailOverlayRenderer', 'content', 'musicPlayButtonRenderer']
NAVIGATION_BROWSE_ID = ['navigationEndpoint', 'browseEndpoint', 'browseId']
NAVIGATION_VIDEO_ID = ['navigationEndpoint', 'watchEndpoint', 'videoId']
CAROUSEL_TITLE = ['header', 'musicCarouselShelfBasicHeaderRenderer', 'title', 'runs', 0]
FRAMEWORK_MUTATIONS = ['frameworkUpdates', 'entityBatchUpdate', 'mutations']
TITLE = ['title', 'runs', 0, 'text']
TITLE = ['title', 'runs', 0]
TITLE_TEXT = ['title', 'runs', 0, 'text']
SUBTITLE = ['subtitle', 'runs', 0, 'text']
SUBTITLE2 = ['subtitle', 'runs', 2, 'text']
SUBTITLE3 = ['subtitle', 'runs', 4, 'text']
def parse_playlists(results):
playlists = []
for result in results:
data = result['musicTwoRowItemRenderer']
playlist = {}
playlist['playlistId'] = nav(data, TITLE + NAVIGATION_BROWSE_ID)[2:]
playlist['title'] = nav(data, TITLE_TEXT)
if len(data['subtitle']['runs']) == 3:
playlist['count'] = nav(data, SUBTITLE2).split(' ')[0]
playlists.append(playlist)
return playlists
def parse_artists(results, uploaded=False):
artists = []
for result in results:
data = result['musicResponsiveListItemRenderer']
artist = {}
artist['browseId'] = nav(data, NAVIGATION_BROWSE_ID)
artist['artist'] = get_item_text(data, 0)
if uploaded:
artist['songs'] = get_item_text(data, 1).split(' ')[0]
else:
subtitle = get_item_text(data, 1)
if subtitle:
artist['subscribers'] = subtitle.split(' ')[0]
artists.append(artist)
return artists
def parse_albums(results):
albums = []
for result in results:
data = result['musicTwoRowItemRenderer']
album = {}
album['browseId'] = nav(data, TITLE + NAVIGATION_BROWSE_ID)
album['title'] = nav(data, TITLE_TEXT)
album['type'] = nav(data, SUBTITLE)
album['artists'] = []
run_count = len(data['subtitle']['runs'])
has_artists = False
if run_count == 3:
if nav(data, SUBTITLE2).isdigit():
album['year'] = nav(data, SUBTITLE2)
else:
has_artists = True
elif run_count > 3:
album['year'] = nav(data, SUBTITLE3)
has_artists = True
if has_artists:
for i in range(1, int(run_count / 2)):
album['artists'].append({
'name': data['subtitle']['runs'][i * 2]['text'],
'id': nav(data['subtitle']['runs'][i * 2], NAVIGATION_BROWSE_ID)
})
albums.append(album)
return albums
def parse_playlist_items(results, owned=False):
songs = []
count = 1
@ -36,10 +105,11 @@ def parse_playlist_items(results, owned=False):
if 'playNavigationEndpoint' in nav(data, PLAY_BUTTON):
videoId = nav(data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
runs = [None] * 3
for i in range(len(runs)):
if 'text' in data['flexColumns'][i]['musicResponsiveListItemFlexColumnRenderer']:
runs[i] = get_item_text(data, i)
title = get_item_text(data, 0)
artists = parse_song_artists(data, 1)
album = parse_song_album(data, 2)
duration = None
if 'fixedColumns' in data:
@ -48,7 +118,7 @@ def parse_playlist_items(results, owned=False):
else:
duration = data['fixedColumns'][0]['musicResponsiveListItemFixedColumnRenderer']['text']['runs'][0]['text']
song = {'videoId': videoId, 'title': runs[0], 'artist': runs[1], 'album': runs[2]}
song = {'videoId': videoId, 'title': title, 'artists': artists, 'album': album}
if duration:
song['duration'] = duration
if owned:
@ -96,10 +166,9 @@ def parse_search_result(data, resultType = None):
if resultType in ['song', 'video']:
search_result['videoId'] = nav(data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
search_result['title'] = get_item_text(data, 0)
search_result['artist'] = get_item_text(data, 1 + default)
if resultType in ['artist', 'album', 'playlist']:
search_result['browseId'] = data['navigationEndpoint']['browseEndpoint']['browseId']
search_result['browseId'] = nav(data, NAVIGATION_BROWSE_ID)
if resultType in ['artist']:
search_result['artist'] = get_item_text(data, 0)
@ -116,12 +185,14 @@ def parse_search_result(data, resultType = None):
search_result['itemCount'] = get_item_text(data, 2 + default).split(' ')[0]
elif resultType in ['song']:
search_result['artists'] = parse_song_artists(data, 1 + default)
hasAlbum = len(data['flexColumns']) == 4 + default
if hasAlbum:
search_result['album'] = get_item_text(data, 2 + default)
search_result['album'] = parse_song_album(data, 2 + default)
search_result['duration'] = get_item_text(data, 2 + hasAlbum + default)
elif resultType in ['video']:
search_result['artist'] = get_item_text(data, 1 + default)
search_result['views'] = get_item_text(data, 2 + default).split(' ')[0]
search_result['duration'] = get_item_text(data, 3 + default)
@ -130,11 +201,44 @@ def parse_search_result(data, resultType = None):
return search_result
def parse_song_artists(data, index):
flex_item = get_flex_column_item(data, index)
artists = []
for j in range(int(len(flex_item['text']['runs']) / 2) + 1):
artists.append({
'name': get_item_text(data, index, j * 2),
'id': get_browse_id(flex_item, j * 2)
})
return artists
def parse_song_album(data, index):
flex_item = get_flex_column_item(data, index)
return None if not flex_item else {
'name': get_item_text(data, index),
'id': get_browse_id(flex_item, 0)
}
def get_item_text(item, index, run_index=0):
if 'runs' not in item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']['text']:
column = get_flex_column_item(item, index)
return column if not column else column['text']['runs'][run_index]['text']
def get_flex_column_item(item, index):
if 'text' not in item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer'] or \
'runs' not in item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']['text']:
return None
return item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][run_index]['text']
return item['flexColumns'][index]['musicResponsiveListItemFlexColumnRenderer']
def get_browse_id(item, index):
if 'navigationEndpoint' not in item['text']['runs'][index]:
return None
else:
return nav(item['text']['runs'][index], NAVIGATION_BROWSE_ID)
def get_continuations(results, continuation_type, per_page, limit, request_func, parse_func):
@ -145,7 +249,8 @@ def get_continuations(results, continuation_type, per_page, limit, request_func,
additionalParams = "&ctoken=" + ctoken + "&continuation=" + ctoken
response = request_func(additionalParams)
results = response['continuationContents'][continuation_type]
items.extend(parse_func(results['contents']))
continuation_contents = 'contents' if 'Shelf' in continuation_type else 'items'
items.extend(parse_func(results[continuation_contents]))
request_count += 1
return items
@ -157,8 +262,20 @@ def nav(root, items):
return root
def find_object_by_key(object_list, key):
def find_object_by_key(object_list, key, nested=None):
for item in object_list:
if nested:
item = item[nested]
if key in item:
return item
return None
def find_objects_by_key(object_list, key, nested=None):
objects = []
for item in object_list:
if nested:
item = item[nested]
if key in item:
objects.append(item)
return objects