From debc11931fe1102f17852fd082d0dac50d477ce9 Mon Sep 17 00:00:00 2001 From: zrose584 <57181548+zrose584@users.noreply.github.com> Date: Wed, 7 Oct 2020 19:03:22 +0200 Subject: add comments.js --- youtube/__init__.py | 1 + youtube/comments.py | 18 ++++++++++++++---- youtube/static/js/comments.js | 20 ++++++++++++++++++++ youtube/static/js/common.js | 12 +++++++++++- youtube/templates/comments.html | 9 ++++++++- youtube/templates/comments_page.html | 25 +++++++++++++++---------- youtube/templates/watch.html | 18 +++++++++++++++++- youtube/watch.py | 2 -- 8 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 youtube/static/js/comments.js (limited to 'youtube') diff --git a/youtube/__init__.py b/youtube/__init__.py index 6c2ec48..c2ce276 100644 --- a/youtube/__init__.py +++ b/youtube/__init__.py @@ -25,6 +25,7 @@ theme_names = { def inject_theme_preference(): return { 'theme_path': '/youtube.com/static/' + theme_names[settings.theme] + '.css', + 'settings': settings, } @yt_app.template_filter('commatize') diff --git a/youtube/comments.py b/youtube/comments.py index 07d4b89..1af9079 100644 --- a/youtube/comments.py +++ b/youtube/comments.py @@ -27,7 +27,7 @@ from flask import request def make_comment_ctoken(video_id, sort=0, offset=0, lc='', secret_key=''): video_id = proto.as_bytes(video_id) secret_key = proto.as_bytes(secret_key) - + page_info = proto.string(4,video_id) + proto.uint(6, sort) offset_information = proto.nested(4, page_info) + proto.uint(5, offset) @@ -41,11 +41,11 @@ def make_comment_ctoken(video_id, sort=0, offset=0, lc='', secret_key=''): result = proto.nested(2, page_params) + proto.uint(3,6) + proto.nested(6, offset_information) return base64.urlsafe_b64encode(result).decode('ascii') -def comment_replies_ctoken(video_id, comment_id, max_results=500): +def comment_replies_ctoken(video_id, comment_id, max_results=500): params = proto.string(2, comment_id) + proto.uint(9, max_results) params = proto.nested(3, params) - + result = proto.nested(2, proto.string(2, video_id)) + proto.uint(3,6) + proto.nested(6, params) return base64.urlsafe_b64encode(result).decode('ascii') @@ -121,10 +121,18 @@ def post_process_comments_info(comments_info): comment['view_replies_text'] = str(reply_count) + ' replies' + def fmt_num(num): + for unit in ['','k']: + if num < 1000: + return "%3.1f%s" % (num, unit) if unit else num + num /= 1000.0 + return "%.1f%s" % (num, 'm') + + # if comment['like_count'] = '👍 ' + str(fmt_num(comment['like_count'])) if comment['like_count'] == 1: comment['likes_text'] = '1 like' else: - comment['likes_text'] = str(comment['like_count']) + ' likes' + comment['likes_text'] = str(fmt_num(comment['like_count'])) + ' likes' comments_info['include_avatars'] = settings.enable_comment_avatars if comments_info['ctoken']: @@ -187,8 +195,10 @@ def get_comments_page(): 'replying': replies, } + return flask.render_template('comments_page.html', comments_info = comments_info, comment_posting_box_info = comment_posting_box_info, + slim = request.args.get('slim', False) ) diff --git a/youtube/static/js/comments.js b/youtube/static/js/comments.js new file mode 100644 index 0000000..845ed3e --- /dev/null +++ b/youtube/static/js/comments.js @@ -0,0 +1,20 @@ +function onClickReplies(e) { + var details = e.target.parentElement; + // e.preventDefault(); + console.log("loading replies .."); + doXhr(details.getAttribute("src") + "&slim=1", (html) => { + var div = details.querySelector(".comment_page"); + div.innerHTML = html; + }); + details.removeEventListener('click', onClickReplies); +} + +window.addEventListener('DOMContentLoaded', function() { + QA("details.replies").forEach(details => { + details.addEventListener('click', onClickReplies); + details.addEventListener('auxclick', (e) => { + if (e.target.parentElement !== details) return; + if (e.button == 1) window.open(details.getAttribute("src")); + }); + }); +}); \ No newline at end of file diff --git a/youtube/static/js/common.js b/youtube/static/js/common.js index 687c6fa..40bc132 100644 --- a/youtube/static/js/common.js +++ b/youtube/static/js/common.js @@ -1,4 +1,5 @@ Q = document.querySelector.bind(document); +QA = document.querySelectorAll.bind(document); function text(msg) { return document.createTextNode(msg); } function clearNode(node) { while (node.firstChild) node.removeChild(node.firstChild); } function toTimestamp(seconds) { @@ -36,6 +37,15 @@ function getDefaultTranscriptTrackIdx() { return textTracks.length - 1; } +function doXhr(url, callback=null) { + var xhr = new XMLHttpRequest(); + xhr.open("GET", url); + xhr.onload = (e) => {callback(e.currentTarget.response)}; + xhr.send(); + return xhr; +} + + window.addEventListener('DOMContentLoaded', function() { cur_track_idx = getDefaultTranscriptTrackIdx(); -}); +}); \ No newline at end of file diff --git a/youtube/templates/comments.html b/youtube/templates/comments.html index f2cdf65..9d93b8c 100644 --- a/youtube/templates/comments.html +++ b/youtube/templates/comments.html @@ -22,7 +22,14 @@ {{ comment['likes_text'] if comment['like_count'] else ''}}