Replace YouTube links

This commit is contained in:
Omar Roth 2018-09-03 22:15:47 -05:00
parent 81c520e0dd
commit 0d8f036bf1
3 changed files with 14 additions and 41 deletions

View file

@ -249,7 +249,7 @@ get "/watch" do |env|
aspect_ratio = "16:9" aspect_ratio = "16:9"
video.description = fill_links(video.description, "https", "www.youtube.com") video.description = fill_links(video.description, "https", "www.youtube.com")
video.description = add_alt_links(video.description) video.description = replace_links(video.description)
description = video.short_description description = video.short_description
host_url = make_host_url(Kemal.config.ssl || CONFIG.https_only, env.request.headers["Host"]?) host_url = make_host_url(Kemal.config.ssl || CONFIG.https_only, env.request.headers["Host"]?)
@ -349,7 +349,7 @@ get "/embed/:id" do |env|
aspect_ratio = nil aspect_ratio = nil
video.description = fill_links(video.description, "https", "www.youtube.com") video.description = fill_links(video.description, "https", "www.youtube.com")
video.description = add_alt_links(video.description) video.description = replace_links(video.description)
description = video.short_description description = video.short_description
host_url = make_host_url(Kemal.config.ssl || CONFIG.https_only, env.request.headers["Host"]?) host_url = make_host_url(Kemal.config.ssl || CONFIG.https_only, env.request.headers["Host"]?)
@ -1936,7 +1936,7 @@ get "/api/v1/comments/:id" do |env|
content_html = template_reddit_comments(comments) content_html = template_reddit_comments(comments)
content_html = fill_links(content_html, "https", "www.reddit.com") content_html = fill_links(content_html, "https", "www.reddit.com")
content_html = add_alt_links(content_html) content_html = replace_links(content_html)
rescue ex rescue ex
reddit_thread = nil reddit_thread = nil
content_html = "" content_html = ""

View file

@ -190,37 +190,21 @@ def template_reddit_comments(root)
return html return html
end end
def add_alt_links(html) def replace_links(html)
alt_links = [] of {String, String} html = XML.parse_html(html)
# This is painful but likely the only way to accomplish this in Crystal, html.xpath_nodes(%q(//a)).each do |anchor|
# 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(anchor["href"]) url = URI.parse(anchor["href"])
if ["www.youtube.com", "m.youtube.com"].includes?(url.host) if ["www.youtube.com", "m.youtube.com"].includes?(url.host)
if url.path == "/redirect" if url.path == "/redirect"
params = HTTP::Params.parse(url.query.not_nil!) params = HTTP::Params.parse(url.query.not_nil!)
alt_url = params["q"]? anchor["href"] = params["q"]?
alt_url ||= "/"
else else
alt_url = url.full_path anchor["href"] = url.full_path
end end
alt_link = <<-END_HTML
<a href="#{alt_url}">
<i class="icon ion-ios-link"></i>
</a>
END_HTML
elsif url.host == "youtu.be" elsif url.host == "youtu.be"
alt_link = <<-END_HTML anchor["href"] = "/watch?v=#{url.path.try &.lchop("/")}&#{url.query}"
<a href="/watch?v=#{url.path.try &.lchop("/")}&#{url.query}">
<i class="icon ion-ios-link"></i>
</a>
END_HTML
elsif url.to_s == "#" elsif url.to_s == "#"
begin begin
length_seconds = decode_length_seconds(anchor.content) length_seconds = decode_length_seconds(anchor.content)
@ -228,23 +212,12 @@ def add_alt_links(html)
length_seconds = decode_time(anchor.content) length_seconds = decode_time(anchor.content)
end end
alt_anchor = <<-END_HTML anchor["href"] = "javascript:void(0)"
<a href="javascript:void(0)" onclick="player.currentTime(#{length_seconds})">#{anchor.content}</a> anchor["onclick"] = "player.currentTime(#{length_seconds})"
END_HTML
html = html.sub(anchor.to_s, alt_anchor)
next
else
alt_link = ""
end end
alt_links << {anchor.to_s, alt_link}
end
alt_links.each do |original, alternate|
html = html.sub(original, original + alternate)
end end
html = html.to_xml(options: XML::SaveOptions::NO_DECL)
return html return html
end end
@ -267,5 +240,5 @@ def fill_links(html, scheme, host)
html = html.to_xml(options: XML::SaveOptions::NO_DECL) html = html.to_xml(options: XML::SaveOptions::NO_DECL)
end end
html return html
end end

View file

@ -130,7 +130,7 @@ def fetch_playlist(plid)
description = description.to_xml.strip(" \n") description = description.to_xml.strip(" \n")
description = description.split("<button ")[0] description = description.split("<button ")[0]
description = fill_links(description, "https", "www.youtube.com") description = fill_links(description, "https", "www.youtube.com")
description = add_alt_links(description) description = replace_links(description)
else else
description = "" description = ""
end end