Support 'sort_by' in reddit /api/v1/comments

This commit is contained in:
Omar Roth 2019-04-14 18:08:00 -05:00
parent b51fd7fc13
commit 80c1ebd768
2 changed files with 13 additions and 7 deletions

View file

@ -1617,7 +1617,7 @@ get "/subscription_ajax" do |env|
} }
post_url = "/subscription_ajax?#{action}=1&c=#{channel_id}" 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) client.post(post_url, headers, form: post_req)
email = user.email email = user.email
else else
@ -2778,12 +2778,12 @@ get "/api/v1/comments/:id" do |env|
format = env.params.query["format"]? format = env.params.query["format"]?
format ||= "json" format ||= "json"
sort_by = env.params.query["sort_by"]?.try &.downcase
sort_by ||= "top"
continuation = env.params.query["continuation"]? continuation = env.params.query["continuation"]?
sort_by = env.params.query["sort_by"]?.try &.downcase
if source == "youtube" if source == "youtube"
sort_by ||= "top"
begin begin
comments = fetch_youtube_comments(id, PG_DB, continuation, proxies, format, locale, thin_mode, region, sort_by: sort_by) comments = fetch_youtube_comments(id, PG_DB, continuation, proxies, format, locale, thin_mode, region, sort_by: sort_by)
rescue ex rescue ex
@ -2794,8 +2794,10 @@ get "/api/v1/comments/:id" do |env|
next comments next comments
elsif source == "reddit" elsif source == "reddit"
sort_by ||= "confidence"
begin 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 = template_reddit_comments(comments, locale)
content_html = fill_links(content_html, "https", "www.reddit.com") content_html = fill_links(content_html, "https", "www.reddit.com")

View file

@ -249,7 +249,7 @@ def fetch_youtube_comments(id, db, continuation, proxies, format, locale, thin_m
return comments return comments
end end
def fetch_reddit_comments(id) def fetch_reddit_comments(id, sort_by = "confidence")
client = make_client(REDDIT_URL) client = make_client(REDDIT_URL)
headers = HTTP::Headers{"User-Agent" => "web:invidious:v#{CURRENT_VERSION} (by /u/omarroth)"} 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 if search_results.status_code == 200
search_results = RedditThing.from_json(search_results.body) 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 = search_results.data.as(RedditListing).children.sort_by { |child| child.data.as(RedditLink).score }[-1]
thread = thread.data.as(RedditLink) 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) result = Array(RedditThing).from_json(result)
elsif search_results.status_code == 302 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 = client.get(search_results.headers["Location"], headers).body
result = Array(RedditThing).from_json(result) result = Array(RedditThing).from_json(result)