Commit 9dd701e5 authored by vicotor's avatar vicotor

profile get followers

parent 17221a8d
...@@ -18,7 +18,7 @@ func generateTOTP(secret string) (string, error) { ...@@ -18,7 +18,7 @@ func generateTOTP(secret string) (string, error) {
return totp.GenerateCode(secret, time.Now()) return totp.GenerateCode(secret, time.Now())
} }
func GetLoginAccount() ([]*twitterscraper.Scraper, error) { func GetLoginAccount() ([]ScraperWithTimer, error) {
accounts, err := GetAvailableAccounts() accounts, err := GetAvailableAccounts()
...@@ -28,7 +28,7 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) { ...@@ -28,7 +28,7 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) {
fmt.Println("len(accounts)", len(accounts)) fmt.Println("len(accounts)", len(accounts))
res := make([]*twitterscraper.Scraper, 0, len(accounts)) res := make([]ScraperWithTimer, 0, len(accounts))
for _, v := range accounts { for _, v := range accounts {
...@@ -49,7 +49,10 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) { ...@@ -49,7 +49,10 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) {
if !scraper.IsLoggedIn() { if !scraper.IsLoggedIn() {
needLogin = true needLogin = true
} else { } else {
res = append(res, scraper) res = append(res, ScraperWithTimer{
Scraper: scraper,
AccountInfo: v,
})
continue continue
} }
} else { } else {
...@@ -80,7 +83,10 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) { ...@@ -80,7 +83,10 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) {
continue continue
} }
res = append(res, scraper) res = append(res, ScraperWithTimer{
Scraper: scraper,
AccountInfo: v,
})
} }
} }
...@@ -89,9 +95,8 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) { ...@@ -89,9 +95,8 @@ func GetLoginAccount() ([]*twitterscraper.Scraper, error) {
type ScraperWithTimer struct { type ScraperWithTimer struct {
*twitterscraper.Scraper *twitterscraper.Scraper
Timer *time.Timer AccountInfo Account
Email string Timer *time.Timer
UserName string
} }
var accChan chan ScraperWithTimer = make(chan ScraperWithTimer, 20) var accChan chan ScraperWithTimer = make(chan ScraperWithTimer, 20)
...@@ -166,28 +171,45 @@ func (f *FollowerRateLimit) Follower(userName string, cursor string) ([]*twitter ...@@ -166,28 +171,45 @@ func (f *FollowerRateLimit) Follower(userName string, cursor string) ([]*twitter
if err := f.RateLimit.Wait(ctx); err != nil { // This is a blocking call. if err := f.RateLimit.Wait(ctx); err != nil { // This is a blocking call.
return nil, "", nil, err return nil, "", nil, err
} }
var (
history = make(map[string]bool)
users []*twitterscraper.Profile
res []*twitter.UserObj
next string
err error
success bool = false
try = 0
)
for !success && try < 10 {
select {
case account := <-accChan:
accChan <- account
history[account.AccountInfo.Username] = true
if _, exist := history[account.AccountInfo.Username]; exist {
// loop all account, exit.
try = 100
break
}
if users, next, err = account.FetchFollowers(userName, 1000, cursor); err != nil {
slog.Error("FetchFollowers", "failed", err.Error())
continue
}
select { success = true
case account := <-accChan: res = make([]*twitter.UserObj, 0, len(users))
users, next, err := account.FetchFollowers(userName, 1000, cursor)
if err != nil {
slog.Error(err.Error())
}
res := make([]*twitter.UserObj, 0, len(users))
for _, v := range users { for _, v := range users {
sDec, _ := base64.StdEncoding.DecodeString(v.UserID) sDec, _ := base64.StdEncoding.DecodeString(v.UserID)
userId, _ := strings.CutPrefix(string(sDec), "User:") userId, _ := strings.CutPrefix(string(sDec), "User:")
res = append(res, &twitter.UserObj{ res = append(res, &twitter.UserObj{
ID: userId, ID: userId,
Name: v.Name, Name: v.Name,
UserName: v.Username, UserName: v.Username,
}) })
}
} }
accChan <- account
return res, next, nil, err
} }
return res, next, nil, err
} }
package main package main
import ( import (
_ "code.wuban.net.cn/odysseus/twitter_syncer/docs"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log/slog" "log/slog"
"time"
_ "code.wuban.net.cn/odysseus/twitter_syncer/docs"
// docs are generated by Swag CLI, you have to import them. // docs are generated by Swag CLI, you have to import them.
// replace with your own docs folder, usually "github.com/username/reponame/docs" // replace with your own docs folder, usually "github.com/username/reponame/docs"
//_ "github.com/gofiber/swagger/example/docs" //_ "github.com/gofiber/swagger/example/docs"
...@@ -42,12 +40,9 @@ func init() { ...@@ -42,12 +40,9 @@ func init() {
slog.Error(err.Error()) slog.Error(err.Error())
} }
for k, v := range accounts { for _, v := range accounts {
//v.Timer = time.NewTimer(time.Duration(k) * time.Duration(5) * time.Minute) //v.Timer = time.NewTimer(time.Duration(k) * time.Duration(5) * time.Minute)
accChan <- ScraperWithTimer{ accChan <- v
Scraper: v,
Timer: time.NewTimer(time.Duration(k) * time.Duration(5) * time.Minute),
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment