redirect latest_version and dash manifest to invidious companion

This commit is contained in:
Emilien Devos 2024-10-20 23:51:00 +02:00 committed by Emilien
parent 3dff7a76cf
commit 73c84baf9f
9 changed files with 52 additions and 17 deletions

View File

@ -68,7 +68,8 @@ db:
## Accepted values: "http(s)://<IP-HOSTNAME>:<Port>"
## Default: <none>
##
#invidious_companion:
# invidious_companion:
# - http://127.0.0.1:8282
#########################################

View File

@ -152,7 +152,7 @@ class Config
property po_token : String? = nil
# Invidious companion
property invidious_companion : String? = nil
property invidious_companion : Array(String)? = nil
# Saved cookies in "name1=value1; name2=value2..." format
@[YAML::Field(converter: Preferences::StringToCookies)]

View File

@ -20,6 +20,10 @@ module Invidious::Routes::API::Manifest
haltf env, status_code: 403
end
if local && CONFIG.invidious_companion
return env.redirect "#{video.invidious_companion["baseUrl"].as_s}#{env.request.path}?#{env.request.query}"
end
if dashmpd = video.dash_manifest_url
response = YT_POOL.client &.get(URI.parse(dashmpd).request_target)

View File

@ -294,6 +294,9 @@ module Invidious::Routes::VideoPlayback
end
if local
if (CONFIG.invidious_companion)
return env.redirect "#{video.invidious_companion["baseUrl"].as_s}#{env.request.path}?#{env.request.query}"
end
url = URI.parse(url).request_target.not_nil!
url += "&title=#{URI.encode_www_form(title, space_to_plus: false)}" if title
end

View File

@ -190,6 +190,13 @@ module Invidious::Routes::Watch
captions: video.captions
)
if (CONFIG.invidious_companion && env.params.query["local"] == true)
env.response.headers["Content-Security-Policy"] =
env.response.headers["Content-Security-Policy"]
.gsub("media-src", "media-src " + video.invidious_companion["baseUrl"].as_s)
.gsub("connect-src", "connect-src " + video.invidious_companion["baseUrl"].as_s)
end
templated "watch"
end

View File

@ -192,6 +192,10 @@ struct Video
}
end
def invidious_companion : Hash(String, JSON::Any)
info["invidiousCompanion"].try &.as_h
end
# Macros defining getters/setters for various types of data
private macro getset_string(name)

View File

@ -141,6 +141,10 @@ def extract_video_info(video_id : String)
params["streamingData"] = streaming_data
end
if CONFIG.invidious_companion
params["invidiousCompanion"] = player_response["invidiousCompanion"]
end
# Data structure version, for cache control
params["version"] = JSON::Any.new(Video::SCHEMA_VERSION.to_i64)
@ -449,11 +453,12 @@ def parse_video_info(video_id : String, player_response : Hash(String, JSON::Any
# Music section
"music" => JSON.parse(music_list.to_json),
# Author infos
"author" => JSON::Any.new(author || ""),
"ucid" => JSON::Any.new(ucid || ""),
"authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""),
"authorVerified" => JSON::Any.new(author_verified || false),
"subCountText" => JSON::Any.new(subs_text || "-"),
"author" => JSON::Any.new(author || ""),
"ucid" => JSON::Any.new(ucid || ""),
"authorThumbnail" => JSON::Any.new(author_thumbnail.try &.as_s || ""),
"authorVerified" => JSON::Any.new(author_verified || false),
"subCountText" => JSON::Any.new(subs_text || "-"),
"invidiousCompanion" => JSON::Any.new(subs_text),
}
return params

View File

@ -22,6 +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["baseUrl"].as_s + src_url if (CONFIG.invidious_companion && params.local)
bitrate = fmt["bitrate"]
mimetype = HTML.escape(fmt["mimeType"].as_s)
@ -34,8 +35,11 @@
<% end %>
<% end %>
<% else %>
<% if params.quality == "dash" %>
<source src="/api/manifest/dash/id/<%= video.id %>?local=true&unique_res=1" type='application/dash+xml' label="dash">
<% if params.quality == "dash"
src_url = "/api/manifest/dash/id/" + video.id + "?local=true&unique_res=1"
src_url = video.invidious_companion["baseUrl"].as_s + src_url if (CONFIG.invidious_companion)
%>
<source src="<%= src_url %>" type='application/dash+xml' label="dash">
<% end %>
<%
@ -44,6 +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["baseUrl"].as_s + src_url if (CONFIG.invidious_companion && params.local)
quality = fmt["quality"]
mimetype = HTML.escape(fmt["mimeType"].as_s)

View File

@ -641,15 +641,21 @@ module YoutubeAPI
LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}")
LOGGER.trace("YoutubeAPI: POST data: #{data}")
invidious_companion_url = CONFIG.invidious_companion
invidious_companion_urls = CONFIG.invidious_companion
# Send the POST request
if invidious_companion_url && endpoint == "/youtubei/v1/player"
if invidious_companion_urls && endpoint == "/youtubei/v1/player"
puts "invidious companion section"
puts invidious_companion_urls[Random.rand(invidious_companion_urls.size)]
begin
body = make_client(URI.parse(invidious_companion_url),
&.post(endpoint, headers: headers, body: data.to_json).body)
rescue
raise InfoException.new("Unable to communicate with Invidious companion.")
response = make_client(URI.parse(invidious_companion_urls[Random.rand(invidious_companion_urls.size)]),
&.post(endpoint, headers: headers, body: data.to_json))
body = response.body
if (response.status_code != 200)
raise Exception.new("status code: " + response.status_code.to_s + " and body: " + body)
end
rescue ex
raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found"))
end
else
body = YT_POOL.client() do |client|
@ -664,8 +670,8 @@ module YoutubeAPI
end
end
if body.nil? && invidious_companion_url
raise InfoException.new("Unable to communicate with Invidious companion.")
if body.nil? && CONFIG.invidious_companion
raise InfoException.new("Error while communicating with Invidious companion: no response data.")
end
# Convert result to Hash