diff --git a/src/handling.cr b/src/handling.cr index 5a0e6d5..732e735 100644 --- a/src/handling.cr +++ b/src/handling.cr @@ -10,11 +10,16 @@ module Handlers j.field "badges" do j.array do badges_tuple[:chatterino]["badges"]?.try &.as_a.each do |item| + images = [item["image1"], item["image2"], item["image3"]] j.object do j.field "name", item["tooltip"] - j.field "url1x", item["image1"] - j.field "url2x", item["image2"] - j.field "url3x", item["image3"] + j.field "urls" do + j.array do + images.each do |imageurl| + j.string imageurl + end + end + end j.field "users", item["users"] end end @@ -79,16 +84,20 @@ module Handlers j.field "badges" do j.array do badges_tuple[:ffz]?.try &.["badges"]?.try &.as_a.each do |item| + images = [item["urls"]["1"], item["urls"]["2"], item["urls"]["4"]] j.object do j.field "id", item["id"] j.field "name", item["name"] j.field "title", item["title"] j.field "color", item["color"] - j.field "url1x", item["urls"]["1"] - j.field "url2x", item["urls"]["2"] - j.field "url4x", item["urls"]["4"] + j.field "urls" do + j.array do + images.each do |imageurl| + j.string imageurl + end + end + end j.field "users", badges_tuple[:ffz]["users"][item["id"].to_s] - # end end end end @@ -102,7 +111,11 @@ module Handlers badges_tuple[:dankchat]?.try &.as_a.each do |item| j.object do j.field "name", item["type"] - j.field "url", item["url"] + j.field "urls" do + j.array do + j.string item["url"] + end + end j.field "users", item["users"] end end @@ -118,12 +131,19 @@ module Handlers # Chatty also returns the badges from FFZ, which is not needed since # we already pull them directly from the api next if item["id"] != "chatty" + + images = [item["image_url"], item["image_url_2"], item["image_url_4"]] j.object do j.field "name", item["meta_title"]? - j.field "badgeUrl", item["meta_url"]? - j.field "url1x", "https:#{item["image_url"]?}" - j.field "url2x", "https:#{item["image_url_2"]?}" - j.field "url4x", "https:#{item["image_url_4"]?}" + # Zonian pidio que lo eliminara + # j.field "badgeUrl", item["meta_url"]? + j.field "urls" do + j.array do + images.each do |imageurl| + j.string imageurl + end + end + end j.field "users", item["usernames"]? end end diff --git a/src/providers/chatty.cr b/src/providers/chatty.cr index 19f3a67..dacf5cf 100644 --- a/src/providers/chatty.cr +++ b/src/providers/chatty.cr @@ -1,8 +1,28 @@ +require "wait_group" + module Providers::Chatty extend self def get_badges res = HTTP::Client.get("https://tduva.com/res/badges", headers: HEADERS) - JSON.parse(res.body) + res = postprocessing(JSON.parse(res.body)) + res + end + + private def postprocessing(res) : Nil + res.as_a.each_with_index do |item, index| + item["id"].as_a if item["id"] != "chatty" + puts item + 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 + end + end + end + pp res end end diff --git a/src/providers/frankerFazeZ.cr b/src/providers/frankerFazeZ.cr index e112c6a..7269eae 100644 --- a/src/providers/frankerFazeZ.cr +++ b/src/providers/frankerFazeZ.cr @@ -45,8 +45,8 @@ module Providers::FFZ JSON.parse(res.body) end - # def get_channel(channel_id : String) - # res = HTTP::Client.get("https://api.frankerfacez.com/v1/room/id/#{channel_id}", headers: HEADERS) - # channel_data = JSON.parse(res.body) - + def get_channel(channel_id : String) + res = HTTP::Client.get("https://api.frankerfacez.com/v1/room/id/#{channel_id}", headers: HEADERS) + JSON.parse(res.body) + end end diff --git a/src/providers/main.cr b/src/providers/main.cr index 18e3399..e9e4986 100644 --- a/src/providers/main.cr +++ b/src/providers/main.cr @@ -16,7 +16,8 @@ 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) } - spawn { info[4].send(Providers::Chatty.get_badges) } + 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 = { @@ -24,7 +25,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/utils/gql.cr b/src/utils/gql.cr new file mode 100644 index 0000000..444b17a --- /dev/null +++ b/src/utils/gql.cr @@ -0,0 +1,22 @@ +class Utils::GQL + private def req(query) + headers = HTTP::Headers{ + "Content-Type" => "application/json", + "Client-Id" => "ue6666qo983tsx6so1t0vnawi233wa", + } + data = {"query" => query} + response = HTTP::Client.post("https://gql.twitch.tv/gql", headers: headers, body: data.to_json) + + if response.success? + response.body + else + raise "GQL Twitch API returned #{response.status_code}: #{response.body}" + end + end + + def get_user_id(username : String) : String | Nil + query = %( query { user(login: "#{username}") { id } } ) + res = JSON.parse(req(query)) + return res["data"]["user"]["id"]?.try &.to_s + end +end diff --git a/src/utils.cr b/src/utils/utils.cr similarity index 76% rename from src/utils.cr rename to src/utils/utils.cr index b54e2a7..f02e4c0 100644 --- a/src/utils.cr +++ b/src/utils/utils.cr @@ -2,11 +2,13 @@ module Utils extend self def create_db + # TODO: Check if the chatty table exists too! if !SQL.query_one "SELECT EXISTS (SELECT 1 FROM sqlite_schema WHERE type='table' AND name='seventv_ids') AND EXISTS (SELECT 1 FROM sqlite_schema WHERE type='table' AND name='seventv_ids');", as: Bool LOGGER.info "Creating sqlite3 database at '#{CONFIG.db}'" begin - SQL.exec "CREATE TABLE IF NOT EXISTS seventv_ids (twitch_id text UNIQUE, seventv_id text UNIQUE, username text UNIQUE)" + SQL.exec "CREATE TABLE IF NOT EXISTS seventv_ids (username text UNIQUE, twitch_id text UNIQUE, seventv_id text UNIQUE, )" + SQL.exec "CREATE TABLE IF NOT EXISTS chatty_ids (username text UNIQUE, twitch_id text UNIQUE)" # SQL.exec "CREATE TABLE IF NOT EXISTS #{CONFIG.ipTableName} (ip text UNIQUE, count integer DEFAULT 0, date integer)" rescue ex LOGGER.fatal "#{ex.message}" diff --git a/src/vanity-tester-backend.cr b/src/vanity-tester-backend.cr index e396a64..d2a47bb 100644 --- a/src/vanity-tester-backend.cr +++ b/src/vanity-tester-backend.cr @@ -7,7 +7,7 @@ require "http/client" require "./config" require "./handling" require "./logger" -require "./utils" +require "./utils/**" require "./providers/**" CONFIG = Config.load