Skip to content

Commit

Permalink
Merge pull request #11 from bcambl/twitch_game
Browse files Browse the repository at this point in the history
add game name to twitch notification
  • Loading branch information
bcambl authored Feb 20, 2021
2 parents dcb0dd1 + 23f45b4 commit e7d4795
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion controllers/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ type StreamData struct {
StartedAt string `json:"started_at"`
}

// TwitchGamesResponse to marshal the json response from /helix/games/
type TwitchGamesResponse struct {
Data []GameData `json:"data"`
}

// GameData to marshal the inner data of
type GameData struct {
ID string `json:"id"`
Name string `json:"name"`
BoxArtURL string `json:"box_art_url"`
}

// retrieve cached twitch access token from database and set in the
// Config struct. This is only called when the token is not set in Config
func (c *Controller) getCachedAccessToken() (string, error) {
Expand Down Expand Up @@ -236,6 +248,59 @@ func (c *Controller) getStreams() ([]StreamData, error) {
return streamResponse.Data, nil
}

func (c *Controller) getGame(gameID string) (GameData, error) {

var (
err error
gamesQuery string
g GameData
)

err = c.validateClientCredentials()
if err != nil {
return g, err
}

accessToken, err := c.twitchAuthToken()
if err != nil {
return g, err
}

gamesQuery = fmt.Sprintf("https://api.twitch.tv/helix/games?id=%s", gameID)

r, err := http.NewRequest("GET", gamesQuery, nil)
if err != nil {
log.Error(err)
}
r.Header.Set("Content-Type", "application/json")
r.Header.Set("client-id", c.Config.TwitchClientID)
r.Header.Set("Authorization", "Bearer "+accessToken)

resp, err := http.DefaultClient.Do(r)
if err != nil {
return g, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return g, err
}

gamesResponse := TwitchGamesResponse{}
err = json.Unmarshal(body, &gamesResponse)
if err != nil {
return g, err
}

if len(gamesResponse.Data) != 1 {
err = fmt.Errorf("game query for '%s' did not return exactly 1 result", gameID)
return g, err
}

return gamesResponse.Data[0], nil
}

func (c *Controller) updateLiveStatus(streams []StreamData) error {

var live bool
Expand Down Expand Up @@ -275,7 +340,11 @@ func (c *Controller) updateLiveStatus(streams []StreamData) error {
if !p.IsTwitchLive() {
c.setTwitchLive(p, s.Type)
streamLink := fmt.Sprintf("https://twitch.tv/%s", p.TwitchStream)
notification := fmt.Sprintf(":movie_camera: %s started a public stream on twitch!\ntitle: %s\nwatch now: `%s`", p.Name, s.Title, streamLink)
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)
c.setTwitchNotification(p, notification)
}
}
Expand Down

0 comments on commit e7d4795

Please sign in to comment.