Extract Twitch API calls to extra module

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-07-29 21:13:19 +02:00
parent 1dcabc7153
commit fa6997f684
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 139 additions and 70 deletions

View file

@ -10,3 +10,4 @@ var (
func ptrDuration(v time.Duration) *time.Duration { return &v }
func ptrInt64(v int64) *int64 { return &v }
func ptrString(v string) *string { return &v }
func ptrTime(v time.Time) *time.Time { return &v }

View file

@ -2,14 +2,10 @@ package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/Luzifer/go_helpers/v2/backoff"
"github.com/bwmarrin/discordgo"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
@ -21,11 +17,6 @@ import (
* @module_desc Posts stream schedule derived from Twitch schedule as embed in Discord channel
*/
const (
twitchAPIRequestLimit = 5
twitchAPIRequestTimeout = 2 * time.Second
)
var (
defaultStreamScheduleEntries = ptrInt64(5)
defaultStreamSchedulePastTime = ptrDuration(15 * time.Minute)
@ -35,39 +26,10 @@ func init() {
RegisterModule("schedule", func() module { return &modStreamSchedule{} })
}
type (
modStreamSchedule struct {
attrs moduleAttributeStore
discord *discordgo.Session
}
twitchStreamScheduleResponse struct {
Data struct {
Segments []struct {
ID string `json:"id"`
StartTime *time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time"`
Title string `json:"title"`
CanceledUntil *time.Time `json:"canceled_until"`
Category *struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"category"`
IsRecurring bool `json:"is_recurring"`
} `json:"segments"`
BroadcasterID string `json:"broadcaster_id"`
BroadcasterName string `json:"broadcaster_name"`
BroadcasterLogin string `json:"broadcaster_login"`
Vacation *struct {
StartTime *time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time"`
} `json:"vacation"`
} `json:"data"`
Pagination struct {
Cursor string `json:"cursor"`
} `json:"pagination"`
}
)
type modStreamSchedule struct {
attrs moduleAttributeStore
discord *discordgo.Session
}
func (m *modStreamSchedule) Initialize(crontab *cron.Cron, discord *discordgo.Session, attrs moduleAttributeStore) error {
m.attrs = attrs
@ -92,36 +54,21 @@ func (m *modStreamSchedule) Initialize(crontab *cron.Cron, discord *discordgo.Se
}
func (m modStreamSchedule) cronUpdateSchedule() {
var data twitchStreamScheduleResponse
if err := backoff.NewBackoff().WithMaxIterations(twitchAPIRequestLimit).Retry(func() error {
ctx, cancel := context.WithTimeout(context.Background(), twitchAPIRequestTimeout)
defer cancel()
u, _ := url.Parse("https://api.twitch.tv/helix/schedule")
params := make(url.Values)
// @attr twitch_channel_id required string "" ID (not name) of the channel to fetch the schedule from
params.Set("broadcaster_id", m.attrs.MustString("twitch_channel_id", nil))
// @attr schedule_past_time optional duration "15m" How long in the past should the schedule contain an entry
params.Set("start_time", time.Now().Add(-m.attrs.MustDuration("schedule_past_time", defaultStreamSchedulePastTime)).Format(time.RFC3339))
u.RawQuery = params.Encode()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
// @attr twitch_token required string "" Token for the user the `twitch_channel_id` belongs to
req.Header.Set("Authorization", "Bearer "+m.attrs.MustString("twitch_token", nil))
twitch := newTwitchAdapter(
// @attr twitch_client_id required string "" Twitch client ID the token was issued for
req.Header.Set("Client-Id", m.attrs.MustString("twitch_client_id", nil))
m.attrs.MustString("twitch_client_id", nil),
// @attr twitch_token required string "" Token for the user the `twitch_channel_id` belongs to
m.attrs.MustString("twitch_token", nil),
)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "fetching schedule")
}
defer resp.Body.Close()
return errors.Wrap(
json.NewDecoder(resp.Body).Decode(&data),
"decoding schedule response",
)
}); err != nil {
data, err := twitch.GetChannelStreamSchedule(
context.Background(),
// @attr twitch_channel_id required string "" ID (not name) of the channel to fetch the schedule from
m.attrs.MustString("twitch_channel_id", nil),
// @attr schedule_past_time optional duration "15m" How long in the past should the schedule contain an entry
ptrTime(time.Now().Add(-m.attrs.MustDuration("schedule_past_time", defaultStreamSchedulePastTime))),
)
if err != nil {
log.WithError(err).Error("Unable to fetch stream schedule")
return
}

121
twitch.go Normal file
View file

@ -0,0 +1,121 @@
package main
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/Luzifer/go_helpers/v2/backoff"
"github.com/pkg/errors"
)
const (
twitchAPIRequestLimit = 5
twitchAPIRequestTimeout = 2 * time.Second
)
type (
twitchAdapter struct {
clientID string
token string
}
twitchStreamSchedule struct {
Data struct {
Segments []struct {
ID string `json:"id"`
StartTime *time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time"`
Title string `json:"title"`
CanceledUntil *time.Time `json:"canceled_until"`
Category *struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"category"`
IsRecurring bool `json:"is_recurring"`
} `json:"segments"`
BroadcasterID string `json:"broadcaster_id"`
BroadcasterName string `json:"broadcaster_name"`
BroadcasterLogin string `json:"broadcaster_login"`
Vacation *struct {
StartTime *time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time"`
} `json:"vacation"`
} `json:"data"`
Pagination struct {
Cursor string `json:"cursor"`
} `json:"pagination"`
}
)
func newTwitchAdapter(clientID, token string) *twitchAdapter {
return &twitchAdapter{
clientID: clientID,
token: token,
}
}
func (t twitchAdapter) GetChannelStreamSchedule(ctx context.Context, broadcasterID string, startTime *time.Time) (*twitchStreamSchedule, error) {
out := &twitchStreamSchedule{}
params := make(url.Values)
params.Set("broadcaster_id", broadcasterID)
if startTime != nil {
params.Set("start_time", startTime.Format(time.RFC3339))
}
return out, backoff.NewBackoff().
WithMaxIterations(twitchAPIRequestLimit).
Retry(func() error {
return errors.Wrap(
t.request(ctx, http.MethodGet, "/helix/schedule", params, nil, out),
"fetching schedule",
)
})
}
func (t twitchAdapter) request(ctx context.Context, method, path string, params url.Values, body io.Reader, output interface{}) error {
ctxTimed, cancel := context.WithTimeout(ctx, twitchAPIRequestTimeout)
defer cancel()
u, _ := url.Parse(strings.Join([]string{
"https://api.twitch.tv",
strings.TrimLeft(path, "/"),
}, "/"))
if params != nil {
u.RawQuery = params.Encode()
}
req, _ := http.NewRequestWithContext(ctxTimed, method, u.String(), body)
req.Header.Set("Authorization", strings.Join([]string{"Bearer", t.token}, " "))
req.Header.Set("Client-Id", t.clientID)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "fetching response")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
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 output == nil {
return nil
}
return errors.Wrap(
json.NewDecoder(resp.Body).Decode(output),
"decoding response",
)
}