-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolly.go
More file actions
76 lines (65 loc) · 1.62 KB
/
colly.go
File metadata and controls
76 lines (65 loc) · 1.62 KB
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
74
75
76
package sitemap
import (
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/MegaBytee/sitemap-go/config"
"github.com/gocolly/colly/v2"
"github.com/gocolly/colly/v2/extensions"
"github.com/velebak/colly-sqlite3-storage/colly/sqlite3"
)
func newCollyScrapper(cfg *config.Config) (*colly.Collector, error) {
c := colly.NewCollector()
dataDir, err := config.GetDataDir()
if err != nil {
return nil, err
}
newDirPath := filepath.Join(dataDir, "colly")
// Check if the directory exists
if _, err := os.Stat(newDirPath); os.IsNotExist(err) {
// Create the directory
err := os.Mkdir(newDirPath, os.ModePerm)
if err != nil {
return nil, err
}
}
storageDir := filepath.Join(newDirPath, "sitemap.db")
storage := &sqlite3.Storage{
Filename: storageDir,
}
err = c.SetStorage(storage)
if err != nil {
return nil, err
}
if cfg.WithCache {
cacheDir := filepath.Join(newDirPath, "sitemap")
c.CacheDir = cacheDir
}
if cfg.WithProxy {
err := c.SetProxy(cfg.ProxyUrl)
if err != nil {
return nil, err
}
}
extensions.RandomUserAgent(c)
extensions.Referer(c)
c.OnRequest(func(r *colly.Request) {
log.Println("visiting:", r.URL)
setDelayInMs(10, 500)
})
c.OnResponse(func(r *colly.Response) {
log.Println(r.Request.URL, "\t", r.StatusCode)
})
c.OnError(func(r *colly.Response, err error) {
log.Println(r.Request.URL, "\t", r.StatusCode, "\nError:", err)
})
return c, nil
}
func setDelayInMs(x, y int) {
delay := rand.Intn(y) + x // Random number between x and y
fmt.Printf("Sleeping for %d ms before the next request...\n", delay)
time.Sleep(time.Duration(delay) * time.Millisecond)
}