2023-04-22 20:23:35 +00:00
|
|
|
package twitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const subInfoCacheTimeout = 300 * time.Second
|
|
|
|
|
|
|
|
type (
|
|
|
|
subInfo struct {
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
Points int64 `json:"points"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetBroadcasterSubscriptionCount gets a list of users that subscribe to the specified broadcaster.
|
|
|
|
func (c *Client) GetBroadcasterSubscriptionCount(ctx context.Context, broadcasterName string) (subCount, subPoints int64, err error) {
|
|
|
|
cacheKey := []string{"broadcasterSubscriptionCountByChannel", broadcasterName}
|
|
|
|
if d := c.apiCache.Get(cacheKey); d != nil {
|
|
|
|
data := d.(subInfo)
|
|
|
|
return data.Total, data.Points, nil
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
broadcaster, err := c.GetIDForUsername(ctx, broadcasterName)
|
2023-04-22 20:23:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, errors.Wrap(err, "getting ID for broadcaster name")
|
|
|
|
}
|
|
|
|
|
|
|
|
var data subInfo
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
if err = c.Request(ctx, ClientRequestOpts{
|
2023-07-01 14:48:21 +00:00
|
|
|
AuthType: AuthTypeBearerToken,
|
2023-04-22 20:23:35 +00:00
|
|
|
Method: http.MethodGet,
|
|
|
|
OKStatus: http.StatusOK,
|
|
|
|
Out: &data,
|
|
|
|
URL: fmt.Sprintf("https://api.twitch.tv/helix/subscriptions?broadcaster_id=%s", broadcaster),
|
|
|
|
}); err != nil {
|
|
|
|
return 0, 0, errors.Wrap(err, "executing request")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lets not annoy the API but only ask every 5m
|
|
|
|
c.apiCache.Set(cacheKey, subInfoCacheTimeout, data)
|
|
|
|
|
|
|
|
return data.Total, data.Points, nil
|
|
|
|
}
|