Skip to content

Commit

Permalink
notify on stream info updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bcambl committed Feb 21, 2021
1 parent 0eb8491 commit 550bde7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var DataBuckets = []string{
"TwitchStreamBucket", // Local publishers -> twitch stream names
"TwitchLiveBucket", // Local publishers -> twitch live stream status
"TwitchNotificationBucket", // Local publishers -> twitch notification state
"StreamInfoBucket", // Local publishers -> generic stream information
}

func init() {
Expand Down
7 changes: 7 additions & 0 deletions controllers/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Publisher struct {
TwitchStream string `json:"twitch_stream"`
TwitchLive string `json:"twitch_live"`
TwitchNotification string `json:"-"`
StreamInfo string `json:"-"`
}

// IsValid perform basic validations on a publisher record
Expand Down Expand Up @@ -64,6 +65,12 @@ func (c *Controller) FetchPublisher(p *Publisher) error {
if err != nil {
return err
}
p.TwitchNotification = string(b)
b, err = c.getBucketValue("SteamInfoBucket", p.Name)
if err != nil {
return err
}
p.StreamInfo = string(b)

return nil
}
Expand Down
23 changes: 16 additions & 7 deletions controllers/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func (c *Controller) updateLiveStatus(streams []StreamData) error {
return err
}

// mark previous live streams -> offline
// mark previous live streams -> offline and notify of stream info change
for i := range publishers {
live = false
p := &publishers[i]
Expand All @@ -316,10 +316,23 @@ func (c *Controller) updateLiveStatus(streams []StreamData) error {
s := streams[x]
if strings.ToLower(s.UserName) == strings.ToLower(p.TwitchStream) {
live = true
// retieve stream info
g, err := c.getGame(s.GameID)
if err != nil {
return err
}
streamInfo := fmt.Sprintf("title: %s\ngame: %s", s.Title, g.Name)
if p.StreamInfo != "" && p.StreamInfo != streamInfo {
// streamer changed their stream info, set notification
notification := fmt.Sprintf("%s switched it up!\n%s", p.Name, p.StreamInfo)
c.setBucketValue("TwitchNotificationBucket", p.Name, notification)
}
p.StreamInfo = streamInfo
}
}
if !live {
c.setBucketValue("TwitchLiveBucket", p.Name, "")
c.setBucketValue("StreamInfoBucket", p.Name, "")
notification := fmt.Sprintf(":checkered_flag: %s finished streaming on twitch", p.Name)
c.setBucketValue("TwitchNotificationBucket", p.Name, notification)
}
Expand All @@ -338,12 +351,8 @@ func (c *Controller) updateLiveStatus(streams []StreamData) error {
if !p.IsTwitchLive() {
c.setBucketValue("TwitchLiveBucket", p.Name, s.Type)
streamLink := fmt.Sprintf("https://twitch.tv/%s", p.TwitchStream)
g, err := c.getGame(s.GameID)
if err != nil {
return err
}
notification := fmt.Sprintf(":movie_camera: %s started a public stream on twitch!"+
"\ntitle: %s\ngame: %s\nwatch now: `%s`", p.Name, s.Title, g.Name, streamLink)
notification := fmt.Sprintf(":movie_camera: %s started streaming on twitch!"+
"\n%s\nwatch now: `%s`", p.Name, p.StreamInfo, streamLink)
c.setBucketValue("TwitchNotificationBucket", p.Name, notification)
}
}
Expand Down

0 comments on commit 550bde7

Please sign in to comment.