2020-12-24 12:18:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2021-01-20 23:35:42 +00:00
|
|
|
"time"
|
2020-12-24 12:18:30 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-03-27 17:55:38 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-12-24 12:18:30 +00:00
|
|
|
)
|
|
|
|
|
2021-03-27 17:55:38 +00:00
|
|
|
var twitch = newTwitchClient()
|
2020-12-24 12:18:30 +00:00
|
|
|
|
2021-03-27 17:55:38 +00:00
|
|
|
type twitchClient struct {
|
|
|
|
apiCache twitchAPICache
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTwitchClient() *twitchClient {
|
|
|
|
return &twitchClient{
|
|
|
|
apiCache: make(twitchAPICache),
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 12:18:30 +00:00
|
|
|
|
|
|
|
func (t twitchClient) getAuthorizedUsername() (string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Data []struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.request(ctx, http.MethodGet, "https://api.twitch.tv/helix/users", nil, &payload); err != nil {
|
|
|
|
return "", errors.Wrap(err, "request channel info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(payload.Data); l != 1 {
|
|
|
|
return "", errors.Errorf("unexpected number of users returned: %d", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload.Data[0].Login, nil
|
|
|
|
}
|
|
|
|
|
2021-01-20 23:35:42 +00:00
|
|
|
func (t twitchClient) GetFollowDate(from, to string) (time.Time, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"followDate", from, to}
|
|
|
|
if d := t.apiCache.Get(cacheKey); d != nil {
|
|
|
|
return d.(time.Time), nil
|
|
|
|
}
|
|
|
|
|
2021-01-20 23:35:42 +00:00
|
|
|
fromID, err := t.getIDForUsername(from)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, errors.Wrap(err, "getting id for 'from' user")
|
|
|
|
}
|
|
|
|
toID, err := t.getIDForUsername(to)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, errors.Wrap(err, "getting id for 'to' user")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Data []struct {
|
|
|
|
FollowedAt time.Time `json:"followed_at"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.request(
|
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf("https://api.twitch.tv/helix/users/follows?to_id=%s&from_id=%s", toID, fromID),
|
|
|
|
nil,
|
|
|
|
&payload,
|
|
|
|
); err != nil {
|
|
|
|
return time.Time{}, errors.Wrap(err, "request follow info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(payload.Data); l != 1 {
|
|
|
|
return time.Time{}, errors.Errorf("unexpected number of records returned: %d", l)
|
|
|
|
}
|
|
|
|
|
2021-03-27 17:55:38 +00:00
|
|
|
// Follow date will not change that often, cache for a long time
|
|
|
|
t.apiCache.Set(cacheKey, 24*time.Hour, payload.Data[0].FollowedAt)
|
|
|
|
|
2021-01-20 23:35:42 +00:00
|
|
|
return payload.Data[0].FollowedAt, nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:04:04 +00:00
|
|
|
func (t twitchClient) HasLiveStream(username string) (bool, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"hasLiveStream", username}
|
|
|
|
if d := t.apiCache.Get(cacheKey); d != nil {
|
|
|
|
return d.(bool), nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:04:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Data []struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
UserLogin string `json:"user_login"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.request(
|
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf("https://api.twitch.tv/helix/streams?user_login=%s", username),
|
|
|
|
nil,
|
|
|
|
&payload,
|
|
|
|
); err != nil {
|
|
|
|
return false, errors.Wrap(err, "request stream info")
|
|
|
|
}
|
|
|
|
|
2021-03-27 17:55:38 +00:00
|
|
|
// Live status might change recently, cache for one minute
|
|
|
|
t.apiCache.Set(cacheKey, time.Minute, len(payload.Data) == 1 && payload.Data[0].Type == "live")
|
|
|
|
|
2021-03-21 13:04:04 +00:00
|
|
|
return len(payload.Data) == 1 && payload.Data[0].Type == "live", nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
func (t twitchClient) getIDForUsername(username string) (string, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"idForUsername", username}
|
|
|
|
if d := t.apiCache.Get(cacheKey); d != nil {
|
|
|
|
return d.(string), nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Data []struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.request(
|
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", username),
|
|
|
|
nil,
|
|
|
|
&payload,
|
|
|
|
); err != nil {
|
|
|
|
return "", errors.Wrap(err, "request channel info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(payload.Data); l != 1 {
|
|
|
|
return "", errors.Errorf("unexpected number of users returned: %d", l)
|
|
|
|
}
|
|
|
|
|
2021-03-27 17:55:38 +00:00
|
|
|
// The ID for an username will not change (often), cache for a long time
|
|
|
|
t.apiCache.Set(cacheKey, 24*time.Hour, payload.Data[0].ID)
|
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
return payload.Data[0].ID, nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:02:44 +00:00
|
|
|
func (t twitchClient) GetRecentStreamInfo(username string) (string, string, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"recentStreamInfo", username}
|
|
|
|
if d := t.apiCache.Get(cacheKey); d != nil {
|
|
|
|
return d.([2]string)[0], d.([2]string)[1], nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
id, err := t.getIDForUsername(username)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", errors.Wrap(err, "getting ID for username")
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Data []struct {
|
|
|
|
BroadcasterID string `json:"broadcaster_id"`
|
|
|
|
GameID string `json:"game_id"`
|
|
|
|
GameName string `json:"game_name"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.request(
|
|
|
|
ctx,
|
|
|
|
http.MethodGet,
|
|
|
|
fmt.Sprintf("https://api.twitch.tv/helix/channels?broadcaster_id=%s", id),
|
|
|
|
nil,
|
|
|
|
&payload,
|
|
|
|
); err != nil {
|
|
|
|
return "", "", errors.Wrap(err, "request channel info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(payload.Data); l != 1 {
|
|
|
|
return "", "", errors.Errorf("unexpected number of users returned: %d", l)
|
|
|
|
}
|
|
|
|
|
2021-03-27 17:55:38 +00:00
|
|
|
// Stream-info can be changed at any moment, cache for a short period of time
|
|
|
|
t.apiCache.Set(cacheKey, time.Minute, [2]string{payload.Data[0].GameName, payload.Data[0].Title})
|
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
return payload.Data[0].GameName, payload.Data[0].Title, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (twitchClient) request(ctx context.Context, method, url string, body io.Reader, out interface{}) error {
|
2021-03-27 17:55:38 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"method": method,
|
|
|
|
"url": url,
|
|
|
|
}).Trace("Execute Twitch API request")
|
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, method, url, body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "assemble request")
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Client-Id", cfg.TwitchClient)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+cfg.TwitchToken)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "execute request")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return errors.Wrap(
|
|
|
|
json.NewDecoder(resp.Body).Decode(out),
|
|
|
|
"parse user info",
|
|
|
|
)
|
|
|
|
}
|