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
|
### Load timeline with tweet replies
|
||||||
|
|
||||||
```golang
|
```golang
|
||||||
|
|
|
||||||
11
api.go
11
api.go
|
|
@ -12,6 +12,17 @@ const bearerToken string = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xn
|
||||||
|
|
||||||
// RequestAPI get JSON from frontend API and decodes it
|
// RequestAPI get JSON from frontend API and decodes it
|
||||||
func (s *Scraper) RequestAPI(req *http.Request, target interface{}) error {
|
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)) {
|
if s.guestToken == "" || s.guestCreatedAt.Before(time.Now().Add(-time.Hour*3)) {
|
||||||
err := s.GetGuestToken()
|
err := s.GetGuestToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
14
scraper.go
14
scraper.go
|
|
@ -7,16 +7,19 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Scraper object
|
// Scraper object
|
||||||
type Scraper struct {
|
type Scraper struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
delay int64
|
||||||
guestToken string
|
guestToken string
|
||||||
guestCreatedAt time.Time
|
guestCreatedAt time.Time
|
||||||
includeReplies bool
|
includeReplies bool
|
||||||
searchMode SearchMode
|
searchMode SearchMode
|
||||||
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchMode type
|
// SearchMode type
|
||||||
|
|
@ -55,6 +58,17 @@ func SetSearchMode(mode SearchMode) *Scraper {
|
||||||
return defaultScraper.SetSearchMode(mode)
|
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
|
// WithReplies enable/disable load timeline with tweet replies
|
||||||
func (s *Scraper) WithReplies(b bool) *Scraper {
|
func (s *Scraper) WithReplies(b bool) *Scraper {
|
||||||
s.includeReplies = b
|
s.includeReplies = b
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue