package main import ( "bytes" "context" "encoding/json" "io/ioutil" "net/http" "os" "strings" "time" log "github.com/sirupsen/logrus" ) type TwitchFollowers struct { Total int64 `json:"total"` Data []*TwitchFollower `json:"data"` Pagination *TwitchPagination `json:"pagination"` } type TwitchPagination struct { Cursor string `json:"cursor"` } type TwitchFollower struct { FromID string `json:"from_id"` FromName string `json:"from_name"` ToID string `json:"to_id"` ToName string `json:"to_name"` FollowedAt time.Time `json:"followed_at"` } func getFollows(u *User, max int) { result := &TwitchFollowers{} after := "" for { client := twitchOauthConfig.Client(context.Background(), u.Token) req, err := http.NewRequest("GET", "https://api.twitch.tv/helix/users/follows?to_id="+u.ID+after, nil) // req, err := http.NewRequest("GET", "https://api.twitch.tv/helix/users/follows?to_id=276021569"+after, nil) if err != nil { log.WithError(err).Error("Unable to create http request to get twitch follower 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 follower data") return } t := &TwitchFollowers{} if err := json.NewDecoder(resp.Body).Decode(&t); err != nil { log.WithError(err).Error("Unable to parse twitch followers data") } resp.Body.Close() if len(t.Data) == 0 { break } result.Total = t.Total result.Data = append(result.Data, t.Data...) if t.Pagination == nil || t.Pagination.Cursor == "" { break } if max > -1 && len(result.Data) >= max { break } after = "&after=" + t.Pagination.Cursor } if len(result.Data) < 1 { log.Info("No followers") return } u.TwitchFollowers = result } func readActiveUsers() map[string]bool { res, err := ioutil.ReadFile(os.Args[1]) if err != nil { log.WithError(err).Fatal("Unable to read active users") } data := make(map[string]bool) if err := json.NewDecoder(bytes.NewBuffer(res)).Decode(&data); err != nil { log.WithError(err).Fatal("Unable to decode json") } return data } func (f *TwitchFollowers) SaveFiles() { fs := []string{} active := readActiveUsers() ids := []string{} for _, follower := range f.Data { name := strings.ToLower(follower.FromName) id := follower.FromID if _, ok := active[name]; ok { continue } fs = append(fs, name) ids = append(ids, id) } log.Infof("We have %d inactive followers", len(fs)) saveJSON("followers", "inactive", fs) saveContent("followers", "inactive_ids", strings.Join(ids, "\n")) }