Spaghetti code do not look

This commit is contained in:
Fijxu 2024-05-18 22:29:36 -04:00
parent 7bd38e0a24
commit 00640f3ab6
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
4 changed files with 154 additions and 101 deletions

View file

@ -15,15 +15,45 @@ require "../twitchapi/*"
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
# puts "checkBanned: #{gql}"
# puts (typeof(gql))
# if gql["userResultByLogin"]
# return false
# if gql["userResultByLogin"]["result"]?
# return true
# else
# return false
# end
# elsif gql["userResultByID"]?
# return false
# if gql["userResultByID"]["reason"]?
# return true
# else
# return false
# end
# else
# return true
# end
# if gql["userResultByLogin"]["reason"]?
# return true
# elsif gql["userResultByID"]["reason"]?
# return true
# else
# return false
# end
# return false
if GqlAPI.urb().includes? "login"
if gql["userResultByLogin"]["reason"]?
return true
else
return false
end
elsif GqlAPI.urb().includes? "id"
if gql["userResultByID"]["reason"]?
return true
else
return false
end
end
end
@ -33,12 +63,12 @@ module Users
# login = helixUsers["data"][0]["login"]
# id = helixUsers["data"][0]["id"]
# rescue
# login = nil
# id = nil
# login = nil
# id = nil
# end
# if login.nil? && params.has_key?("login")
# login = params["login"]
# login = params["login"]
# elsif id.nil? && params.has_key?("id")
# id = params["id"]
# end
@ -48,17 +78,20 @@ module Users
# a = Channel(JSON::Any).new
# spawn do
# a.send fetchGQL(id.to_s)
# a.send fetchGQL(id.to_s)
# end
gql = GqlAPI.query(params)
panels = gql["user"]["panels"]
banned = checkBanned(gql)
pp gql
banned = checkBanned(gql)
# puts banned
# puts ("urb #{GqlAPI.urb()}")
# pp gql
json_data = [
{
# "banned" => banned, # TODO: FIX BANNED
"banned" => banned,
"reason" => nil,
"displayName" => gql["user"]["displayName"],
"login" => gql["user"]["login"],
"id" => gql["user"]["id"],
@ -76,23 +109,23 @@ module Users
"deletedAt" => gql["user"]["deletedAt"],
"emotePrefix" => gql["user"]["emoticonPrefix"]["name"],
"roles" => {
"isAffiliate" => gql["user"]["roles"]["isAffiliate"],
"isPartner" => gql["user"]["roles"]["isPartner"],
"isStaff" => gql["user"]["roles"]["isStaff"],
"isAffiliate" => gql["user"]["roles"]["isAffiliate"],
"isPartner" => gql["user"]["roles"]["isPartner"],
"isStaff" => gql["user"]["roles"]["isStaff"],
},
"badges" => gql["user"]["displayBadges"],
"chatterCount" => gql["user"]["channel"]["chatters"]["count"],
"chatSettings" => gql["user"]["chatSettings"],
"stream" => gql["user"]["stream"],
"lastBroadcast" => gql["user"]["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
# if json_data[0]["banned"].nil?
# puts "test"
# json_data[0]["reason"] = gql["userResultByID"]["reason"]
# end
return json_data.to_json
end

View file

@ -5,37 +5,36 @@ class Config
property helixOAuth : String?
property helixClientID : String?
property gqlOAuth : String?
property gqlOAuth : String?
property gqlClientID : String?
property apiEndpoint : String?
property gqlEndpoint : String?
property apiEndpoint : String?
property gqlEndpoint : String?
def self.load
config_file = "config/config.yml"
config_yaml = File.read(config_file)
config = Config.from_yaml(config_yaml)
def self.load
config_file = "config/config.yml"
config_yaml = File.read(config_file)
config = Config.from_yaml(config_yaml)
if config.helixOAuth.to_s.empty?
puts "Config: 'helixOAuth' is required/can't be empty"
exit(1)
end
if config.helixClientID.to_s.empty?
puts "Config: 'helixOAuth' is required/can't be empty"
exit(1)
end
if config.apiEndpoint.to_s.empty?
puts "Config: 'apiEndpoint' is required/can't be empty"
exit(1)
end
if config.helixOAuth.to_s.empty?
puts "Config: 'helixOAuth' is required/can't be empty"
exit(1)
end
if config.gqlEndpoint.to_s.empty?
puts "Config: 'apiEndpoint' is required/can't be empty"
exit(1)
end
if config.helixClientID.to_s.empty?
puts "Config: 'helixOAuth' is required/can't be empty"
exit(1)
end
return config
end
if config.apiEndpoint.to_s.empty?
puts "Config: 'apiEndpoint' is required/can't be empty"
exit(1)
end
if config.gqlEndpoint.to_s.empty?
puts "Config: 'apiEndpoint' is required/can't be empty"
exit(1)
end
return config
end
end

View file

@ -18,32 +18,32 @@ get "/twitch/user" do |env|
query = env.request.query
if query
params = URI::Params.parse(query)
if params.has_key?("login")
begin
Users.parseData(params)
rescue ex
env.response.status_code = 401
err = { "error" => "#{ex.message}" }
err.to_json
end
elsif params.has_key?("id")
begin
Users.parseData(params)
rescue ex
env.response.status_code = 401
err = { "error" => "#{ex.message}" }
err.to_json
end
else
env.response.status_code = 401
err = { "error" => "Parameter 'login' or 'id' is missing" }
err.to_json
end
else
env.response.status_code = 401
err = { "error" => "No query parameters found" }
err.to_json
end
if params.has_key?("login")
begin
Users.parseData(params)
rescue ex
env.response.status_code = 401
err = {"error" => "#{ex.message}"}
err.to_json
end
elsif params.has_key?("id")
begin
Users.parseData(params)
rescue ex
env.response.status_code = 401
err = {"error" => "#{ex.message}"}
err.to_json
end
else
env.response.status_code = 401
err = {"error" => "Parameter 'login' or 'id' is missing"}
err.to_json
end
else
env.response.status_code = 401
err = {"error" => "No query parameters found"}
err.to_json
end
end
Kemal.run

View file

@ -8,34 +8,47 @@ module GqlAPI
"Authorization" => "OAuth #{CONFIG.gqlOAuth}",
"Client-Id" => "#{CONFIG.gqlClientID}",
}
@@urb : String = "fuck"
def self.userResultBy (params)
if params.has_key?("id")
def self.urb
return @@urb
end
def self.userResultBy(params)
if params.has_key?("id")
return %(userResultByID(id: "#{params["id"]}"))
else
return %(userResultByLogin(login: "#{params["login"]}"))
return %(userResultByLogin(login: "#{params["login"]}"))
end
end
end
# def self.channel (params)
# if params.has_key?("id")
def self.loginOrID(params)
if params.has_key?("id")
return %(userResultByID(id: "#{params["id"]}")), %(user(id: "#{params["id"]}" lookupType: ALL))
else
return %(userResultByLogin(login: "#{params["login"]}")), %(user(login: "#{params["login"]}" lookupType: ALL))
end
end
# def self.channel (params)
# if params.has_key?("id")
# return %(channel(id: "#{params["id"]}"))
# else
# return %(channel(name: "#{params["login"]}"))
# return %(channel(name: "#{params["login"]}"))
# end
# end
# end
def self.user (params)
if params.has_key?("id")
def self.user(params)
if params.has_key?("id")
return %(user(id: "#{params["id"]}" lookupType: ALL))
else
return %(user(login: "#{params["login"]}" lookupType: ALL))
return %(user(login: "#{params["login"]}" lookupType: ALL))
end
end
end
def self.query(params)
puts params
# id = params["id"]
# puts params
# id = params["id"]
# data = { "query" => "{user(id:#{id}){
# bannerImageURL,
@ -54,9 +67,10 @@ module GqlAPI
# stream{averageFPS,bitrate,codec,createdAt,width,height,id,viewersCount,type,game{displayName}},
# lastBroadcast{game{displayName},id,startedAt,title}
# }}" }
@@urb = userResultBy(params)
query = %(
query {
#{userResultBy(params)} {
#{@@urb} {
... on UserDoesNotExist {
key
reason
@ -143,7 +157,12 @@ module GqlAPI
}
}
)
return JSON.parse(gqlReq(params, query))["data"]
# puts JSON.parse(gqlReqq)["data"]
# puts ("gqlReq JSON.parse typeof: #{typeof (gqlReqq)}")
# puts ("gqlReq JSON.parse typeof: #{typeof(JSON.parse(gqlReq(params, query))["data"])}")
# puts ("gqlReq JSON.parse.to-H typeof: #{typeof (gqlReqq.to_h)}")
# puts ((JSON.parse(gqlReq(params, query)))["data"])["userResultByID"]["reason"]?
return (JSON.parse(gqlReq(params, query)))["data"]
end
# def self.isBanned(id : String)
@ -153,8 +172,8 @@ module GqlAPI
# def self.channel(id : String)
# data = {"query" => "{channel(id:#{id}){
# chatters{count,moderators{login},vips{login}}
# }}"}
# chatters{count,moderators{login},vips{login}}
# }}"}
# return JSON.parse(gqlReq(id, data.to_json))["data"]["channel"]
# end
@ -167,9 +186,11 @@ module GqlAPI
def self.gqlReq(params, query)
data = {"query" => query}
# puts (typeof(data))
response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: data.to_json)
if response.success?
# puts (typeof(response.body))
return (response.body)
else
raise "GQL Twitch API returned #{response.status_code}: #{response.body.to_s}"