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

View file

@ -5,37 +5,36 @@ class Config
property helixOAuth : String? property helixOAuth : String?
property helixClientID : String? property helixClientID : String?
property gqlOAuth : String? property gqlOAuth : String?
property gqlClientID : String? property gqlClientID : String?
property apiEndpoint : String? property apiEndpoint : String?
property gqlEndpoint : String? property gqlEndpoint : String?
def self.load def self.load
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)
if config.helixOAuth.to_s.empty? if config.helixOAuth.to_s.empty?
puts "Config: 'helixOAuth' is required/can't be empty" puts "Config: 'helixOAuth' is required/can't be empty"
exit(1) exit(1)
end 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.gqlEndpoint.to_s.empty? if config.helixClientID.to_s.empty?
puts "Config: 'apiEndpoint' is required/can't be empty" puts "Config: 'helixOAuth' is required/can't be empty"
exit(1) exit(1)
end end
return config if config.apiEndpoint.to_s.empty?
end 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 end

View file

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

View file

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