add more parameter to look a lot like official youtube client
Some checks failed
Invidious CI / build (push) Failing after 1m17s

Signed-off-by: Fijxu <fijxu@nadeko.net>

change accept header globally - set it the same as youtube

Signed-off-by: Fijxu <fijxu@nadeko.net>
This commit is contained in:
Emilien Devos 2024-08-15 18:25:10 +02:00 committed by Fijxu
parent e3da8f408d
commit 6dd598bbda
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
2 changed files with 91 additions and 37 deletions

View file

@ -52,7 +52,7 @@ def add_yt_headers(request)
request.headers["User-Agent"] ||= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" request.headers["User-Agent"] ||= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
request.headers["Accept-Charset"] ||= "ISO-8859-1,utf-8;q=0.7,*;q=0.7" 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"] ||= "*"
request.headers["Accept-Language"] ||= "en-us,en;q=0.5" request.headers["Accept-Language"] ||= "en-us,en;q=0.5"
# Preserve original cookies and add new YT consent cookie for EU servers # Preserve original cookies and add new YT consent cookie for EU servers

View file

@ -23,6 +23,10 @@ module YoutubeAPI
private IOS_USER_AGENT = "com.google.ios.youtube/#{IOS_APP_VERSION} (iPhone14,5; U; CPU iOS 17_6 like Mac OS X;)" private IOS_USER_AGENT = "com.google.ios.youtube/#{IOS_APP_VERSION} (iPhone14,5; U; CPU iOS 17_6 like Mac OS X;)"
private IOS_VERSION = "17.6.1.21G93" # Major.Minor.Patch.Build private IOS_VERSION = "17.6.1.21G93" # Major.Minor.Patch.Build
# From reddit: https://web.archive.org/web/20210506123457/https://www.reddit.com/r/youtube/comments/n660l1/how_to_access_tv_mode_in_2020/
private YOUTUBE_TV_USER_AGENT = "Mozilla/5.0 (Web0S; Linux/SmartTV) AppleWebKit/538.2 (KHTML, like Gecko) Large Screen Safari/538.2 LG Browser/7.00.00(LGE; 24LF4820-BU; 03.20.14; 1; DTV_W15L); webOS.TV-2015; LG NetCast.TV-2013 Compatible (LGE, 24LF4820-BU, wireless),gzip(gfe)"
private WEB_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36,gzip(gfe)"
private WINDOWS_VERSION = "10.0" 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
@ -54,7 +58,10 @@ module YoutubeAPI
screen: "WATCH_FULL_SCREEN", screen: "WATCH_FULL_SCREEN",
os_name: "Windows", os_name: "Windows",
os_version: WINDOWS_VERSION, os_version: WINDOWS_VERSION,
browser_name: "Chrome",
browser_version: "128.0.0.0",
platform: "DESKTOP", platform: "DESKTOP",
user_agent: WEB_USER_AGENT,
}, },
ClientType::WebEmbeddedPlayer => { ClientType::WebEmbeddedPlayer => {
name: "WEB_EMBEDDED_PLAYER", name: "WEB_EMBEDDED_PLAYER",
@ -63,7 +70,10 @@ module YoutubeAPI
screen: "EMBED", screen: "EMBED",
os_name: "Windows", os_name: "Windows",
os_version: WINDOWS_VERSION, os_version: WINDOWS_VERSION,
browser_name: "Chrome",
browser_version: "128.0.0.0",
platform: "DESKTOP", platform: "DESKTOP",
user_agent: WEB_USER_AGENT,
}, },
ClientType::WebMobile => { ClientType::WebMobile => {
name: "MWEB", name: "MWEB",
@ -71,7 +81,10 @@ module YoutubeAPI
version: "2.20240813.02.00", version: "2.20240813.02.00",
os_name: "Android", os_name: "Android",
os_version: ANDROID_VERSION, os_version: ANDROID_VERSION,
browser_name: "Chrome",
browser_version: "128.0.0.0",
platform: "MOBILE", platform: "MOBILE",
user_agent: WEB_USER_AGENT,
}, },
ClientType::WebScreenEmbed => { ClientType::WebScreenEmbed => {
name: "WEB", name: "WEB",
@ -80,7 +93,10 @@ module YoutubeAPI
screen: "EMBED", screen: "EMBED",
os_name: "Windows", os_name: "Windows",
os_version: WINDOWS_VERSION, os_version: WINDOWS_VERSION,
browser_name: "Chrome",
browser_version: "128.0.0.0",
platform: "DESKTOP", platform: "DESKTOP",
user_agent: WEB_USER_AGENT,
}, },
# Android # Android
@ -99,6 +115,7 @@ module YoutubeAPI
name: "ANDROID_EMBEDDED_PLAYER", name: "ANDROID_EMBEDDED_PLAYER",
name_proto: "55", name_proto: "55",
version: ANDROID_APP_VERSION, version: ANDROID_APP_VERSION,
user_agent: ANDROID_USER_AGENT,
}, },
ClientType::AndroidScreenEmbed => { ClientType::AndroidScreenEmbed => {
name: "ANDROID", name: "ANDROID",
@ -170,6 +187,7 @@ module YoutubeAPI
name_proto: "85", name_proto: "85",
version: "2.0", version: "2.0",
screen: "EMBED", screen: "EMBED",
user_agent: YOUTUBE_TV_USER_AGENT,
}, },
} }
@ -232,8 +250,8 @@ module YoutubeAPI
HARDCODED_CLIENTS[@client_type][:android_sdk_version]? HARDCODED_CLIENTS[@client_type][:android_sdk_version]?
end end
def user_agent : String? def user_agent : String
HARDCODED_CLIENTS[@client_type][:user_agent]? HARDCODED_CLIENTS[@client_type][:user_agent]
end end
def os_name : String? def os_name : String?
@ -252,6 +270,14 @@ module YoutubeAPI
HARDCODED_CLIENTS[@client_type][:os_version]? HARDCODED_CLIENTS[@client_type][:os_version]?
end end
def browser_name : String?
HARDCODED_CLIENTS[@client_type][:browser_name]?
end
def browser_version : String?
HARDCODED_CLIENTS[@client_type][:browser_version]?
end
def platform : String? def platform : String?
HARDCODED_CLIENTS[@client_type][:platform]? HARDCODED_CLIENTS[@client_type][:platform]?
end end
@ -284,6 +310,15 @@ module YoutubeAPI
"gl" => client_config.region || "US", # Can't be empty! "gl" => client_config.region || "US", # Can't be empty!
"clientName" => client_config.name, "clientName" => client_config.name,
"clientVersion" => client_config.version, "clientVersion" => client_config.version,
"memoryTotalKbytes" => "8000000",
"originalUrl" => "https://www.youtube.com",
"screenDensityFloat" => 1,
"screenHeightPoints" => 1440,
"screenPixelDensity" => 1,
"screenWidthPoints" => 2560,
"userInterfaceTheme" => "USER_INTERFACE_THEME_LIGHT",
"userAgent" => client_config.user_agent,
"clientFormFactor" => "SMALL_FORM_FACTOR",
} of String => String | Int64, } of String => String | Int64,
} }
@ -302,6 +337,14 @@ module YoutubeAPI
client_context["client"]["androidSdkVersion"] = android_sdk_version client_context["client"]["androidSdkVersion"] = android_sdk_version
end end
if browser_name = client_config.browser_name
client_context["client"]["osName"] = browser_name
end
if browser_version = client_config.browser_version
client_context["client"]["osVersion"] = browser_version
end
if device_make = client_config.device_make if device_make = client_config.device_make
client_context["client"]["deviceMake"] = device_make client_context["client"]["deviceMake"] = device_make
end end
@ -470,7 +513,13 @@ module YoutubeAPI
playback_ctx = { playback_ctx = {
"html5Preference" => "HTML5_PREF_WANTS", "html5Preference" => "HTML5_PREF_WANTS",
"referer" => "https://www.youtube.com/watch?v=#{video_id}", "referer" => "https://www.youtube.com/watch?v=#{video_id}",
} of String => String | Int64 "lactMilliseconds" => "-1",
"splay" => false,
"vis" => 0,
"autoCaptionsDefaultOn" => false,
"autonavState" => "STATE_ON",
"currentUrl" => "/watch?v=#{video_id}",
} of String => String | Int64 | Bool
if {"WEB", "TVHTML5"}.any? { |s| client_config.name.starts_with? s } if {"WEB", "TVHTML5"}.any? { |s| client_config.name.starts_with? s }
if sts = DECRYPT_FUNCTION.try &.get_sts if sts = DECRYPT_FUNCTION.try &.get_sts
@ -486,6 +535,11 @@ module YoutubeAPI
"racyCheckOk" => true, "racyCheckOk" => true,
"user" => { "user" => {
"lockedSafetyMode" => false, "lockedSafetyMode" => false,
"enableSafetyMode" => false,
},
"request" => {
"internalExperimentFlags" => {} of String => Hash(Nil, Nil),
"useSsl" => true,
}, },
"playbackContext" => { "playbackContext" => {
"contentPlaybackContext" => playback_ctx, "contentPlaybackContext" => playback_ctx,