mirror of https://github.com/iv-org/invidious.git
Move reddit comment logic to extension shard
This commit is contained in:
parent
49c40b3bc3
commit
bbb9d3f80b
|
@ -31,6 +31,8 @@ dependencies:
|
||||||
http_proxy:
|
http_proxy:
|
||||||
github: mamantoha/http_proxy
|
github: mamantoha/http_proxy
|
||||||
version: ~> 0.10.3
|
version: ~> 0.10.3
|
||||||
|
extendious-reddit-comments:
|
||||||
|
github: "syeopite/extendious-reddit-comments"
|
||||||
|
|
||||||
development_dependencies:
|
development_dependencies:
|
||||||
spectator:
|
spectator:
|
||||||
|
|
|
@ -64,7 +64,6 @@ HMAC_KEY = CONFIG.hmac_key
|
||||||
PG_DB = DB.open CONFIG.database_url
|
PG_DB = DB.open CONFIG.database_url
|
||||||
ARCHIVE_URL = URI.parse("https://archive.org")
|
ARCHIVE_URL = URI.parse("https://archive.org")
|
||||||
PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")
|
PUBSUB_URL = URI.parse("https://pubsubhubbub.appspot.com")
|
||||||
REDDIT_URL = URI.parse("https://www.reddit.com")
|
|
||||||
YT_URL = URI.parse("https://www.youtube.com")
|
YT_URL = URI.parse("https://www.youtube.com")
|
||||||
HOST_URL = make_host_url(Kemal.config)
|
HOST_URL = make_host_url(Kemal.config)
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -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
|
|
||||||
<div class="pure-g">
|
|
||||||
<div class="pure-u-1-24">
|
|
||||||
</div>
|
|
||||||
<div class="pure-u-23-24">
|
|
||||||
END_HTML
|
|
||||||
else
|
|
||||||
html << <<-END_HTML
|
|
||||||
<div class="pure-g">
|
|
||||||
<div class="pure-u-1">
|
|
||||||
END_HTML
|
|
||||||
end
|
|
||||||
|
|
||||||
html << <<-END_HTML
|
|
||||||
<p>
|
|
||||||
<a href="javascript:void(0)" data-onclick="toggle_parent">[ − ]</a>
|
|
||||||
<b><a href="https://www.reddit.com/user/#{child.author}">#{child.author}</a></b>
|
|
||||||
#{translate_count(locale, "comments_points_count", child.score, NumberFormatting::Separator)}
|
|
||||||
<span title="#{child.created_utc.to_s("%a %B %-d %T %Y UTC")}">#{translate(locale, "`x` ago", recode_date(child.created_utc, locale))}</span>
|
|
||||||
<a href="https://www.reddit.com#{child.permalink}" title="#{translate(locale, "permalink")}">#{translate(locale, "permalink")}</a>
|
|
||||||
</p>
|
|
||||||
<div>
|
|
||||||
#{body_html}
|
|
||||||
#{replies_html}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
END_HTML
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -353,37 +353,6 @@ module Invidious::Routes::API::V1::Videos
|
||||||
end
|
end
|
||||||
|
|
||||||
return comments
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -89,25 +89,6 @@ module Invidious::Routes::Watch
|
||||||
begin
|
begin
|
||||||
comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"]
|
comment_html = JSON.parse(Comments.fetch_youtube(id, nil, "html", locale, preferences.thin_mode, region))["contentHtml"]
|
||||||
rescue ex
|
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
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue