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"` FollowedAt time.Time `json:"followed_at"`
} }
func getFollows(u *User) { func getFollows(u *User, max int) {
result := &TwitchFollowers{} result := &TwitchFollowers{}
after := "" after := ""
@ -64,6 +64,11 @@ func getFollows(u *User) {
if t.Pagination == nil || t.Pagination.Cursor == "" { if t.Pagination == nil || t.Pagination.Cursor == "" {
break break
} }
if max > -1 && len(result.Data) >= max {
break
}
after = "&after=" + t.Pagination.Cursor after = "&after=" + t.Pagination.Cursor
} }

21
main.go
View File

@ -48,7 +48,12 @@ func main() {
if user != nil { if user != nil {
handleSaves() 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 { for range c {
if user == nil { if user == nil {
continue continue
@ -77,12 +82,22 @@ func handleSaves() {
user.TwitchChannel.SaveFiles() 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 { if user.TwitchFollowers != nil {
user.TwitchFollowers.SaveFiles() 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 { if user.TwitchSubscriptions != nil {
user.TwitchSubscriptions.SaveFiles() user.TwitchSubscriptions.SaveFiles()
} }

View File

@ -17,11 +17,14 @@ var (
) )
type Settings struct { type Settings struct {
ClientID string `yaml:"client_id"` ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"` ClientSecret string `yaml:"client_secret"`
RedirectURL string `yaml:"redirect_url"` MaxFollowers int `yaml:"max_followers"`
VerificationToken string `yaml:"verification_token"` MaxSubs int `yaml:"max_subs"`
WebserverPort string `yaml:"webserver_port"` RedirectURL string `yaml:"redirect_url"`
UpdateInterval time.Duration `yaml:"update_interval"`
VerificationToken string `yaml:"verification_token"`
WebserverPort string `yaml:"webserver_port"`
} }
func loadSettings() { func loadSettings() {

View File

@ -37,7 +37,7 @@ type TwitchSubUser struct {
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
} }
func getSubs(u *User) { func getSubs(u *User, max int) {
result := &TwitchSubscriptions{} result := &TwitchSubscriptions{}
limit := 100 limit := 100
@ -74,6 +74,9 @@ func getSubs(u *User) {
result.Total = t.Total result.Total = t.Total
result.Subscriptions = append(result.Subscriptions, t.Subscriptions...) result.Subscriptions = append(result.Subscriptions, t.Subscriptions...)
if max > -1 && len(result.Subscriptions) >= max {
break
}
offset += limit offset += limit
} }