summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOmar Roth <omarroth@hotmail.com>2018-03-06 22:00:35 -0600
committerOmar Roth <omarroth@hotmail.com>2018-03-06 22:00:35 -0600
commitebe51c91d762a3e9c4804fe04ee81b81da64967b (patch)
tree62e6416406eeef76e6ad26fe4146739b79c4363e /src
parente9f214cdc015e00a7b3e04eb73a735b046fd7014 (diff)
downloadinvidious-ebe51c91d762a3e9c4804fe04ee81b81da64967b.tar.gz
invidious-ebe51c91d762a3e9c4804fe04ee81b81da64967b.tar.bz2
invidious-ebe51c91d762a3e9c4804fe04ee81b81da64967b.zip
Add local alternatives for video links
Diffstat (limited to 'src')
-rw-r--r--src/helpers.cr48
-rw-r--r--src/invidious.cr8
-rw-r--r--src/views/watch.ecr4
3 files changed, 57 insertions, 3 deletions
diff --git a/src/helpers.cr b/src/helpers.cr
index 2d2b9337..fae9653e 100644
--- a/src/helpers.cr
+++ b/src/helpers.cr
@@ -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
diff --git a/src/invidious.cr b/src/invidious.cr
index 9758349c..4b3cdd64 100644
--- a/src/invidious.cr
+++ b/src/invidious.cr
@@ -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
diff --git a/src/views/watch.ecr b/src/views/watch.ecr
index 18307e83..a9600320 100644
--- a/src/views/watch.ecr
+++ b/src/views/watch.ecr
@@ -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>