Skip to content

Commit

Permalink
Videos: Send player sts when required
Browse files Browse the repository at this point in the history
  • Loading branch information
SamantazFox committed Jul 4, 2024
1 parent a89545f commit 0fc4034
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/invidious/helpers/signatures.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ struct Invidious::DecryptFunction
LOGGER.trace(ex.inspect_with_backtrace)
return nil
end

def get_sts : UInt64?
self.check_update
return SigHelper::Client.get_sts
rescue ex
LOGGER.debug(ex.message || "Signature: Unknown error")
LOGGER.trace(ex.inspect_with_backtrace)
return nil
end
end
24 changes: 18 additions & 6 deletions src/invidious/yt_backend/youtube_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# This file contains youtube API wrappers
#

private STS_FETCHER = IV::DecryptFunction.new

module YoutubeAPI
extend self

Expand Down Expand Up @@ -293,7 +295,7 @@ module YoutubeAPI
# Return, as a Hash, the "context" data required to request the
# youtube API endpoints.
#
private def make_context(client_config : ClientConfig | Nil) : Hash
private def make_context(client_config : ClientConfig | Nil, video_id = "dQw4w9WgXcQ") : Hash
# Use the default client config if nil is passed
client_config ||= DEFAULT_CLIENT_CONFIG

Expand All @@ -313,7 +315,7 @@ module YoutubeAPI

if client_config.screen == "EMBED"
client_context["thirdParty"] = {
"embedUrl" => "https://www.youtube.com/embed/dQw4w9WgXcQ",
"embedUrl" => "https://www.youtube.com/embed/#{video_id}",
} of String => String | Int64
end

Expand Down Expand Up @@ -474,19 +476,29 @@ module YoutubeAPI
params : String,
client_config : ClientConfig | Nil = nil
)
# Playback context, separate because it can be different between clients
playback_ctx = {
"html5Preference" => "HTML5_PREF_WANTS",
"referer" => "https://www.youtube.com/watch?v=#{video_id}",
} of String => String | Int64

if {"WEB", "TVHTML5"}.any? { |s| client_config.name.starts_with? s }
if sts = STS_FETCHER.get_sts
playback_ctx["signatureTimestamp"] = sts.to_i64
end
end

# JSON Request data, required by the API
data = {
"contentCheckOk" => true,
"videoId" => video_id,
"context" => self.make_context(client_config),
"context" => self.make_context(client_config, video_id),
"racyCheckOk" => true,
"user" => {
"lockedSafetyMode" => false,
},
"playbackContext" => {
"contentPlaybackContext" => {
"html5Preference": "HTML5_PREF_WANTS",
},
"contentPlaybackContext" => playback_ctx,
},
}

Expand Down

0 comments on commit 0fc4034

Please sign in to comment.