-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
403 lines (351 loc) · 9.81 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/danbrakeley/frog"
"github.com/danbrakeley/needl/internal/buildvar"
"github.com/danbrakeley/needl/internal/config"
"github.com/danbrakeley/needl/internal/scraper"
)
const (
defaultConfigPath = "needl.toml"
defaultScrapersPath = "scrapers.toml"
defaultThreadCount = 4
)
func PrintUsage() {
version := "<local build>"
if len(buildvar.Version) > 0 {
version = buildvar.Version
}
buildTime := "<not set>"
if len(buildvar.BuildTime) > 0 {
buildTime = buildvar.BuildTime
}
url := "https://github.com/danbrakeley/needl"
if len(buildvar.ReleaseURL) > 0 {
url = buildvar.ReleaseURL
}
fmt.Fprintf(flag.CommandLine.Output(),
strings.Join([]string{
"",
"needl %s, created %s",
"%s",
"",
"Usage:",
"\tneedl [options] <scraper_name> <download_path>",
"\tneedl --version",
"\tneedl --help",
"Options:",
"\t-c, --config PATH Config TOML file (default: '%s')",
"\t --scrapers PATH Scrapers TOML file (default: '%s')",
"\t-t, --threads NUM Max number of concurrent downloads (default: '%d')",
"\t-v, --verbose Extra output (for debugging)",
"\t --version Print just the version number (to stdout)",
"\t-h, --help Print this message (to stderr)",
"",
}, "\n"), version, buildTime, url, defaultConfigPath, defaultScrapersPath, defaultThreadCount,
)
}
func main() {
status := mainExit()
if status != 0 {
// From os/proc.go: "For portability, the status code should be in the range [0, 125]."
if status < 0 || status > 125 {
status = 125
}
os.Exit(status)
}
}
type LocalFile struct {
Name string
SortName string
Timestamp time.Time
Size int64
}
func mainExit() int {
start := time.Now()
flag.Usage = PrintUsage
var configPath string
var scrapersPath string
var threadCount int
var verbose bool
var showVersion bool
var showHelp bool
flag.StringVar(&configPath, "config", defaultConfigPath, "path to optional config file")
flag.StringVar(&configPath, "c", defaultConfigPath, "path to optional config file")
flag.StringVar(&scrapersPath, "scrapers", defaultScrapersPath, "path to scrapers file")
flag.IntVar(&threadCount, "threads", 0, "number of simultaneous downloads")
flag.IntVar(&threadCount, "t", 0, "number of simultaneous downloads")
flag.BoolVar(&verbose, "v", false, "extra logging for debugging")
flag.BoolVar(&verbose, "verbose", false, "extra logging for debugging")
flag.BoolVar(&showVersion, "version", false, "show version info")
flag.BoolVar(&showHelp, "h", false, "show this help message")
flag.BoolVar(&showHelp, "help", false, "show this help message")
flag.Parse()
if showVersion {
if len(buildvar.Version) == 0 {
fmt.Printf("unknown\n")
return 1
}
fmt.Printf("%s\n", strings.TrimPrefix(buildvar.Version, "v"))
return 0
}
if showHelp {
flag.Usage()
return 0
}
if len(flag.Args()) > 2 {
fmt.Printf("unrecognized arguments: %v\n", strings.Join(flag.Args(), " "))
flag.Usage()
return 1
}
log := frog.New(frog.Auto, frog.POFieldIndent(26))
if verbose {
log.SetMinLevel(frog.Verbose)
}
defer func() {
dur := time.Now().Sub(start)
log.Info("Done", frog.Dur("time", dur))
log.Close()
}()
// parse arguments
var scraperName string
var dstPath string
scraperName = flag.Arg(0)
dstPath = flag.Arg(1)
log.Info("Loading config...", frog.Path(configPath))
cfg, err := config.Load(configPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
log.Error("loading config", frog.PathAbs(configPath), frog.Err(err))
return 5
}
// override config with command-line flags
if len(scraperName) > 0 {
cfg.Scraper = scraperName
}
if len(dstPath) > 0 {
cfg.LocalPath = dstPath
}
if threadCount > 0 {
cfg.Threads = threadCount
} else if cfg.Threads == 0 {
cfg.Threads = defaultThreadCount
}
if verbose {
cfg.Verbose = true
}
// now that the config is loaded, ensure the log level is set properly
if cfg.Verbose {
log.SetMinLevel(frog.Verbose)
} else {
log.SetMinLevel(frog.Info)
}
log.Info("Loading scrapers...", frog.Path(scrapersPath))
scrapers, err := config.LoadScrapers(scrapersPath)
if err != nil {
log.Error("loading scrapers", frog.PathAbs(scrapersPath), frog.Err(err))
return 6
}
scfg, ok := scrapers[cfg.Scraper]
if !ok {
log.Error("scraper not found", frog.String("name", cfg.Scraper), frog.PathAbs(scrapersPath))
log.Close()
flag.CommandLine.SetOutput(os.Stderr)
flag.Usage()
if len(scrapers) == 0 {
fmt.Fprintf(os.Stderr, "\nno scrapers found in %s\n", filepath.ToSlash(scrapersPath))
} else {
fmt.Fprintf(os.Stderr, "\navailable scrapers (from %s):\n", filepath.ToSlash(scrapersPath))
for k := range scrapers {
fmt.Fprintf(os.Stderr, "\t%s\n", k)
}
}
return 7
}
// ensure local path exists
if err := os.MkdirAll(cfg.LocalPath, 0o755); err != nil {
log.Error("creating local path", frog.PathAbs(cfg.LocalPath), frog.Err(err))
}
// list local and remote files
locals, remotes, errno := listFiles(log, cfg, scfg)
if errno > 0 {
return errno
}
// diff local vs remote
extra, missing, changed := diffSortedFiles(locals, remotes)
// call out files that are local-only
for _, v := range extra {
log.Info("Local file not in remote", frog.String("name", v.Name))
}
var wg sync.WaitGroup
ch := make(chan scraper.RemoteFile)
// spawn workers
wg.Add(cfg.Threads)
for i := 0; i < cfg.Threads; i++ {
go func() {
for r := range ch {
log.Info("Start download",
frog.String("name", r.Name), frog.Int64("size", r.Size),
frog.Time("time", r.Timestamp), frog.String("url", r.URL),
)
path := filepath.Join(cfg.LocalPath, r.Name)
res, err := DownloadToFile(log, r.URL, path,
DownloadOptions{ExpectedSize: r.Size, ExpectedLastModified: r.Timestamp},
)
if err != nil {
log.Error("unrecoverable error",
frog.String("name", r.Name), frog.Int64("size", res.ActualSize),
frog.Time("time", res.LastModified), frog.String("url", r.URL),
frog.PathAbs(path), frog.Err(err),
)
continue
}
log.Info("File written", frog.String("name", r.Name),
frog.Time("time", r.Timestamp), frog.Int64("size", r.Size),
frog.Path(path),
)
}
wg.Done()
}()
}
if cfg.Verbose {
for _, v := range changed {
log.Verbose("queuing changed file", frog.String("name", v.Name))
}
for _, v := range missing {
log.Verbose("queuing missing file", frog.String("name", v.Name))
}
}
// feed work to the workers
for _, v := range changed {
ch <- v
}
for _, v := range missing {
ch <- v
}
// let idle workers know they can stop
close(ch)
// wait for all workers to complete and shutdown
wg.Wait()
return 0
}
// listFiles concurrently lists both the local and remote files
func listFiles(log frog.Logger, cfg config.Config, scfg config.Scraper) ([]LocalFile, []scraper.RemoteFile, int) {
var locals []LocalFile
var errLocal error
var remotes []scraper.RemoteFile
var errRemote error
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
log.Info("Listing local files...", frog.Path(cfg.LocalPath))
locals, errLocal = getSortedLocals(cfg.LocalPath)
}()
go func() {
defer wg.Done()
log.Info("Listing remote files...", frog.String("url", scfg.URL))
remotes, errRemote = getSortedRemotes(scfg)
}()
wg.Wait()
if errLocal != nil {
log.Error("list local files", frog.Err(errLocal), frog.PathAbs(cfg.LocalPath))
return nil, nil, 20
}
if errRemote != nil {
log.Error("list remote files", frog.Err(errRemote), frog.String("url", scfg.URL))
return nil, nil, 30
}
return locals, remotes, 0
}
func getSortedLocals(path string) ([]LocalFile, error) {
locals := make([]LocalFile, 0, 256)
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
for _, e := range entries {
i, err := e.Info()
if err != nil {
return nil, err
}
locals = append(locals, LocalFile{
Name: e.Name(),
SortName: strings.ToLower(e.Name()),
Timestamp: i.ModTime().UTC(),
Size: i.Size(),
})
}
sort.Slice(locals, func(i, j int) bool {
return locals[i].SortName < locals[j].SortName
})
return locals, nil
}
func getSortedRemotes(scfg config.Scraper) ([]scraper.RemoteFile, error) {
s, err := scraper.Create(scfg.Type, scraper.BaseURL(scfg.URL))
if err != nil {
return nil, fmt.Errorf("error creating scraper of type '%s': %w", scfg.Type, err)
}
remotes, err := s.ScrapeRemotes()
if err != nil {
return nil, fmt.Errorf("error while scraping: %w", err)
}
sort.Slice(remotes, func(i, j int) bool {
return remotes[i].SortName < remotes[j].SortName
})
return remotes, nil
}
// diffSortedFiles compares two sorted lists of files and returns the differences.
// Because the input is already sorted, this diff has a linear running time.
// If the remote file has no timestamp or size, then those fields are ignored.
func diffSortedFiles(
locals []LocalFile,
remotes []scraper.RemoteFile,
) (
extra []LocalFile,
missing []scraper.RemoteFile,
changed []scraper.RemoteFile,
) {
extra = make([]LocalFile, 0, len(locals))
missing = make([]scraper.RemoteFile, 0, len(remotes))
changed = make([]scraper.RemoteFile, 0, len(remotes))
i, j := 0, 0
for i < len(locals) && j < len(remotes) {
local := locals[i]
remote := remotes[j]
if local.SortName < remote.SortName {
extra = append(extra, local)
i++
continue
}
if local.SortName > remote.SortName {
missing = append(missing, remote)
j++
continue
}
if !remote.Timestamp.IsZero() && !local.Timestamp.Equal(remote.Timestamp) {
changed = append(changed, remote)
} else if remote.Size > 0 && local.Size != remote.Size {
changed = append(changed, remote)
}
i++
j++
}
for i < len(locals) {
extra = append(extra, locals[i])
i++
}
for j < len(remotes) {
missing = append(missing, remotes[j])
j++
}
return extra, missing, changed
}