parent
780d258cb4
commit
1bb87788f8
@ -1,109 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type TwitchSubscriptions struct {
|
||||
Total int64 `json:"_total"`
|
||||
Subscriptions []*TwitchSubscription `json:"subscriptions"`
|
||||
Data []*TwitchSubscription `json:"data"`
|
||||
Pagination *TwitchPagination `json:"pagination"`
|
||||
Total int64 `json:"total"`
|
||||
Points int64 `json:"points"`
|
||||
}
|
||||
|
||||
type TwitchSubscription struct {
|
||||
ID string `json:"_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
IsGift bool `json:"is_gift"`
|
||||
SubPlan string `json:"sub_plan"`
|
||||
SubPlanName string `json:"sub_plan_name"`
|
||||
User *TwitchSubUser `json:"user"`
|
||||
}
|
||||
|
||||
type TwitchSubUser struct {
|
||||
ID string `json:"_id"`
|
||||
Bio string `json:"bio"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Logo string `json:"logo"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
BroadcasterID string `json:"broadcaster_id"`
|
||||
BroadcasterLogin string `json:"broadcaster_login"`
|
||||
BroadcasterName string `json:"broadcaster_name"`
|
||||
GifterID string `json:"gifter_id"`
|
||||
GifterLogin string `json:"gifter_login"`
|
||||
GifterName string `json:"gifter_name"`
|
||||
IsGift bool `json:"is_gift"`
|
||||
Tier string `json:"gift"`
|
||||
PlanName string `json:"plan_name"`
|
||||
UserID string `json:"user_id"`
|
||||
UserLogin string `json:"user_login"`
|
||||
UserName string `json:"user_name"`
|
||||
}
|
||||
|
||||
func getSubs(u *User, max int) {
|
||||
result := &TwitchSubscriptions{}
|
||||
|
||||
limit := 100
|
||||
offset := 0
|
||||
|
||||
after := ""
|
||||
for {
|
||||
client := http.Client{}
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.twitch.tv/kraken/channels/%s/subscriptions?limit=%d&offset=%d", u.TwitchChannel.ID, limit, offset), nil)
|
||||
client := twitchOauthConfig.Client(context.Background(), u.Token)
|
||||
req, err := http.NewRequest("GET", "https://api.twitch.tv/helix/subscriptions?broadcaster_id="+u.ID+after, nil)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Unable to create http request to get twitch subs data")
|
||||
log.WithError(err).Error("Unable to create http request to get twitch streams data")
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Set("Client-ID", settings.ClientID)
|
||||
req.Header.Set("Accept", "application/vnd.twitchtv.v5+json")
|
||||
req.Header.Set("Authorization", "OAuth "+u.Token.AccessToken)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Unable to get twitch subs data")
|
||||
log.WithError(err).Error("Unable to get twitch stream data")
|
||||
return
|
||||
}
|
||||
|
||||
t := &TwitchSubscriptions{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&t); err != nil {
|
||||
log.WithError(err).Error("Unable to parse twitch followers data")
|
||||
log.WithError(err).Error("Unable to parse twitch streams data")
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
if len(t.Subscriptions) == 0 {
|
||||
if len(t.Data) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
result.Total = t.Total
|
||||
result.Subscriptions = append(result.Subscriptions, t.Subscriptions...)
|
||||
|
||||
if max > -1 && len(result.Subscriptions) >= max {
|
||||
result.Data = append(result.Data, t.Data...)
|
||||
if t.Pagination == nil || t.Pagination.Cursor == "" {
|
||||
break
|
||||
}
|
||||
offset += limit
|
||||
after = "&after=" + t.Pagination.Cursor
|
||||
}
|
||||
|
||||
if len(result.Subscriptions) < 1 {
|
||||
log.Info("No Subs")
|
||||
if len(result.Data) < 1 {
|
||||
log.Info("No streams")
|
||||
return
|
||||
}
|
||||
|
||||
u.TwitchSubscriptions = result
|
||||
if len(result.Data) > 0 {
|
||||
u.TwitchSubscriptions = result
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TwitchSubscriptions) SaveFiles() {
|
||||
saveContent("subscriptions", "total", strconv.FormatInt(s.Total, 10))
|
||||
saveJSON("subscriptions", "complete_list", s)
|
||||
|
||||
sort.Slice(s.Subscriptions, func(i, j int) bool {
|
||||
return s.Subscriptions[i].CreatedAt.Before(s.Subscriptions[j].CreatedAt)
|
||||
})
|
||||
start := len(s.Subscriptions) - 10
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
lastSubs := s.Subscriptions[start:]
|
||||
|
||||
var lastSubsSlice []string
|
||||
for _, v := range lastSubs {
|
||||
lastSubsSlice = append(lastSubsSlice, v.User.DisplayName)
|
||||
}
|
||||
|
||||
saveContent("subscriptions", "last_ten", strings.Join(lastSubsSlice, "\n"))
|
||||
}
|
||||
|
Loading…
Reference in new issue