Tokens: Refresh po_token and visitor_data every 5 seconds
All checks were successful
Invidious CI / build (push) Successful in 8m45s

Closes #11
This commit is contained in:
Fijxu 2024-10-13 15:57:51 -03:00
parent 6587528ed9
commit 66b481713d
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
6 changed files with 46 additions and 10 deletions

View file

@ -193,6 +193,10 @@ if !CONFIG.external_videoplayback_proxy.empty?
Invidious::Jobs.register Invidious::Jobs::CheckExternalProxy.new
end
if CONFIG.refresh_tokens
Invidious::Jobs.register Invidious::Jobs::RefreshTokens.new
end
Invidious::Jobs.start_all
def popular_videos

View file

@ -184,6 +184,9 @@ class Config
# at the start of the URI
property external_videoplayback_proxy : Array(String) = [] of String
# Job to refresh tokens from a Redis compatible DB
property refresh_tokens : Bool = true
# Materialious redirects
property materialious_domain : String?

View file

@ -0,0 +1,25 @@
module Tokens
extend self
@@po_token : String | Nil
@@visitor_data : String | Nil
def refresh_tokens
@@po_token = REDIS_DB.get("invidious:po_token")
@@visitor_data = REDIS_DB.get("invidious:visitor_data")
LOGGER.debug("RefreshTokens: Tokens are:")
LOGGER.debug("RefreshTokens: po_token: #{@@po_token}")
LOGGER.debug("RefreshTokens: visitor_data: #{@@visitor_data}")
end
def get_tokens
return {@@po_token, @@visitor_data}
end
def get_po_token
return @@po_token
end
def get_visitor_data
return @@visitor_data
end
end

View file

@ -397,11 +397,3 @@ 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

View file

@ -0,0 +1,13 @@
class Invidious::Jobs::RefreshTokens < Invidious::Jobs::BaseJob
def initialize
end
def begin
loop do
Tokens.refresh_tokens
LOGGER.info("RefreshTokens: Done, sleeping for 5 seconds")
sleep 5.seconds
Fiber.yield
end
end
end

View file

@ -54,8 +54,7 @@ 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()
redis_po_token, redis_visitor_data = Tokens.get_tokens
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