From 4aa51045eb9c8b607c6a29467aa3ccaa1ab9603d Mon Sep 17 00:00:00 2001 From: Fijxu Date: Tue, 15 Oct 2024 18:50:17 -0300 Subject: [PATCH] Chatty: Replace usernames by Twitch IDs --- src/config.cr | 1 + src/providers/chatty.cr | 31 +++++++++++++++++++++---------- src/providers/main.cr | 5 ++--- src/vanity-tester-backend.cr | 7 ++++--- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/config.cr b/src/config.cr index 0932d77..27c7a36 100644 --- a/src/config.cr +++ b/src/config.cr @@ -7,6 +7,7 @@ class Config property db : String = "./db.sqlite3" property redisUrl : String = "127.0.0.1:6379" property log_level : LogLevel = LogLevel::Debug + property key_expiration_time : Int32 = 60 def self.load config_file = "./config.yml" diff --git a/src/providers/chatty.cr b/src/providers/chatty.cr index dacf5cf..49fe2cf 100644 --- a/src/providers/chatty.cr +++ b/src/providers/chatty.cr @@ -9,20 +9,31 @@ module Providers::Chatty res end - private def postprocessing(res) : Nil + private def postprocessing(res) : JSON::Any + users = 0 + + # Counts the chatty supporters + res.as_a.each do |item| + next if item["id"] != "chatty" + users += item["usernames"].size + end + + wg = WaitGroup.new(users) + res.as_a.each_with_index do |item, index| - item["id"].as_a if item["id"] != "chatty" - puts item + next if item["id"] != "chatty" + item["usernames"].as_a.each_with_index do |username, index| - item["usernames"].as_a.size.times do - spawn do - id = JSON::Any.new(Utils::GQL.new.get_user_id(username.to_s)) - puts "username: #{username}, id: #{id}" - item["usernames"].as_a[index] = id - end + spawn do + id = JSON::Any.new(Utils::GQL.new.get_user_id(username.to_s)) + item["usernames"].as_a[index] = id + ensure + wg.done end end end - pp res + # Waits for all the fibers to complete before returning res + wg.wait + return res end end diff --git a/src/providers/main.cr b/src/providers/main.cr index e9e4986..18e3399 100644 --- a/src/providers/main.cr +++ b/src/providers/main.cr @@ -16,8 +16,7 @@ module Providers spawn { info[1].send(Providers::BTTV.get_badges) } spawn { info[2].send(Providers::FFZ.get_badges) } spawn { info[3].send(Providers::DankChat.get_badges) } - Providers::Chatty.get_badges - # spawn { info[4].send(Providers::Chatty.get_badges) } + spawn { info[4].send(Providers::Chatty.get_badges) } # spawn { info[5].send(Providers::SevenTV.get_badges) } # items = {chatterino: info[0].receive, bttv: info.receive, ffz: ffz.receive, dankchat: dankchat.receive, chatty: chatty.receive} items = { @@ -25,7 +24,7 @@ module Providers bttv: info[1].receive, ffz: info[2].receive, dankchat: info[3].receive, - # chatty: info[4].receive, + chatty: info[4].receive, # seventv: info[5].receive, } return items diff --git a/src/vanity-tester-backend.cr b/src/vanity-tester-backend.cr index d2a47bb..69860dd 100644 --- a/src/vanity-tester-backend.cr +++ b/src/vanity-tester-backend.cr @@ -17,6 +17,7 @@ Kemal.config.app_name = "vanity-tester-backend" SQL = DB.open("sqlite3://#{CONFIG.db}") REDIS_DB = Redis::Client.new(URI.parse("redis://#{CONFIG.redisUrl}")) LOGGER = LogHandler.new(STDOUT, CONFIG.log_level) +REDIS_PREFIX = "vanity-tester-backend" Utils.create_db @@ -33,7 +34,7 @@ get "/badges" do |env| next data end data = Handlers.parse_badges(env) - Utils::Redis.save("badges", data, 300) + Utils::Redis.save("badges", data, CONFIG.key_expiration_time) data end @@ -42,7 +43,7 @@ get "/7tv/badges" do |env| next data end data = Handlers.parse_7tv_badges(env) - Utils::Redis.save("7tvbadges", data, 300) + Utils::Redis.save("7tvbadges", data, CONFIG.key_expiration_time) data end @@ -51,7 +52,7 @@ get "/7tv/paints" do |env| next data end data = Handlers.parse_7tv_paints(env) - Utils::Redis.save("7tvpaints", data, 300) + Utils::Redis.save("7tvpaints", data, CONFIG.key_expiration_time) data end