2021-08-19 13:33:56 +00:00
|
|
|
package twitch
|
2020-12-24 12:18:30 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-10 17:09:10 +00:00
|
|
|
"bytes"
|
2020-12-24 12:18:30 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-12-24 18:59:20 +00:00
|
|
|
"io/ioutil"
|
2020-12-24 12:18:30 +00:00
|
|
|
"net/http"
|
2021-09-10 17:09:10 +00:00
|
|
|
"net/url"
|
2021-09-22 13:36:45 +00:00
|
|
|
"strconv"
|
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"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/go_helpers/v2/backoff"
|
2021-12-24 21:04:48 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/str"
|
2020-12-24 12:18:30 +00:00
|
|
|
)
|
|
|
|
|
2021-06-02 11:21:13 +00:00
|
|
|
const (
|
|
|
|
timeDay = 24 * time.Hour
|
|
|
|
|
2021-09-02 21:28:15 +00:00
|
|
|
twitchMinCacheTime = time.Second * 30
|
|
|
|
|
2021-06-02 11:21:13 +00:00
|
|
|
twitchRequestRetries = 5
|
|
|
|
twitchRequestTimeout = 2 * time.Second
|
|
|
|
)
|
2021-04-03 12:11:47 +00:00
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
const (
|
|
|
|
authTypeUnauthorized authType = iota
|
|
|
|
authTypeAppAccessToken
|
|
|
|
authTypeBearerToken
|
|
|
|
)
|
|
|
|
|
2021-09-10 17:09:10 +00:00
|
|
|
type (
|
|
|
|
Category struct {
|
|
|
|
BoxArtURL string `json:"box_art_url"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2020-12-24 12:18:30 +00:00
|
|
|
|
2021-09-10 17:09:10 +00:00
|
|
|
Client struct {
|
2021-12-24 18:59:20 +00:00
|
|
|
clientID string
|
|
|
|
clientSecret string
|
|
|
|
token string
|
|
|
|
|
|
|
|
appAccessToken string
|
2021-09-10 17:09:10 +00:00
|
|
|
|
|
|
|
apiCache *APICache
|
|
|
|
}
|
2021-09-22 13:36:45 +00:00
|
|
|
|
2021-10-13 12:30:45 +00:00
|
|
|
StreamInfo struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
UserLogin string `json:"user_login"`
|
|
|
|
UserName string `json:"user_name"`
|
|
|
|
GameID string `json:"game_id"`
|
|
|
|
GameName string `json:"game_name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
ViewerCount int64 `json:"viewer_count"`
|
|
|
|
StartedAt time.Time `json:"started_at"`
|
|
|
|
Language string `json:"language"`
|
|
|
|
ThumbnailURL string `json:"thumbnail_url"`
|
|
|
|
TagIds []string `json:"tag_ids"`
|
|
|
|
IsMature bool `json:"is_mature"`
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
User struct {
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Login string `json:"login"`
|
|
|
|
ProfileImageURL string `json:"profile_image_url"`
|
|
|
|
}
|
2021-12-24 18:59:20 +00:00
|
|
|
|
|
|
|
authType uint8
|
|
|
|
|
|
|
|
clientRequestOpts struct {
|
|
|
|
AuthType authType
|
|
|
|
Body io.Reader
|
|
|
|
Context context.Context
|
|
|
|
Method string
|
|
|
|
OKStatus int
|
|
|
|
Out interface{}
|
|
|
|
URL string
|
|
|
|
}
|
2021-09-10 17:09:10 +00:00
|
|
|
)
|
2021-03-27 17:55:38 +00:00
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
func New(clientID, clientSecret, token string) *Client {
|
2021-08-19 13:33:56 +00:00
|
|
|
return &Client{
|
2021-12-24 18:59:20 +00:00
|
|
|
clientID: clientID,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
token: token,
|
2021-08-19 13:33:56 +00:00
|
|
|
|
2021-04-01 10:28:51 +00:00
|
|
|
apiCache: newTwitchAPICache(),
|
2021-03-27 17:55:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-24 12:18:30 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (c Client) APICache() *APICache { return c.apiCache }
|
|
|
|
|
|
|
|
func (c Client) GetAuthorizedUsername() (string, error) {
|
2020-12-24 12:18:30 +00:00
|
|
|
var payload struct {
|
2021-09-22 13:36:45 +00:00
|
|
|
Data []User `json:"data"`
|
2020-12-24 12:18:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &payload,
|
|
|
|
URL: "https://api.twitch.tv/helix/users",
|
|
|
|
}); err != nil {
|
2020-12-24 12:18:30 +00:00
|
|
|
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-08-19 13:33:56 +00:00
|
|
|
func (c Client) GetDisplayNameForUser(username string) (string, error) {
|
2021-05-24 19:42:55 +00:00
|
|
|
cacheKey := []string{"displayNameForUsername", username}
|
2021-08-19 13:33:56 +00:00
|
|
|
if d := c.apiCache.Get(cacheKey); d != nil {
|
2021-05-24 19:42:55 +00:00
|
|
|
return d.(string), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload struct {
|
2021-09-22 13:36:45 +00:00
|
|
|
Data []User `json:"data"`
|
2021-05-24 19:42:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Out: &payload,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", username),
|
|
|
|
}); err != nil {
|
2021-05-24 19:42:55 +00:00
|
|
|
return "", errors.Wrap(err, "request channel info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(payload.Data); l != 1 {
|
|
|
|
return "", errors.Errorf("unexpected number of users returned: %d", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The DisplayName for an username will not change (often), cache for a decent time
|
2021-08-19 13:33:56 +00:00
|
|
|
c.apiCache.Set(cacheKey, time.Hour, payload.Data[0].DisplayName)
|
2021-05-24 19:42:55 +00:00
|
|
|
|
|
|
|
return payload.Data[0].DisplayName, nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (c Client) GetFollowDate(from, to string) (time.Time, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"followDate", from, to}
|
2021-08-19 13:33:56 +00:00
|
|
|
if d := c.apiCache.Get(cacheKey); d != nil {
|
2021-03-27 17:55:38 +00:00
|
|
|
return d.(time.Time), nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:08:49 +00:00
|
|
|
fromID, err := c.GetIDForUsername(from)
|
2021-01-20 23:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, errors.Wrap(err, "getting id for 'from' user")
|
|
|
|
}
|
2021-09-10 17:08:49 +00:00
|
|
|
toID, err := c.GetIDForUsername(to)
|
2021-01-20 23:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, errors.Wrap(err, "getting id for 'to' user")
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Data []struct {
|
|
|
|
FollowedAt time.Time `json:"followed_at"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &payload,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/users/follows?to_id=%s&from_id=%s", toID, fromID),
|
|
|
|
}); err != nil {
|
2021-01-20 23:35:42 +00:00
|
|
|
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
|
2021-08-19 13:33:56 +00:00
|
|
|
c.apiCache.Set(cacheKey, timeDay, payload.Data[0].FollowedAt)
|
2021-03-27 17:55:38 +00:00
|
|
|
|
2021-01-20 23:35:42 +00:00
|
|
|
return payload.Data[0].FollowedAt, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
func (c Client) GetUserInformation(user string) (*User, error) {
|
|
|
|
var (
|
|
|
|
out User
|
|
|
|
param = "login"
|
|
|
|
payload struct {
|
|
|
|
Data []User `json:"data"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
cacheKey := []string{"userInformation", user}
|
|
|
|
if d := c.apiCache.Get(cacheKey); d != nil {
|
|
|
|
out = d.(User)
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := strconv.ParseInt(user, 10, 64); err == nil {
|
|
|
|
param = "id"
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &payload,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/users?%s=%s", param, user),
|
|
|
|
}); err != nil {
|
2021-09-22 13:36:45 +00:00
|
|
|
return nil, errors.Wrap(err, "request user info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(payload.Data); l != 1 {
|
|
|
|
return nil, errors.Errorf("unexpected number of records returned: %d", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Follow date will not change that often, cache for a long time
|
|
|
|
c.apiCache.Set(cacheKey, timeDay, payload.Data[0])
|
|
|
|
out = payload.Data[0]
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:09:10 +00:00
|
|
|
func (c Client) SearchCategories(ctx context.Context, name string) ([]Category, error) {
|
|
|
|
var out []Category
|
|
|
|
|
|
|
|
params := make(url.Values)
|
|
|
|
params.Set("query", name)
|
|
|
|
params.Set("first", "100")
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Data []Category `json:"data"`
|
|
|
|
Pagination struct {
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
} `json:"pagination"`
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: ctx,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &resp,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/search/categories?%s", params.Encode()),
|
|
|
|
}); err != nil {
|
2021-09-10 17:09:10 +00:00
|
|
|
return nil, errors.Wrap(err, "executing request")
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, resp.Data...)
|
|
|
|
|
|
|
|
if resp.Pagination.Cursor == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
params.Set("after", resp.Pagination.Cursor)
|
|
|
|
resp.Pagination.Cursor = "" // Clear from struct as struct is reused
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (c Client) HasLiveStream(username string) (bool, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"hasLiveStream", username}
|
2021-08-19 13:33:56 +00:00
|
|
|
if d := c.apiCache.Get(cacheKey); d != nil {
|
2021-03-27 17:55:38 +00:00
|
|
|
return d.(bool), nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:04:04 +00:00
|
|
|
var payload struct {
|
|
|
|
Data []struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
UserLogin string `json:"user_login"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &payload,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/streams?user_login=%s", username),
|
|
|
|
}); err != nil {
|
2021-03-21 13:04:04 +00:00
|
|
|
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
|
2021-09-02 21:28:15 +00:00
|
|
|
c.apiCache.Set(cacheKey, twitchMinCacheTime, len(payload.Data) == 1 && payload.Data[0].Type == "live")
|
2021-03-27 17:55:38 +00:00
|
|
|
|
2021-03-21 13:04:04 +00:00
|
|
|
return len(payload.Data) == 1 && payload.Data[0].Type == "live", nil
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:30:45 +00:00
|
|
|
func (c Client) GetCurrentStreamInfo(username string) (*StreamInfo, error) {
|
|
|
|
cacheKey := []string{"currentStreamInfo", username}
|
|
|
|
if si := c.apiCache.Get(cacheKey); si != nil {
|
|
|
|
return si.(*StreamInfo), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := c.GetIDForUsername(username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getting ID for username")
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload struct {
|
|
|
|
Data []*StreamInfo `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &payload,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/streams?user_id=%s", id),
|
|
|
|
}); err != nil {
|
2021-10-13 12:30:45 +00:00
|
|
|
return nil, errors.Wrap(err, "request channel info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := len(payload.Data); l != 1 {
|
|
|
|
return nil, errors.Errorf("unexpected number of users returned: %d", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream-info can be changed at any moment, cache for a short period of time
|
|
|
|
c.apiCache.Set(cacheKey, twitchMinCacheTime, payload.Data[0])
|
|
|
|
|
|
|
|
return payload.Data[0], nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:08:49 +00:00
|
|
|
func (c Client) GetIDForUsername(username string) (string, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"idForUsername", username}
|
2021-08-19 13:33:56 +00:00
|
|
|
if d := c.apiCache.Get(cacheKey); d != nil {
|
2021-03-27 17:55:38 +00:00
|
|
|
return d.(string), nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
var payload struct {
|
2021-09-22 13:36:45 +00:00
|
|
|
Data []User `json:"data"`
|
2020-12-24 12:18:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &payload,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s", username),
|
|
|
|
}); err != nil {
|
2020-12-24 12:18:30 +00:00
|
|
|
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
|
2021-08-19 13:33:56 +00:00
|
|
|
c.apiCache.Set(cacheKey, timeDay, payload.Data[0].ID)
|
2021-03-27 17:55:38 +00:00
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
return payload.Data[0].ID, nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (c Client) GetRecentStreamInfo(username string) (string, string, error) {
|
2021-03-27 17:55:38 +00:00
|
|
|
cacheKey := []string{"recentStreamInfo", username}
|
2021-08-19 13:33:56 +00:00
|
|
|
if d := c.apiCache.Get(cacheKey); d != nil {
|
2021-03-27 17:55:38 +00:00
|
|
|
return d.([2]string)[0], d.([2]string)[1], nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:08:49 +00:00
|
|
|
id, err := c.GetIDForUsername(username)
|
2020-12-24 12:18:30 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Context: context.Background(),
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &payload,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/channels?broadcaster_id=%s", id),
|
|
|
|
}); err != nil {
|
2020-12-24 12:18:30 +00:00
|
|
|
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
|
2021-09-02 21:28:15 +00:00
|
|
|
c.apiCache.Set(cacheKey, twitchMinCacheTime, [2]string{payload.Data[0].GameName, payload.Data[0].Title})
|
2021-03-27 17:55:38 +00:00
|
|
|
|
2020-12-24 12:18:30 +00:00
|
|
|
return payload.Data[0].GameName, payload.Data[0].Title, nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:20:31 +00:00
|
|
|
func (c Client) ModifyChannelInformation(ctx context.Context, broadcasterName string, game, title *string) error {
|
2021-09-10 17:09:10 +00:00
|
|
|
if game == nil && title == nil {
|
|
|
|
return errors.New("netiher game nor title provided")
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:20:31 +00:00
|
|
|
broadcaster, err := c.GetIDForUsername(broadcasterName)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting ID for broadcaster name")
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:09:10 +00:00
|
|
|
data := struct {
|
|
|
|
GameID *string `json:"game_id,omitempty"`
|
|
|
|
Title *string `json:"title,omitempty"`
|
|
|
|
}{
|
|
|
|
Title: title,
|
|
|
|
}
|
|
|
|
|
|
|
|
if game != nil {
|
|
|
|
categories, err := c.SearchCategories(ctx, *game)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "searching for game")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(categories) {
|
|
|
|
case 0:
|
|
|
|
return errors.New("no matching game found")
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
data.GameID = &categories[0].ID
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Multiple matches: Search for exact one
|
|
|
|
for _, c := range categories {
|
|
|
|
if c.Name == *game {
|
2021-09-12 23:57:03 +00:00
|
|
|
gid := c.ID
|
|
|
|
data.GameID = &gid
|
2021-09-10 17:09:10 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.GameID == nil {
|
|
|
|
// No exact match found: This is an error
|
|
|
|
return errors.New("no exact game match found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
if err := json.NewEncoder(body).Encode(data); err != nil {
|
|
|
|
return errors.Wrap(err, "encoding payload")
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(
|
2021-12-24 18:59:20 +00:00
|
|
|
c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeBearerToken,
|
|
|
|
Body: body,
|
|
|
|
Context: ctx,
|
|
|
|
Method: http.MethodPatch,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/channels?broadcaster_id=%s", broadcaster),
|
|
|
|
}),
|
2021-09-10 17:09:10 +00:00
|
|
|
"executing request",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
func (c *Client) createEventSubSubscription(ctx context.Context, sub eventSubSubscription) (*eventSubSubscription, error) {
|
|
|
|
var (
|
|
|
|
buf = new(bytes.Buffer)
|
|
|
|
resp struct {
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
Data []eventSubSubscription `json:"data"`
|
|
|
|
Pagination struct {
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
} `json:"pagination"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.NewEncoder(buf).Encode(sub); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "assemble subscribe payload")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeAppAccessToken,
|
|
|
|
Body: buf,
|
|
|
|
Context: ctx,
|
|
|
|
Method: http.MethodPost,
|
|
|
|
OKStatus: http.StatusAccepted,
|
|
|
|
Out: &resp,
|
|
|
|
URL: "https://api.twitch.tv/helix/eventsub/subscriptions",
|
|
|
|
}); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "executing request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.Data[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) deleteEventSubSubscription(ctx context.Context, id string) error {
|
|
|
|
return errors.Wrap(c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeAppAccessToken,
|
|
|
|
Context: ctx,
|
|
|
|
Method: http.MethodDelete,
|
|
|
|
OKStatus: http.StatusNoContent,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/eventsub/subscriptions?id=%s", id),
|
|
|
|
}), "executing request")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getEventSubSubscriptions(ctx context.Context) ([]eventSubSubscription, error) {
|
|
|
|
var (
|
|
|
|
out []eventSubSubscription
|
|
|
|
params = make(url.Values)
|
|
|
|
resp struct {
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
Data []eventSubSubscription `json:"data"`
|
|
|
|
Pagination struct {
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
} `json:"pagination"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeAppAccessToken,
|
|
|
|
Context: ctx,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &resp,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/eventsub/subscriptions?%s", params.Encode()),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "executing request")
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, resp.Data...)
|
|
|
|
|
|
|
|
if resp.Pagination.Cursor == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
params.Set("after", resp.Pagination.Cursor)
|
|
|
|
resp.Pagination.Cursor = "" // Clear from struct as struct is reused
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getTwitchAppAccessToken() (string, error) {
|
|
|
|
if c.appAccessToken != "" {
|
|
|
|
return c.appAccessToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var rData struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
Scope []interface{} `json:"scope"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(url.Values)
|
|
|
|
params.Set("client_id", c.clientID)
|
|
|
|
params.Set("client_secret", c.clientSecret)
|
|
|
|
params.Set("grant_type", "client_credentials")
|
|
|
|
|
|
|
|
u, _ := url.Parse("https://id.twitch.tv/oauth2/token")
|
|
|
|
u.RawQuery = params.Encode()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := c.request(clientRequestOpts{
|
|
|
|
AuthType: authTypeUnauthorized,
|
|
|
|
Context: ctx,
|
|
|
|
Method: http.MethodPost,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &rData,
|
|
|
|
URL: u.String(),
|
|
|
|
}); err != nil {
|
|
|
|
return "", errors.Wrap(err, "fetching token response")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.appAccessToken = rData.AccessToken
|
|
|
|
return rData.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) request(opts clientRequestOpts) error {
|
2021-03-27 17:55:38 +00:00
|
|
|
log.WithFields(log.Fields{
|
2021-12-24 18:59:20 +00:00
|
|
|
"method": opts.Method,
|
|
|
|
"url": opts.URL,
|
2021-03-27 17:55:38 +00:00
|
|
|
}).Trace("Execute Twitch API request")
|
|
|
|
|
2021-12-24 21:04:48 +00:00
|
|
|
var retries uint64 = twitchRequestRetries
|
|
|
|
if str.StringInSlice(opts.Method, []string{
|
|
|
|
http.MethodPost, // Creates stuff, must not be retried without being asked
|
|
|
|
}) {
|
|
|
|
retries = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return backoff.NewBackoff().WithMaxIterations(retries).Retry(func() error {
|
2021-12-24 18:59:20 +00:00
|
|
|
reqCtx, cancel := context.WithTimeout(opts.Context, twitchRequestTimeout)
|
2021-06-02 11:21:13 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
req, err := http.NewRequestWithContext(reqCtx, opts.Method, opts.URL, opts.Body)
|
2021-06-02 11:21:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "assemble request")
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2021-12-24 18:59:20 +00:00
|
|
|
|
|
|
|
switch opts.AuthType {
|
|
|
|
case authTypeUnauthorized:
|
|
|
|
// Nothing to do
|
|
|
|
|
|
|
|
case authTypeAppAccessToken:
|
|
|
|
accessToken, err := c.getTwitchAppAccessToken()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting app-access-token")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
req.Header.Set("Client-Id", c.clientID)
|
|
|
|
|
|
|
|
case authTypeBearerToken:
|
|
|
|
req.Header.Set("Authorization", "Bearer "+c.token)
|
|
|
|
req.Header.Set("Client-Id", c.clientID)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.New("invalid auth type specified")
|
|
|
|
}
|
2021-06-02 11:21:13 +00:00
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "execute request")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2021-12-24 18:59:20 +00:00
|
|
|
if opts.OKStatus != 0 && resp.StatusCode != opts.OKStatus {
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "unexpected status %d and cannot read body", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return errors.Errorf("unexpected status %d: %s", resp.StatusCode, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Out == nil {
|
2021-09-10 17:09:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-02 11:21:13 +00:00
|
|
|
return errors.Wrap(
|
2021-12-24 18:59:20 +00:00
|
|
|
json.NewDecoder(resp.Body).Decode(opts.Out),
|
2021-06-02 11:21:13 +00:00
|
|
|
"parse user info",
|
|
|
|
)
|
|
|
|
})
|
2020-12-24 12:18:30 +00:00
|
|
|
}
|