-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.go
73 lines (61 loc) · 1.63 KB
/
scraper.go
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
66
67
68
69
70
71
72
73
package xivish
import (
"fmt"
"time"
"github.com/gocolly/colly/v2"
"github.com/gocolly/colly/v2/queue"
)
var (
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
)
type Scraper struct {
collector *colly.Collector
queue *queue.Queue
logger Logger
}
// NewScraper creates a new scraper
func NewScraper() Scraper {
return Scraper{
collector: nil,
queue: nil,
logger: Logger{DisableAll: true},
}
}
// createBaseCollector creates a collector with some defaults
func (s *Scraper) createBaseCollector() {
s.attachCollector(colly.NewCollector(
colly.UserAgent(userAgent),
))
_ = s.SetLimiter(1*time.Second, 1)
}
// SetLimiter sets the rate limiter for the scraper
func (s Scraper) SetLimiter(duration time.Duration, parallel int) error {
s.logger.LogInfo("limiter", fmt.Sprintf("limiting to %d request per %s", parallel, duration))
err := s.collector.Limit(&colly.LimitRule{DomainGlob: "*", Delay: duration, Parallelism: parallel})
if err != nil {
s.logger.LogError("limiter", err)
return err
}
return nil
}
// attachCollector attaches a collector to the scraper
func (s *Scraper) attachCollector(c *colly.Collector) {
s.collector = c
}
// Close closes the scraper
func (s Scraper) Close() {
s.collector.Wait()
s.logger.LogInfo("scraper status", "closed")
s.logger.Close()
}
// Restart stops the queue and starts it again after a delay
func (s Scraper) Restart(delay time.Duration) error {
s.queue.Stop()
time.Sleep(delay)
err := s.queue.Run(s.collector)
if err != nil {
s.logger.LogError("queue", err)
return err
}
return nil
}