Merge branch 'login'

This commit is contained in:
Valentine 2024-10-01 01:41:12 +03:00
commit ec8e559b93
5 changed files with 119 additions and 37 deletions

23
auth.go
View file

@ -14,6 +14,8 @@ import (
"strconv"
"strings"
"time"
"math/rand"
)
const (
@ -84,7 +86,7 @@ func (s *Scraper) getFlow(data map[string]interface{}) (*flow, error) {
headers := http.Header{
"Authorization": []string{"Bearer " + s.bearerToken},
"Content-Type": []string{"application/json"},
"User-Agent": []string{"TwitterAndroid/99"},
"User-Agent": []string{s.userAgent},
"X-Guest-Token": []string{s.guestToken},
"X-Twitter-Auth-Type": []string{"OAuth2Client"},
"X-Twitter-Active-User": []string{"yes"},
@ -160,6 +162,12 @@ func (s *Scraper) IsLoggedIn() bool {
return s.isLogged
}
// randomDelay introduces a random delay between 1 and 3 seconds
func randomDelay() {
delay := time.Duration(3000+rand.Intn(5000)) * time.Millisecond
time.Sleep(delay)
}
// Login to Twitter
// Use Login(username, password) for ordinary login
// or Login(username, password, email) for login if you have email confirmation
@ -182,6 +190,8 @@ func (s *Scraper) Login(credentials ...string) error {
return err
}
randomDelay()
// flow start
data := map[string]interface{}{
"flow_name": "login",
@ -197,6 +207,8 @@ func (s *Scraper) Login(credentials ...string) error {
return err
}
randomDelay()
// flow instrumentation step
data = map[string]interface{}{
"flow_token": flowToken,
@ -212,6 +224,8 @@ func (s *Scraper) Login(credentials ...string) error {
return err
}
randomDelay()
// flow username step
data = map[string]interface{}{
"flow_token": flowToken,
@ -235,6 +249,8 @@ func (s *Scraper) Login(credentials ...string) error {
return err
}
randomDelay()
// flow password step
data = map[string]interface{}{
"flow_token": flowToken,
@ -250,6 +266,8 @@ func (s *Scraper) Login(credentials ...string) error {
return err
}
randomDelay()
// flow duplication check
data = map[string]interface{}{
"flow_token": flowToken,
@ -273,6 +291,9 @@ func (s *Scraper) Login(credentials ...string) error {
if confirmation == "" {
return fmt.Errorf("confirmation data required for %v", confirmationSubtask)
}
randomDelay()
// flow confirmation
data = map[string]interface{}{
"flow_token": flowToken,