apply all the suggestions + rework invidious_companion parameter

This commit is contained in:
Emilien 2024-12-08 22:21:10 +01:00
parent 7a070fa710
commit f710dd37bf
9 changed files with 48 additions and 51 deletions

View File

@ -59,16 +59,27 @@ db:
## for loading the video streams from YouTube servers.
##
## When this setting is commented out, Invidious companion is not used.
## Otherwise, Invidious will proxy the requests to Invidious companion.
##
## When this setting is configured, then Invidious will proxy the requests
## to Invidious companion.
## Or randomly choose one if multiple Invidious companion are configured.
## Note: multiple URL can be configured. In this case, invidious will
## randomly pick one every time video data needs to be retrieved. This
## URL is then kept in the video metadata cache to allow video playback
## to work. Once said cache has expired, requesting that video's data
## again will cause a new companion URL to be picked.
##
## The parameter private_url needs to be configured for the internal
## communication between the companion and Invidious.
## And public_url is the public URL from which companion is listening
## to the requests from the user(s).
## Both parameter can have identical URL when Invidious is hosted in
## an internal network or at home.
##
## Accepted values: "http(s)://<IP-HOSTNAME>:<Port>"
## Default: <none>
##
#invidious_companion:
# - http://127.0.0.1:8282
# - private_url: "http://localhost:8282"
# public_url: "http://localhost:8282"
##
## API key for Invidious companion

View File

@ -67,26 +67,14 @@ end
class Config
include YAML::Serializable
module URIArrayConverter
def self.to_yaml(values : Array(URI), yaml : YAML::Nodes::Builder)
yaml.sequence do
values.each { |v| yaml.scalar v.to_s }
end
end
class CompanionConfig
include YAML::Serializable
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Array(URI)
if node.is_a?(YAML::Nodes::Sequence)
node.map do |child|
unless child.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{child.class}"
end
@[YAML::Field(converter: Preferences::URIConverter)]
property private_url : URI = URI.parse("")
URI.parse(child.value)
end
else
node.raise "Expected sequence, not #{node.class}"
end
end
@[YAML::Field(converter: Preferences::URIConverter)]
property public_url : URI = URI.parse("")
end
# Number of threads to use for crawling videos from channels (for updating subscriptions)
@ -174,8 +162,7 @@ class Config
property po_token : String? = nil
# Invidious companion
@[YAML::Field(converter: Config::URIArrayConverter)]
property invidious_companion : Array(URI) = [] of URI
property invidious_companion : Array(CompanionConfig) = [] of CompanionConfig
# Invidious companion API key
property invidious_companion_key : String = ""

View File

@ -1,10 +1,6 @@
module Invidious::Routes::API::Manifest
# /api/manifest/dash/id/:id
def self.get_dash_video_id(env)
if !CONFIG.invidious_companion.empty?
return error_template(403, "This endpoint is not permitted because it is handled by Invidious companion.")
end
env.response.headers.add("Access-Control-Allow-Origin", "*")
env.response.content_type = "application/dash+xml"
@ -12,6 +8,11 @@ module Invidious::Routes::API::Manifest
id = env.params.url["id"]
region = env.params.query["region"]?
if !CONFIG.invidious_companion.empty?
invidious_companion = CONFIG.invidious_companion.sample
return env.redirect "#{invidious_companion.public_url.to_s}/api/manifest/dash/id/#{id}?#{env.params.query}"
end
# Since some implementations create playlists based on resolution regardless of different codecs,
# we can opt to only add a source to a representation if it has a unique height within that representation
unique_res = env.params.query["unique_res"]?.try { |q| (q == "true" || q == "1").to_unsafe }

View File

@ -201,11 +201,11 @@ module Invidious::Routes::Embed
return env.redirect url
end
if (!CONFIG.invidious_companion.empty?)
if companion_base_url = video.invidious_companion.try &.["baseUrl"].as_s
env.response.headers["Content-Security-Policy"] =
env.response.headers["Content-Security-Policy"]
.gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s)
.gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s)
.gsub("media-src", "media-src #{companion_base_url}")
.gsub("connect-src", "connect-src #{companion_base_url}")
end
rendered "embed"

View File

@ -254,7 +254,8 @@ module Invidious::Routes::VideoPlayback
# so we have a mechanism here to redirect to the latest version
def self.latest_version(env)
if !CONFIG.invidious_companion.empty?
return error_template(403, "This endpoint is not permitted because it is handled by Invidious companion.")
invidious_companion = CONFIG.invidious_companion.sample
return env.redirect "#{invidious_companion.public_url.to_s}/latest_version?#{env.params.query}"
end
id = env.params.query["id"]?

View File

@ -190,11 +190,11 @@ module Invidious::Routes::Watch
captions: video.captions
)
if (!CONFIG.invidious_companion.empty?)
if companion_base_url = video.invidious_companion.try &.["baseUrl"].as_s
env.response.headers["Content-Security-Policy"] =
env.response.headers["Content-Security-Policy"]
.gsub("media-src", "media-src " + video.invidious_companion.not_nil!["baseUrl"].as_s)
.gsub("connect-src", "connect-src " + video.invidious_companion.not_nil!["baseUrl"].as_s)
.gsub("media-src", "media-src #{companion_base_url}")
.gsub("connect-src", "connect-src #{companion_base_url}")
end
templated "watch"
@ -327,18 +327,16 @@ module Invidious::Routes::Watch
env.params.query["label"] = URI.decode_www_form(label.as_s)
return Invidious::Routes::API::V1::Videos.captions(env)
elsif itag = download_widget["itag"]?.try &.as_i
itag = itag.to_s
elsif itag = download_widget["itag"]?.try &.as_i.to_s
# URL params specific to /latest_version
env.params.query["id"] = video_id
env.params.query["itag"] = itag
env.params.query["title"] = filename
env.params.query["local"] = "true"
if (!CONFIG.invidious_companion.empty?)
video = get_video(video_id)
return env.redirect "#{video.invidious_companion.not_nil!["baseUrl"].as_s}/latest_version?id=#{video_id}&itag=#{itag}&local=true"
return env.redirect "#{video.invidious_companion["baseUrl"].as_s}/latest_version?#{env.params.query}"
else
return Invidious::Routes::VideoPlayback.latest_version(env)
end

View File

@ -193,7 +193,7 @@ struct Video
end
def invidious_companion : Hash(String, JSON::Any)?
info["invidiousCompanion"]?.try &.as_h
info["invidiousCompanion"]?.try &.as_h || {} of String => JSON::Any
end
# Macros defining getters/setters for various types of data

View File

@ -22,7 +22,7 @@
audio_streams.each_with_index do |fmt, i|
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}"
src_url += "&local=true" if params.local
src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
bitrate = fmt["bitrate"]
mimetype = HTML.escape(fmt["mimeType"].as_s)
@ -37,7 +37,7 @@
<% else %>
<% if params.quality == "dash"
src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1"
src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
%>
<source src="<%= src_url %>" type='application/dash+xml' label="dash">
<% end %>
@ -48,7 +48,7 @@
fmt_stream.each_with_index do |fmt, i|
src_url = "/latest_version?id=#{video.id}&itag=#{fmt["itag"]}"
src_url += "&local=true" if params.local
src_url = video.invidious_companion.not_nil!["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
src_url = video.invidious_companion["baseUrl"].as_s + src_url if (!CONFIG.invidious_companion.empty?)
quality = fmt["quality"]
mimetype = HTML.escape(fmt["mimeType"].as_s)

View File

@ -685,7 +685,7 @@ module YoutubeAPI
) : Hash(String, JSON::Any)
headers = HTTP::Headers{
"Content-Type" => "application/json; charset=UTF-8",
"Authorization" => "Bearer " + CONFIG.invidious_companion_key,
"Authorization" => "Bearer #{CONFIG.invidious_companion_key}",
}
# Logging
@ -695,21 +695,20 @@ module YoutubeAPI
# Send the POST request
begin
response = make_client(CONFIG.invidious_companion.sample,
invidious_companion = CONFIG.invidious_companion.sample
response = make_client(invidious_companion.private_url,
&.post(endpoint, headers: headers, body: data.to_json))
body = response.body
if (response.status_code != 200)
raise Exception.new("Error while communicating with Invidious companion: \
status code: " + response.status_code.to_s + " and body: " + body)
raise Exception.new(
"Error while communicating with Invidious companion: \
status code: #{response.status_code} and body: #{body.dump}"
)
end
rescue ex
raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found"))
end
if body.nil?
raise InfoException.new("Error while communicating with Invidious companion: no response data.")
end
# Convert result to Hash
initial_data = JSON.parse(body).as_h