get_watch_playlist: parse and return counterpart for songs with video counterparts

This commit is contained in:
sigma67 2022-03-10 17:36:51 +01:00
commit 7ebb57d2a5
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
2 changed files with 91 additions and 34 deletions

View file

@ -20,26 +20,72 @@ class WatchMixin:
:param playlistId: playlistId of the played playlist or album :param playlistId: playlistId of the played playlist or album
:param limit: minimum number of watch playlist items to return :param limit: minimum number of watch playlist items to return
:param params: only used internally by :py:func:`get_watch_playlist_shuffle` :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:: Example::
{ {
"tracks": [ "tracks": [
{ {
"title": "Interstellar (Main Theme) - Piano Version", "videoId": "9mWr4c_ig54",
"byline": "Patrik Pietschmann • 47M views", "title": "Foolish Of Me (feat. Jonathan Mendelsohn)",
"length": "4:47", "length": "3:07",
"videoId": "4y33h81phKU",
"thumbnail": [ "thumbnail": [
{ {
"url": "https://i.ytimg.com/vi/4y...", "url": "https://lh3.googleusercontent.com/ulK2YaLtOW0PzcN7ufltG6e4ae3WZ9Bvg8CCwhe6LOccu1lCKxJy2r5AsYrsHeMBSLrGJCNpJqXgwczk=w60-h60-l90-rj",
"width": 400, "width": 60,
"height": 225 "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": [], "album": {
"likeStatus": "LIKE" "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", "playlistId": "RDAMVM4y33h81phKU",

View file

@ -4,15 +4,28 @@ from .songs import *
def parse_watch_playlist(results): def parse_watch_playlist(results):
tracks = [] tracks = []
PPVWR = 'playlistPanelVideoWrapperRenderer'
PPVR = 'playlistPanelVideoRenderer'
for result in results: for result in results:
if 'playlistPanelVideoWrapperRenderer' in result: counterpart = None
result = result['playlistPanelVideoWrapperRenderer']['primaryRenderer'] if PPVWR in result:
if 'playlistPanelVideoRenderer' not in result: counterpart = result[PPVWR]['counterpart'][0]['counterpartRenderer'][PPVR]
result = result[PPVWR]['primaryRenderer']
if PPVR not in result:
continue continue
data = result['playlistPanelVideoRenderer'] data = result[PPVR]
if 'unplayableText' in data: if 'unplayableText' in data:
continue continue
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 feedback_tokens = like_status = None
for item in nav(data, MENU_ITEMS): for item in nav(data, MENU_ITEMS):
if TOGGLE_MENU in item: if TOGGLE_MENU in item:
@ -33,6 +46,4 @@ def parse_watch_playlist(results):
'likeStatus': like_status 'likeStatus': like_status
} }
track.update(song_info) track.update(song_info)
tracks.append(track) return track
return tracks