From 56e7751da7f8bb8b8108871ce46e50310dc73a9f Mon Sep 17 00:00:00 2001 From: James Taylor Date: Sun, 8 Mar 2020 16:17:04 -0700 Subject: Fix failure to parse comments when there's one from deleted channel Specifically, fix failures when any of the fields from the parsed comment are None, such as author, author_url, etc. (failure due to string concatenation when building urls). --- youtube/comments.py | 35 +++++++++++++++++++++++------------ youtube/util.py | 9 +++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/youtube/comments.py b/youtube/comments.py index 4e79d8b..07d4b89 100644 --- a/youtube/comments.py +++ b/youtube/comments.py @@ -1,4 +1,5 @@ from youtube import proto, util, yt_data_extract, accounts +from youtube.util import concat_or_none from youtube import yt_app import settings @@ -88,22 +89,29 @@ def single_comment_ctoken(video_id, comment_id): def post_process_comments_info(comments_info): for comment in comments_info['comments']: - comment['author_url'] = util.URL_ORIGIN + comment['author_url'] - comment['author_avatar'] = '/' + comment['author_avatar'] + comment['author_url'] = concat_or_none( + util.URL_ORIGIN, comment['author_url']) + comment['author_avatar'] = concat_or_none( + '/', comment['author_avatar']) - comment['permalink'] = util.URL_ORIGIN + '/watch?v=' + comments_info['video_id'] + '&lc=' + comment['id'] + comment['permalink'] = concat_or_none(util.URL_ORIGIN, '/watch?v=', + comments_info['video_id'], '&lc=', comment['id']) if comment['author_id'] in accounts.accounts: - comment['delete_url'] = (util.URL_ORIGIN + '/delete_comment?video_id=' - + comments_info['video_id'] - + '&channel_id='+ comment['author_id'] - + '&comment_id=' + comment['id']) + comment['delete_url'] = concat_or_none(util.URL_ORIGIN, + '/delete_comment?video_id=', comments_info['video_id'], + '&channel_id=', comment['author_id'], + '&comment_id=', comment['id']) reply_count = comment['reply_count'] if reply_count == 0: - comment['replies_url'] = util.URL_ORIGIN + '/post_comment?parent_id=' + comment['id'] + "&video_id=" + comments_info['video_id'] + comment['replies_url'] = concat_or_none(util.URL_ORIGIN, + '/post_comment?parent_id=', comment['id'], + '&video_id=', comments_info['video_id']) else: - comment['replies_url'] = util.URL_ORIGIN + '/comments?parent_id=' + comment['id'] + "&video_id=" + comments_info['video_id'] + comment['replies_url'] = concat_or_none(util.URL_ORIGIN, + '/comments?parent_id=', comment['id'], + '&video_id=', comments_info['video_id']) if reply_count == 0: comment['view_replies_text'] = 'Reply' @@ -120,7 +128,8 @@ def post_process_comments_info(comments_info): comments_info['include_avatars'] = settings.enable_comment_avatars if comments_info['ctoken']: - comments_info['more_comments_url'] = util.URL_ORIGIN + '/comments?ctoken=' + comments_info['ctoken'] + comments_info['more_comments_url'] = concat_or_none(util.URL_ORIGIN, + '/comments?ctoken=', comments_info['ctoken']) comments_info['page_number'] = page_number = str(int(comments_info['offset']/20) + 1) @@ -128,8 +137,10 @@ def post_process_comments_info(comments_info): comments_info['sort_text'] = 'top' if comments_info['sort'] == 0 else 'newest' - comments_info['video_url'] = util.URL_ORIGIN + '/watch?v=' + comments_info['video_id'] - comments_info['video_thumbnail'] = '/i.ytimg.com/vi/'+ comments_info['video_id'] + '/mqdefault.jpg' + comments_info['video_url'] = concat_or_none(util.URL_ORIGIN, + '/watch?v=', comments_info['video_id']) + comments_info['video_thumbnail'] = concat_or_none('/i.ytimg.com/vi/', + comments_info['video_id'], '/mqdefault.jpg') def video_comments(video_id, sort=0, offset=0, lc='', secret_key=''): diff --git a/youtube/util.py b/youtube/util.py index a70bd7e..ec25d40 100644 --- a/youtube/util.py +++ b/youtube/util.py @@ -357,6 +357,15 @@ def left_remove(string, substring): return string[len(substring):] return string +def concat_or_none(*strings): + '''Concatenates strings. Returns None if any of the arguments are None''' + result = '' + for string in strings: + if string is None: + return None + result += string + return result + def prefix_urls(item): try: -- cgit v1.2.3