diff --git a/src/invidious.cr b/src/invidious.cr index c3e6ee7a..8d1ccd8e 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -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 diff --git a/src/invidious/config.cr b/src/invidious/config.cr index 2d11778d..ff63f869 100644 --- a/src/invidious/config.cr +++ b/src/invidious/config.cr @@ -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? diff --git a/src/invidious/helpers/redis_tokens.cr b/src/invidious/helpers/redis_tokens.cr new file mode 100644 index 00000000..8b9289e1 --- /dev/null +++ b/src/invidious/helpers/redis_tokens.cr @@ -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 diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr index 402cdcc7..d98c933b 100644 --- a/src/invidious/helpers/utils.cr +++ b/src/invidious/helpers/utils.cr @@ -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 \ No newline at end of file diff --git a/src/invidious/jobs/refresh_tokens.cr b/src/invidious/jobs/refresh_tokens.cr new file mode 100644 index 00000000..0d381c80 --- /dev/null +++ b/src/invidious/jobs/refresh_tokens.cr @@ -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 diff --git a/src/invidious/videos/parser.cr b/src/invidious/videos/parser.cr index b133e147..1c1a1642 100644 --- a/src/invidious/videos/parser.cr +++ b/src/invidious/videos/parser.cr @@ -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