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; break;
case 'get_youtube_replies': case 'get_youtube_replies':
var load_more = e.getAttribute('data-load-more') !== null; 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; break;
case 'toggle_parent': case 'toggle_parent':
toggle_parent(e); toggle_parent(e);

View file

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

View file

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

View file

@ -56,7 +56,7 @@ class RedditListing
property modhash : String property modhash : String
end 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) video = get_video(id, db, region: region)
session_token = video.session_token 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, "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) 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"]? if !response["response"]["continuationContents"]?
raise InfoException.new("Could not fetch comments") raise InfoException.new("Could not fetch comments")
end end
@ -228,7 +233,7 @@ def fetch_youtube_comments(id, db, cursor, format, locale, thin_mode, region, so
if format == "html" if format == "html"
response = JSON.parse(response) 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| response = JSON.build do |json|
json.object do json.object do
@ -281,7 +286,7 @@ def fetch_reddit_comments(id, sort_by = "confidence")
return comments, thread return comments, thread
end end
def template_youtube_comments(comments, locale, thin_mode) def template_youtube_comments(comments, locale, thin_mode, is_replies = false)
String.build do |html| String.build do |html|
root = comments["comments"].as_a root = comments["comments"].as_a
root.each do |child| root.each do |child|
@ -292,7 +297,7 @@ def template_youtube_comments(comments, locale, thin_mode)
<div class="pure-u-23-24"> <div class="pure-u-23-24">
<p> <p>
<a href="javascript:void(0)" data-continuation="#{child["replies"]["continuation"]}" <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> </p>
</div> </div>
</div> </div>
@ -412,7 +417,7 @@ def template_youtube_comments(comments, locale, thin_mode)
<div class="pure-u-1"> <div class="pure-u-1">
<p> <p>
<a href="javascript:void(0)" data-continuation="#{comments["continuation"]}" <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> </p>
</div> </div>
</div> </div>