Skip to content

Commit

Permalink
feat(twitter): Implement random sleep and improve login process (#601)
Browse files Browse the repository at this point in the history
- Add RandomSleep function to introduce variability in request timing
- Update NewScraper to use RandomSleep before and after login attempts
- Adjust sleep duration range to 500ms - 2s for more natural behavior
- Improve error handling and logging in the login process
  • Loading branch information
teslashibe authored Oct 15, 2024
1 parent 01ec8c4 commit a8a77a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/scrapers/twitter/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func NewScraper(account *TwitterAccount, cookieDir string) *Scraper {
}
}

ShortSleep()
RandomSleep()

if err := scraper.Login(account.Username, account.Password, account.TwoFACode); err != nil {
logrus.WithError(err).Warnf("Login failed for %s", account.Username)
return nil
}

ShortSleep()
RandomSleep()

if err := SaveCookies(scraper.Scraper, account, cookieDir); err != nil {
logrus.WithError(err).Errorf("Failed to save cookies for %s", account.Username)
Expand Down
22 changes: 18 additions & 4 deletions pkg/scrapers/twitter/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package twitter

import (
"math/rand"
"time"

"github.com/sirupsen/logrus"
)

const (
ShortSleepDuration = 20 * time.Millisecond
RateLimitDuration = 15 * time.Minute
minSleepDuration = 500 * time.Millisecond
maxSleepDuration = 2 * time.Second
RateLimitDuration = 15 * time.Minute
)

var (
rng *rand.Rand
)

func ShortSleep() {
time.Sleep(ShortSleepDuration)
func init() {
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
}

func RandomSleep() {
duration := minSleepDuration + time.Duration(rng.Int63n(int64(maxSleepDuration-minSleepDuration)))
logrus.Debugf("Sleeping for %v", duration)
time.Sleep(duration)
}

func GetRateLimitDuration() time.Duration {
Expand Down

0 comments on commit a8a77a6

Please sign in to comment.