Middlewares refactored to multiple files in middleware package.

Extractors removed as they introduce complexity to scraper. Both in learning and developing.
This commit is contained in:
Musab Gültekin
2019-07-04 21:04:29 +03:00
parent 9adff75509
commit 2cab68d2ce
19 changed files with 202 additions and 304 deletions

30
middleware/delay.go Normal file
View File

@ -0,0 +1,30 @@
package middleware
import (
"github.com/geziyor/geziyor/client"
"math/rand"
"time"
)
// delay delays requests
type delay struct {
requestDelayRandomize bool
requestDelay time.Duration
}
func NewDelay(requestDelayRandomize bool, requestDelay time.Duration) RequestProcessor {
if requestDelayRandomize {
rand.Seed(time.Now().UnixNano())
}
return &delay{requestDelayRandomize: requestDelayRandomize, requestDelay: requestDelay}
}
func (a *delay) ProcessRequest(r *client.Request) {
if a.requestDelayRandomize {
min := float64(a.requestDelay) * 0.5
max := float64(a.requestDelay) * 1.5
time.Sleep(time.Duration(rand.Intn(int(max-min)) + int(min)))
} else {
time.Sleep(a.requestDelay)
}
}