-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (111 loc) · 3.25 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"errors"
"fmt"
"log"
"os"
"sync"
"github.com/ardanlabs/conf/v3"
"github.com/musabgultekin/quantumscraper/logging"
"github.com/musabgultekin/quantumscraper/metrics"
"github.com/musabgultekin/quantumscraper/worker"
)
var build = "develop"
func main() {
if err := run(); err != nil {
log.Println("error:", err)
os.Exit(1)
}
}
func run() error {
// -------------------------------------------------------------------------
// Configuration
log.SetOutput(&logging.FilteredWriter{os.Stderr})
// Enforce proxy
if os.Getenv("PROXY_URL") == "" {
log.Println("Please set proxy environment variable")
os.Exit(1)
}
cfg := struct {
conf.Version
Crawler struct {
Concurrency int `conf:"default:100"`
}
UrlList struct {
URL string `conf:"default:https://tranco-list.eu/download/Z249G/full"`
CachePath string `conf:"default:data/url_cache.csv"`
ParquetDir string `conf:"default:data/cc-index/"`
}
}{
Version: conf.Version{
Build: build,
Desc: "MIT",
},
}
const prefix = "SCRAPER"
help, err := conf.Parse(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
return nil
}
return fmt.Errorf("parsing config: %w", err)
}
// -------------------------------------------------------------------------
// Initialization
// nsqdServer, err := storage.NewNSQDServer()
// if err != nil {
// return fmt.Errorf("start nsqd embedded server: %w", err)
// }
// queue, err := storage.NewQueue(path.Join("data/visited_urls"), cfg.Crawler.Concurrency)
// if err != nil {
// return fmt.Errorf("visited url storage creation: %w", err)
// }
// if _, err := http.FetchReliableDNSRecords("https://public-dns.info/nameserver/us.json"); err != nil {
// return fmt.Errorf("dns server loading error: %w", err)
// }
go metrics.StartMetricsServer()
// -------------------------------------------------------------------------
// App Starting
// Queue URLs
// go func() {
// if err := urlloader.StartQueueingURLs(cfg.UrlList.URL, cfg.UrlList.CachePath, queue); err != nil {
// log.Fatal(err)
// }
// }()
// Start queue workers
// consumers, err := worker.StartQueueWorkers(cfg.Crawler.Concurrency, queue)
// if err != nil {
// return fmt.Errorf("worker process: %w", err)
// }
var workerWg sync.WaitGroup
worker.StartWorkers(cfg.UrlList.URL, cfg.UrlList.CachePath, cfg.UrlList.ParquetDir, &workerWg, cfg.Crawler.Concurrency)
// -------------------------------------------------------------------------
// Shutdown
// Wait until SIGTERM
// shutdown := make(chan os.Signal, 1)
// signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
// select {
// case <-shutdown:
// // case <-nsqdServer.Error():
// // log.Println("nsqd server error! scraping will be stopped: ", err)
// }
// log.Println("Stopping signal received")
// Wait until closed
workerWg.Wait()
// queue.StopSignal()
// time.Sleep(time.Millisecond * 100)
// for _, consumer := range consumers {
// consumer.Stop()
// }
// for _, consumer := range consumers {
// <-consumer.StopChan
// }
// queue.StopProducer()
// nsqdServer.Stop()
// if err := queue.CloseDB(); err != nil {
// log.Println("Queue CloseDB error:", err)
// }
log.Panic("Scraping successfully finished")
return nil
}