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
|
||||
const POLL_TIMEOUT_MS: int = 30000
|
||||
const MAX_ITERS: int = 100
|
||||
#endregion
|
||||
|
||||
#region Static Exports
|
||||
|
|
@ -68,6 +69,7 @@ var _cache_users: Dictionary[String, TwitchUser] = {}
|
|||
var _log: TwitchLogger = TwitchLogger.new("TwitcherExtended")
|
||||
var _commands: Dictionary[String, TwitchCommand] = {}
|
||||
var _is_processing_streams: bool = false
|
||||
var _is_processing_games: bool = false
|
||||
#endregion
|
||||
|
||||
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]:
|
||||
var tusers: Array[TwitchUser] = []
|
||||
var qusers: Array[String] = []
|
||||
if user_ids[0] is Array:
|
||||
user_ids = user_ids[0]
|
||||
for user_id in user_ids:
|
||||
var nusers: Array[String] = []
|
||||
|
||||
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):
|
||||
tusers.append(_cache_users[user_id])
|
||||
else:
|
||||
|
|
@ -332,11 +340,18 @@ func get_users_by_id(...user_ids: Array) -> Array[TwitchUser]:
|
|||
func get_users(...usernames: Array) -> Array[TwitchUser]:
|
||||
var tusers: Array[TwitchUser] = []
|
||||
var qusers: Array[String] = []
|
||||
var nusers: Array[String] = []
|
||||
|
||||
for i in usernames.size():
|
||||
usernames[i] = usernames[i].trim_prefix("@")
|
||||
for x in usernames:
|
||||
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):
|
||||
tusers.append(_cache_users[username])
|
||||
else:
|
||||
|
|
@ -498,23 +513,30 @@ func get_cheermote(definition: TwitchCheermoteDefinition) -> Dictionary:
|
|||
#endregion
|
||||
|
||||
#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:
|
||||
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:
|
||||
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 opt := TwitchGetStreams.Opt.new()
|
||||
opt.type = "live"
|
||||
|
||||
var iter: int = 0
|
||||
const MAX_ITER = 100
|
||||
while not user_ids.is_empty():
|
||||
iter += 1
|
||||
if iter > MAX_ITER:
|
||||
if iter > MAX_ITERS:
|
||||
_log.e("Reached max iterations while getting live stream data.")
|
||||
break
|
||||
|
||||
|
|
@ -530,6 +552,47 @@ func get_live_streamers_data(user_ids: Array = []) -> Dictionary[String, TwitchS
|
|||
_is_processing_streams = false
|
||||
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:
|
||||
var opt := TwitchGetTeams.Opt.new()
|
||||
opt.name = team_name
|
||||
|
|
@ -538,4 +601,69 @@ func get_team_info(team_name: String) -> TwitchTeam:
|
|||
return team_resp.data[0]
|
||||
else:
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue