From 0aa284e860e5091684d4c193a22f354589ed9262 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Thu, 26 Aug 2021 22:51:16 +0200 Subject: [PATCH] get_charts: fix views, add video artists --- ytmusicapi/parsers/browsing.py | 5 +++-- ytmusicapi/parsers/explore.py | 2 +- ytmusicapi/parsers/songs.py | 18 +++++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/ytmusicapi/parsers/browsing.py b/ytmusicapi/parsers/browsing.py index 76919a6..5cb081f 100644 --- a/ytmusicapi/parsers/browsing.py +++ b/ytmusicapi/parsers/browsing.py @@ -179,14 +179,15 @@ def parse_single(result): def parse_video(result): + runs = result['subtitle']['runs'] video = { 'title': nav(result, TITLE_TEXT), 'videoId': nav(result, NAVIGATION_VIDEO_ID), + 'artists': parse_song_artists_runs(runs[:-2]), 'playlistId': nav(result, NAVIGATION_PLAYLIST_ID, True), 'thumbnails': nav(result, THUMBNAIL_RENDERER, True) } - if len(result['subtitle']['runs']) == 3: - video['views'] = nav(result, SUBTITLE2).split(' ')[0] + video['views'] = runs[-1]['text'].split(' ')[0] return video diff --git a/ytmusicapi/parsers/explore.py b/ytmusicapi/parsers/explore.py index 2b64854..5abbd36 100644 --- a/ytmusicapi/parsers/explore.py +++ b/ytmusicapi/parsers/explore.py @@ -20,7 +20,7 @@ def parse_chart_song(data): } else: flex_1 = get_flex_column_item(data, 1) - parsed['views'] = nav(flex_1, ['text', 'runs', 2, 'text']).split(' ')[0] + parsed['views'] = nav(flex_1, ['text', 'runs', -1, 'text']).split(' ')[0] parsed.update(parse_ranking(data)) return parsed diff --git a/ytmusicapi/parsers/songs.py b/ytmusicapi/parsers/songs.py index a5dcd82..60bbdde 100644 --- a/ytmusicapi/parsers/songs.py +++ b/ytmusicapi/parsers/songs.py @@ -8,13 +8,17 @@ def parse_song_artists(data, index): return None else: runs = flex_item['text']['runs'] - artists = [] - for j in range(int(len(runs) / 2) + 1): - artists.append({ - 'name': runs[j * 2]['text'], - 'id': nav(runs[j * 2], NAVIGATION_BROWSE_ID, True) - }) - return artists + return parse_song_artists_runs(runs) + + +def parse_song_artists_runs(runs): + artists = [] + for j in range(int(len(runs) / 2) + 1): + artists.append({ + 'name': runs[j * 2]['text'], + 'id': nav(runs[j * 2], NAVIGATION_BROWSE_ID, True) + }) + return artists def parse_song_runs(runs):