Updated TwitcherExtended
Moved MAX_ITERS to class level constant. Added _is_processing_game boolean. Updated all get_*_*() functions to accept both vararg, and arrays. Added get_streamer_data() function to get streamer data. Added get_games() function to get Game/Category data from Twitch. Added get_channel_information() function to get a Streamer Channel data.
This commit is contained in:
parent
9c4371b150
commit
5d69fadcec
1 changed files with 137 additions and 9 deletions
|
|
@ -10,6 +10,7 @@ signal chatbot_token_validated()
|
||||||
|
|
||||||
#region Constants
|
#region Constants
|
||||||
const POLL_TIMEOUT_MS: int = 30000
|
const POLL_TIMEOUT_MS: int = 30000
|
||||||
|
const MAX_ITERS: int = 100
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Static Exports
|
#region Static Exports
|
||||||
|
|
@ -68,6 +69,7 @@ var _cache_users: Dictionary[String, TwitchUser] = {}
|
||||||
var _log: TwitchLogger = TwitchLogger.new("TwitcherExtended")
|
var _log: TwitchLogger = TwitchLogger.new("TwitcherExtended")
|
||||||
var _commands: Dictionary[String, TwitchCommand] = {}
|
var _commands: Dictionary[String, TwitchCommand] = {}
|
||||||
var _is_processing_streams: bool = false
|
var _is_processing_streams: bool = false
|
||||||
|
var _is_processing_games: bool = false
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
enum AuthStatus {
|
enum AuthStatus {
|
||||||
|
|
@ -308,9 +310,15 @@ func reply_message(message: String, msg_id: String, as_streamer: bool = false) -
|
||||||
func get_users_by_id(...user_ids: Array) -> Array[TwitchUser]:
|
func get_users_by_id(...user_ids: Array) -> Array[TwitchUser]:
|
||||||
var tusers: Array[TwitchUser] = []
|
var tusers: Array[TwitchUser] = []
|
||||||
var qusers: Array[String] = []
|
var qusers: Array[String] = []
|
||||||
if user_ids[0] is Array:
|
var nusers: Array[String] = []
|
||||||
user_ids = user_ids[0]
|
|
||||||
for user_id in user_ids:
|
for x in user_ids:
|
||||||
|
if x is Array:
|
||||||
|
nusers.append_array(x)
|
||||||
|
else:
|
||||||
|
nusers.append(x)
|
||||||
|
|
||||||
|
for user_id in nusers:
|
||||||
if _cache_users.has(user_id):
|
if _cache_users.has(user_id):
|
||||||
tusers.append(_cache_users[user_id])
|
tusers.append(_cache_users[user_id])
|
||||||
else:
|
else:
|
||||||
|
|
@ -332,11 +340,18 @@ func get_users_by_id(...user_ids: Array) -> Array[TwitchUser]:
|
||||||
func get_users(...usernames: Array) -> Array[TwitchUser]:
|
func get_users(...usernames: Array) -> Array[TwitchUser]:
|
||||||
var tusers: Array[TwitchUser] = []
|
var tusers: Array[TwitchUser] = []
|
||||||
var qusers: Array[String] = []
|
var qusers: Array[String] = []
|
||||||
|
var nusers: Array[String] = []
|
||||||
|
|
||||||
for i in usernames.size():
|
for x in usernames:
|
||||||
usernames[i] = usernames[i].trim_prefix("@")
|
if x is Array:
|
||||||
|
nusers.append_array(x)
|
||||||
|
else:
|
||||||
|
nusers.append(x)
|
||||||
|
|
||||||
for username in usernames:
|
for i in nusers.size():
|
||||||
|
nusers[i] = nusers[i].trim_prefix("@")
|
||||||
|
|
||||||
|
for username in nusers:
|
||||||
if _cache_users.has(username):
|
if _cache_users.has(username):
|
||||||
tusers.append(_cache_users[username])
|
tusers.append(_cache_users[username])
|
||||||
else:
|
else:
|
||||||
|
|
@ -498,23 +513,30 @@ func get_cheermote(definition: TwitchCheermoteDefinition) -> Dictionary:
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Extended Methods
|
#region Extended Methods
|
||||||
func get_live_streamers_data(user_ids: Array = []) -> Dictionary[String, TwitchStream]:
|
func get_live_streamers_data(...user_ids: Array) -> Dictionary[String, TwitchStream]:
|
||||||
if _is_processing_streams:
|
if _is_processing_streams:
|
||||||
return {}
|
return {}
|
||||||
_is_processing_streams = true
|
_is_processing_streams = true
|
||||||
if user_ids.is_empty():
|
if user_ids.is_empty():
|
||||||
var known := Globals.context.get_known_streamers()
|
var known := Globals.context.get_known_streamers()
|
||||||
user_ids = known.map(func(x: Chatter): return x.twitch_id)
|
user_ids = known.map(func(x: Chatter): return x.twitch_id)
|
||||||
|
else:
|
||||||
|
var nusers: Array[String] = []
|
||||||
|
for x in user_ids:
|
||||||
|
if x is Array:
|
||||||
|
nusers.append_array(x)
|
||||||
|
else:
|
||||||
|
nusers.append(x)
|
||||||
|
user_ids = nusers
|
||||||
|
|
||||||
var streams_data: Dictionary[String, TwitchStream] = {}
|
var streams_data: Dictionary[String, TwitchStream] = {}
|
||||||
var opt := TwitchGetStreams.Opt.new()
|
var opt := TwitchGetStreams.Opt.new()
|
||||||
opt.type = "live"
|
opt.type = "live"
|
||||||
|
|
||||||
var iter: int = 0
|
var iter: int = 0
|
||||||
const MAX_ITER = 100
|
|
||||||
while not user_ids.is_empty():
|
while not user_ids.is_empty():
|
||||||
iter += 1
|
iter += 1
|
||||||
if iter > MAX_ITER:
|
if iter > MAX_ITERS:
|
||||||
_log.e("Reached max iterations while getting live stream data.")
|
_log.e("Reached max iterations while getting live stream data.")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -530,6 +552,47 @@ func get_live_streamers_data(user_ids: Array = []) -> Dictionary[String, TwitchS
|
||||||
_is_processing_streams = false
|
_is_processing_streams = false
|
||||||
return streams_data
|
return streams_data
|
||||||
|
|
||||||
|
func get_streamer_data(...user_ids: Array) -> Dictionary[String, TwitchStream]:
|
||||||
|
var nusers: Array[String] = []
|
||||||
|
if _is_processing_streams:
|
||||||
|
return {}
|
||||||
|
_is_processing_streams = true
|
||||||
|
|
||||||
|
if user_ids.is_empty():
|
||||||
|
var known = Globals.context.get_known_streamers()
|
||||||
|
user_ids = known.map(func(x: Chatter): return x.twitch_id)
|
||||||
|
else:
|
||||||
|
for x in user_ids:
|
||||||
|
if x is Array:
|
||||||
|
nusers.append_array(x)
|
||||||
|
else:
|
||||||
|
nusers.append(x)
|
||||||
|
user_ids = nusers
|
||||||
|
|
||||||
|
var streams_data: Dictionary[String, TwitchStream] = {}
|
||||||
|
var opt := TwitchGetStreams.Opt.new()
|
||||||
|
opt.type = "all"
|
||||||
|
|
||||||
|
var iter: int = 0
|
||||||
|
while not user_ids.is_empty():
|
||||||
|
iter += 1
|
||||||
|
if iter > MAX_ITERS:
|
||||||
|
_log.e("Reached max iterations while getting stream data.")
|
||||||
|
break
|
||||||
|
|
||||||
|
var new_batch: Array[String] = []
|
||||||
|
new_batch.assign(user_ids.slice(0,99))
|
||||||
|
user_ids = user_ids.slice(99)
|
||||||
|
opt.user_id = new_batch
|
||||||
|
var streams_iterator := await api.get_streams(opt)
|
||||||
|
for promise in streams_iterator:
|
||||||
|
var data: TwitchStream = await promise
|
||||||
|
if data:
|
||||||
|
streams_data[data.user_id] = data
|
||||||
|
|
||||||
|
_is_processing_streams = false
|
||||||
|
return streams_data
|
||||||
|
|
||||||
func get_team_info(team_name: String) -> TwitchTeam:
|
func get_team_info(team_name: String) -> TwitchTeam:
|
||||||
var opt := TwitchGetTeams.Opt.new()
|
var opt := TwitchGetTeams.Opt.new()
|
||||||
opt.name = team_name
|
opt.name = team_name
|
||||||
|
|
@ -538,4 +601,69 @@ func get_team_info(team_name: String) -> TwitchTeam:
|
||||||
return team_resp.data[0]
|
return team_resp.data[0]
|
||||||
else:
|
else:
|
||||||
return null
|
return null
|
||||||
|
|
||||||
|
func get_games(...game_ids: Array) -> Dictionary[String, TwitchGame]:
|
||||||
|
var ngames: Array[String] = []
|
||||||
|
if game_ids.is_empty():
|
||||||
|
push_error("Need to provide 1 or more game id's to get the game information.")
|
||||||
|
return {}
|
||||||
|
|
||||||
|
if _is_processing_games:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
for x in game_ids:
|
||||||
|
if x is Array:
|
||||||
|
ngames.append_array(x)
|
||||||
|
else:
|
||||||
|
ngames.append(x)
|
||||||
|
game_ids = ngames
|
||||||
|
|
||||||
|
var games_data: Dictionary[String, TwitchGame] = {}
|
||||||
|
var opt := TwitchGetGames.Opt.new()
|
||||||
|
|
||||||
|
var iter: int = 0
|
||||||
|
while not game_ids.is_empty():
|
||||||
|
iter += 1
|
||||||
|
if iter > MAX_ITERS:
|
||||||
|
_log.e("Reached max iterations while getting game information.")
|
||||||
|
break
|
||||||
|
|
||||||
|
var new_batch: Array[String] = []
|
||||||
|
new_batch.assign(game_ids.slice(0,99))
|
||||||
|
game_ids = game_ids.slice(99)
|
||||||
|
opt.id = new_batch
|
||||||
|
var games := await api.get_games(opt)
|
||||||
|
for gi: TwitchGame in games.data:
|
||||||
|
games_data[gi.id] = gi
|
||||||
|
|
||||||
|
_is_processing_games = false
|
||||||
|
return games_data
|
||||||
|
|
||||||
|
func get_channel_information(...broadcaster_ids: Array) -> Dictionary[String, TwitchChannelInformation]:
|
||||||
|
var nchannels: Array[String] = []
|
||||||
|
|
||||||
|
for x in broadcaster_ids:
|
||||||
|
if x is Array:
|
||||||
|
nchannels.append_array(x)
|
||||||
|
else:
|
||||||
|
nchannels.append(x)
|
||||||
|
broadcaster_ids = nchannels
|
||||||
|
|
||||||
|
var data: Dictionary[String, TwitchChannelInformation] = {}
|
||||||
|
var iter: int = 0
|
||||||
|
while not broadcaster_ids.is_empty():
|
||||||
|
iter += 1
|
||||||
|
if iter > MAX_ITERS:
|
||||||
|
_log.e("Reached max iterations while getting game information.")
|
||||||
|
break
|
||||||
|
|
||||||
|
var new_batch: Array[String] = []
|
||||||
|
new_batch.assign(broadcaster_ids.slice(0,99))
|
||||||
|
broadcaster_ids = broadcaster_ids.slice(99)
|
||||||
|
var resp := await api.get_channel_information(new_batch)
|
||||||
|
if resp != null:
|
||||||
|
for tci: TwitchChannelInformation in resp.data:
|
||||||
|
data[tci.broadcaster_id] = tci
|
||||||
|
|
||||||
|
return data
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue