From fc0a3ab307b8fd6eb971ccf5397117ca2c84cbdb Mon Sep 17 00:00:00 2001 From: Fijxu Date: Sat, 12 Oct 2024 02:04:14 -0300 Subject: [PATCH] Feat: Experimental support for potoken inside redis Using https://git.nadeko.net/Fijxu/youtube-po-token-generator --- src/invidious/helpers/utils.cr | 8 ++++++++ src/invidious/videos.cr | 2 +- src/invidious/videos/parser.cr | 14 ++++++++++---- src/invidious/yt_backend/youtube_api.cr | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index d98c933b..402cdcc7 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -397,3 +397,11 @@ def gen_videoplayback_proxy_list end return external_videoplayback_proxy end + +def get_po_token + REDIS_DB.get("invidious:po_token") +end + +def get_visitor_data + REDIS_DB.get("invidious:visitor_data") +end \ No newline at end of file diff --git a/src/invidious/videos.cr b/src/invidious/videos.cr index 7e817928..96dcdb25 100644 --- a/src/invidious/videos.cr +++ b/src/invidious/videos.cr @@ -324,7 +324,7 @@ rescue DB::Error end def fetch_video(id, region, po_token, visitor_data) - info = extract_video_info(video_id: id, po_token: po_token, visitor_data: visitor_data) + info = extract_video_info(video_id: id, user_po_token: po_token, user_visitor_data: visitor_data) if reason = info["reason"]? if reason == "Video unavailable" diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index ad891dc4..b133e147 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -50,10 +50,16 @@ def parse_related_video(related : JSON::Any) : Hash(String, JSON::Any)? } end -def extract_video_info(video_id : String, po_token, visitor_data) +def extract_video_info(video_id : String, user_po_token, user_visitor_data) # Init client config for the API client_config = YoutubeAPI::ClientConfig.new + redis_po_token = get_po_token() + redis_visitor_data = get_visitor_data() + + po_token = (user_po_token if !user_po_token.empty?) || redis_po_token || CONFIG.po_token + visitor_data = (user_visitor_data if !user_visitor_data.empty?) || redis_visitor_data || CONFIG.visitor_data + # Fetch data from the player endpoint player_response = YoutubeAPI.player(video_id: video_id, params: "2AMB", client_config: client_config, po_token: po_token, visitor_data: visitor_data) @@ -104,7 +110,7 @@ def extract_video_info(video_id : String, po_token, visitor_data) # Don't use Android client if po_token is passed because po_token doesn't # work for Android client. - if reason.nil? && CONFIG.po_token.nil? + if reason.nil? && po_token.nil? # Fetch the video streams using an Android client in order to get the # decrypted URLs and maybe fix throttling issues (#2194). See the # following issue for an explanation about decrypted URLs: @@ -117,7 +123,7 @@ def extract_video_info(video_id : String, po_token, visitor_data) # Only trigger if reason found and po_token or didn't work wth Android client. # TvHtml5ScreenEmbed now requires sig helper for it to work but po_token is not required # if the IP address is not blocked. - if CONFIG.po_token && reason || CONFIG.po_token.nil? && new_player_response.nil? + if po_token.nil? && reason || po_token.nil? && new_player_response.nil? client_config.client_type = YoutubeAPI::ClientType::TvHtml5ScreenEmbed new_player_response = try_fetch_streaming_data(video_id, client_config, po_token, visitor_data) end @@ -473,7 +479,7 @@ private def convert_url(fmt, po_token) n = DECRYPT_FUNCTION.try &.decrypt_nsig(params["n"]) params["n"] = n if n - if !po_token.empty? + if !po_token.nil? params["pot"] = po_token elsif token = CONFIG.po_token params["pot"] = token diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 564976c3..b2760532 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -460,8 +460,8 @@ module YoutubeAPI *, # Force the following parameters to be passed by name params : String, client_config : ClientConfig | Nil = nil, - po_token : String, - visitor_data : String + po_token : String | Nil, + visitor_data : String | Nil, ) if visitor_data @@visitor_data = visitor_data