84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type TwitchChannels struct {
|
|
Data []*TwitchChannel `json:"data"`
|
|
Pagination *TwitchPagination `json:"pagination"`
|
|
}
|
|
|
|
type TwitchChannel struct {
|
|
BroadcasterID string `json:"broadcaster_id"`
|
|
BroadcasterLogin string `json:"broadcaster_login"`
|
|
BroadcasterName string `json:"broadcaster_name"`
|
|
BroadcasterLanguage string `json:"broadcaster_language"`
|
|
GameID string `json:"game_id"`
|
|
GameName string `json:"game_name"`
|
|
Title string `json:"title"`
|
|
Delay int `json:"delay"`
|
|
}
|
|
|
|
func getChannel(u *User) {
|
|
result := &TwitchChannels{}
|
|
|
|
after := ""
|
|
for {
|
|
client := twitchOauthConfig.Client(context.Background(), u.Token)
|
|
req, err := http.NewRequest("GET", "https://api.twitch.tv/helix/channels?broadcaster_id="+u.ID+after, nil)
|
|
if err != nil {
|
|
log.WithError(err).Error("Unable to create http request to get twitch streams data")
|
|
return
|
|
}
|
|
req.Header.Set("Client-ID", settings.ClientID)
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.WithError(err).Error("Unable to get twitch stream data")
|
|
return
|
|
}
|
|
|
|
t := &TwitchChannels{}
|
|
if err := json.NewDecoder(resp.Body).Decode(&t); err != nil {
|
|
log.WithError(err).Error("Unable to parse twitch streams data")
|
|
}
|
|
resp.Body.Close()
|
|
|
|
if len(t.Data) == 0 {
|
|
break
|
|
}
|
|
|
|
result.Data = append(result.Data, t.Data...)
|
|
if t.Pagination == nil || t.Pagination.Cursor == "" {
|
|
break
|
|
}
|
|
after = "&after=" + t.Pagination.Cursor
|
|
}
|
|
|
|
if len(result.Data) < 1 {
|
|
log.Info("No channels")
|
|
return
|
|
}
|
|
|
|
if len(result.Data) > 0 {
|
|
u.TwitchChannel = result.Data[0]
|
|
}
|
|
}
|
|
|
|
func (c *TwitchChannel) SaveFiles() {
|
|
data, err := fieldsToMap(c)
|
|
if err != nil {
|
|
log.WithError(err).Error("Unable to convert channel to map")
|
|
return
|
|
}
|
|
|
|
for k, v := range data {
|
|
saveContent("channel", k, v)
|
|
}
|
|
}
|