From 7ebb57d2a53ab43920e6b3f2639eea395ecaa266 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Thu, 10 Mar 2022 17:36:51 +0100 Subject: [PATCH] get_watch_playlist: parse and return counterpart for songs with video counterparts --- ytmusicapi/mixins/watch.py | 66 +++++++++++++++++++++++++++++++------ ytmusicapi/parsers/watch.py | 59 +++++++++++++++++++-------------- 2 files changed, 91 insertions(+), 34 deletions(-) diff --git a/ytmusicapi/mixins/watch.py b/ytmusicapi/mixins/watch.py index bbe3315..190b851 100644 --- a/ytmusicapi/mixins/watch.py +++ b/ytmusicapi/mixins/watch.py @@ -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", diff --git a/ytmusicapi/parsers/watch.py b/ytmusicapi/parsers/watch.py index 46f9057..f6e5ba0 100644 --- a/ytmusicapi/parsers/watch.py +++ b/ytmusicapi/parsers/watch.py @@ -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