-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
166 lines (138 loc) · 4 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* MIT License
*
* Copyright (c) 2020 Kasun Vithanage
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package main
import (
"flag"
"os"
"path/filepath"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type SaveFileRequest struct {
s *SubmissionData
q string
}
var saveChan = make(chan SaveFileRequest)
func saveFiles(wg *sync.WaitGroup, config *Config) {
for {
select {
case sf := <-saveChan:
wg.Add(1)
if err := SaveDownload(config, sf.q, sf.s); err != nil {
log.Errorf("error downloading %s:%s", sf.q, sf.s.HackerUsername)
}
wg.Done()
}
}
}
func exec(cfg *Config) {
wg := sync.WaitGroup{}
client := CreateClient(cfg)
log.Infof("creating output directory: %v", cfg.OutDir)
err := os.MkdirAll(cfg.OutDir, os.ModePerm)
if err != nil {
log.Fatalf("error creating output directory, %v", err)
}
log.Info("fetching all question meta data")
qs, err := GetAllQuestions(client, cfg)
if err != nil {
log.Fatal(err)
}
var subs = make(map[string]SubmissionMap)
for _, q := range qs.Models {
s, err := GetAllSubmissions(client, cfg, q.Slug)
if err != nil {
log.Fatalf("error fetching submissions, %v", err)
}
subs[q.Slug] = s
log.Info("waiting 1s...")
time.Sleep(1 * time.Second)
}
// start file saving service
go saveFiles(&wg, cfg)
// limit concurrency
semaphore := make(chan struct{}, cfg.ParallelDownloads)
// have a max rate
rate := make(chan struct{}, cfg.Rate)
for i := 0; i < cap(rate); i++ {
rate <- struct{}{}
}
// leaky bucket
go func() {
ticker := time.NewTicker(time.Duration(cfg.MaxWaitTime) * time.Second)
defer ticker.Stop()
for range ticker.C {
_, ok := <-rate
// if this isn't going to run indefinitely, signal
// this to return by closing the rate channel.
if !ok {
return
}
}
}()
for q, submissionMap := range subs {
log.Infof("preparing download %d submissions for %s", len(submissionMap), q)
for _, submission := range submissionMap {
wg.Add(1)
go func(qs string, s Submission) {
defer wg.Done()
// wait for the rate limiter
rate <- struct{}{}
// check the concurrency semaphore
semaphore <- struct{}{}
defer func() {
<-semaphore
}()
ds, err := DownloadSubmission(client, cfg, &s)
if err != nil {
log.Errorf("error downloading, %v", err)
return
}
saveChan <- SaveFileRequest{
s: ds,
q: qs,
}
}(q, submission)
}
}
log.Info("waiting to finish all tasks")
wg.Wait()
close(saveChan)
close(rate)
log.Info("finished executing")
}
func main() {
var configPath string
flag.StringVar(&configPath, "config", "config.yaml", "Provide config for the tool")
flag.Parse()
cfg, err := ParseConfig(configPath)
if err != nil {
log.Fatal(err)
}
// create output directory with current time
cfg.OutDir = filepath.Join(filepath.FromSlash(cfg.Output), time.Now().Format("2006-01-02-15-04"))
log.Infof("Parallel: %d, Rate: %d, Delay: %ds [%d/%ds]", cfg.ParallelDownloads, cfg.Rate, cfg.MaxWaitTime, cfg.Rate, cfg.MaxWaitTime)
exec(cfg)
}