From 7bd38e0a24e7b834d5afd6a3b93693d41e144ebd Mon Sep 17 00:00:00 2001 From: Fijxu Date: Sat, 18 May 2024 02:40:40 -0400 Subject: [PATCH] upd --- src/api/users.cr | 108 +++++++++++++++------- src/config.cr | 14 +-- src/main.cr | 4 +- src/twitchapi/gql.cr | 199 +++++++++++++++++++++++++++++++++-------- src/twitchapi/helix.cr | 23 ++--- 5 files changed, 261 insertions(+), 87 deletions(-) diff --git a/src/api/users.cr b/src/api/users.cr index d5310b8..7d927e6 100644 --- a/src/api/users.cr +++ b/src/api/users.cr @@ -1,51 +1,99 @@ require "json" require "../twitchapi/*" +# def fetchGQL(id : String) : JSON::Any +# GqlAPI.query(id) +# end + +# def fetchGQLchannel(id : String) : JSON::Any +# GqlAPI.channel(id) +# end + +# def fetchGQLbanned(id : String) : JSON::Any +# GqlAPI.isBanned(id) +# end + module Users + def self.checkBanned(gql) + puts "tst2" + pp gql["userResultByLogin"] + pp gql["userResultByID"] + if gql["userResultByLogin"].nil? + return nil + elsif gql["userResultByID"].nil? + return nil + else + return true + end + end + def self.parseData(params) - helixUsers = JSON.parse(HelixAPI.users(params)) - login = helixUsers["data"][0]["login"] - id = helixUsers["data"][0]["id"] + # helixUsers = JSON.parse(HelixAPI.users(params)) + # begin + # login = helixUsers["data"][0]["login"] + # id = helixUsers["data"][0]["id"] + # rescue + # login = nil + # id = nil + # end - gqlUser = GqlAPI.user(id.to_s) - gqlChannel = GqlAPI.channel(id.to_s) - # helixChatColor = JSON.parse(HelixAPI.chatColor(id)) + # if login.nil? && params.has_key?("login") + # login = params["login"] + # elsif id.nil? && params.has_key?("id") + # id = params["id"] + # end - panels = gqlUser["panels"] + # gqlUser = GqlAPI.user(id.to_s) + # gqlChannel = GqlAPI.channel(id.to_s) + + # a = Channel(JSON::Any).new + # spawn do + # a.send fetchGQL(id.to_s) + # end + + gql = GqlAPI.query(params) + panels = gql["user"]["panels"] + banned = checkBanned(gql) + pp gql json_data = [ { - "banned" => false, - "displayName" => helixUsers["data"][0]["display_name"], - "login" => helixUsers["data"][0]["login"], - "id" => helixUsers["data"][0]["id"], - "bio" => helixUsers["data"][0]["description"], - "follows" => nil, - "followers" => gqlUser["followers"]["totalCount"], + # "banned" => banned, # TODO: FIX BANNED + "displayName" => gql["user"]["displayName"], + "login" => gql["user"]["login"], + "id" => gql["user"]["id"], + "bio" => gql["user"]["description"], + "follows" => nil, + "followers" => gql["user"]["followers"]["totalCount"], "profileViewCount" => nil, # Always null "panelCount" => panels.size, - # "chatColor" => helixChatColor["data"][0]["color"], - "logo" => gqlUser["profileImageURL"], - "banner" => gqlUser["bannerImageURL"], + "chatColor" => gql["user"]["chatColor"], + "logo" => gql["user"]["profileImageURL"], + "banner" => gql["user"]["bannerImageURL"], "verifiedBot" => nil, # Deprecated by twitch - "createdAt" => gqlUser["createdAt"], - "updatedAt" => gqlUser["updatedAt"], - "deletedAt" => gqlUser["deletedAt"], - "emotePrefix" => "lol", + "createdAt" => gql["user"]["createdAt"], + "updatedAt" => gql["user"]["updatedAt"], + "deletedAt" => gql["user"]["deletedAt"], + "emotePrefix" => gql["user"]["emoticonPrefix"]["name"], "roles" => { - "isAffiliate" => gqlUser["roles"]["isAffiliate"], - "isPartner" => gqlUser["roles"]["isPartner"], - "isStaff" => gqlUser["roles"]["isStaff"], + "isAffiliate" => gql["user"]["roles"]["isAffiliate"], + "isPartner" => gql["user"]["roles"]["isPartner"], + "isStaff" => gql["user"]["roles"]["isStaff"], }, - "badges" => gqlUser["displayBadges"], - "chatterCount" => gqlChannel["chatters"]["count"], - "chatSettings" => gqlUser["chatSettings"], - "stream" => gqlUser["stream"], - "lastBroadcast" => gqlUser["lastBroadcast"], - "panels" => panels, + "badges" => gql["user"]["displayBadges"], + "chatterCount" => gql["user"]["channel"]["chatters"]["count"], + "chatSettings" => gql["user"]["chatSettings"], + "stream" => gql["user"]["stream"], + "lastBroadcast" => gql["user"]["lastBroadcast"], + "panels" => panels, }, ] + # if json_data[0]["banned"].nil? + # puts "test" + # json_data[0]["reason"] = gql["userResultByID"]["reason"] + # end + return json_data.to_json end end diff --git a/src/config.cr b/src/config.cr index e174dcc..51ed439 100644 --- a/src/config.cr +++ b/src/config.cr @@ -3,8 +3,10 @@ require "yaml" class Config include YAML::Serializable - property oauthToken : String? - property clientID : String? + property helixOAuth : String? + property helixClientID : String? + property gqlOAuth : String? + property gqlClientID : String? property apiEndpoint : String? property gqlEndpoint : String? @@ -13,13 +15,13 @@ class Config config_yaml = File.read(config_file) config = Config.from_yaml(config_yaml) - if config.oauthToken.to_s.empty? - puts "Config: 'oauthToken' is required/can't be empty" + if config.helixOAuth.to_s.empty? + puts "Config: 'helixOAuth' is required/can't be empty" exit(1) end - if config.clientID.to_s.empty? - puts "Config: 'oauthToken' is required/can't be empty" + if config.helixClientID.to_s.empty? + puts "Config: 'helixOAuth' is required/can't be empty" exit(1) end diff --git a/src/main.cr b/src/main.cr index df94cd5..2d319b0 100644 --- a/src/main.cr +++ b/src/main.cr @@ -7,8 +7,8 @@ require "./api/*" require "./twitchapi/*" CONFIG = Config.load -# puts("oauthToken: #{CONFIG.oauthToken}") -# puts("clientID: #{CONFIG.clientID}") +# puts("helixOAuth: #{CONFIG.helixOAuth}") +# puts("helixClientID: #{CONFIG.helixClientID}") before_all "/twitch/*" do |env| env.response.content_type = "application/json" diff --git a/src/twitchapi/gql.cr b/src/twitchapi/gql.cr index da621f9..2cbe443 100644 --- a/src/twitchapi/gql.cr +++ b/src/twitchapi/gql.cr @@ -2,48 +2,172 @@ require "json" require "uri" module GqlAPI + @@headers = HTTP::Headers{ + "Content-Type" => "application/json", + # "Client-Id" => "kimne78kx3ncx6brgo4mv6wki5h1ko", # Can cause problems due to Client-Integrity + "Authorization" => "OAuth #{CONFIG.gqlOAuth}", + "Client-Id" => "#{CONFIG.gqlClientID}", + } - @@headers = HTTP::Headers{ - "Content-Type" => "application/json", - # "Client-Id" => "kimne78kx3ncx6brgo4mv6wki5h1ko", # Can cause problems due to Client-Integrity - "Client-Id" => "ue6666qo983tsx6so1t0vnawi233wa", - } + def self.userResultBy (params) + if params.has_key?("id") + return %(userResultByID(id: "#{params["id"]}")) + else + return %(userResultByLogin(login: "#{params["login"]}")) + end + end - def self.user(id : String) - data = { "query" => "{user(id:#{id}){ - bannerImageURL, - profileImageURL(width: 600), - createdAt, - updatedAt, - deletedAt, - panels(hideExtensions: false){id,type}, - follows(first: 1, after: buh, filter: ALL order: ASC){totalCount}, - followers(first: 1, after: buh, order: ASC){totalCount}, - roles{isStaff,isAffiliate,isPartner,isExtensionsDeveloper}, - displayBadges(){setID, title, description,version}, - chatSettings{blockLinks,chatDelayMs,slowModeDurationSeconds,followersOnlyDurationMinutes,isBroadcasterLanguageModeEnabled,isEmoteOnlyModeEnabled,isFastSubsModeEnabled,isSubscribersOnlyModeEnabled,isUniqueChatModeEnabled,requireVerifiedAccount,rules}, - stream{averageFPS,bitrate,codec,createdAt,width,height,id,viewersCount,type,game{displayName}}, - lastBroadcast{game{displayName},id,startedAt,title} - }}" } - return JSON.parse(gqlReq(id, data.to_json))["data"]["user"] + # def self.channel (params) + # if params.has_key?("id") + # return %(channel(id: "#{params["id"]}")) + # else + # return %(channel(name: "#{params["login"]}")) + # end + # end + + def self.user (params) + if params.has_key?("id") + return %(user(id: "#{params["id"]}" lookupType: ALL)) + else + return %(user(login: "#{params["login"]}" lookupType: ALL)) + end + end + + def self.query(params) + puts params + # id = params["id"] + + # data = { "query" => "{user(id:#{id}){ + # bannerImageURL, + # profileImageURL(width: 600), + # createdAt, + # updatedAt, + # deletedAt, + # chatColor, + # emoticonPrefix{name}, + # panels(hideExtensions: false){id,type}, + # follows(first: 1, after: buh, filter: ALL order: ASC){totalCount}, + # followers(first: 1, after: buh, order: ASC){totalCount}, + # roles{isStaff,isAffiliate,isPartner,isExtensionsDeveloper}, + # displayBadges(){setID, title, description,version}, + # chatSettings{blockLinks,chatDelayMs,slowModeDurationSeconds,followersOnlyDurationMinutes,isBroadcasterLanguageModeEnabled,isEmoteOnlyModeEnabled,isFastSubsModeEnabled,isSubscribersOnlyModeEnabled,isUniqueChatModeEnabled,requireVerifiedAccount,rules}, + # stream{averageFPS,bitrate,codec,createdAt,width,height,id,viewersCount,type,game{displayName}}, + # lastBroadcast{game{displayName},id,startedAt,title} + # }}" } + query = %( + query { + #{userResultBy(params)} { + ... on UserDoesNotExist { + key + reason + } + } + #{user(params)} { + id + language + login + displayName + description + bannerImageURL + profileImageURL(width: 600) + createdAt + updatedAt + deletedAt + chatColor + emoticonPrefix { + name + } + panels(hideExtensions: false) { + id + type + } + followers { + totalCount + } + roles { + isStaff + isAffiliate + isPartner + isExtensionsDeveloper + } + displayBadges { + setID + title + description + version + } + chatSettings { + autoModLevel + blockLinks + chatDelayMs + followersOnlyDurationMinutes + isBroadcasterLanguageModeEnabled + isEmoteOnlyModeEnabled + isFastSubsModeEnabled + isOptedOutOfGlobalBannedWordsList + isSubscribersOnlyModeEnabled + isUniqueChatModeEnabled + requireVerifiedAccount + rules + slowModeDurationSeconds + } + stream { + averageFPS + bitrate + codec + createdAt + width + height + id + viewersCount + type + game{displayName} + } + lastBroadcast { + game{displayName} + id + startedAt + title + } + channel { + chatters { + count + moderators { + login + } + vips { + login + } + } + } + } + } + ) + return JSON.parse(gqlReq(params, query))["data"] end - def self.channel(id : String) - data = { "query" => "{channel(id:#{id}){ - chatters{count,moderators{login},vips{login}} - }}" } - return JSON.parse(gqlReq(id, data.to_json))["data"]["channel"] - end + # def self.isBanned(id : String) + # data = {"query" => "{userResultByID(id:#{id}){... on UserDoesNotExist{reason}}}"} + # return JSON.parse(gqlReq(id, data.to_json))["data"]["userResultByID"] + # end - def self.query(id : String) - data = { "query" => "{query(id:#{id}){ - chatters{count,moderators{login},vips{login}} - }}" } - return JSON.parse(gqlReq(id, data.to_json))["data"]["channel"] - end + # def self.channel(id : String) + # data = {"query" => "{channel(id:#{id}){ + # chatters{count,moderators{login},vips{login}} + # }}"} + # return JSON.parse(gqlReq(id, data.to_json))["data"]["channel"] + # end - def self.gqlReq(id : String, data : String) - response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: data) + # def self.query(id : String) + # data = { "query" => "{query(id:#{id}){ + # chatters{count,moderators{login},vips{login}} + # }}" } + # return JSON.parse(gqlReq(id, data.to_json))["data"]["channel"] + # end + + def self.gqlReq(params, query) + data = {"query" => query} + response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: data.to_json) if response.success? return (response.body) @@ -54,7 +178,7 @@ module GqlAPI # def self.gqlReq(operation : String, login : String) # gqlRequest = gqlOperation2() - # puts gqlRequest + # puts gqlRequest # response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: gqlRequest) @@ -65,4 +189,3 @@ module GqlAPI # end # end end - diff --git a/src/twitchapi/helix.cr b/src/twitchapi/helix.cr index d1977bf..fa07e7e 100644 --- a/src/twitchapi/helix.cr +++ b/src/twitchapi/helix.cr @@ -1,8 +1,8 @@ module HelixAPI @@headers = HTTP::Headers{ "Content-Type" => "application/json", - "Authorization" => "Bearer #{CONFIG.oauthToken}", - "Client-Id" => "#{CONFIG.clientID}", + "Authorization" => "Bearer #{CONFIG.helixOAuth}", + "Client-Id" => "#{CONFIG.helixClientID}", } def self.users(params) @@ -14,6 +14,7 @@ module HelixAPI response = HTTP::Client.get(endpoint, headers: @@headers) + puts response.body if response.success? return (response.body) else @@ -21,15 +22,15 @@ module HelixAPI end end - def self.chatColor(id) - endpoint = "#{CONFIG.apiEndpoint}/chat/color?user_id=#{id}" + # def self.chatColor(id) + # endpoint = "#{CONFIG.apiEndpoint}/chat/color?user_id=#{id}" - response = HTTP::Client.get(endpoint, headers: @@headers) + # response = HTTP::Client.get(endpoint, headers: @@headers) - if response.success? - return (response.body) - else - raise "Helix Twitch API returned #{response.status_code}: #{response.body.to_s}" - end - end + # if response.success? + # return (response.body) + # else + # raise "Helix Twitch API returned #{response.status_code}: #{response.body.to_s}" + # end + # end end