Restructure parsers
This commit is contained in:
parent
3b46fda393
commit
5a727607ec
14 changed files with 484 additions and 475 deletions
94
ytmusicapi/parsers/utils.py
Normal file
94
ytmusicapi/parsers/utils.py
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
from . import *
|
||||
|
||||
|
||||
def parse_song_artists(data, index):
|
||||
flex_item = get_flex_column_item(data, index)
|
||||
if not flex_item:
|
||||
return None
|
||||
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, none_if_absent=False):
|
||||
column = get_flex_column_item(item, index)
|
||||
if not column:
|
||||
return None
|
||||
if none_if_absent and len(column['text']['runs']) < run_index + 1:
|
||||
return None
|
||||
return 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']
|
||||
|
||||
|
||||
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, limit, request_func, parse_func, ctoken_path=""):
|
||||
items = []
|
||||
while 'continuations' in results and len(items) < limit:
|
||||
ctoken = nav(
|
||||
results,
|
||||
['continuations', 0, 'next' + ctoken_path + 'ContinuationData', 'continuation'])
|
||||
additionalParams = "&ctoken=" + ctoken + "&continuation=" + ctoken
|
||||
response = request_func(additionalParams)
|
||||
results = response['continuationContents'][continuation_type]
|
||||
continuation_contents = 'contents' if 'contents' in results else 'items'
|
||||
items.extend(parse_func(results[continuation_contents]))
|
||||
|
||||
return items
|
||||
|
||||
|
||||
def nav(root, items, none_if_absent=False):
|
||||
"""Access a nested object in root by item sequence."""
|
||||
try:
|
||||
for k in items:
|
||||
root = root[k]
|
||||
return root
|
||||
except Exception as err:
|
||||
if none_if_absent:
|
||||
return None
|
||||
else:
|
||||
raise err
|
||||
|
||||
|
||||
def find_object_by_key(object_list, key, nested=None, is_key=False):
|
||||
for item in object_list:
|
||||
if nested:
|
||||
item = item[nested]
|
||||
if key in item:
|
||||
return item[key] if is_key else 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
|
||||
Loading…
Reference in a new issue