diff --git a/follower.go b/follower.go index 2b506dc..652ee83 100644 --- a/follower.go +++ b/follower.go @@ -30,7 +30,7 @@ type TwitchFollower struct { FollowedAt time.Time `json:"followed_at"` } -func getFollows(u *User) { +func getFollows(u *User, max int) { result := &TwitchFollowers{} after := "" @@ -64,6 +64,11 @@ func getFollows(u *User) { if t.Pagination == nil || t.Pagination.Cursor == "" { break } + + if max > -1 && len(result.Data) >= max { + break + } + after = "&after=" + t.Pagination.Cursor } diff --git a/main.go b/main.go index e18aeb8..92baf70 100644 --- a/main.go +++ b/main.go @@ -48,7 +48,12 @@ func main() { if user != nil { handleSaves() } - c := time.Tick(20 * time.Second) + interval := 20 * time.Second + if settings.UpdateInterval > 10*time.Second { + interval = settings.UpdateInterval + } + log.Infof("Update interval: %s", interval) + c := time.Tick(interval) for range c { if user == nil { continue @@ -77,12 +82,22 @@ func handleSaves() { user.TwitchChannel.SaveFiles() } - getFollows(user) + maxFollower := -1 + if settings.MaxFollowers > 0 { + maxFollower = settings.MaxFollowers + } + log.Infof("Fetch max follower: %d", maxFollower) + getFollows(user, maxFollower) if user.TwitchFollowers != nil { user.TwitchFollowers.SaveFiles() } - getSubs(user) + maxSubs := -1 + if settings.MaxSubs > 0 { + maxSubs = settings.MaxSubs + } + log.Infof("Fetch max subs: %d", maxSubs) + getSubs(user, maxSubs) if user.TwitchSubscriptions != nil { user.TwitchSubscriptions.SaveFiles() } diff --git a/settings.go b/settings.go index 7d3e4a5..b77d711 100644 --- a/settings.go +++ b/settings.go @@ -17,11 +17,14 @@ var ( ) type Settings struct { - ClientID string `yaml:"client_id"` - ClientSecret string `yaml:"client_secret"` - RedirectURL string `yaml:"redirect_url"` - VerificationToken string `yaml:"verification_token"` - WebserverPort string `yaml:"webserver_port"` + ClientID string `yaml:"client_id"` + ClientSecret string `yaml:"client_secret"` + MaxFollowers int `yaml:"max_followers"` + MaxSubs int `yaml:"max_subs"` + RedirectURL string `yaml:"redirect_url"` + UpdateInterval time.Duration `yaml:"update_interval"` + VerificationToken string `yaml:"verification_token"` + WebserverPort string `yaml:"webserver_port"` } func loadSettings() { diff --git a/subs.go b/subs.go index b2e0b8f..d603439 100644 --- a/subs.go +++ b/subs.go @@ -37,7 +37,7 @@ type TwitchSubUser struct { UpdatedAt time.Time `json:"updated_at"` } -func getSubs(u *User) { +func getSubs(u *User, max int) { result := &TwitchSubscriptions{} limit := 100 @@ -74,6 +74,9 @@ func getSubs(u *User) { result.Total = t.Total result.Subscriptions = append(result.Subscriptions, t.Subscriptions...) + if max > -1 && len(result.Subscriptions) >= max { + break + } offset += limit }