From 9f93b9429c77e631972186049fbc7518e2cf5d4b Mon Sep 17 00:00:00 2001 From: James Taylor Date: Thu, 9 May 2019 23:07:43 -0700 Subject: Fix comment parsing error due to comments from deleted channels --- youtube/comments.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/youtube/comments.py b/youtube/comments.py index 4087b47..94b086e 100644 --- a/youtube/comments.py +++ b/youtube/comments.py @@ -234,10 +234,7 @@ def parse_comments_polymer(content, replies=False): comment_raw = comment_raw['commentRenderer'] comment = { - 'author': yt_data_extract.get_plain_text(comment_raw['authorText']), - 'author_url': comment_raw['authorEndpoint']['commandMetadata']['webCommandMetadata']['url'], - 'author_channel_id': comment_raw['authorEndpoint']['browseEndpoint']['browseId'], - 'author_id': comment_raw['authorId'], + 'author_id': comment_raw.get('authorId', ''), 'author_avatar': comment_raw['authorThumbnail']['thumbnails'][0]['url'], 'likes': comment_raw['likeCount'], 'published': yt_data_extract.get_plain_text(comment_raw['publishedTimeText']), @@ -247,6 +244,16 @@ def parse_comments_polymer(content, replies=False): 'video_id': video_id, 'comment_id': comment_raw['commentId'], } + + if 'authorText' in comment_raw: # deleted channels have no name or channel link + comment['author'] = yt_data_extract.get_plain_text(comment_raw['authorText']) + comment['author_url'] = comment_raw['authorEndpoint']['commandMetadata']['webCommandMetadata']['url'] + comment['author_channel_id'] = comment_raw['authorEndpoint']['browseEndpoint']['browseId'] + else: + comment['author'] = '' + comment['author_url'] = '' + comment['author_channel_id'] = '' + comments.append(comment) except Exception as e: print('Error parsing comments: ' + str(e)) -- cgit v1.2.3