;$TWITCHAPIURL
alias -l TWITCHAPIURL return https://api.twitch.tv/kraken/
;/loadTwitch [stream]
alias loadTwitch {
if (!$hget(twitch)) {
hmake twitch 10
}
if ($1) {
channelFollows $1
streamInfo $1
channelViewers $1
if (!$hget(twitch,$+($1,_stream))) {
channelInfo $1
}
hadd twitch $+($1,_latestFollower) $hget(twitch,$+($1,_follower_0_user))
}
}
;/channelInfo stream
alias channelInfo {
jsonopen -du twitch $+($TWITCHAPIURL,channels/,$1)
if (!$json(twitch).error) {
hadd twitch $+($1,_mature) $json(twitch,mature)
hadd twitch $+($1,_views) $json(twitch,views)
hadd twitch $+($1,_followers) $json(twitch,followers)
}
jsonclose twitch
}
;channelFollows stream
alias channelFollows {
jsonopen -du twitch $+($TWITCHAPIURL,channels/,$1,/follows?limit=3)
if (!$json(twitch).error) {
var %f = 0
while ($json(twitch,follows,%f,created_at)) {
hadd twitch $+($1,_follower_,%f,_date) $replace($remove($json(twitch,follows,%f,created_at),Z),T,$chr(32))
hadd twitch $+($1,_follower_,%f,_user) $json(twitch,follows,%f,user,name)
inc %f
}
}
jsonclose twitch
}
;streamInfo stream
alias streamInfo {
jsonopen -du twitch $+($TWITCHAPIURL,streams/,$1)
if (!$json(twitch).error) {
if (!$json(twitch,stream)) {
hadd twitch $+($1,_stream) 0
}
else {
hadd twitch $+($1,_stream) 1
hadd twitch $+($1,_game) $json(twitch,stream,game)
hadd twitch $+($1,_viewers) $json(twitch,stream,viewers)
hadd twitch $+($1,_mature) $json(twitch,stream,channel,mature)
hadd twitch $+($1,_status) $json(twitch,stream,channel,status)
hadd twitch $+($1,_views) $json(twitch,stream,channel,views)
hadd twitch $+($1,_followers) $json(twitch,stream,channel,followers)
}
}
jsonclose twitch
}
;$newFollower(stream)
alias newFollower {
var %a = $null
channelFollows $1
if ($hget(twitch,$+($1,_follower_0_user)) != $hget(twitch,$+($1,_latestFollower))) {
%a = $v1
hadd twitch $+($1,_latestFollower) %a
}
return %a
}
;/channelViewers stream
alias channelViewers {
jsonopen -du twitch $+(https://tmi.twitch.tv/group/user/,$1,/chatters)
if (!$json(twitch).error) {
hdel -w twitch $+($1,_viewer_*)
hadd twitch $+($1,_chatters) $json(twitch,chatter_count)
var %types = moderators,staff,admins,global_mods,viewers
var %t = 1
while ($gettok(%types,%t,44)) {
var %type = $v1
hdel -w twitch $+($1,_,%type,_*)
var %v = 0
while ($json(twitch,chatters,%type,%v)) {
var %viewer = $v1
if (%viewer != $me) && (%viewer != $1) {
hadd twitch $+($1,_,%type,_,%viewer) 1
hadd twitch $+($1,_viewer_,%viewer) 1
}
inc %v
}
inc %t
}
}
jsonclose twitch
}