forked from midou/invidious
Add local alternatives for video links
This commit is contained in:
parent
e9f214cdc0
commit
ebe51c91d7
@ -290,6 +290,9 @@ def template_comments(root)
|
||||
score = child["data"]["score"]
|
||||
body_html = HTML.unescape(child["data"]["body_html"].as_s)
|
||||
|
||||
# Replace local links wtih links back to Reddit
|
||||
body_html = fill_links(body_html, "https", "www.reddit.com")
|
||||
|
||||
replies_html = ""
|
||||
if child["data"]["replies"] != ""
|
||||
replies_html = template_comments(child["data"]["replies"]["data"]["children"])
|
||||
@ -341,3 +344,48 @@ def arg_array(array)
|
||||
|
||||
return args
|
||||
end
|
||||
|
||||
def add_alt_links(html)
|
||||
alt_links = [] of {Int32, String}
|
||||
|
||||
# This is painful but is likely the only way to accomplish this in Crystal,
|
||||
# as Crystigiri and others are not able to insert XML Nodes into a document.
|
||||
# The goal here is to use as little regex as possible
|
||||
html.scan(/<a[^>]*>([^<]+)<\/a>/) do |match|
|
||||
anchor = XML.parse_html(match[0])
|
||||
anchor = anchor.xpath_node("//a").not_nil!
|
||||
url = URI.parse(HTML.unescape(anchor["href"]))
|
||||
|
||||
if ["www.youtube.com", "youtu.be", "m.youtube.com"].includes?(url.host) && url.path == "/watch"
|
||||
alt_link = <<-END_HTML
|
||||
<a class="link" href="#{url.full_path}">
|
||||
<i class="fa fa-link" aria-hidden="true"></i>
|
||||
</a>
|
||||
END_HTML
|
||||
|
||||
alt_links << {match.end.not_nil!, alt_link}
|
||||
end
|
||||
end
|
||||
|
||||
alt_links.reverse!
|
||||
alt_links.each do |position, alt_link|
|
||||
html = html.insert(position, alt_link)
|
||||
end
|
||||
|
||||
return html
|
||||
end
|
||||
|
||||
def fill_links(html, scheme, host)
|
||||
html = XML.parse_html(html)
|
||||
|
||||
html.xpath_nodes("//a").each do |match|
|
||||
url = URI.parse(match["href"])
|
||||
if !url.host # If reddit link
|
||||
url.scheme = scheme
|
||||
url.host = host
|
||||
match["href"] = url
|
||||
end
|
||||
end
|
||||
|
||||
html = html.to_xml
|
||||
end
|
||||
|
@ -218,11 +218,17 @@ get "/watch" do |env|
|
||||
headers = HTTP::Headers{"User-Agent" => "web:invidio.us:v0.1.0 (by /u/omarroth)"}
|
||||
begin
|
||||
reddit_comments, reddit_thread = get_reddit_comments(id, reddit_client, headers)
|
||||
reddit_html = template_comments(reddit_comments)
|
||||
|
||||
reddit_html = add_alt_links(reddit_html)
|
||||
rescue ex
|
||||
reddit_comments = JSON.parse("[]")
|
||||
reddit_thread = nil
|
||||
reddit_html = ""
|
||||
end
|
||||
|
||||
video.description = fill_links(video.description, "https", "www.youtube.com")
|
||||
video.description = add_alt_links(video.description)
|
||||
|
||||
templated "watch"
|
||||
end
|
||||
|
||||
|
@ -105,13 +105,13 @@ function toggle(target) {
|
||||
<%= video.description %>
|
||||
</div>
|
||||
<hr style="margin-right:1em;">
|
||||
<% if reddit_thread && !reddit_comments.as_a.empty? %>
|
||||
<% if reddit_thread && !reddit_html.empty? %>
|
||||
<div style="margin-right:1em; overflow-wrap:break-word; word-wrap:break-word;">
|
||||
<h3><%= reddit_thread.data.title %></h3>
|
||||
<b>
|
||||
<a target="_blank" class="link" href="https://reddit.com<%= reddit_thread.data.permalink %>">View comments on Reddit</a>
|
||||
</b>
|
||||
<%= template_comments(reddit_comments) %>
|
||||
<%= reddit_html %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user