Rewrite song/video search result parsing to be less error-prone
This commit is contained in:
parent
a45e872d16
commit
5cbbf73c50
3 changed files with 51 additions and 71 deletions
|
|
@ -26,29 +26,6 @@ class Parser:
|
||||||
resultType = result_types[result_types_local.index(resultType)]
|
resultType = result_types[result_types_local.index(resultType)]
|
||||||
|
|
||||||
search_result['resultType'] = resultType
|
search_result['resultType'] = resultType
|
||||||
last_artist_index = default_offset
|
|
||||||
runs = []
|
|
||||||
|
|
||||||
if resultType in ['song', 'video']:
|
|
||||||
search_result['videoId'] = nav(
|
|
||||||
data, PLAY_BUTTON + ['playNavigationEndpoint', 'watchEndpoint', 'videoId'],
|
|
||||||
True)
|
|
||||||
search_result['title'] = get_item_text(data, 0)
|
|
||||||
|
|
||||||
runs = get_flex_column_item(data, 1)['text']['runs']
|
|
||||||
last_artist_index = get_last_artist_index(runs)
|
|
||||||
|
|
||||||
# unavailable song in default search
|
|
||||||
if not search_result['videoId'] and default_offset:
|
|
||||||
default_offset = 0
|
|
||||||
|
|
||||||
search_result['artists'] = parse_song_artists_runs(
|
|
||||||
runs[default_offset:last_artist_index + 1])
|
|
||||||
|
|
||||||
if resultType in ['artist', 'album', 'playlist']:
|
|
||||||
search_result['browseId'] = nav(data, NAVIGATION_BROWSE_ID, True)
|
|
||||||
if not search_result['browseId']:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if resultType in ['artist']:
|
if resultType in ['artist']:
|
||||||
search_result['artist'] = get_item_text(data, 0)
|
search_result['artist'] = get_item_text(data, 0)
|
||||||
|
|
@ -67,10 +44,7 @@ class Parser:
|
||||||
default_offset + 2).split(' ')[0]
|
default_offset + 2).split(' ')[0]
|
||||||
|
|
||||||
elif resultType in ['song']:
|
elif resultType in ['song']:
|
||||||
search_result['album'] = parse_song_album_runs(runs, last_artist_index)
|
search_result['duration'] = None
|
||||||
search_result['duration'] = None if not re.match(
|
|
||||||
r"^\d+:\d+$", runs[-1]['text']) else runs[-1]['text']
|
|
||||||
search_result['isExplicit'] = nav(data, BADGE_LABEL, True) == 'Explicit'
|
|
||||||
if 'menu' in data:
|
if 'menu' in data:
|
||||||
toggle_menu = find_object_by_key(nav(data, MENU_ITEMS),
|
toggle_menu = find_object_by_key(nav(data, MENU_ITEMS),
|
||||||
'toggleMenuServiceItemRenderer')
|
'toggleMenuServiceItemRenderer')
|
||||||
|
|
@ -78,9 +52,7 @@ class Parser:
|
||||||
search_result['feedbackTokens'] = parse_song_menu_tokens(toggle_menu)
|
search_result['feedbackTokens'] = parse_song_menu_tokens(toggle_menu)
|
||||||
|
|
||||||
elif resultType in ['video']:
|
elif resultType in ['video']:
|
||||||
search_result['views'] = get_item_text(data, 1,
|
search_result['views'] = None
|
||||||
2 + last_artist_index).split(' ')[0]
|
|
||||||
search_result['duration'] = get_item_text(data, 1, 4 + last_artist_index, True)
|
|
||||||
|
|
||||||
elif resultType in ['upload']:
|
elif resultType in ['upload']:
|
||||||
search_result['title'] = get_item_text(data, 0)
|
search_result['title'] = get_item_text(data, 0)
|
||||||
|
|
@ -121,6 +93,20 @@ class Parser:
|
||||||
search_result['releaseDate'] = runs[2]
|
search_result['releaseDate'] = runs[2]
|
||||||
search_result['resultType'] = 'album'
|
search_result['resultType'] = 'album'
|
||||||
|
|
||||||
|
if resultType in ['song', 'video']:
|
||||||
|
search_result['videoId'] = nav(
|
||||||
|
data, PLAY_BUTTON + ['playNavigationEndpoint', 'watchEndpoint', 'videoId'],
|
||||||
|
True)
|
||||||
|
search_result['title'] = get_item_text(data, 0)
|
||||||
|
|
||||||
|
song_info = parse_song_runs(get_flex_column_item(data, 1)['text']['runs'])
|
||||||
|
search_result.update(song_info)
|
||||||
|
|
||||||
|
if resultType in ['artist', 'album', 'playlist']:
|
||||||
|
search_result['browseId'] = nav(data, NAVIGATION_BROWSE_ID, True)
|
||||||
|
if not search_result['browseId']:
|
||||||
|
continue
|
||||||
|
|
||||||
if resultType in ['song', 'album']:
|
if resultType in ['song', 'album']:
|
||||||
search_result['isExplicit'] = nav(data, BADGE_LABEL, True) == 'Explicit'
|
search_result['isExplicit'] = nav(data, BADGE_LABEL, True) == 'Explicit'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
from . import *
|
from . import *
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
def parse_song_artists(data, index):
|
def parse_song_artists(data, index):
|
||||||
|
|
@ -6,34 +7,42 @@ def parse_song_artists(data, index):
|
||||||
if not flex_item:
|
if not flex_item:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return parse_song_artists_runs(flex_item['text']['runs'])
|
runs = flex_item['text']['runs']
|
||||||
|
|
||||||
|
|
||||||
def parse_song_artists_runs(runs):
|
|
||||||
artists = []
|
artists = []
|
||||||
for j in range(int(len(runs) / 2) + 1):
|
for j in range(int(len(runs) / 2) + 1):
|
||||||
artists.append({
|
artists.append({
|
||||||
'name': runs[j * 2]['text'],
|
'name': runs[j * 2]['text'],
|
||||||
'id': nav(runs[j * 2], NAVIGATION_BROWSE_ID, True)
|
'id': nav(runs[j * 2], NAVIGATION_BROWSE_ID, True)
|
||||||
})
|
})
|
||||||
|
|
||||||
return artists
|
return artists
|
||||||
|
|
||||||
|
|
||||||
def get_last_artist_index(runs):
|
def parse_song_runs(runs):
|
||||||
try:
|
parsed = {'artists': []}
|
||||||
return next(
|
for run in runs:
|
||||||
len(runs) - i - 1 for i, run in enumerate(reversed(runs))
|
text = run['text']
|
||||||
if 'navigationEndpoint' in run and
|
if 'navigationEndpoint' in run: # artist or album
|
||||||
(nav(run, NAVIGATION_BROWSE_ID).startswith('UC') or nav(
|
item = {
|
||||||
run, NAVIGATION_BROWSE_ID).startswith('FEmusic_library_privately_owned_artist')))
|
'name': text,
|
||||||
except StopIteration: # try to find album if no artist IDs available
|
'id': nav(run, NAVIGATION_BROWSE_ID, True)
|
||||||
if 'navigationEndpoint' in runs[-3] or runs[-3]['text'].endswith('views'): # has album
|
}
|
||||||
return len(runs) - 5
|
|
||||||
elif runs[-1]['text'].endswith('views'):
|
if item['id'] and item['id'].startswith('MPRE'): # album
|
||||||
return len(runs) - 3
|
parsed['album'] = item
|
||||||
|
else: # artist
|
||||||
|
parsed['artists'].append(item)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return 0
|
if text.endswith(' views'):
|
||||||
|
parsed['views'] = text.split(' ')[0]
|
||||||
|
|
||||||
|
elif re.match(r"^\d+:\d+$", text):
|
||||||
|
parsed['duration'] = text
|
||||||
|
|
||||||
|
elif re.match(r"^\d{4}$", text):
|
||||||
|
parsed['year'] = text
|
||||||
|
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
def parse_song_album(data, index):
|
def parse_song_album(data, index):
|
||||||
|
|
@ -44,17 +53,6 @@ def parse_song_album(data, index):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def parse_song_album_runs(runs, last_artist_index):
|
|
||||||
if len(runs) > last_artist_index + 1 and type(
|
|
||||||
runs[last_artist_index + 2]) is dict: # 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):
|
def parse_song_menu_tokens(item):
|
||||||
library_add_token = library_remove_token = None
|
library_add_token = library_remove_token = None
|
||||||
toggle_menu = item['toggleMenuServiceItemRenderer']
|
toggle_menu = item['toggleMenuServiceItemRenderer']
|
||||||
|
|
|
||||||
|
|
@ -20,22 +20,18 @@ def parse_watch_playlist(results):
|
||||||
status = ['LIKE', 'INDIFFERENT']
|
status = ['LIKE', 'INDIFFERENT']
|
||||||
like_status = status[status.index(service['likeEndpoint']['status']) - 1]
|
like_status = status[status.index(service['likeEndpoint']['status']) - 1]
|
||||||
|
|
||||||
runs = data['longBylineText']['runs']
|
song_info = parse_song_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 = {
|
track = {
|
||||||
'videoId': data['videoId'],
|
'videoId': data['videoId'],
|
||||||
'title': nav(data, TITLE_TEXT),
|
'title': nav(data, TITLE_TEXT),
|
||||||
'artists': artists,
|
|
||||||
'album': album,
|
|
||||||
'length': nav(data, ['lengthText', 'runs', 0, 'text'], True),
|
'length': nav(data, ['lengthText', 'runs', 0, 'text'], True),
|
||||||
'playlistId': nav(data, NAVIGATION_PLAYLIST_ID),
|
'playlistId': nav(data, NAVIGATION_PLAYLIST_ID),
|
||||||
'thumbnail': nav(data, THUMBNAIL),
|
'thumbnail': nav(data, THUMBNAIL),
|
||||||
'feedbackTokens': feedback_tokens,
|
'feedbackTokens': feedback_tokens,
|
||||||
'likeStatus': like_status
|
'likeStatus': like_status
|
||||||
}
|
}
|
||||||
|
track.update(song_info)
|
||||||
tracks.append(track)
|
tracks.append(track)
|
||||||
|
|
||||||
return tracks
|
return tracks
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue