summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2023-05-06 20:02:42 +0200
committerSamantaz Fox <coding@samantaz.fr>2023-05-25 22:53:53 +0200
commit634e913da9381f5212a1017e2f4a37e7d7075204 (patch)
tree471e807329533c18613f41ed86683f61e81ff70d
parent1b25737b013d0589f396fa938ba2747e9a76af93 (diff)
downloadinvidious-634e913da9381f5212a1017e2f4a37e7d7075204.tar.gz
invidious-634e913da9381f5212a1017e2f4a37e7d7075204.tar.bz2
invidious-634e913da9381f5212a1017e2f4a37e7d7075204.zip
Comments: Move 'fetch_reddit' function to own file + module
-rw-r--r--src/invidious/comments.cr38
-rw-r--r--src/invidious/comments/reddit.cr41
-rw-r--r--src/invidious/routes/api/v1/videos.cr2
-rw-r--r--src/invidious/routes/watch.cr4
4 files changed, 44 insertions, 41 deletions
diff --git a/src/invidious/comments.cr b/src/invidious/comments.cr
index 07579cf3..07b92786 100644
--- a/src/invidious/comments.cr
+++ b/src/invidious/comments.cr
@@ -1,41 +1,3 @@
-def fetch_reddit_comments(id, sort_by = "confidence")
- client = make_client(REDDIT_URL)
- headers = HTTP::Headers{"User-Agent" => "web:invidious:v#{CURRENT_VERSION} (by github.com/iv-org/invidious)"}
-
- # TODO: Use something like #479 for a static list of instances to use here
- query = URI::Params.encode({q: "(url:3D#{id} OR url:#{id}) AND (site:invidio.us OR site:youtube.com OR site:youtu.be)"})
- search_results = client.get("/search.json?#{query}", headers)
-
- 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
- threads = search_results.data.as(RedditListing).children
- thread = threads.max_by?(&.data.as(RedditLink).score).try(&.data.as(RedditLink))
- result = thread.try do |t|
- body = client.get("/r/#{t.subreddit}/comments/#{t.id}.json?limit=100&sort=#{sort_by}", headers).body
- Array(RedditThing).from_json(body)
- end
- result ||= [] of RedditThing
- 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)
-
- thread = result[0].data.as(RedditListing).children[0].data.as(RedditLink)
- else
- raise NotFoundException.new("Comments not found.")
- end
-
- client.close
-
- comments = result[1]?.try(&.data.as(RedditListing).children)
- comments ||= [] of RedditThing
- return comments, thread
-end
-
def template_youtube_comments(comments, locale, thin_mode, is_replies = false)
String.build do |html|
root = comments["comments"].as_a
diff --git a/src/invidious/comments/reddit.cr b/src/invidious/comments/reddit.cr
new file mode 100644
index 00000000..ba9c19f1
--- /dev/null
+++ b/src/invidious/comments/reddit.cr
@@ -0,0 +1,41 @@
+module Invidious::Comments
+ extend self
+
+ def fetch_reddit(id, sort_by = "confidence")
+ client = make_client(REDDIT_URL)
+ headers = HTTP::Headers{"User-Agent" => "web:invidious:v#{CURRENT_VERSION} (by github.com/iv-org/invidious)"}
+
+ # TODO: Use something like #479 for a static list of instances to use here
+ query = URI::Params.encode({q: "(url:3D#{id} OR url:#{id}) AND (site:invidio.us OR site:youtube.com OR site:youtu.be)"})
+ search_results = client.get("/search.json?#{query}", headers)
+
+ 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
+ threads = search_results.data.as(RedditListing).children
+ thread = threads.max_by?(&.data.as(RedditLink).score).try(&.data.as(RedditLink))
+ result = thread.try do |t|
+ body = client.get("/r/#{t.subreddit}/comments/#{t.id}.json?limit=100&sort=#{sort_by}", headers).body
+ Array(RedditThing).from_json(body)
+ end
+ result ||= [] of RedditThing
+ 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)
+
+ thread = result[0].data.as(RedditListing).children[0].data.as(RedditLink)
+ else
+ raise NotFoundException.new("Comments not found.")
+ end
+
+ client.close
+
+ comments = result[1]?.try(&.data.as(RedditListing).children)
+ comments ||= [] of RedditThing
+ return comments, thread
+ end
+end
diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr
index ce3e96d2..cb1008ac 100644
--- a/src/invidious/routes/api/v1/videos.cr
+++ b/src/invidious/routes/api/v1/videos.cr
@@ -345,7 +345,7 @@ module Invidious::Routes::API::V1::Videos
sort_by ||= "confidence"
begin
- comments, reddit_thread = fetch_reddit_comments(id, sort_by: sort_by)
+ comments, reddit_thread = Comments.fetch_reddit(id, sort_by: sort_by)
rescue ex
comments = nil
reddit_thread = nil
diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr
index 861b25c2..b08e6fbe 100644
--- a/src/invidious/routes/watch.cr
+++ b/src/invidious/routes/watch.cr
@@ -98,7 +98,7 @@ module Invidious::Routes::Watch
comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"]
rescue ex
if preferences.comments[1] == "reddit"
- comments, reddit_thread = fetch_reddit_comments(id)
+ comments, reddit_thread = Comments.fetch_reddit(id)
comment_html = template_reddit_comments(comments, locale)
comment_html = fill_links(comment_html, "https", "www.reddit.com")
@@ -107,7 +107,7 @@ module Invidious::Routes::Watch
end
elsif source == "reddit"
begin
- comments, reddit_thread = fetch_reddit_comments(id)
+ comments, reddit_thread = Comments.fetch_reddit(id)
comment_html = template_reddit_comments(comments, locale)
comment_html = fill_links(comment_html, "https", "www.reddit.com")