upd
This commit is contained in:
parent
1c507cf7d6
commit
7bd38e0a24
5 changed files with 261 additions and 87 deletions
108
src/api/users.cr
108
src/api/users.cr
|
@ -1,51 +1,99 @@
|
||||||
require "json"
|
require "json"
|
||||||
require "../twitchapi/*"
|
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
|
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)
|
def self.parseData(params)
|
||||||
helixUsers = JSON.parse(HelixAPI.users(params))
|
# helixUsers = JSON.parse(HelixAPI.users(params))
|
||||||
login = helixUsers["data"][0]["login"]
|
# begin
|
||||||
id = helixUsers["data"][0]["id"]
|
# login = helixUsers["data"][0]["login"]
|
||||||
|
# id = helixUsers["data"][0]["id"]
|
||||||
|
# rescue
|
||||||
|
# login = nil
|
||||||
|
# id = nil
|
||||||
|
# end
|
||||||
|
|
||||||
gqlUser = GqlAPI.user(id.to_s)
|
# if login.nil? && params.has_key?("login")
|
||||||
gqlChannel = GqlAPI.channel(id.to_s)
|
# login = params["login"]
|
||||||
# helixChatColor = JSON.parse(HelixAPI.chatColor(id))
|
# 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 = [
|
json_data = [
|
||||||
{
|
{
|
||||||
"banned" => false,
|
# "banned" => banned, # TODO: FIX BANNED
|
||||||
"displayName" => helixUsers["data"][0]["display_name"],
|
"displayName" => gql["user"]["displayName"],
|
||||||
"login" => helixUsers["data"][0]["login"],
|
"login" => gql["user"]["login"],
|
||||||
"id" => helixUsers["data"][0]["id"],
|
"id" => gql["user"]["id"],
|
||||||
"bio" => helixUsers["data"][0]["description"],
|
"bio" => gql["user"]["description"],
|
||||||
"follows" => nil,
|
"follows" => nil,
|
||||||
"followers" => gqlUser["followers"]["totalCount"],
|
"followers" => gql["user"]["followers"]["totalCount"],
|
||||||
"profileViewCount" => nil, # Always null
|
"profileViewCount" => nil, # Always null
|
||||||
"panelCount" => panels.size,
|
"panelCount" => panels.size,
|
||||||
# "chatColor" => helixChatColor["data"][0]["color"],
|
"chatColor" => gql["user"]["chatColor"],
|
||||||
"logo" => gqlUser["profileImageURL"],
|
"logo" => gql["user"]["profileImageURL"],
|
||||||
"banner" => gqlUser["bannerImageURL"],
|
"banner" => gql["user"]["bannerImageURL"],
|
||||||
"verifiedBot" => nil, # Deprecated by twitch
|
"verifiedBot" => nil, # Deprecated by twitch
|
||||||
"createdAt" => gqlUser["createdAt"],
|
"createdAt" => gql["user"]["createdAt"],
|
||||||
"updatedAt" => gqlUser["updatedAt"],
|
"updatedAt" => gql["user"]["updatedAt"],
|
||||||
"deletedAt" => gqlUser["deletedAt"],
|
"deletedAt" => gql["user"]["deletedAt"],
|
||||||
"emotePrefix" => "lol",
|
"emotePrefix" => gql["user"]["emoticonPrefix"]["name"],
|
||||||
"roles" => {
|
"roles" => {
|
||||||
"isAffiliate" => gqlUser["roles"]["isAffiliate"],
|
"isAffiliate" => gql["user"]["roles"]["isAffiliate"],
|
||||||
"isPartner" => gqlUser["roles"]["isPartner"],
|
"isPartner" => gql["user"]["roles"]["isPartner"],
|
||||||
"isStaff" => gqlUser["roles"]["isStaff"],
|
"isStaff" => gql["user"]["roles"]["isStaff"],
|
||||||
},
|
},
|
||||||
"badges" => gqlUser["displayBadges"],
|
"badges" => gql["user"]["displayBadges"],
|
||||||
"chatterCount" => gqlChannel["chatters"]["count"],
|
"chatterCount" => gql["user"]["channel"]["chatters"]["count"],
|
||||||
"chatSettings" => gqlUser["chatSettings"],
|
"chatSettings" => gql["user"]["chatSettings"],
|
||||||
"stream" => gqlUser["stream"],
|
"stream" => gql["user"]["stream"],
|
||||||
"lastBroadcast" => gqlUser["lastBroadcast"],
|
"lastBroadcast" => gql["user"]["lastBroadcast"],
|
||||||
"panels" => panels,
|
"panels" => panels,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# if json_data[0]["banned"].nil?
|
||||||
|
# puts "test"
|
||||||
|
# json_data[0]["reason"] = gql["userResultByID"]["reason"]
|
||||||
|
# end
|
||||||
|
|
||||||
return json_data.to_json
|
return json_data.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,8 +3,10 @@ require "yaml"
|
||||||
class Config
|
class Config
|
||||||
include YAML::Serializable
|
include YAML::Serializable
|
||||||
|
|
||||||
property oauthToken : String?
|
property helixOAuth : String?
|
||||||
property clientID : String?
|
property helixClientID : String?
|
||||||
|
property gqlOAuth : String?
|
||||||
|
property gqlClientID : String?
|
||||||
property apiEndpoint : String?
|
property apiEndpoint : String?
|
||||||
property gqlEndpoint : String?
|
property gqlEndpoint : String?
|
||||||
|
|
||||||
|
@ -13,13 +15,13 @@ class Config
|
||||||
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.oauthToken.to_s.empty?
|
if config.helixOAuth.to_s.empty?
|
||||||
puts "Config: 'oauthToken' is required/can't be empty"
|
puts "Config: 'helixOAuth' is required/can't be empty"
|
||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
if config.clientID.to_s.empty?
|
if config.helixClientID.to_s.empty?
|
||||||
puts "Config: 'oauthToken' is required/can't be empty"
|
puts "Config: 'helixOAuth' is required/can't be empty"
|
||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@ require "./api/*"
|
||||||
require "./twitchapi/*"
|
require "./twitchapi/*"
|
||||||
|
|
||||||
CONFIG = Config.load
|
CONFIG = Config.load
|
||||||
# puts("oauthToken: #{CONFIG.oauthToken}")
|
# puts("helixOAuth: #{CONFIG.helixOAuth}")
|
||||||
# puts("clientID: #{CONFIG.clientID}")
|
# puts("helixClientID: #{CONFIG.helixClientID}")
|
||||||
|
|
||||||
before_all "/twitch/*" do |env|
|
before_all "/twitch/*" do |env|
|
||||||
env.response.content_type = "application/json"
|
env.response.content_type = "application/json"
|
||||||
|
|
|
@ -2,48 +2,172 @@ require "json"
|
||||||
require "uri"
|
require "uri"
|
||||||
|
|
||||||
module GqlAPI
|
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{
|
def self.userResultBy (params)
|
||||||
"Content-Type" => "application/json",
|
if params.has_key?("id")
|
||||||
# "Client-Id" => "kimne78kx3ncx6brgo4mv6wki5h1ko", # Can cause problems due to Client-Integrity
|
return %(userResultByID(id: "#{params["id"]}"))
|
||||||
"Client-Id" => "ue6666qo983tsx6so1t0vnawi233wa",
|
else
|
||||||
}
|
return %(userResultByLogin(login: "#{params["login"]}"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.user(id : String)
|
# def self.channel (params)
|
||||||
data = { "query" => "{user(id:#{id}){
|
# if params.has_key?("id")
|
||||||
bannerImageURL,
|
# return %(channel(id: "#{params["id"]}"))
|
||||||
profileImageURL(width: 600),
|
# else
|
||||||
createdAt,
|
# return %(channel(name: "#{params["login"]}"))
|
||||||
updatedAt,
|
# end
|
||||||
deletedAt,
|
# end
|
||||||
panels(hideExtensions: false){id,type},
|
|
||||||
follows(first: 1, after: buh, filter: ALL order: ASC){totalCount},
|
def self.user (params)
|
||||||
followers(first: 1, after: buh, order: ASC){totalCount},
|
if params.has_key?("id")
|
||||||
roles{isStaff,isAffiliate,isPartner,isExtensionsDeveloper},
|
return %(user(id: "#{params["id"]}" lookupType: ALL))
|
||||||
displayBadges(){setID, title, description,version},
|
else
|
||||||
chatSettings{blockLinks,chatDelayMs,slowModeDurationSeconds,followersOnlyDurationMinutes,isBroadcasterLanguageModeEnabled,isEmoteOnlyModeEnabled,isFastSubsModeEnabled,isSubscribersOnlyModeEnabled,isUniqueChatModeEnabled,requireVerifiedAccount,rules},
|
return %(user(login: "#{params["login"]}" lookupType: ALL))
|
||||||
stream{averageFPS,bitrate,codec,createdAt,width,height,id,viewersCount,type,game{displayName}},
|
end
|
||||||
lastBroadcast{game{displayName},id,startedAt,title}
|
end
|
||||||
}}" }
|
|
||||||
return JSON.parse(gqlReq(id, data.to_json))["data"]["user"]
|
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
|
end
|
||||||
|
|
||||||
def self.channel(id : String)
|
# def self.isBanned(id : String)
|
||||||
data = { "query" => "{channel(id:#{id}){
|
# data = {"query" => "{userResultByID(id:#{id}){... on UserDoesNotExist{reason}}}"}
|
||||||
chatters{count,moderators{login},vips{login}}
|
# return JSON.parse(gqlReq(id, data.to_json))["data"]["userResultByID"]
|
||||||
}}" }
|
# end
|
||||||
return JSON.parse(gqlReq(id, data.to_json))["data"]["channel"]
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.query(id : String)
|
# def self.channel(id : String)
|
||||||
data = { "query" => "{query(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
|
||||||
|
|
||||||
def self.gqlReq(id : String, data : String)
|
# def self.query(id : String)
|
||||||
response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: data)
|
# 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?
|
if response.success?
|
||||||
return (response.body)
|
return (response.body)
|
||||||
|
@ -54,7 +178,7 @@ module GqlAPI
|
||||||
|
|
||||||
# def self.gqlReq(operation : String, login : String)
|
# def self.gqlReq(operation : String, login : String)
|
||||||
# gqlRequest = gqlOperation2()
|
# gqlRequest = gqlOperation2()
|
||||||
# puts gqlRequest
|
# puts gqlRequest
|
||||||
|
|
||||||
# response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: gqlRequest)
|
# response = HTTP::Client.post(CONFIG.gqlEndpoint.to_s, headers: @@headers, body: gqlRequest)
|
||||||
|
|
||||||
|
@ -65,4 +189,3 @@ module GqlAPI
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
module HelixAPI
|
module HelixAPI
|
||||||
@@headers = HTTP::Headers{
|
@@headers = HTTP::Headers{
|
||||||
"Content-Type" => "application/json",
|
"Content-Type" => "application/json",
|
||||||
"Authorization" => "Bearer #{CONFIG.oauthToken}",
|
"Authorization" => "Bearer #{CONFIG.helixOAuth}",
|
||||||
"Client-Id" => "#{CONFIG.clientID}",
|
"Client-Id" => "#{CONFIG.helixClientID}",
|
||||||
}
|
}
|
||||||
|
|
||||||
def self.users(params)
|
def self.users(params)
|
||||||
|
@ -14,6 +14,7 @@ module HelixAPI
|
||||||
|
|
||||||
response = HTTP::Client.get(endpoint, headers: @@headers)
|
response = HTTP::Client.get(endpoint, headers: @@headers)
|
||||||
|
|
||||||
|
puts response.body
|
||||||
if response.success?
|
if response.success?
|
||||||
return (response.body)
|
return (response.body)
|
||||||
else
|
else
|
||||||
|
@ -21,15 +22,15 @@ module HelixAPI
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.chatColor(id)
|
# def self.chatColor(id)
|
||||||
endpoint = "#{CONFIG.apiEndpoint}/chat/color?user_id=#{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?
|
# if response.success?
|
||||||
return (response.body)
|
# return (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.to_s}"
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue