From 80c1ebd7689041ac313bc2713ef357326b020e37 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 14 Apr 2019 18:08:00 -0500 Subject: [PATCH] Support 'sort_by' in reddit /api/v1/comments --- src/invidious.cr | 12 +++++++----- src/invidious/comments.cr | 8 ++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 2d566ac05..d13882882 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -1617,7 +1617,7 @@ get "/subscription_ajax" do |env| } post_url = "/subscription_ajax?#{action}=1&c=#{channel_id}" - # Sync subscription with YouTube + # Sync subscriptions with YouTube client.post(post_url, headers, form: post_req) email = user.email else @@ -2778,12 +2778,12 @@ get "/api/v1/comments/:id" do |env| format = env.params.query["format"]? format ||= "json" - sort_by = env.params.query["sort_by"]?.try &.downcase - sort_by ||= "top" - continuation = env.params.query["continuation"]? + sort_by = env.params.query["sort_by"]?.try &.downcase if source == "youtube" + sort_by ||= "top" + begin comments = fetch_youtube_comments(id, PG_DB, continuation, proxies, format, locale, thin_mode, region, sort_by: sort_by) rescue ex @@ -2794,8 +2794,10 @@ get "/api/v1/comments/:id" do |env| next comments elsif source == "reddit" + sort_by ||= "confidence" + begin - comments, reddit_thread = fetch_reddit_comments(id) + comments, reddit_thread = fetch_reddit_comments(id, sort_by: sort_by) content_html = template_reddit_comments(comments, locale) content_html = fill_links(content_html, "https", "www.reddit.com") diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr index cd96565b1..645ca2a8f 100644 --- a/src/invidious/comments.cr +++ b/src/invidious/comments.cr @@ -249,7 +249,7 @@ def fetch_youtube_comments(id, db, continuation, proxies, format, locale, thin_m return comments end -def fetch_reddit_comments(id) +def fetch_reddit_comments(id, sort_by = "confidence") client = make_client(REDDIT_URL) headers = HTTP::Headers{"User-Agent" => "web:invidious:v#{CURRENT_VERSION} (by /u/omarroth)"} @@ -259,12 +259,16 @@ def fetch_reddit_comments(id) if search_results.status_code == 200 search_results = RedditThing.from_json(search_results.body) + # For videos that have more than one thread, choose the one with the highest score thread = search_results.data.as(RedditListing).children.sort_by { |child| child.data.as(RedditLink).score }[-1] thread = thread.data.as(RedditLink) - result = client.get("/r/#{thread.subreddit}/comments/#{thread.id}.json?limit=100&sort=top", headers).body + result = client.get("/r/#{thread.subreddit}/comments/#{thread.id}.json?limit=100&sort=#{sort_by}", headers).body result = Array(RedditThing).from_json(result) elsif search_results.status_code == 302 + # Previously, if there was only one result then the API would redirect to that result. + # Now, it appears it will still return a listing so this section is likely unnecessary. + result = client.get(search_results.headers["Location"], headers).body result = Array(RedditThing).from_json(result)