limit requests

This commit is contained in:
Martin Thielecke 2020-08-19 23:26:26 +02:00
parent 9f71865570
commit 79f7b617da
Signed by: mthie
GPG Key ID: D1D25A85C8604DFB
4 changed files with 36 additions and 10 deletions

View File

@ -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
}

21
main.go
View File

@ -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()
}

View File

@ -19,7 +19,10 @@ var (
type Settings struct {
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"`
}

View File

@ -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
}