Add optional delay between API requests
This commit is contained in:
parent
1c3fd0e0c9
commit
01ac1b672c
3 changed files with 33 additions and 0 deletions
|
|
@ -176,6 +176,14 @@ if err != nil {
|
|||
}
|
||||
```
|
||||
|
||||
### Delay requests
|
||||
|
||||
Add delay between API requests (in seconds)
|
||||
|
||||
```golang
|
||||
scraper.WithDelay(5)
|
||||
```
|
||||
|
||||
### Load timeline with tweet replies
|
||||
|
||||
```golang
|
||||
|
|
|
|||
11
api.go
11
api.go
|
|
@ -12,6 +12,17 @@ const bearerToken string = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xn
|
|||
|
||||
// RequestAPI get JSON from frontend API and decodes it
|
||||
func (s *Scraper) RequestAPI(req *http.Request, target interface{}) error {
|
||||
s.wg.Wait()
|
||||
if s.delay > 0 {
|
||||
defer func() {
|
||||
s.wg.Add(1)
|
||||
go func() {
|
||||
time.Sleep(time.Second * time.Duration(s.delay))
|
||||
s.wg.Done()
|
||||
}()
|
||||
}()
|
||||
}
|
||||
|
||||
if s.guestToken == "" || s.guestCreatedAt.Before(time.Now().Add(-time.Hour*3)) {
|
||||
err := s.GetGuestToken()
|
||||
if err != nil {
|
||||
|
|
|
|||
14
scraper.go
14
scraper.go
|
|
@ -7,16 +7,19 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Scraper object
|
||||
type Scraper struct {
|
||||
client *http.Client
|
||||
delay int64
|
||||
guestToken string
|
||||
guestCreatedAt time.Time
|
||||
includeReplies bool
|
||||
searchMode SearchMode
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// SearchMode type
|
||||
|
|
@ -55,6 +58,17 @@ func SetSearchMode(mode SearchMode) *Scraper {
|
|||
return defaultScraper.SetSearchMode(mode)
|
||||
}
|
||||
|
||||
// WithDelay add delay between API requests (in seconds)
|
||||
func (s *Scraper) WithDelay(seconds int64) *Scraper {
|
||||
s.delay = seconds
|
||||
return s
|
||||
}
|
||||
|
||||
// WithDelay wrapper for default Scraper
|
||||
func WithDelay(seconds int64) *Scraper {
|
||||
return defaultScraper.WithDelay(seconds)
|
||||
}
|
||||
|
||||
// WithReplies enable/disable load timeline with tweet replies
|
||||
func (s *Scraper) WithReplies(b bool) *Scraper {
|
||||
s.includeReplies = b
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue