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).
This commit is contained in:
parent
fa112592fa
commit
56e7751da7
@ -1,4 +1,5 @@
|
|||||||
from youtube import proto, util, yt_data_extract, accounts
|
from youtube import proto, util, yt_data_extract, accounts
|
||||||
|
from youtube.util import concat_or_none
|
||||||
from youtube import yt_app
|
from youtube import yt_app
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
@ -88,22 +89,29 @@ def single_comment_ctoken(video_id, comment_id):
|
|||||||
|
|
||||||
def post_process_comments_info(comments_info):
|
def post_process_comments_info(comments_info):
|
||||||
for comment in comments_info['comments']:
|
for comment in comments_info['comments']:
|
||||||
comment['author_url'] = util.URL_ORIGIN + comment['author_url']
|
comment['author_url'] = concat_or_none(
|
||||||
comment['author_avatar'] = '/' + comment['author_avatar']
|
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:
|
if comment['author_id'] in accounts.accounts:
|
||||||
comment['delete_url'] = (util.URL_ORIGIN + '/delete_comment?video_id='
|
comment['delete_url'] = concat_or_none(util.URL_ORIGIN,
|
||||||
+ comments_info['video_id']
|
'/delete_comment?video_id=', comments_info['video_id'],
|
||||||
+ '&channel_id='+ comment['author_id']
|
'&channel_id=', comment['author_id'],
|
||||||
+ '&comment_id=' + comment['id'])
|
'&comment_id=', comment['id'])
|
||||||
|
|
||||||
reply_count = comment['reply_count']
|
reply_count = comment['reply_count']
|
||||||
if reply_count == 0:
|
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:
|
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:
|
if reply_count == 0:
|
||||||
comment['view_replies_text'] = 'Reply'
|
comment['view_replies_text'] = 'Reply'
|
||||||
@ -120,7 +128,8 @@ def post_process_comments_info(comments_info):
|
|||||||
|
|
||||||
comments_info['include_avatars'] = settings.enable_comment_avatars
|
comments_info['include_avatars'] = settings.enable_comment_avatars
|
||||||
if comments_info['ctoken']:
|
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)
|
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['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_url'] = concat_or_none(util.URL_ORIGIN,
|
||||||
comments_info['video_thumbnail'] = '/i.ytimg.com/vi/'+ comments_info['video_id'] + '/mqdefault.jpg'
|
'/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=''):
|
def video_comments(video_id, sort=0, offset=0, lc='', secret_key=''):
|
||||||
|
@ -357,6 +357,15 @@ def left_remove(string, substring):
|
|||||||
return string[len(substring):]
|
return string[len(substring):]
|
||||||
return string
|
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):
|
def prefix_urls(item):
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user