0.2.2: Fix something that I do not remember. Remove duplicated code.

This commit is contained in:
Fijxu 2024-06-23 01:08:23 -04:00
parent 89b7ae32bc
commit 5ba089cc72
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
9 changed files with 70 additions and 344 deletions

View file

@ -1,5 +1,5 @@
name: ivr-crystal name: ivr-crystal
version: 0.2.0 version: 0.2.2
targets: targets:
ivr-api-crystal: ivr-api-crystal:

View file

@ -18,6 +18,6 @@ class Config
config_file = "config/config.yml" config_file = "config/config.yml"
config_yaml = File.read(config_file) config_yaml = File.read(config_file)
config = Config.from_yaml(config_yaml) config = Config.from_yaml(config_yaml)
return config config
end end
end end

View file

@ -19,39 +19,26 @@ module TwAPI::Datahandlers::Twitch
env.params.query["skipCache"]? env.params.query["skipCache"]?
end end
def isBannedLogin(gql) def isBanned(gql, mode)
if gql["userResultByLogin"]["reason"]? if gql["userResultBy#{mode}"]["reason"]?
return true true
else else
return false false
end end
end end
def isBannedId(gql) def user(env)
if gql["userResultByID"]["reason"]? mode = env.params.query.has_key?("id") ? "ID" : "Login"
return true
else
return false
end
end
def banReason(gql)
if gql["userResultByLogin"] == nil
return false
end
return true
end
def userLogin(env)
if skipCache != "true" if skipCache != "true"
if (json = Utils::Redis.retrieveFromCache("user-login_#{env.params.query["login"].try &.split(',').to_s}")) if json = Utils::Redis.retrieveFromCache("user_#{env.params.query["#{mode.downcase}"].try &.split(',')}")
json = JSON.parse(json) json = JSON.parse(json)
return json.to_json return json.to_json
end end
end end
begin begin
gql = GqlAPI.getUserByLogin(env.params.query) gql = TwitchAPI::GQL.getUser(env.params.query["#{mode.downcase}"], mode)
rescue ex rescue ex
twitchError twitchError
end end
@ -61,98 +48,17 @@ module TwAPI::Datahandlers::Twitch
gql.each do |gql| gql.each do |gql|
gql = gql["data"] gql = gql["data"]
if (gql["user"] == nil) if gql["user"] == nil
# v3 could add some feedback error message instead of an empty array.
nil # Do not add anything to the array. Just like api.ivr.fi does.
else
panels = gql["user"]["panels"]
banned = isBannedLogin(gql)
json.object do
json.field "banned", banned
if banned == true
json.field "banReason", gql["userResultByLogin"]["reason"]
end
json.field "displayName", gql["user"]["displayName"]
json.field "login", gql["user"]["login"]
json.field "id", gql["user"]["id"]
json.field "bio", gql["user"]["description"]
json.field "follows", nil
json.field "followers", gql["user"]["followers"]["totalCount"]
json.field "profileViewCount", nil # Always null
json.field "panelCount", panels.size
json.field "chatColor", gql["user"]["chatColor"]
json.field "logo", gql["user"]["profileImageURL"]
json.field "banner", gql["user"]["bannerImageURL"]
json.field "verifiedBot", nil # Deprecated by twitch
json.field "createdAt", gql["user"]["createdAt"]
json.field "updatedAt", gql["user"]["updatedAt"]
json.field "deletedAt", gql["user"]["deletedAt"]
json.field "emotePrefix", gql["user"]["emoticonPrefix"]["name"]
if gql["user"]["primaryTeam"] != nil
json.field "team", gql["user"]["primaryTeam"]["displayName"]
else
json.field "team", nil
end
if gql["user"]["primaryTeam"] != nil
json.field "contract", gql["user"]["programAgreement"]["type"]
else
json.field "contract", nil
end
json.field "roles", gql["user"]["roles"]
json.field "badges", gql["user"]["displayBadges"]
json.field "chatterCount", gql["user"]["channel"]["chatters"]["count"]
json.field "chatSettings", gql["user"]["chatSettings"]
json.field "stream", gql["user"]["stream"]
json.field "lastBroadcast", gql["user"]["lastBroadcast"]
json.field "panels", panels
end
end
end
end
end
begin
Utils::Redis.saveJson("user-login_#{env.params.query["login"].try &.split(',')}", json_data)
rescue ex
puts ex.message
end
return json_data
end
def userId(env)
if skipCache != "true"
if (json = Utils::Redis.retrieveFromCache("user-id_#{env.params.query["id"].try &.split(',').to_s}"))
json = JSON.parse(json)
return json.to_json
end
end
begin
gql = GqlAPI.getUserById(env.params.query)
rescue ex
twitchError
end
json_data = JSON.build do |json|
json.array do
gql.each do |gql|
pp gql = gql["data"]
pp typeof(gql)
if (gql["user"] == nil)
# v3 could add some feedback error message instead of an empty array. # v3 could add some feedback error message instead of an empty array.
# Do not add anything to the array. Just like api.ivr.fi does. # Do not add anything to the array. Just like api.ivr.fi does.
nil nil
else else
panels = gql["user"]["panels"] banned = isBanned(gql, mode)
banned = isBannedId(gql)
json.object do json.object do
json.field "banned", banned json.field "banned", banned
if banned == true if banned == true
json.field "banReason", gql["userResultByID"]["reason"] json.field "banReason", gql["userResultBy#{mode}"]["reason"]
end end
json.field "displayName", gql["user"]["displayName"] json.field "displayName", gql["user"]["displayName"]
json.field "login", gql["user"]["login"] json.field "login", gql["user"]["login"]
@ -161,7 +67,7 @@ module TwAPI::Datahandlers::Twitch
json.field "follows", nil json.field "follows", nil
json.field "followers", gql["user"]["followers"]["totalCount"] json.field "followers", gql["user"]["followers"]["totalCount"]
json.field "profileViewCount", nil # Always null json.field "profileViewCount", nil # Always null
json.field "panelCount", panels.size json.field "panelCount", gql["user"]["panels"].size
json.field "chatColor", gql["user"]["chatColor"] json.field "chatColor", gql["user"]["chatColor"]
json.field "logo", gql["user"]["profileImageURL"] json.field "logo", gql["user"]["profileImageURL"]
json.field "banner", gql["user"]["bannerImageURL"] json.field "banner", gql["user"]["bannerImageURL"]
@ -170,18 +76,12 @@ module TwAPI::Datahandlers::Twitch
json.field "updatedAt", gql["user"]["updatedAt"] json.field "updatedAt", gql["user"]["updatedAt"]
json.field "deletedAt", gql["user"]["deletedAt"] json.field "deletedAt", gql["user"]["deletedAt"]
json.field "emotePrefix", gql["user"]["emoticonPrefix"]["name"] json.field "emotePrefix", gql["user"]["emoticonPrefix"]["name"]
# failed attempts
# pp "1: #{gql["user"]["primaryTeam"]? || nil}"
# pp "2: #{gql["user"].try &.["primaryTeam"]?.try &.["displayName"]? || nil}"
# json.field "team", gql["user"]["primaryTeam"].try &.["displayName"]?
#
if gql["user"]["primaryTeam"] != nil if gql["user"]["primaryTeam"] != nil
json.field "team", gql["user"]["primaryTeam"]["displayName"] json.field "team", gql["user"]["primaryTeam"]["displayName"]
else else
json.field "team", nil json.field "team", nil
end end
if gql["user"]["primaryTeam"] != nil if gql["user"]["programAgreement"] != nil
json.field "contract", gql["user"]["programAgreement"]["type"] json.field "contract", gql["user"]["programAgreement"]["type"]
else else
json.field "contract", nil json.field "contract", nil
@ -192,7 +92,7 @@ module TwAPI::Datahandlers::Twitch
json.field "chatSettings", gql["user"]["chatSettings"] json.field "chatSettings", gql["user"]["chatSettings"]
json.field "stream", gql["user"]["stream"] json.field "stream", gql["user"]["stream"]
json.field "lastBroadcast", gql["user"]["lastBroadcast"] json.field "lastBroadcast", gql["user"]["lastBroadcast"]
json.field "panels", panels json.field "panels", gql["user"]["panels"]
end end
end end
end end
@ -200,12 +100,12 @@ module TwAPI::Datahandlers::Twitch
end end
begin begin
Utils::Redis.saveJson("user-id_#{env.params.query["id"].try &.split(',')}", json_data) Utils::Redis.saveJson("user_#{env.params.query["#{mode.downcase}"].try &.split(',')}", json_data)
rescue ex rescue ex
puts ex.message puts ex.message
end end
return json_data json_data
end end
def modvip(env) def modvip(env)
@ -219,13 +119,13 @@ module TwAPI::Datahandlers::Twitch
end end
begin begin
gql = GqlAPI.getModsVips(channel) gql = TwitchAPI::GQL.getModsVips(channel)
gql = gql["data"]["user"] gql = gql["data"]["user"]
rescue ex rescue ex
twitchError twitchError
end end
if (gql == nil) if gql == nil
error("Specified channel does no exist") error("Specified channel does no exist")
end end
@ -281,7 +181,7 @@ module TwAPI::Datahandlers::Twitch
end end
Utils::Redis.saveJson("modvip-#{channel}", json_data) Utils::Redis.saveJson("modvip-#{channel}", json_data)
return json_data json_data
end end
# TODO: Add error message if query doesn't exist at all # TODO: Add error message if query doesn't exist at all
@ -297,14 +197,14 @@ module TwAPI::Datahandlers::Twitch
end end
begin begin
gql = GqlAPI.getFounders(login) gql = TwitchAPI::GQL.getFounders(login)
gql = gql["data"]["user"] gql = gql["data"]["user"]
rescue ex rescue ex
twitchError twitchError
end end
# TODO: use begin rescue for this too! # TODO: use begin rescue for this too!
if (gql == nil) if gql == nil
error("Specified channel does no exist") error("Specified channel does no exist")
end end
@ -341,7 +241,7 @@ module TwAPI::Datahandlers::Twitch
end end
Utils::Redis.saveJson("founders-#{login}", json_data) Utils::Redis.saveJson("founders-#{login}", json_data)
return json_data json_data
end end
def clip(env) def clip(env)
@ -355,14 +255,14 @@ module TwAPI::Datahandlers::Twitch
end end
begin begin
gql = GqlAPI.getClips(slug) gql = TwitchAPI::GQL.getClips(slug)
gql = gql["data"]["clip"] gql = gql["data"]["clip"]
rescue ex rescue ex
twitchError twitchError
end end
# TODO: use begin rescue for this too! (again) # TODO: use begin rescue for this too! (again)
if (gql == nil) if gql == nil
error("Slug does not exist") error("Slug does not exist")
end end
@ -434,7 +334,7 @@ module TwAPI::Datahandlers::Twitch
end end
Utils::Redis.saveJson("clip-#{slug}", json_data) Utils::Redis.saveJson("clip-#{slug}", json_data)
return json_data json_data
end end
def subage(env) def subage(env)
@ -449,10 +349,10 @@ module TwAPI::Datahandlers::Twitch
end end
begin begin
gql = GqlAPI.getSubage(user, channel) gql = TwitchAPI::GQL.getSubage(user, channel)
gql = gql["data"] gql = gql["data"]
rescue ex rescue ex
if (gql == nil) if gql == nil
error("Channel does not exist") error("Channel does not exist")
end end
twitchError twitchError
@ -508,7 +408,7 @@ module TwAPI::Datahandlers::Twitch
end end
end end
end end
if (gql["info"]["relationship"]["meta"] == nil) if gql["info"]["relationship"]["meta"] == nil
json.field "meta", nil json.field "meta", nil
else else
json.field "meta" do json.field "meta" do
@ -525,7 +425,7 @@ module TwAPI::Datahandlers::Twitch
end end
Utils::Redis.saveJson("subage-user_#{user}_channel_#{channel}", json_data) Utils::Redis.saveJson("subage-user_#{user}_channel_#{channel}", json_data)
return json_data json_data
end end
def emotes(env) def emotes(env)
@ -539,7 +439,7 @@ module TwAPI::Datahandlers::Twitch
end end
begin begin
gql = GqlAPI.getEmotes(emoteName) gql = TwitchAPI::GQL.getEmotes(emoteName)
gql = gql["data"]["emote"] gql = gql["data"]["emote"]
if gql == nil if gql == nil
raise "Emote does not exist" raise "Emote does not exist"
@ -571,11 +471,11 @@ module TwAPI::Datahandlers::Twitch
end end
Utils::Redis.saveJson("emotes-#{emoteName}", json_data) Utils::Redis.saveJson("emotes-#{emoteName}", json_data)
return json_data json_data
end end
def channelEmotes(env) def channelEmotes(env)
gql = GqlAPI.getChannelEmotes(env.params.query["channel"]) # TODO: Add support for IDs an not just the channel name gql = TwitchAPI::GQL.getChannelEmotes(env.params.query["channel"]) # TODO: Add support for IDs an not just the channel name
json_data = JSON.build do |json| json_data = JSON.build do |json|
json.object do json.object do

2
src/routes/kick.cr Normal file
View file

@ -0,0 +1,2 @@
module TwAPI::Routes::Kick
end

View file

@ -30,21 +30,10 @@ module TwAPI::Routes
def register_all_twitch def register_all_twitch
get "/v2/twitch/user", TwAPI::Routes::Twitch, :user get "/v2/twitch/user", TwAPI::Routes::Twitch, :user
get "/v2/twitch/modvip/", TwAPI::Routes::Twitch, :modvip
get "/v2/twitch/modvip/:channel", TwAPI::Routes::Twitch, :modvip get "/v2/twitch/modvip/:channel", TwAPI::Routes::Twitch, :modvip
get "/v2/twitch/founders", TwAPI::Routes::Twitch, :founders
get "/v2/twitch/founders/:login", TwAPI::Routes::Twitch, :founders get "/v2/twitch/founders/:login", TwAPI::Routes::Twitch, :founders
get "/v2/twitch/clip", TwAPI::Routes::Twitch, :clip
get "/v2/twitch/clip/:slug", TwAPI::Routes::Twitch, :clip get "/v2/twitch/clip/:slug", TwAPI::Routes::Twitch, :clip
get "/v2/twitch/subage", TwAPI::Routes::Twitch, :subage
get "/v2/twitch/subage/:user/:channel", TwAPI::Routes::Twitch, :subage get "/v2/twitch/subage/:user/:channel", TwAPI::Routes::Twitch, :subage
get "/v2/twitch/emotes", TwAPI::Routes::Twitch, :emotes
get "/v2/twitch/emotes/:emote", TwAPI::Routes::Twitch, :emotes get "/v2/twitch/emotes/:emote", TwAPI::Routes::Twitch, :emotes
# get "/v2/twitch/emotes/channel", TwAPI::Routes::Twitch, :channelEmotes
end end
end end

View file

@ -12,19 +12,7 @@ module TwAPI::Routes::Twitch
end end
def user(env) def user(env)
if env.params.query.has_key?("login") handleData(user)
begin
handleData(userLogin)
rescue ex
puts ex.message
end
else
begin
handleData(userId)
rescue ex
puts ex.message
end
end
end end
def modvip(env) def modvip(env)
@ -55,6 +43,6 @@ end
# err = "[]" # err = "[]"
# return err.to_json # return err.to_json
# else # else
# gql = GqlAPI.getChannelEmotes(env.params.query) # gql = TwitchAPI::GQL.getChannelEmotes(env.params.query)
# json_data = JSON.build do |json| # json_data = JSON.build do |json|
# end # end

View file

@ -1,46 +1,19 @@
module TwAPI::GqlAPI module TwAPI::TwitchAPI::GQL
extend self extend self
def userResultBy(params) def getUser(params, mode)
if params.has_key?("id") identifiers = params.try &.split(',')
return %(userResultByID(id: "#{params["id"]}"))
else
return %(userResultByLogin(login: "#{params["login"]}"))
end
end
def targetUser(params)
if params.has_key?("id")
return %(targetUserID: "#{params["id"]}" )
else
return %(targetUserLogin: "#{params["login"]}")
end
end
def loginOrID(params)
if params.has_key?("id")
return %(id: "#{params["id"]}")
else
return %(login: "#{params["login"]}")
end
end
def getUserByLogin(params)
logins = params["login"].try &.split(',')
channel = Channel(JSON::Any).new channel = Channel(JSON::Any).new
responses = [] of JSON::Any responses = [] of JSON::Any
queries = [] of String queries = [] of String
logins.each do |login| identifiers.each do |identifier|
query = %( query = %(
query { query {
userResultByLogin(login: "#{login}") { userResultBy#{mode}(#{mode.downcase}: "#{identifier}") {
... on UserDoesNotExist { ... on UserDoesNotExist { key reason }
key ... on UserError { key } }
reason user(#{mode.downcase}: "#{identifier}" lookupType: ALL) {
}
}
user(login: "#{login}" lookupType: ALL) {
id id
language language
login login
@ -54,16 +27,9 @@ module TwAPI::GqlAPI
updatedAt updatedAt
deletedAt deletedAt
chatColor chatColor
emoticonPrefix { emoticonPrefix { name }
name panels(hideExtensions: false) { id type }
} followers { totalCount }
panels(hideExtensions: false) {
id
type
}
followers {
totalCount
}
roles { roles {
isStaff isStaff
isAffiliate isAffiliate
@ -112,12 +78,8 @@ module TwAPI::GqlAPI
channel { channel {
chatters { chatters {
count count
moderators { moderators { login }
login vips { login }
}
vips {
login
}
} }
} }
} }
@ -137,122 +99,7 @@ module TwAPI::GqlAPI
responses << channel.receive responses << channel.receive
end end
return responses responses
end
def getUserById(params)
ids = params["id"].try &.split(',')
channel = Channel(JSON::Any).new
responses = [] of JSON::Any
queries = [] of String
ids.each do |id|
query = %(
query {
userResultByID(id: "#{id}") {
... on UserDoesNotExist {
key
reason
}
}
user(id: "#{id}" lookupType: ALL) {
id
language
login
displayName
programAgreement { type }
primaryTeam { displayName name }
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
}
}
}
}
}
)
queries << query
end
queries.each do |query|
spawn do
result = JSON.parse(req(query))
channel.send(result)
end
end
queries.each do
responses << channel.receive
end
return responses
end end
def getModsVips(params) def getModsVips(params)
@ -283,7 +130,7 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def getFounders(params) def getFounders(params)
@ -308,7 +155,7 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def getClips(params) def getClips(params)
@ -348,11 +195,11 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def getSubage(user, channel) def getSubage(user, channel)
channelId = HelixAPI.getId(channel) channelId = TwitchAPI::Helix.getId(channel)
query = %( query = %(
query { query {
userData: userResultByLogin (login: "#{user}") { userData: userResultByLogin (login: "#{user}") {
@ -401,7 +248,7 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def getEmotes(emote) def getEmotes(emote)
@ -432,7 +279,7 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def getChannelEmotes(channel) def getChannelEmotes(channel)
@ -466,7 +313,7 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def getEmotes(emote) def getEmotes(emote)
@ -497,7 +344,7 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def getChannelBadges(channel) def getChannelBadges(channel)
@ -524,7 +371,7 @@ module TwAPI::GqlAPI
} }
} }
) )
return JSON.parse(req(query)) JSON.parse(req(query))
end end
def req(query) def req(query)
@ -535,12 +382,12 @@ module TwAPI::GqlAPI
"Client-Id" => "#{CONFIG.gqlClientID}", "Client-Id" => "#{CONFIG.gqlClientID}",
} }
data = {"query" => query} data = {"query" => query}
response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: headers, body: data.to_json) response = HTTP::Client.post("#{CONFIG.gqlEndpoint}", headers: headers, body: data.to_json)
if response.success? if response.success?
return (response.body) (response.body)
else else
raise "GQL Twitch API returned #{response.status_code}: #{response.body.to_s}" raise "GQL Twitch API returned #{response.status_code}: #{response.body}"
end end
end end
end end

View file

@ -1,9 +1,9 @@
module HelixAPI module TwAPI::TwitchAPI::Helix
extend self extend self
def getId(login) def getId(login)
request = "#{CONFIG.apiEndpoint.to_s}/users?login=#{login}" request = "#{CONFIG.apiEndpoint}/users?login=#{login}"
return JSON.parse(req(request))["data"][0]["id"] JSON.parse(req(request))["data"][0]["id"]
end end
def req(request) def req(request)
@ -15,9 +15,9 @@ module HelixAPI
response = HTTP::Client.get(request, headers: headers) response = HTTP::Client.get(request, headers: headers)
if response.success? if response.success?
return (response.body) (response.body)
else else
raise "Helix Twitch API returned #{response.status_code}: #{response.body.to_s}" raise "Helix Twitch API returned #{response.status_code}: #{response.body}"
end end
end end
end end

View file

@ -2,7 +2,7 @@ module TwAPI::Utils::Redis
extend self extend self
def getExpireTime(key) def getExpireTime(key)
return REDIS_DB.ttl(key) REDIS_DB.ttl(key)
end end
def retrieveFromCache(keyName) def retrieveFromCache(keyName)