fix comment replies

This commit is contained in:
Andrew Zhao 2021-03-18 01:23:32 -04:00
parent 3286328de4
commit 89fd35e02d
4 changed files with 22 additions and 10 deletions

View File

@ -22,7 +22,8 @@
break;
case 'get_youtube_replies':
var load_more = e.getAttribute('data-load-more') !== null;
get_youtube_replies(e, load_more);
var load_replies = e.getAttribute('data-load-replies') !== null;
get_youtube_replies(e, load_more, load_replies);
break;
case 'toggle_parent':
toggle_parent(e);

View File

@ -359,7 +359,7 @@ function get_youtube_comments(retries) {
xhr.send();
}
function get_youtube_replies(target, load_more) {
function get_youtube_replies(target, load_more, load_replies) {
var continuation = target.getAttribute('data-continuation');
var body = target.parentNode.parentNode;
@ -371,7 +371,10 @@ function get_youtube_replies(target, load_more) {
'?format=html' +
'&hl=' + video_data.preferences.locale +
'&thin_mode=' + video_data.preferences.thin_mode +
'&continuation=' + continuation;
'&continuation=' + continuation
if (load_replies) {
url += '&action=action_get_comment_replies';
}
var xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.timeout = 10000;

View File

@ -2054,6 +2054,9 @@ get "/api/v1/comments/:id" do |env|
format = env.params.query["format"]?
format ||= "json"
action = env.params.query["action"]?
action ||= "action_get_comments"
continuation = env.params.query["continuation"]?
sort_by = env.params.query["sort_by"]?.try &.downcase
@ -2061,7 +2064,7 @@ get "/api/v1/comments/:id" do |env|
sort_by ||= "top"
begin
comments = fetch_youtube_comments(id, PG_DB, continuation, format, locale, thin_mode, region, sort_by: sort_by)
comments = fetch_youtube_comments(id, PG_DB, continuation, format, locale, thin_mode, region, sort_by: sort_by, action: action)
rescue ex
next error_json(500, ex)
end

View File

@ -56,7 +56,7 @@ class RedditListing
property modhash : String
end
def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, sort_by = "top")
def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, sort_by = "top", action = "action_get_comments")
video = get_video(id, db, region: region)
session_token = video.session_token
@ -88,9 +88,14 @@ def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, so
"cookie" => video.cookie,
}
response = YT_POOL.client(region, &.post("/comment_service_ajax?action_get_comments=1&hl=en&gl=US&pbj=1", headers, form: post_req))
response = YT_POOL.client(region, &.post("/comment_service_ajax?#{action}=1&hl=en&gl=US&pbj=1", headers, form: post_req))
response = JSON.parse(response.body)
# For some reason youtube puts it in an array for comment_replies but otherwise it's the same
if action == "action_get_comment_replies"
response = response[1]
end
if !response["response"]["continuationContents"]?
raise InfoException.new("Could not fetch comments")
end
@ -228,7 +233,7 @@ def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, so
if format == "html"
response = JSON.parse(response)
content_html = template_youtube_comments(response, locale, thin_mode)
content_html = template_youtube_comments(response, locale, thin_mode, action == "action_get_comment_replies")
response = JSON.build do |json|
json.object do
@ -281,7 +286,7 @@ def fetch_reddit_comments(id, sort_by = "confidence")
return comments, thread
end
def template_youtube_comments(comments, locale, thin_mode)
def template_youtube_comments(comments, locale, thin_mode, is_replies = false)
String.build do |html|
root = comments["comments"].as_a
root.each do |child|
@ -292,7 +297,7 @@ def template_youtube_comments(comments, locale, thin_mode)
<div class="pure-u-23-24">
<p>
<a href="javascript:void(0)" data-continuation="#{child["replies"]["continuation"]}"
data-onclick="get_youtube_replies">#{translate(locale, "View `x` replies", number_with_separator(child["replies"]["replyCount"]))}</a>
data-onclick="get_youtube_replies" data-load-replies>#{translate(locale, "View `x` replies", number_with_separator(child["replies"]["replyCount"]))}</a>
</p>
</div>
</div>
@ -412,7 +417,7 @@ def template_youtube_comments(comments, locale, thin_mode)
<div class="pure-u-1">
<p>
<a href="javascript:void(0)" data-continuation="#{comments["continuation"]}"
data-onclick="get_youtube_replies" data-load-more>#{translate(locale, "Load more")}</a>
data-onclick="get_youtube_replies" data-load-more #{"data-load-replies" if is_replies}>#{translate(locale, "Load more")}</a>
</p>
</div>
</div>