blob: bc90c027e3ea64f2d3c24bcc8da348defa4f6ac0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package flyscrape
import (
"encoding/json"
"fmt"
"net/http"
"sync"
)
type Module interface {
ID() string
}
type Transport interface {
Transport(*http.Request) (*http.Response, error)
}
type CanRequest interface {
CanRequest(url string, depth int) bool
}
type OnRequest interface {
OnRequest(*Request)
}
type OnResponse interface {
OnResponse(*Response)
}
type OnLoad interface {
OnLoad(Visitor)
}
type OnComplete interface {
OnComplete()
}
func RegisterModule(m Module) {
id := m.ID()
if id == "" {
panic("module id is missing")
}
globalModulesMu.Lock()
defer globalModulesMu.Unlock()
if _, ok := globalModules[id]; ok {
panic(fmt.Sprintf("module %s already registered", id))
}
globalModules[id] = m
}
func LoadModules(s *Scraper, opts Options) {
globalModulesMu.RLock()
defer globalModulesMu.RUnlock()
for _, mod := range globalModules {
json.Unmarshal(opts, mod)
s.LoadModule(mod)
}
}
var (
globalModules = map[string]Module{}
globalModulesMu sync.RWMutex
)
|