Documentation
¶
Index ¶
- Constants
- Variables
- func New(options ...func(*scrapeMate) error) (*scrapeMate, error)
- func WithConcurrency(concurrency int) func(*scrapeMate) error
- func WithContext(ctx context.Context, cancelFn context.CancelCauseFunc) func(*scrapeMate) error
- func WithFailed() func(*scrapeMate) error
- func WithHtmlParser(parser HtmlParser) func(*scrapeMate) error
- func WithHttpFetcher(client HttpFetcher) func(*scrapeMate) error
- func WithJobProvider(provider JobProvider) func(*scrapeMate) error
- func WithLogger(log logging.Logger) func(*scrapeMate) error
- type HtmlParser
- type HttpFetcher
- type IJob
- type Job
- func (j *Job) BrowserActions(browser playwright.Browser) Response
- func (j *Job) DoCheckResponse(resp Response) bool
- func (j *Job) DoScreenshot() bool
- func (j *Job) GetBody() []byte
- func (j *Job) GetHeaders() map[string]string
- func (j *Job) GetID() string
- func (j *Job) GetMaxRetries() int
- func (j *Job) GetMaxRetryDelay() time.Duration
- func (j *Job) GetMethod() string
- func (j *Job) GetPriority() int
- func (j *Job) GetRetryPolicy() RetryPolicy
- func (j *Job) GetTimeout() time.Duration
- func (j *Job) GetURL() string
- func (j *Job) GetUrlParams() map[string]string
- func (j *Job) Process(ctx context.Context, resp Response) (any, []IJob, error)
- func (j *Job) String() string
- type JobProvider
- type Response
- type Result
- type RetryPolicy
Constants ¶
const ( // DefaultUserAgent is the default user agent scrape mate uses DefaultUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.79 Safari/537.36" // RetryJob retry a job RetryJob = 0 // DiscardJob just discard it in case crawling fails DiscardJob = 1 // RefreshIP refresh the api and then retry job RefreshIP = 2 // StopScraping exit scraping completely when an error happens StopScraping = 3 // DefaultMaxRetryDelay the default max delay between 2 consequive retries DefaultMaxRetryDelay = 2 * time.Second )
Variables ¶
var ( // ErrorNoJobProvider returned when you do not set a job provider in initialization ErrorNoJobProvider = errors.New("no job provider set") // ErroExitSignal is returned when scrapemate exits because of a system interrupt ErrorExitSignal = errors.New("exit signal received") // ErrorNoLogger returned when you try to initialize it with a nil logger ErrorNoLogger = errors.New("no logger set") // ErrorNoContext returned when you try to initialized it with a nil context ErrorNoContext = errors.New("no context set") // ErrorConcurrency returned when you try to initialize it with concurrency <1 ErrorConcurrency = errors.New("concurrency must be greater than 0") // ErrorNoHttpFetcher returned when you try to initialize with a nil httpFetcher ErrorNoHttpFetcher = errors.New("no http fetcher set") // ErrorNoHtmlParser returned when you try to initialized with a nil HtmlParser ErrorNoHtmlParser = errors.New("no html parser set") )
Functions ¶
func WithConcurrency ¶
WithConcurrency sets the concurrency for the scrapemate
func WithContext ¶
func WithContext(ctx context.Context, cancelFn context.CancelCauseFunc) func(*scrapeMate) error
WithContext sets the context for the scrapemate
func WithFailed ¶
func WithFailed() func(*scrapeMate) error
WithFailed sets the failed jobs channel for the scrapemate
func WithHtmlParser ¶
func WithHtmlParser(parser HtmlParser) func(*scrapeMate) error
WithHtmlParser sets the html parser for the scrapemate
func WithHttpFetcher ¶
func WithHttpFetcher(client HttpFetcher) func(*scrapeMate) error
WithHttpFetcher sets the http fetcher for the scrapemate
func WithJobProvider ¶
func WithJobProvider(provider JobProvider) func(*scrapeMate) error
WithJobProvider sets the job provider for the scrapemate
func WithLogger ¶
WithLogger sets the logger for the scrapemate
Types ¶
type HtmlParser ¶
HtmlParser is an interface for html parsers
type HttpFetcher ¶
HttpFetcher is an interface for http fetchers
type IJob ¶
type IJob interface {
fmt.Stringer
// GetID returns the unique identifier of the job.
GetID() string
// GetMethod returns the http method to use
GetMethod() string
// GetBody returns the body of the request
GetBody() []byte
// GetURL returns the url to request
GetURL() string
// GetHeaders returns the headers to use
GetHeaders() map[string]string
// GetUrlParams returns the url params to use
GetUrlParams() map[string]string
// GetTimeout returns the timeout of the job
GetTimeout() time.Duration
// GetPriority returns the priority of the job
GetPriority() int
// CheckResponse checks the response of the job
DoCheckResponse(resp Response) bool
// GetActionOnResponse returns the action to perform on the response
GetRetryPolicy() RetryPolicy
// GetMaxRetries returns the max retries of the job
GetMaxRetries() int
// Process processes the job
Process(ctx context.Context, resp Response) (any, []IJob, error)
// GetMaxRetryDelay returns the delay to wait before retrying
GetMaxRetryDelay() time.Duration
BrowserActions(browser playwright.Browser) Response
// DoScreenshot takes a screenshot of the page
// Only works if the scraper uses jsfetcher
DoScreenshot() bool
}
IJob is a job to be processed by the scrapemate
type Job ¶
type Job struct {
// ID is an identifier for the job
ID string
// Method can be one valid HTTP method
Method string
// Body is the request's body
Body []byte
// URL is the url to sent a request
URL string
// Headers is the map of headers to use in HTTP
Headers map[string]string
// UrlParams are the url parameters to use in the query string
UrlParams map[string]string
// Timeout is the timeout of that job. By timeout we mean the time
// it takes to finish a single crawl
Timeout time.Duration
// Priority is a number indicating the priority. By convention the higher
// the priority
Priority int
// MaxRetries defines the maximum number of retries when a job fails
MaxRetries int
// CheckResponse is a function that takes as an input a Response and returns:
// true: when the response is to be accepted
// false: when the response is to be rejected
// By default a response is accepted if status code is 200
CheckResponse func(resp Response) bool
// RetryPolicy can be one of:
// RetryJob: to retry the job untl it's sucessful
// DiscardJob:for not accepted responses just discard them and do not retry the job
// RefreshIP: Similar to RetryJob with an importan difference
// Before the job is retried the IP is refreshed.
RetryPolicy RetryPolicy
// MaxRetryDelay By default when a job is rejected is retried with an exponential backof
// for a MaxRetries numbers of time. If the sleep time between the retries is more than
// MaxRetryDelay then it's capped to that. (Default is 2 seconds)
MaxRetryDelay time.Duration
//TakeScreenshot if true takes a screenshot of the page
TakeScreenshot bool
Response Response
}
Job is the base job that we may use
func (*Job) BrowserActions ¶
func (j *Job) BrowserActions(browser playwright.Browser) Response
BrowserActions is the function that will be executed in the browser This is the function that will be executed in the browser this is a default implementation that will just return the response override this function to perform actions in the browser
func (*Job) DoCheckResponse ¶
CheckResponse checks the response of the job
func (*Job) DoScreenshot ¶
DoScreenshot used to check if we need a screenshot It's here since it's a common use case
func (*Job) GetHeaders ¶
GetHeaders returns the headers to use
func (*Job) GetMaxRetries ¶
GetMaxRetry returns the max retry of the job
func (*Job) GetMaxRetryDelay ¶
GetRetryDelay returns the delay to wait before retrying
func (*Job) GetPriority ¶
GetPriority returns the priority of the job
func (*Job) GetRetryPolicy ¶
func (j *Job) GetRetryPolicy() RetryPolicy
GetRetryPolicy returns the action to perform on the response
func (*Job) GetTimeout ¶
GetTimeout returns the timeout of the job
func (*Job) GetUrlParams ¶
GetUrlParams returns the url params to use
type JobProvider ¶
type JobProvider interface {
Jobs(ctx context.Context) (<-chan IJob, <-chan error)
// Push pushes a job to the job provider
Push(ctx context.Context, job IJob) error
}
JobProvider is an interface for job providers a job provider is a service that provides jobs to scrapemate scrapemate will call the job provider to get jobs
type Response ¶
type Response struct {
URL string
StatusCode int
Headers http.Header
Duration time.Duration
Body []byte
Error error
Meta map[string]any
Screenshot []byte
// Document is the parsed document
// if you don't set an html parser the document will be nil
// Since each html parser has it's own document type
// the document is an interface
// You need to cast it to the type of the parser you are using
// For example if you are using goquery you need to cast it to *goquery.Document
// If you are using the stdib parser net/html the it will be *html.Node
Document any
}
Response is the struct that it is returned when crawling finishes
type RetryPolicy ¶
type RetryPolicy int
Directories
¶
| Path | Synopsis |
|---|---|
|
adapters
|
|
|
Package mock is a generated GoMock package.
|
Package mock is a generated GoMock package. |