2021-09-02 15:09:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-09-02 15:09:30 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
twitchChannelState struct {
|
|
|
|
Category string
|
|
|
|
IsLive bool
|
|
|
|
Title string
|
|
|
|
}
|
|
|
|
|
|
|
|
twitchWatcher struct {
|
|
|
|
ChannelStatus map[string]*twitchChannelState
|
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t twitchChannelState) Equals(c twitchChannelState) bool {
|
|
|
|
return t.Category == c.Category &&
|
|
|
|
t.IsLive == c.IsLive &&
|
|
|
|
t.Title == c.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTwitchWatcher() *twitchWatcher {
|
|
|
|
return &twitchWatcher{
|
|
|
|
ChannelStatus: make(map[string]*twitchChannelState),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *twitchWatcher) AddChannel(channel string) error {
|
|
|
|
r.lock.RLock()
|
|
|
|
if _, ok := r.ChannelStatus[channel]; ok {
|
|
|
|
r.lock.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.lock.RUnlock()
|
|
|
|
|
|
|
|
return r.updateChannelFromAPI(channel, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *twitchWatcher) Check() {
|
|
|
|
var channels []string
|
|
|
|
r.lock.RLock()
|
|
|
|
for c := range r.ChannelStatus {
|
|
|
|
channels = append(channels, c)
|
|
|
|
}
|
|
|
|
r.lock.RUnlock()
|
|
|
|
|
|
|
|
for _, ch := range channels {
|
|
|
|
if err := r.updateChannelFromAPI(ch, true); err != nil {
|
|
|
|
log.WithError(err).WithField("channel", ch).Error("Unable to update channel status")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *twitchWatcher) RemoveChannel(channel string) error {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
|
|
|
delete(r.ChannelStatus, channel)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *twitchWatcher) updateChannelFromAPI(channel string, sendUpdate bool) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
status twitchChannelState
|
|
|
|
)
|
|
|
|
|
|
|
|
status.IsLive, err = twitchClient.HasLiveStream(channel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting live status")
|
|
|
|
}
|
|
|
|
|
|
|
|
status.Category, status.Title, err = twitchClient.GetRecentStreamInfo(channel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting stream info")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
|
|
|
if r.ChannelStatus[channel] != nil && r.ChannelStatus[channel].Equals(status) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:40:38 +00:00
|
|
|
if sendUpdate && r.ChannelStatus[channel] != nil {
|
|
|
|
if r.ChannelStatus[channel].Category != status.Category {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"channel": channel,
|
|
|
|
"category": status.Category,
|
|
|
|
}).Debug("Twitch metadata changed")
|
2021-09-02 21:26:39 +00:00
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeTwitchCategoryUpdate, plugins.FieldCollection{
|
2021-09-02 20:21:27 +00:00
|
|
|
"channel": channel,
|
2021-09-02 15:40:38 +00:00
|
|
|
"category": status.Category,
|
|
|
|
})
|
|
|
|
}
|
2021-09-02 15:39:21 +00:00
|
|
|
|
2021-09-02 15:40:38 +00:00
|
|
|
if r.ChannelStatus[channel].Title != status.Title {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"channel": channel,
|
|
|
|
"title": status.Title,
|
|
|
|
}).Debug("Twitch metadata changed")
|
2021-09-02 21:26:39 +00:00
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeTwitchTitleUpdate, plugins.FieldCollection{
|
2021-09-02 20:21:27 +00:00
|
|
|
"channel": channel,
|
|
|
|
"title": status.Title,
|
2021-09-02 15:40:38 +00:00
|
|
|
})
|
|
|
|
}
|
2021-09-02 15:09:30 +00:00
|
|
|
|
2021-09-02 15:40:38 +00:00
|
|
|
if r.ChannelStatus[channel].IsLive != status.IsLive {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"channel": channel,
|
|
|
|
"isLive": status.IsLive,
|
|
|
|
}).Debug("Twitch metadata changed")
|
2021-09-02 15:09:30 +00:00
|
|
|
|
2021-09-02 15:40:38 +00:00
|
|
|
evt := eventTypeTwitchStreamOnline
|
|
|
|
if !status.IsLive {
|
|
|
|
evt = eventTypeTwitchStreamOffline
|
|
|
|
}
|
2021-09-02 15:33:48 +00:00
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
go handleMessage(ircHdl.Client(), nil, evt, plugins.FieldCollection{
|
2021-09-02 20:21:27 +00:00
|
|
|
"channel": channel,
|
|
|
|
})
|
2021-09-02 15:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ChannelStatus[channel] = &status
|
|
|
|
return nil
|
|
|
|
}
|