From ebe51c91d762a3e9c4804fe04ee81b81da64967b Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Tue, 6 Mar 2018 22:00:35 -0600 Subject: [PATCH] Add local alternatives for video links --- src/helpers.cr | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/invidious.cr | 8 +++++++- src/views/watch.ecr | 4 ++-- 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>/) 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 + + + + 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 %>
- <% if reddit_thread && !reddit_comments.as_a.empty? %> + <% if reddit_thread && !reddit_html.empty? %>

<%= reddit_thread.data.title %>

View comments on Reddit - <%= template_comments(reddit_comments) %> + <%= reddit_html %>
<% end %>