2024-02-27 22:13:57 -05:00
|
|
|
|
package ads
|
|
|
|
|
|
|
|
|
|
import (
|
2024-02-27 23:03:40 -05:00
|
|
|
|
"context"
|
2024-02-27 22:13:57 -05:00
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-02-27 23:03:40 -05:00
|
|
|
|
"net/http"
|
2024-02-27 22:13:57 -05:00
|
|
|
|
"net/url"
|
|
|
|
|
"time"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
|
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-02-27 22:13:57 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GetAdScheduleResponse struct {
|
|
|
|
|
// A list that contains information related to the channel’s ad schedule.
|
|
|
|
|
Data []GetAdScheduleData `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetAdScheduleData struct {
|
|
|
|
|
// The number of snoozes available for the broadcaster.
|
|
|
|
|
SnoozeCount int `json:"snooze_count"`
|
|
|
|
|
|
|
|
|
|
// The UTC timestamp when the broadcaster will gain an additional snooze, in RFC3339 format.
|
|
|
|
|
SnoozeRefreshAt time.Time `json:"snooze_refresh_at"`
|
|
|
|
|
|
|
|
|
|
// The UTC timestamp of the broadcaster’s next scheduled ad, in RFC3339 format. Empty if the channel has no ad scheduled or is not live.
|
|
|
|
|
NextAdAt time.Time `json:"next_ad_at"`
|
|
|
|
|
|
|
|
|
|
// The length in seconds of the scheduled upcoming ad break.
|
|
|
|
|
Duration int `json:"duration"`
|
|
|
|
|
|
|
|
|
|
// The UTC timestamp of the broadcaster’s last ad-break, in RFC3339 format. Empty if the channel has not run an ad or is not live.
|
|
|
|
|
LastAdAt time.Time `json:"last_ad_at"`
|
|
|
|
|
|
|
|
|
|
// The amount of pre-roll free time remaining for the channel in seconds. Returns 0 if they are currently not pre-roll free.
|
|
|
|
|
PrerollFreeTime int `json:"preroll_free_time"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This endpoint returns ad schedule related information, including snooze, when the last ad was run, when the next ad is scheduled,
|
|
|
|
|
// and if the channel is currently in pre-roll free time. Note that a new ad cannot be run until 8 minutes after running a previous ad.
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the channel:read:ads scope.
|
|
|
|
|
// The user_id in the user access token must match the broadcaster_id.
|
2024-03-07 20:52:42 -05:00
|
|
|
|
func (a *Ads) GetAdSchedule(ctx context.Context, broadcasterID string) (*GetAdScheduleResponse, error) {
|
|
|
|
|
v := url.Values{"broadcaster_id": {broadcasterID}}
|
2024-02-27 22:13:57 -05:00
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(a.baseUrl, "channels/ads", v), nil)
|
2024-02-27 22:13:57 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
res, err := a.client.Do(req)
|
2024-02-27 23:03:40 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
2024-02-27 22:13:57 -05:00
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to get ad schedule (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 22:13:57 -05:00
|
|
|
|
var data GetAdScheduleResponse
|
2024-02-27 23:03:40 -05:00
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
2024-02-27 22:13:57 -05:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|