From 4bdbdfdc128fd9f02ef51043cc9a9a304441a620 Mon Sep 17 00:00:00 2001 From: mooleshacat <43627985+mooleshacat@users.noreply.github.com> Date: Tue, 22 Oct 2024 03:02:14 -0400 Subject: [PATCH 1/2] tokenmon cleanup --- src/invidious/jobs/token_monitor.cr | 17 ----- src/invidious/reloadtoken.cr | 83 ------------------------- src/invidious/yt_backend/youtube_api.cr | 12 ++-- 3 files changed, 6 insertions(+), 106 deletions(-) delete mode 100644 src/invidious/jobs/token_monitor.cr delete mode 100644 src/invidious/reloadtoken.cr diff --git a/src/invidious/jobs/token_monitor.cr b/src/invidious/jobs/token_monitor.cr deleted file mode 100644 index d2b0f884..00000000 --- a/src/invidious/jobs/token_monitor.cr +++ /dev/null @@ -1,17 +0,0 @@ - -class Invidious::Jobs::MonitorCfgTokensJob < Invidious::Jobs::BaseJob - include Invidious - def begin - loop do - - LOGGER.info("jobs: running MonitorCfgTokensJob job") - - ReloadTokens.get_tokens - - LOGGER.info("jobs: MonitorCfgTokensJob: pot: " + ReloadTokens.pot.as(String)) - LOGGER.info("jobs: MonitorCfgTokensJob: vdata: " + ReloadTokens.vdata.as(String)) - - sleep 1.minutes - end - end -end diff --git a/src/invidious/reloadtoken.cr b/src/invidious/reloadtoken.cr deleted file mode 100644 index f9428184..00000000 --- a/src/invidious/reloadtoken.cr +++ /dev/null @@ -1,83 +0,0 @@ -class ReloadTokens - - @@instance = new - - def self.pot - @@pot - end - - def self.vdata - @@vdata - end - - def initialize - - @@pot = "error" - @@vdata = "error" - - end - - def self.get_tokens - - # Load config from file or YAML string env var - env_config_file = "INVIDIOUS_CONFIG_FILE" - env_config_yaml = "INVIDIOUS_CONFIG" - - config_file = ENV.has_key?(env_config_file) ? ENV.fetch(env_config_file) : "config/config.yml" - config_yaml = ENV.has_key?(env_config_yaml) ? ENV.fetch(env_config_yaml) : File.read(config_file) - - config = Config.from_yaml(config_yaml) - - - - - # Update config from env vars (upcased and prefixed with "INVIDIOUS_") - {% for ivar in Config.instance_vars %} - {% env_id = "INVIDIOUS_#{ivar.id.upcase}" %} - - if ENV.has_key?({{env_id}}) - env_value = ENV.fetch({{env_id}}) - success = false - - # Use YAML converter if specified - {% ann = ivar.annotation(::YAML::Field) %} - {% if ann && ann[:converter] %} - config.{{ivar.id}} = {{ann[:converter]}}.from_yaml(YAML::ParseContext.new, YAML::Nodes.parse(ENV.fetch({{env_id}})).nodes[0]) - success = true - - # Use regular YAML parser otherwise - {% else %} - {% ivar_types = ivar.type.union? ? ivar.type.union_types : [ivar.type] %} - # Sort types to avoid parsing nulls and numbers as strings - {% ivar_types = ivar_types.sort_by { |ivar_type| ivar_type == Nil ? 0 : ivar_type == Int32 ? 1 : 2 } %} - {{ivar_types}}.each do |ivar_type| - if !success - begin - config.{{ivar.id}} = ivar_type.from_yaml(env_value) - success = true - rescue - # nop - end - end - end - {% end %} - - # Exit on fail - if !success - puts %(Config.{{ivar.id}} failed to parse #{env_value} as {{ivar.type}}) - exit(1) - end - end - {% end %} - - - @@pot = config.po_token - @@vdata = config.visitor_data - - end - - def self.get_instance - return @@instance - end - -end \ No newline at end of file diff --git a/src/invidious/yt_backend/youtube_api.cr b/src/invidious/yt_backend/youtube_api.cr index 3570efbe..441e612f 100644 --- a/src/invidious/yt_backend/youtube_api.cr +++ b/src/invidious/yt_backend/youtube_api.cr @@ -279,8 +279,8 @@ module YoutubeAPI # determine po_token and visitor_data if CONFIG.tokenmon_enabled # get the pot/vdata for usage later - pot = ReloadTokens.pot.as(String) - vdata = ReloadTokens.vdata.as(String) + pot = Invidious::TokenMon.pot.as(String) + vdata = Invidious::TokenMon.vdata.as(String) else # Use the configured pot pot = CONFIG.po_token.as(String) @@ -486,8 +486,8 @@ module YoutubeAPI # determine po_token and visitor_data if CONFIG.tokenmon_enabled # get the pot/vdata for usage later - pot = ReloadTokens.pot.as(String) - vdata = ReloadTokens.vdata.as(String) + pot = Invidious::TokenMon.pot.as(String) + vdata = Invidious::TokenMon.vdata.as(String) else # Use the configured pot pot = CONFIG.po_token.as(String) @@ -644,8 +644,8 @@ module YoutubeAPI # determine po_token and visitor_data if CONFIG.tokenmon_enabled # get the pot/vdata for usage later - pot = ReloadTokens.pot.as(String) - vdata = ReloadTokens.vdata.as(String) + pot = Invidious::TokenMon.pot.as(String) + vdata = Invidious::TokenMon.vdata.as(String) else # Use the configured pot pot = CONFIG.po_token.as(String) From 588247ddfac94f02c9c8bcf217d89b0ded1835eb Mon Sep 17 00:00:00 2001 From: mooleshacat <43627985+mooleshacat@users.noreply.github.com> Date: Tue, 22 Oct 2024 03:02:21 -0400 Subject: [PATCH 2/2] tokenmon cleanup --- src/invidious/helpers/tokenmon.cr | 40 +++++++++++++++++++++++++++++++ src/invidious/jobs/tokenmon.cr | 17 +++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/invidious/helpers/tokenmon.cr create mode 100644 src/invidious/jobs/tokenmon.cr diff --git a/src/invidious/helpers/tokenmon.cr b/src/invidious/helpers/tokenmon.cr new file mode 100644 index 00000000..58aeb2ce --- /dev/null +++ b/src/invidious/helpers/tokenmon.cr @@ -0,0 +1,40 @@ +class Invidious::TokenMon + + @@instance = new + + def self.pot + @@pot + end + + def self.vdata + @@vdata + end + + def initialize + + @@pot = "error" + @@vdata = "error" + + end + + def self.get_tokens + + # Load config from file or YAML string env var + env_config_file = "INVIDIOUS_CONFIG_FILE" + env_config_yaml = "INVIDIOUS_CONFIG" + + config_file = ENV.has_key?(env_config_file) ? ENV.fetch(env_config_file) : "config/config.yml" + config_yaml = ENV.has_key?(env_config_yaml) ? ENV.fetch(env_config_yaml) : File.read(config_file) + + config = Config.from_yaml(config_yaml) + + @@pot = config.po_token + @@vdata = config.visitor_data + + end + + def self.get_instance + return @@instance + end + +end \ No newline at end of file diff --git a/src/invidious/jobs/tokenmon.cr b/src/invidious/jobs/tokenmon.cr new file mode 100644 index 00000000..b96c5557 --- /dev/null +++ b/src/invidious/jobs/tokenmon.cr @@ -0,0 +1,17 @@ + +class Invidious::Jobs::MonitorCfgTokensJob < Invidious::Jobs::BaseJob + include Invidious + def begin + loop do + + LOGGER.info("jobs: running MonitorCfgTokensJob job") + + Invidious::TokenMon.get_tokens + + LOGGER.info("jobs: MonitorCfgTokensJob: pot: " + Invidious::TokenMon.pot.as(String)) + LOGGER.info("jobs: MonitorCfgTokensJob: vdata: " + Invidious::TokenMon.vdata.as(String)) + + sleep 1.minutes + end + end +end