add support for invidious companion

This commit is contained in:
Emilien Devos 2024-10-20 02:10:55 +02:00 committed by Emilien
parent 9892604758
commit 3dff7a76cf
4 changed files with 71 additions and 27 deletions

View File

@ -54,6 +54,22 @@ db:
## ##
#signature_server: #signature_server:
##
## Path to the Invidious companion.
## An external program for loading the video streams from YouTube servers.
##
## When this setting is commented out, Invidious companion is not used.
##
## When this setting is configured and "external_port" is used then
## you need to configure Invidious companion routes into your reverse proxy.
## If "external_port" is not configured then Invidious will proxy the requests
## to Invidious companion.
##
## Accepted values: "http(s)://<IP-HOSTNAME>:<Port>"
## Default: <none>
##
#invidious_companion:
######################################### #########################################
# #

View File

@ -151,6 +151,9 @@ class Config
# poToken for passing bot attestation # poToken for passing bot attestation
property po_token : String? = nil property po_token : String? = nil
# Invidious companion
property invidious_companion : String? = nil
# Saved cookies in "name1=value1; name2=value2..." format # Saved cookies in "name1=value1; name2=value2..." format
@[YAML::Field(converter: Preferences::StringToCookies)] @[YAML::Field(converter: Preferences::StringToCookies)]
property cookies : HTTP::Cookies = HTTP::Cookies.new property cookies : HTTP::Cookies = HTTP::Cookies.new

View File

@ -100,27 +100,30 @@ def extract_video_info(video_id : String)
params = parse_video_info(video_id, player_response) params = parse_video_info(video_id, player_response)
params["reason"] = JSON::Any.new(reason) if reason params["reason"] = JSON::Any.new(reason) if reason
new_player_response = nil if CONFIG.invidious_companion.nil?
new_player_response = nil
# Don't use Android test suite client if po_token is passed because po_token doesn't # Don't use Android test suite client if po_token is passed because po_token doesn't
# work for Android test suite client. # work for Android test suite client.
if reason.nil? && CONFIG.po_token.nil? if reason.nil? && CONFIG.po_token.nil?
# Fetch the video streams using an Android client in order to get the # Fetch the video streams using an Android client in order to get the
# decrypted URLs and maybe fix throttling issues (#2194). See the # decrypted URLs and maybe fix throttling issues (#2194). See the
# following issue for an explanation about decrypted URLs: # following issue for an explanation about decrypted URLs:
# https://github.com/TeamNewPipe/NewPipeExtractor/issues/562 # https://github.com/TeamNewPipe/NewPipeExtractor/issues/562
client_config.client_type = YoutubeAPI::ClientType::AndroidTestSuite client_config.client_type = YoutubeAPI::ClientType::AndroidTestSuite
new_player_response = try_fetch_streaming_data(video_id, client_config) new_player_response = try_fetch_streaming_data(video_id, client_config)
end end
# Replace player response and reset reason # Replace player response and reset reason
if !new_player_response.nil? if !new_player_response.nil?
# Preserve captions & storyboard data before replacement # Preserve captions & storyboard data before replacement
new_player_response["storyboards"] = player_response["storyboards"] if player_response["storyboards"]? new_player_response["storyboards"] = player_response["storyboards"] if player_response["storyboards"]?
new_player_response["captions"] = player_response["captions"] if player_response["captions"]? new_player_response["captions"] = player_response["captions"] if player_response["captions"]?
player_response = new_player_response player_response = new_player_response
params.delete("reason") params.delete("reason")
end
end
end end
{"captions", "playabilityStatus", "playerConfig", "storyboards"}.each do |f| {"captions", "playabilityStatus", "playerConfig", "storyboards"}.each do |f|

View File

@ -615,12 +615,19 @@ module YoutubeAPI
headers = HTTP::Headers{ headers = HTTP::Headers{
"Content-Type" => "application/json; charset=UTF-8", "Content-Type" => "application/json; charset=UTF-8",
"Accept-Encoding" => "gzip, deflate",
"x-goog-api-format-version" => "2", "x-goog-api-format-version" => "2",
"x-youtube-client-name" => client_config.name_proto, "x-youtube-client-name" => client_config.name_proto,
"x-youtube-client-version" => client_config.version, "x-youtube-client-version" => client_config.version,
} }
if CONFIG.invidious_companion && endpoint == "/youtubei/v1/player"
headers["Authorization"] = "Bearer " + CONFIG.hmac_key
end
if !CONFIG.invidious_companion
headers["Accept-Encoding"] = "gzip, deflate"
end
if user_agent = client_config.user_agent if user_agent = client_config.user_agent
headers["User-Agent"] = user_agent headers["User-Agent"] = user_agent
end end
@ -634,16 +641,31 @@ module YoutubeAPI
LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}") LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}")
LOGGER.trace("YoutubeAPI: POST data: #{data}") LOGGER.trace("YoutubeAPI: POST data: #{data}")
invidious_companion_url = CONFIG.invidious_companion
# Send the POST request # Send the POST request
body = YT_POOL.client() do |client| if invidious_companion_url && endpoint == "/youtubei/v1/player"
client.post(url, headers: headers, body: data.to_json) do |response| begin
if response.status_code != 200 body = make_client(URI.parse(invidious_companion_url),
raise InfoException.new("Error: non 200 status code. Youtube API returned \ &.post(endpoint, headers: headers, body: data.to_json).body)
status code #{response.status_code}. See <a href=\"https://docs.invidious.io/youtube-errors-explained/\"> \ rescue
https://docs.invidious.io/youtube-errors-explained/</a> for troubleshooting.") raise InfoException.new("Unable to communicate with Invidious companion.")
end
self._decompress(response.body_io, response.headers["Content-Encoding"]?)
end end
else
body = YT_POOL.client() do |client|
client.post(url, headers: headers, body: data.to_json) do |response|
if response.status_code != 200
raise InfoException.new("Error: non 200 status code. Youtube API returned \
status code #{response.status_code}. See <a href=\"https://docs.invidious.io/youtube-errors-explained/\"> \
https://docs.invidious.io/youtube-errors-explained/</a> for troubleshooting.")
end
self._decompress(response.body_io, response.headers["Content-Encoding"]?)
end
end
end
if body.nil? && invidious_companion_url
raise InfoException.new("Unable to communicate with Invidious companion.")
end end
# Convert result to Hash # Convert result to Hash