From ffeccc5ac3740080a6597e74af35e03e53425318 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Sat, 1 Sep 2018 21:53:40 -0700 Subject: Reorganize comment system --- youtube/comments.py | 112 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 86 insertions(+), 26 deletions(-) (limited to 'youtube/comments.py') diff --git a/youtube/comments.py b/youtube/comments.py index 2913bde..16c90a4 100644 --- a/youtube/comments.py +++ b/youtube/comments.py @@ -8,6 +8,15 @@ import urllib import html import settings import re +comment_area_template = Template(''' +
+$video-metadata +$comment-links +$comment-box +$comments +$more-comments-button +
+''') comment_template = Template('''
@@ -80,11 +89,11 @@ def ctoken_metadata(ctoken): result['offset'] = offset_information.get(5, 0) result['is_replies'] = False - if (3 in offset_information) and (2 in offset_information[3]): + if (3 in offset_information) and (2 in proto.parse(offset_information[3])): result['is_replies'] = True else: try: - result['sort'] = offset_information[4][6] + result['sort'] = proto.parse(offset_information[4])[6] except KeyError: result['sort'] = 0 return result @@ -119,8 +128,8 @@ def request_comments(ctoken, replies=False): print("got , retrying") continue break - '''with open('debug/comments_debug', 'wb') as f: - f.write(content)''' + with open('debug/comments_debug', 'wb') as f: + f.write(content) return content def parse_comments_ajax(content, replies=False): @@ -163,11 +172,15 @@ def parse_comments_polymer(content, replies=False): try: video_title = '' content = json.loads(uppercase_escape(content.decode('utf-8'))) + url = content[1]['url'] + ctoken = urllib.parse.parse_qs(url[url.find('?')+1:])['ctoken'][0] + video_id = ctoken_metadata(ctoken)['video_id'] #print(content) try: comments_raw = content[1]['response']['continuationContents']['commentSectionContinuation']['items'] except KeyError: comments_raw = content[1]['response']['continuationContents']['commentRepliesContinuation']['contents'] + replies = True ctoken = default_multi_get(content, 1, 'response', 'continuationContents', 'commentSectionContinuation', 'continuations', 0, 'nextContinuationData', 'continuation', default='') @@ -192,6 +205,10 @@ def parse_comments_polymer(content, replies=False): view_replies_text = '1 reply' else: view_replies_text = match.group(1) + " replies" + elif not replies: + view_replies_text = "Reply" + parent_id = comment_raw['comment']['commentRenderer']['commentId'] + replies_url = URL_ORIGIN + '/post_comment?parent_id=' + parent_id + "&video_id=" + video_id comment_raw = comment_raw['comment'] comment_raw = comment_raw['commentRenderer'] @@ -216,9 +233,9 @@ def parse_comments_polymer(content, replies=False): -def get_comments_html(result): +def get_comments_html(comments): html_result = '' - for comment in result['comments']: + for comment in comments: replies = '' if comment['replies_url']: replies = reply_link_template.substitute(url=comment['replies_url'], view_replies_text=html.escape(comment['view_replies_text'])) @@ -240,13 +257,38 @@ def get_comments_html(result): replies=replies, #replies='', ) - return html_result, result['ctoken'] + return html_result def video_comments(video_id, sort=0, offset=0, secret_key=''): if settings.enable_comments: - result = parse_comments_polymer(request_comments(make_comment_ctoken(video_id, sort, offset, secret_key))) - return get_comments_html(result) - return '', '' + post_comment_url = common.URL_ORIGIN + "/post_comment?video_id=" + video_id + post_comment_link = '''Post comment''' + + other_sort_url = common.URL_ORIGIN + '/comments?ctoken=' + make_comment_ctoken(video_id, sort=1 - sort) + other_sort_name = 'newest' if sort == 0 else 'top' + other_sort_link = '''Sort by ''' + other_sort_name + '''''' + + comment_links = '''''' + + comment_info = parse_comments_polymer(request_comments(make_comment_ctoken(video_id, sort, offset, secret_key))) + ctoken = comment_info['ctoken'] + + if ctoken == '': + more_comments_button = '' + else: + more_comments_button = more_comments_template.substitute(url = common.URL_ORIGIN + '/comments?ctoken=' + ctoken) + + result = '''
\n''' + result += comment_links + '\n' + result += '
\n' + result += get_comments_html(comment_info['comments']) + '\n' + result += '
\n' + result += more_comments_button + '\n' + result += '''
''' + return result + return '' more_comments_template = Template('''More comments''') video_metadata_template = Template('''