replicate headers and params made by yt apps

pull/3290/head
Émilien Devos 2 years ago
parent a7d9df5516
commit 31244cbcc8

@ -7,17 +7,19 @@
{% end %} {% end %}
def add_yt_headers(request) def add_yt_headers(request)
request.headers["user-agent"] ||= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36" if request.headers["User-Agent"] == "Crystal"
request.headers["accept-charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7" request.headers["User-Agent"] ||= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
request.headers["accept"] ||= "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" end
request.headers["accept-language"] ||= "en-us,en;q=0.5" request.headers["Accept-Charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
request.headers["Accept"] ||= "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
request.headers["Accept-Language"] ||= "en-us,en;q=0.5"
return if request.resource.starts_with? "/sorry/index" return if request.resource.starts_with? "/sorry/index"
request.headers["x-youtube-client-name"] ||= "1" request.headers["x-youtube-client-name"] ||= "1"
request.headers["x-youtube-client-version"] ||= "2.20200609" request.headers["x-youtube-client-version"] ||= "2.20200609"
# Preserve original cookies and add new YT consent cookie for EU servers # Preserve original cookies and add new YT consent cookie for EU servers
request.headers["cookie"] = "#{request.headers["cookie"]?}; CONSENT=YES+" request.headers["Cookie"] = "#{request.headers["cookie"]?}; CONSENT=YES+"
if !CONFIG.cookies.empty? if !CONFIG.cookies.empty?
request.headers["cookie"] = "#{(CONFIG.cookies.map { |c| "#{c.name}=#{c.value}" }).join("; ")}; #{request.headers["cookie"]?}" request.headers["Cookie"] = "#{(CONFIG.cookies.map { |c| "#{c.name}=#{c.value}" }).join("; ")}; #{request.headers["cookie"]?}"
end end
end end

@ -7,9 +7,12 @@ module YoutubeAPI
private DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" private DEFAULT_API_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
private ANDROID_APP_VERSION = "17.29.35" private ANDROID_APP_VERSION = "17.33.42"
private ANDROID_SDK_VERSION = 30_i64 private ANDROID_USER_AGENT = "com.google.android.youtube/17.33.42 (Linux; U; Android 12; US)"
private IOS_APP_VERSION = "17.30.1" private ANDROID_SDK_VERSION = 31_i64
private ANDROID_VERSION = "12"
private IOS_APP_VERSION = "17.33.2"
private WINDOWS_VERSION = "10.0"
# Enumerate used to select one of the clients supported by the API # Enumerate used to select one of the clients supported by the API
enum ClientType enum ClientType
@ -33,27 +36,39 @@ module YoutubeAPI
# List of hard-coded values used by the different clients # List of hard-coded values used by the different clients
HARDCODED_CLIENTS = { HARDCODED_CLIENTS = {
ClientType::Web => { ClientType::Web => {
name: "WEB", name: "WEB",
version: "2.20220804.07.00", version: "2.20220804.07.00",
api_key: DEFAULT_API_KEY, api_key: DEFAULT_API_KEY,
screen: "WATCH_FULL_SCREEN", screen: "WATCH_FULL_SCREEN",
os_name: "Windows",
os_version: WINDOWS_VERSION,
platform: "DESKTOP",
}, },
ClientType::WebEmbeddedPlayer => { ClientType::WebEmbeddedPlayer => {
name: "WEB_EMBEDDED_PLAYER", # 56 name: "WEB_EMBEDDED_PLAYER", # 56
version: "1.20220803.01.00", version: "1.20220803.01.00",
api_key: DEFAULT_API_KEY, api_key: DEFAULT_API_KEY,
screen: "EMBED", screen: "EMBED",
os_name: "Windows",
os_version: WINDOWS_VERSION,
platform: "DESKTOP",
}, },
ClientType::WebMobile => { ClientType::WebMobile => {
name: "MWEB", name: "MWEB",
version: "2.20220805.01.00", version: "2.20220805.01.00",
api_key: DEFAULT_API_KEY, api_key: DEFAULT_API_KEY,
os_name: "Android",
os_version: ANDROID_VERSION,
platform: "MOBILE",
}, },
ClientType::WebScreenEmbed => { ClientType::WebScreenEmbed => {
name: "WEB", name: "WEB",
version: "2.20220804.00.00", version: "2.20220804.00.00",
api_key: DEFAULT_API_KEY, api_key: DEFAULT_API_KEY,
screen: "EMBED", screen: "EMBED",
os_name: "Windows",
os_version: WINDOWS_VERSION,
platform: "DESKTOP",
}, },
# Android # Android
@ -63,6 +78,10 @@ module YoutubeAPI
version: ANDROID_APP_VERSION, version: ANDROID_APP_VERSION,
api_key: "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w", api_key: "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w",
android_sdk_version: ANDROID_SDK_VERSION, android_sdk_version: ANDROID_SDK_VERSION,
user_agent: ANDROID_USER_AGENT,
os_name: "Android",
os_version: ANDROID_VERSION,
platform: "MOBILE",
}, },
ClientType::AndroidEmbeddedPlayer => { ClientType::AndroidEmbeddedPlayer => {
name: "ANDROID_EMBEDDED_PLAYER", # 55 name: "ANDROID_EMBEDDED_PLAYER", # 55
@ -75,6 +94,10 @@ module YoutubeAPI
api_key: DEFAULT_API_KEY, api_key: DEFAULT_API_KEY,
screen: "EMBED", screen: "EMBED",
android_sdk_version: ANDROID_SDK_VERSION, android_sdk_version: ANDROID_SDK_VERSION,
user_agent: ANDROID_USER_AGENT,
os_name: "Android",
os_version: ANDROID_VERSION,
platform: "MOBILE",
}, },
# IOS # IOS
@ -179,6 +202,22 @@ module YoutubeAPI
HARDCODED_CLIENTS[@client_type][:android_sdk_version]? HARDCODED_CLIENTS[@client_type][:android_sdk_version]?
end end
def user_agent : String?
HARDCODED_CLIENTS[@client_type][:user_agent]?
end
def os_name : String?
HARDCODED_CLIENTS[@client_type][:os_name]?
end
def os_version : String?
HARDCODED_CLIENTS[@client_type][:os_version]?
end
def platform : String?
HARDCODED_CLIENTS[@client_type][:platform]?
end
# Convert to string, for logging purposes # Convert to string, for logging purposes
def to_s def to_s
return { return {
@ -226,6 +265,18 @@ module YoutubeAPI
client_context["client"]["androidSdkVersion"] = android_sdk_version client_context["client"]["androidSdkVersion"] = android_sdk_version
end end
if os_name = client_config.os_name
client_context["client"]["osName"] = os_name
end
if os_version = client_config.os_version
client_context["client"]["osVersion"] = os_version
end
if platform = client_config.platform
client_context["client"]["platform"] = platform
end
return client_context return client_context
end end
@ -361,8 +412,18 @@ module YoutubeAPI
) )
# JSON Request data, required by the API # JSON Request data, required by the API
data = { data = {
"videoId" => video_id, "contentCheckOk" => true,
"context" => self.make_context(client_config), "videoId" => video_id,
"context" => self.make_context(client_config),
"racyCheckOk" => true,
"user" => {
"lockedSafetyMode" => false,
},
"playbackContext" => {
"contentPlaybackContext" => {
"html5Preference": "HTML5_PREF_WANTS",
},
},
} }
# Append the additional parameters if those were provided # Append the additional parameters if those were provided
@ -460,10 +521,15 @@ module YoutubeAPI
url = "#{endpoint}?key=#{client_config.api_key}&prettyPrint=false" url = "#{endpoint}?key=#{client_config.api_key}&prettyPrint=false"
headers = HTTP::Headers{ headers = HTTP::Headers{
"Content-Type" => "application/json; charset=UTF-8", "Content-Type" => "application/json; charset=UTF-8",
"Accept-Encoding" => "gzip, deflate", "Accept-Encoding" => "gzip, deflate",
"x-goog-api-format-version" => "2",
} }
if user_agent = client_config.user_agent
headers["User-Agent"] = user_agent
end
# Logging # Logging
LOGGER.debug("YoutubeAPI: Using endpoint: \"#{endpoint}\"") LOGGER.debug("YoutubeAPI: Using endpoint: \"#{endpoint}\"")
LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}") LOGGER.trace("YoutubeAPI: ClientConfig: #{client_config}")

Loading…
Cancel
Save