From ae07c7694fb1a7418e1396e88e37113884e57602 Mon Sep 17 00:00:00 2001 From: syeopite Date: Thu, 15 Apr 2021 23:20:44 -0700 Subject: [PATCH] Fetch external links favicon and add to about page --- src/invidious.cr | 33 +++++++++++++++++++++++++++ src/invidious/channels.cr | 7 +++--- src/invidious/views/channel_about.ecr | 1 + 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 7e7e238a..7cefc52c 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -3879,6 +3879,39 @@ get "/vi/:id/:name" do |env| end end +get "/fetch_link_favicon" do |env| + url = env.params.query["url"] + url = URI.parse(url) + + if url.host.to_s.ends_with?("gstatic.com") + # headers = HTTP::Headers{"Content-Encoding" => "gzip"} + headers = HTTP::Headers{} of String => String + + REQUEST_HEADERS_WHITELIST.each do |header| + if env.request.headers[header]? + headers[header] = env.request.headers[header] + end + end + + begin + HTTP::Client.get(url.to_s, headers) do |favicon_response| + env.response.status_code = favicon_response.status_code + favicon_response.headers.each do |key, value| + if !RESPONSE_HEADERS_BLACKLIST.includes?(key.downcase) + env.response.headers[key] = value + break + end + end + + env.response.headers["Access-Control-Allow-Origin"] = "*" + proxy_file(favicon_response, env) + end + + rescue ex + end + end +end + get "/Captcha" do |env| headers = HTTP::Headers{":authority" => "accounts.google.com"} response = YT_POOL.client &.get(env.request.resource, headers) diff --git a/src/invidious/channels.cr b/src/invidious/channels.cr index a76c3673..e6a0748f 100644 --- a/src/invidious/channels.cr +++ b/src/invidious/channels.cr @@ -136,7 +136,7 @@ struct AboutChannel property allowed_regions : Array(String) property related_channels : Array(AboutRelatedChannel) property tabs : Array(String) - property links : Array(Tuple(String, String)) + property links : Array(Tuple(String, String, String)) end class ChannelRedirect < Exception @@ -888,7 +888,7 @@ def get_about_info(ucid, locale) total_views = 0_i64 joined = Time.unix(0) tabs = [] of String - links = [] of {String, String} + links = [] of {String, String, String} tabs_json = initdata["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]?.try &.as_a? if !tabs_json.nil? @@ -910,6 +910,7 @@ def get_about_info(ucid, locale) channel_about_meta["primaryLinks"]?.try &.as_a.each do |link| link_title = link["title"]["simpleText"].as_s link_url = URI.parse(link["navigationEndpoint"]["urlEndpoint"]["url"].to_s) + link_icon_url = link["icon"]?.try &.["thumbnails"][0]["url"].to_s || "" if {"m.youtube.com", "www.youtube.com", "youtu.be"}.includes? link_url.host if link_url.path == "/redirect" @@ -921,7 +922,7 @@ def get_about_info(ucid, locale) link_url = link_url.to_s end - links << {link_title, link_url} + links << {link_title, link_url, link_icon_url} end country = channel_about_meta["country"]?.try &.["simpleText"].as_s || "" diff --git a/src/invidious/views/channel_about.ecr b/src/invidious/views/channel_about.ecr index 8272c1ea..e81bd88f 100644 --- a/src/invidious/views/channel_about.ecr +++ b/src/invidious/views/channel_about.ecr @@ -27,6 +27,7 @@

<%= translate(locale, "Links") %>

<% channel.links.each do |link_tuple| %>

+ <%=link_tuple[0]%>

<% end %>