twitch: refactor alert code

This commit is contained in:
Chris Sexton 2022-09-25 17:29:18 -04:00
parent dd262f524e
commit dfa6302757
1 changed files with 90 additions and 74 deletions

View File

@ -39,8 +39,9 @@ type Twitch struct {
} }
type Twitcher struct { type Twitcher struct {
name string name string
gameID string gameID string
isStreaming bool
} }
func (t Twitcher) URL() string { func (t Twitcher) URL() string {
@ -262,6 +263,7 @@ func (p *Twitch) twitchChannelLoop(c bot.Connector, channel string) {
time.Sleep(time.Duration(frequency) * time.Second) time.Sleep(time.Duration(frequency) * time.Second)
for _, twitcherName := range p.c.GetArray("Twitch."+channel+".Users", []string{}) { for _, twitcherName := range p.c.GetArray("Twitch."+channel+".Users", []string{}) {
log.Debug().Msgf("checking %s on twiwch", twitcherName)
twitcherName = strings.ToLower(twitcherName) twitcherName = strings.ToLower(twitcherName)
if err := p.checkTwitch(c, channel, p.twitchList[twitcherName], false); err != nil { if err := p.checkTwitch(c, channel, p.twitchList[twitcherName], false); err != nil {
log.Error().Err(err).Msgf("error in twitch loop") log.Error().Err(err).Msgf("error in twitch loop")
@ -299,6 +301,12 @@ errCase:
return []byte{}, resp.StatusCode, false return []byte{}, resp.StatusCode, false
} }
type twitchInfo struct {
Name string
Game string
URL string
}
func (p *Twitch) checkTwitch(c bot.Connector, channel string, twitcher *Twitcher, alwaysPrintStatus bool) error { func (p *Twitch) checkTwitch(c bot.Connector, channel string, twitcher *Twitcher, alwaysPrintStatus bool) error {
baseURL, err := url.Parse("https://api.twitch.tv/helix/streams") baseURL, err := url.Parse("https://api.twitch.tv/helix/streams")
if err != nil { if err != nil {
@ -333,7 +341,10 @@ func (p *Twitch) checkTwitch(c bot.Connector, channel string, twitcher *Twitcher
games := s.Data games := s.Data
gameID, title := "", "" gameID, title := "", ""
if len(games) > 0 { if len(games) == 0 {
p.twitchList[twitcher.name].isStreaming = false
} else {
p.twitchList[twitcher.name].isStreaming = true
gameID = games[0].GameID gameID = games[0].GameID
if gameID == "" { if gameID == "" {
gameID = "unknown" gameID = "unknown"
@ -341,88 +352,93 @@ func (p *Twitch) checkTwitch(c bot.Connector, channel string, twitcher *Twitcher
title = games[0].Title title = games[0].Title
} }
notStreamingTpl := p.c.Get("Twitch.NotTpl", notStreamingTplFallback) info := twitchInfo{
isStreamingTpl := p.c.Get("Twitch.IsTpl", isStreamingTplFallback)
stoppedStreamingTpl := p.c.Get("Twitch.StoppedTpl", stoppedStreamingTplFallback)
buf := bytes.Buffer{}
info := struct {
Name string
Game string
URL string
}{
twitcher.name, twitcher.name,
title, title,
twitcher.URL(), twitcher.URL(),
} }
// This is a logic mess and needs to be rejiggered. I am not doing that today. (2022-09-18) log.Debug().Interface("info", info).Interface("twitcher", twitcher).Msgf("checking twitch")
if alwaysPrintStatus {
if gameID == "" { if alwaysPrintStatus && twitcher.isStreaming {
t, err := template.New("notStreaming").Parse(notStreamingTpl) p.streaming(c, channel, info)
if err != nil { } else if alwaysPrintStatus && !twitcher.isStreaming {
log.Error().Err(err) p.notStreaming(c, channel, info)
p.b.Send(c, bot.Message, channel, err) } else if twitcher.gameID != "" && !twitcher.isStreaming {
t = template.Must(template.New("notStreaming").Parse(notStreamingTplFallback))
}
t.Execute(&buf, info)
p.b.Send(c, bot.Message, channel, buf.String())
} else {
t, err := template.New("isStreaming").Parse(isStreamingTpl)
if err != nil {
log.Error().Err(err)
p.b.Send(c, bot.Message, channel, err)
t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback))
}
t.Execute(&buf, info)
p.b.Send(c, bot.Message, channel, buf.String())
}
} else if gameID == "" {
if twitcher.gameID != "" {
t, err := template.New("stoppedStreaming").Parse(stoppedStreamingTpl)
if err != nil {
log.Error().Err(err)
p.b.Send(c, bot.Message, channel, err)
t = template.Must(template.New("stoppedStreaming").Parse(stoppedStreamingTplFallback))
}
t.Execute(&buf, info)
p.b.Send(c, bot.Message, channel, buf.String())
log.Debug().Msgf("Disconnecting bridge: %s -> %+v", twitcher.name, p.bridgeMap)
for threadID, ircCh := range p.bridgeMap {
if strings.HasSuffix(ircCh, twitcher.name) {
delete(p.bridgeMap, threadID)
p.b.Send(c, bot.Message, threadID, "Stopped tracking #"+twitcher.name)
}
}
}
twitcher.gameID = "" twitcher.gameID = ""
} else { p.disconnectBridge(c, twitcher)
if twitcher.gameID != gameID { p.stopped(c, channel, info)
t, err := template.New("isStreaming").Parse(isStreamingTpl) twitcher.gameID = ""
if err != nil { } else if twitcher.gameID != gameID && twitcher.isStreaming {
log.Error().Err(err) p.streaming(c, channel, info)
p.b.Send(c, bot.Message, channel, err) p.connectBridge(c, channel, info, twitcher)
t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback))
}
t.Execute(&buf, info)
p.b.Send(c, bot.Message, channel, buf.String())
// start the bridge here
msg := fmt.Sprintf("This post is tracking #%s\n<%s>", twitcher.name, twitcher.URL())
err = p.startBridgeMsg(
fmt.Sprintf("%s-%s-%s", info.Name, info.Game, time.Now().Format("2006-01-02-15:04")),
twitcher.name,
msg,
)
if err != nil {
log.Error().Err(err).Msgf("unable to start bridge")
return err
}
}
twitcher.gameID = gameID twitcher.gameID = gameID
} }
return nil return nil
} }
func (p *Twitch) connectBridge(c bot.Connector, ch string, info twitchInfo, twitcher *Twitcher) {
msg := fmt.Sprintf("This post is tracking #%s\n<%s>", twitcher.name, twitcher.URL())
err := p.startBridgeMsg(
fmt.Sprintf("%s-%s-%s", info.Name, info.Game, time.Now().Format("2006-01-02-15:04")),
twitcher.name,
msg,
)
if err != nil {
log.Error().Err(err).Msgf("unable to start bridge")
p.b.Send(c, bot.Message, ch, fmt.Sprintf("Unable to start bridge: %s", err))
}
}
func (p *Twitch) disconnectBridge(c bot.Connector, twitcher *Twitcher) {
log.Debug().Msgf("Disconnecting bridge: %s -> %+v", twitcher.name, p.bridgeMap)
for threadID, ircCh := range p.bridgeMap {
if strings.HasSuffix(ircCh, twitcher.name) {
delete(p.bridgeMap, threadID)
p.b.Send(c, bot.Message, threadID, "Stopped tracking #"+twitcher.name)
}
}
}
func (p *Twitch) stopped(c bot.Connector, ch string, info twitchInfo) {
notStreamingTpl := p.c.Get("Twitch.StoppedTpl", stoppedStreamingTplFallback)
buf := bytes.Buffer{}
t, err := template.New("notStreaming").Parse(notStreamingTpl)
if err != nil {
log.Error().Err(err)
p.b.Send(c, bot.Message, ch, err)
t = template.Must(template.New("notStreaming").Parse(stoppedStreamingTplFallback))
}
t.Execute(&buf, info)
p.b.Send(c, bot.Message, ch, buf.String())
}
func (p *Twitch) streaming(c bot.Connector, channel string, info twitchInfo) {
isStreamingTpl := p.c.Get("Twitch.IsTpl", isStreamingTplFallback)
buf := bytes.Buffer{}
t, err := template.New("isStreaming").Parse(isStreamingTpl)
if err != nil {
log.Error().Err(err)
p.b.Send(c, bot.Message, channel, err)
t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback))
}
t.Execute(&buf, info)
p.b.Send(c, bot.Message, channel, buf.String())
}
func (p *Twitch) notStreaming(c bot.Connector, ch string, info twitchInfo) {
notStreamingTpl := p.c.Get("Twitch.NotTpl", notStreamingTplFallback)
buf := bytes.Buffer{}
t, err := template.New("notStreaming").Parse(notStreamingTpl)
if err != nil {
log.Error().Err(err)
p.b.Send(c, bot.Message, ch, err)
t = template.Must(template.New("notStreaming").Parse(notStreamingTplFallback))
}
t.Execute(&buf, info)
p.b.Send(c, bot.Message, ch, buf.String())
}
func (p *Twitch) validateCredentials() error { func (p *Twitch) validateCredentials() error {
cid := p.c.Get("twitch.clientid", "") cid := p.c.Get("twitch.clientid", "")
token := p.c.Get("twitch.token", "") token := p.c.Get("twitch.token", "")