From bbb9d3f80bfbb973b6f004f9a1c9085691aeda9a Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 10 Nov 2024 00:49:59 -0800 Subject: [PATCH] Move reddit comment logic to extension shard --- shard.yml | 2 + src/invidious.cr | 1 - src/invidious/comments/reddit.cr | 41 ---------------- src/invidious/comments/reddit_types.cr | 57 ----------------------- src/invidious/frontend/comments_reddit.cr | 50 -------------------- src/invidious/routes/api/v1/videos.cr | 31 ------------ src/invidious/routes/watch.cr | 19 -------- 7 files changed, 2 insertions(+), 199 deletions(-) delete mode 100644 src/invidious/comments/reddit.cr delete mode 100644 src/invidious/comments/reddit_types.cr delete mode 100644 src/invidious/frontend/comments_reddit.cr diff --git a/shard.yml b/shard.yml index 513e5db3..4ce4856a 100644 --- a/shard.yml +++ b/shard.yml @@ -31,6 +31,8 @@ dependencies: http_proxy: github: mamantoha/http_proxy version: ~> 0.10.3 + extendious-reddit-comments: + github: "syeopite/extendious-reddit-comments" development_dependencies: spectator: diff --git a/src/invidious.cr b/src/invidious.cr index e9cfccd5..3863b13a 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -64,7 +64,6 @@ HMAC_KEY = CONFIG.hmac_key PG_DB = DB.open CONFIG.database_url ARCHIVE_URL = URI.parse("https://archive.org") PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com") -REDDIT_URL = URI.parse("https://www.reddit.com") YT_URL = URI.parse("https://www.youtube.com") HOST_URL = make_host_url(Kemal.config) diff --git a/src/invidious/comments/reddit.cr b/src/invidious/comments/reddit.cr deleted file mode 100644 index ba9c19f1..00000000 --- a/src/invidious/comments/reddit.cr +++ /dev/null @@ -1,41 +0,0 @@ -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/comments/reddit_types.cr b/src/invidious/comments/reddit_types.cr deleted file mode 100644 index 796a1183..00000000 --- a/src/invidious/comments/reddit_types.cr +++ /dev/null @@ -1,57 +0,0 @@ -class RedditThing - include JSON::Serializable - - property kind : String - property data : RedditComment | RedditLink | RedditMore | RedditListing -end - -class RedditComment - include JSON::Serializable - - property author : String - property body_html : String - property replies : RedditThing | String - property score : Int32 - property depth : Int32 - property permalink : String - - @[JSON::Field(converter: RedditComment::TimeConverter)] - property created_utc : Time - - module TimeConverter - def self.from_json(value : JSON::PullParser) : Time - Time.unix(value.read_float.to_i) - end - - def self.to_json(value : Time, json : JSON::Builder) - json.number(value.to_unix) - end - end -end - -struct RedditLink - include JSON::Serializable - - property author : String - property score : Int32 - property subreddit : String - property num_comments : Int32 - property id : String - property permalink : String - property title : String -end - -struct RedditMore - include JSON::Serializable - - property children : Array(String) - property count : Int32 - property depth : Int32 -end - -class RedditListing - include JSON::Serializable - - property children : Array(RedditThing) - property modhash : String -end diff --git a/src/invidious/frontend/comments_reddit.cr b/src/invidious/frontend/comments_reddit.cr deleted file mode 100644 index 4dda683e..00000000 --- a/src/invidious/frontend/comments_reddit.cr +++ /dev/null @@ -1,50 +0,0 @@ -module Invidious::Frontend::Comments - extend self - - def template_reddit(root, locale) - String.build do |html| - root.each do |child| - if child.data.is_a?(RedditComment) - child = child.data.as(RedditComment) - body_html = HTML.unescape(child.body_html) - - replies_html = "" - if child.replies.is_a?(RedditThing) - replies = child.replies.as(RedditThing) - replies_html = self.template_reddit(replies.data.as(RedditListing).children, locale) - end - - if child.depth > 0 - html << <<-END_HTML -
-
-
-
- END_HTML - else - html << <<-END_HTML -
-
- END_HTML - end - - html << <<-END_HTML -

- [ − ] - #{child.author} - #{translate_count(locale, "comments_points_count", child.score, NumberFormatting::Separator)} - #{translate(locale, "`x` ago", recode_date(child.created_utc, locale))} - #{translate(locale, "permalink")} -

-
- #{body_html} - #{replies_html} -
-
-
- END_HTML - end - end - end - end -end diff --git a/src/invidious/routes/api/v1/videos.cr b/src/invidious/routes/api/v1/videos.cr index 368304ac..8cec8b8d 100644 --- a/src/invidious/routes/api/v1/videos.cr +++ b/src/invidious/routes/api/v1/videos.cr @@ -353,37 +353,6 @@ module Invidious::Routes::API::V1::Videos end return comments - elsif source == "reddit" - sort_by ||= "confidence" - - begin - comments, reddit_thread = Comments.fetch_reddit(id, sort_by: sort_by) - rescue ex - comments = nil - reddit_thread = nil - end - - if !reddit_thread || !comments - return error_json(404, "No reddit threads found") - end - - if format == "json" - reddit_thread = JSON.parse(reddit_thread.to_json).as_h - reddit_thread["comments"] = JSON.parse(comments.to_json) - - return reddit_thread.to_json - else - content_html = Frontend::Comments.template_reddit(comments, locale) - content_html = Comments.fill_links(content_html, "https", "www.reddit.com") - content_html = Comments.replace_links(content_html) - response = { - "title" => reddit_thread.title, - "permalink" => reddit_thread.permalink, - "contentHtml" => content_html, - } - - return response.to_json - end end end diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index aabe8dfc..d919c89a 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -89,25 +89,6 @@ module Invidious::Routes::Watch begin 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 = Comments.fetch_reddit(id) - comment_html = Frontend::Comments.template_reddit(comments, locale) - - comment_html = Comments.fill_links(comment_html, "https", "www.reddit.com") - comment_html = Comments.replace_links(comment_html) - end - end - elsif source == "reddit" - begin - comments, reddit_thread = Comments.fetch_reddit(id) - comment_html = Frontend::Comments.template_reddit(comments, locale) - - comment_html = Comments.fill_links(comment_html, "https", "www.reddit.com") - comment_html = Comments.replace_links(comment_html) - rescue ex - if preferences.comments[1] == "youtube" - comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"] - end end end else