get_watch_playlist: parse and return counterpart for songs with video counterparts
This commit is contained in:
parent
e1c77f1da0
commit
7ebb57d2a5
2 changed files with 91 additions and 34 deletions
|
|
@ -20,26 +20,72 @@ class WatchMixin:
|
|||
:param playlistId: playlistId of the played playlist or album
|
||||
:param limit: minimum number of watch playlist items to return
|
||||
:param params: only used internally by :py:func:`get_watch_playlist_shuffle`
|
||||
:return: List of watch playlist items.
|
||||
:return: List of watch playlist items. The counterpart key is optional and only
|
||||
appears if a song has a corresponding video counterpart (UI song/video
|
||||
switcher).
|
||||
|
||||
Example::
|
||||
|
||||
{
|
||||
"tracks": [
|
||||
{
|
||||
"title": "Interstellar (Main Theme) - Piano Version",
|
||||
"byline": "Patrik Pietschmann • 47M views",
|
||||
"length": "4:47",
|
||||
"videoId": "4y33h81phKU",
|
||||
"videoId": "9mWr4c_ig54",
|
||||
"title": "Foolish Of Me (feat. Jonathan Mendelsohn)",
|
||||
"length": "3:07",
|
||||
"thumbnail": [
|
||||
{
|
||||
"url": "https://i.ytimg.com/vi/4y...",
|
||||
"width": 400,
|
||||
"height": 225
|
||||
"url": "https://lh3.googleusercontent.com/ulK2YaLtOW0PzcN7ufltG6e4ae3WZ9Bvg8CCwhe6LOccu1lCKxJy2r5AsYrsHeMBSLrGJCNpJqXgwczk=w60-h60-l90-rj",
|
||||
"width": 60,
|
||||
"height": 60
|
||||
}...
|
||||
],
|
||||
"feedbackTokens": {
|
||||
"add": "AB9zfpIGg9XN4u2iJ...",
|
||||
"remove": "AB9zfpJdzWLcdZtC..."
|
||||
},
|
||||
"likeStatus": "INDIFFERENT",
|
||||
"artists": [
|
||||
{
|
||||
"name": "Seven Lions",
|
||||
"id": "UCYd2yzYRx7b9FYnBSlbnknA"
|
||||
},
|
||||
{
|
||||
"name": "Jason Ross",
|
||||
"id": "UCVCD9Iwnqn2ipN9JIF6B-nA"
|
||||
},
|
||||
{
|
||||
"name": "Crystal Skies",
|
||||
"id": "UCTJZESxeZ0J_M7JXyFUVmvA"
|
||||
}
|
||||
],
|
||||
"feedbackTokens": [],
|
||||
"likeStatus": "LIKE"
|
||||
"album": {
|
||||
"name": "Foolish Of Me",
|
||||
"id": "MPREb_C8aRK1qmsDJ"
|
||||
},
|
||||
"year": "2020",
|
||||
"counterpart": {
|
||||
"videoId": "E0S4W34zFMA",
|
||||
"title": "Foolish Of Me [ABGT404] (feat. Jonathan Mendelsohn)",
|
||||
"length": "3:07",
|
||||
"thumbnail": [...],
|
||||
"feedbackTokens": null,
|
||||
"likeStatus": "LIKE",
|
||||
"artists": [
|
||||
{
|
||||
"name": "Jason Ross",
|
||||
"id": null
|
||||
},
|
||||
{
|
||||
"name": "Seven Lions",
|
||||
"id": null
|
||||
},
|
||||
{
|
||||
"name": "Crystal Skies",
|
||||
"id": null
|
||||
}
|
||||
],
|
||||
"views": "6.6K"
|
||||
}
|
||||
},...
|
||||
],
|
||||
"playlistId": "RDAMVM4y33h81phKU",
|
||||
|
|
|
|||
|
|
@ -4,35 +4,46 @@ from .songs import *
|
|||
|
||||
def parse_watch_playlist(results):
|
||||
tracks = []
|
||||
PPVWR = 'playlistPanelVideoWrapperRenderer'
|
||||
PPVR = 'playlistPanelVideoRenderer'
|
||||
for result in results:
|
||||
if 'playlistPanelVideoWrapperRenderer' in result:
|
||||
result = result['playlistPanelVideoWrapperRenderer']['primaryRenderer']
|
||||
if 'playlistPanelVideoRenderer' not in result:
|
||||
counterpart = None
|
||||
if PPVWR in result:
|
||||
counterpart = result[PPVWR]['counterpart'][0]['counterpartRenderer'][PPVR]
|
||||
result = result[PPVWR]['primaryRenderer']
|
||||
if PPVR not in result:
|
||||
continue
|
||||
data = result['playlistPanelVideoRenderer']
|
||||
data = result[PPVR]
|
||||
if 'unplayableText' in data:
|
||||
continue
|
||||
|
||||
feedback_tokens = like_status = None
|
||||
for item in nav(data, MENU_ITEMS):
|
||||
if TOGGLE_MENU in item:
|
||||
service = item[TOGGLE_MENU]['defaultServiceEndpoint']
|
||||
if 'feedbackEndpoint' in service:
|
||||
feedback_tokens = parse_song_menu_tokens(item)
|
||||
if 'likeEndpoint' in service:
|
||||
like_status = parse_like_status(service)
|
||||
|
||||
song_info = parse_song_runs(data['longBylineText']['runs'])
|
||||
|
||||
track = {
|
||||
'videoId': data['videoId'],
|
||||
'title': nav(data, TITLE_TEXT),
|
||||
'length': nav(data, ['lengthText', 'runs', 0, 'text'], True),
|
||||
'thumbnail': nav(data, THUMBNAIL),
|
||||
'feedbackTokens': feedback_tokens,
|
||||
'likeStatus': like_status
|
||||
}
|
||||
track.update(song_info)
|
||||
track = parse_watch_track(data)
|
||||
if counterpart:
|
||||
track['counterpart'] = parse_watch_track(counterpart)
|
||||
tracks.append(track)
|
||||
|
||||
return tracks
|
||||
|
||||
|
||||
def parse_watch_track(data):
|
||||
feedback_tokens = like_status = None
|
||||
for item in nav(data, MENU_ITEMS):
|
||||
if TOGGLE_MENU in item:
|
||||
service = item[TOGGLE_MENU]['defaultServiceEndpoint']
|
||||
if 'feedbackEndpoint' in service:
|
||||
feedback_tokens = parse_song_menu_tokens(item)
|
||||
if 'likeEndpoint' in service:
|
||||
like_status = parse_like_status(service)
|
||||
|
||||
song_info = parse_song_runs(data['longBylineText']['runs'])
|
||||
|
||||
track = {
|
||||
'videoId': data['videoId'],
|
||||
'title': nav(data, TITLE_TEXT),
|
||||
'length': nav(data, ['lengthText', 'runs', 0, 'text'], True),
|
||||
'thumbnail': nav(data, THUMBNAIL),
|
||||
'feedbackTokens': feedback_tokens,
|
||||
'likeStatus': like_status
|
||||
}
|
||||
track.update(song_info)
|
||||
return track
|
||||
|
|
|
|||
Loading…
Reference in a new issue