-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (68 loc) · 1.73 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
package webmGrabber
import (
"fmt"
"log"
"sync"
"github.com/d0kur0/webm-grabber/types"
"github.com/pkg/errors"
)
var channel chan types.ChannelMessage
var done chan bool
var waitGroup sync.WaitGroup
func catchingFilesChannel(output *types.Output) {
for {
select {
case message := <-channel:
for _, file := range message.Files {
item := types.OutputItem{
VendorName: message.VendorName,
BoardName: message.Thread.Board.Name,
SourceThread: message.SourceThread,
File: file,
}
*output = append(*output, item)
}
case <-done:
return
}
}
}
func fetch(vendor types.VendorInterface, thread types.Thread) {
defer waitGroup.Done()
files, err := vendor.FetchFiles(thread)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("FetchFiles error, vendor: %s, thread: %d, board: %s", vendor.VendorName(), thread.ID, thread.Board.Name))
return
}
if len(files) == 0 {
return
}
channel <- types.ChannelMessage{
VendorName: vendor.VendorName(),
SourceThread: vendor.GetThreadUrl(thread),
Thread: thread,
Files: files,
}
}
func GrabberProcess(grabberSchemas []types.GrabberSchema) (output types.Output) {
channel = make(chan types.ChannelMessage)
done = make(chan bool)
go catchingFilesChannel(&output)
for _, schema := range grabberSchemas {
for _, board := range schema.Boards {
threads, err := schema.Vendor.FetchThreads(board)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("FetchThreads error: vendor %s, board: %s", schema.Vendor.VendorName(), board.Name))
log.Println(err)
continue
}
for _, thread := range threads {
waitGroup.Add(1)
go fetch(schema.Vendor, thread)
}
}
}
waitGroup.Wait()
done <- true
return output
}