2024-03-03 14:16:33 -05:00
|
|
|
|
package hypetrain
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-03-03 14:16:33 -05:00
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-03-03 14:16:33 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GetHypeTrainEventsParams struct {
|
|
|
|
|
|
|
|
|
|
// The ID of the broadcaster that’s running the Hype Train. This ID must match the User ID in the user access token.
|
|
|
|
|
BroadcasterID string `url:"broadcaster_id"`
|
|
|
|
|
|
|
|
|
|
// The maximum number of items to return per page in the response.
|
|
|
|
|
// The minimum page size is 1 item per page and the maximum is 100 items per page.
|
|
|
|
|
// The default is 1.
|
2024-03-05 22:29:23 -05:00
|
|
|
|
First *int `url:"first,omitempty"`
|
2024-03-03 14:16:33 -05:00
|
|
|
|
|
|
|
|
|
// The cursor used to get the next page of results. The Pagination object in the response contains the cursor’s value.
|
|
|
|
|
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
|
2024-03-05 22:29:23 -05:00
|
|
|
|
After *types.Cursor `url:"after,omitempty"`
|
2024-03-03 14:16:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Event struct {
|
|
|
|
|
// An ID that identifies this event.
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
|
|
// The type of event. The string is in the form, hypetrain.{event_name}.
|
|
|
|
|
// The request returns only progress event types (i.e., hypetrain.progression).
|
|
|
|
|
EventType string `json:"event_type"`
|
|
|
|
|
|
|
|
|
|
// The UTC date and time (in RFC3339 format) that the event occurred.
|
|
|
|
|
EventTimestamp time.Time `json:"event_timestamp"`
|
|
|
|
|
|
|
|
|
|
// The version number of the definition of the event’s data.
|
|
|
|
|
// For example, the value is 1 if the data in event_data uses the first definition of the event’s data.
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
|
|
|
|
|
// The event’s data.
|
|
|
|
|
EventData EventData `json:"event_data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EventData struct {
|
|
|
|
|
// The ID of the broadcaster that’s running the Hype Train.
|
|
|
|
|
BroadcasterID string `json:"broadcaster_id"`
|
|
|
|
|
|
|
|
|
|
// The UTC date and time (in RFC3339 format) that another Hype Train can start.
|
|
|
|
|
CooldownEndTime time.Time `json:"cooldown_end_time"`
|
|
|
|
|
|
|
|
|
|
// The UTC date and time (in RFC3339 format) that the Hype Train ends.
|
|
|
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
|
|
|
|
|
|
|
|
// The value needed to reach the next level.
|
|
|
|
|
Goal int `json:"goal"`
|
|
|
|
|
|
|
|
|
|
// An ID that identifies this Hype Train.
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
|
|
|
|
|
// The most recent contribution towards the Hype Train’s goal.
|
|
|
|
|
LastContribution Contribution `json:"last_contribution"`
|
|
|
|
|
|
|
|
|
|
// The highest level that the Hype Train reached (the levels are 1 through 5).
|
|
|
|
|
Level int `json:"level"`
|
|
|
|
|
|
|
|
|
|
// The UTC date and time (in RFC3339 format) that this Hype Train started.
|
|
|
|
|
StartedAt time.Time `json:"started_at"`
|
|
|
|
|
|
|
|
|
|
// The top contributors for each contribution type.
|
|
|
|
|
// For example, the top contributor using BITS (by aggregate) and the top contributor using SUBS (by count).
|
|
|
|
|
TopContributions []Contribution `json:"top_contributions"`
|
|
|
|
|
|
|
|
|
|
// The total amount raised.
|
|
|
|
|
Total int `json:"total"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Contribution struct {
|
|
|
|
|
// The total amount contributed.
|
|
|
|
|
//
|
|
|
|
|
// If type is BITS, total represents the amount of Bits used.
|
|
|
|
|
//
|
|
|
|
|
// If type is SUBS, total is 500, 1000, or 2500 to represent tier 1, 2, or 3 subscriptions, respectively.
|
|
|
|
|
Total int `json:"total"`
|
|
|
|
|
|
|
|
|
|
// The contribution method used.
|
|
|
|
|
//
|
|
|
|
|
// Possible values are:
|
|
|
|
|
//
|
|
|
|
|
// BITS — Cheering with Bits.
|
|
|
|
|
//
|
|
|
|
|
// SUBS — Subscription activity like subscribing or gifting subscriptions.
|
|
|
|
|
//
|
|
|
|
|
// OTHER — Covers other contribution methods not listed.
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
|
|
|
|
|
// The ID of the user that made the contribution.
|
|
|
|
|
User string `json:"user"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetHypeTrainEventsResponse struct {
|
|
|
|
|
// The list of Hype Train events.
|
|
|
|
|
// The list is empty if the broadcaster hasn’t run a Hype Train within the last 5 days.
|
|
|
|
|
Data []Event `json:"data"`
|
|
|
|
|
|
|
|
|
|
// Contains the information used to page through the list of results. The object is empty if there are no more pages left to page through.
|
|
|
|
|
// Read More: https://dev.twitch.tv/docs/api/guide#pagination
|
|
|
|
|
Pagination types.Pagination `json:"pagination"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gets information about the broadcaster’s current or most recent Hype Train event.
|
|
|
|
|
//
|
|
|
|
|
// Instead of polling for events, consider subscribing to Hype Train events (Begin, Progress, End).
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the channel:read:hype_train scope.
|
|
|
|
|
func (h *Hypetrain) GetHypeTrainEvents(ctx context.Context, params *GetHypeTrainEventsParams) (*GetHypeTrainEventsResponse, error) {
|
|
|
|
|
v, _ := query.Values(params)
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.Make(h.baseUrl, "hypetrain/events", v), nil)
|
2024-03-03 14:16:33 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := h.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to get Hype Train events (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 14:16:33 -05:00
|
|
|
|
var data GetHypeTrainEventsResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|