-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (142 loc) · 3.56 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime/pprof"
"sync"
"github.com/dmtaylor/go-chan-scraper/extractors"
"github.com/dmtaylor/go-chan-scraper/util"
"github.com/jessevdk/go-flags"
)
type options struct {
MaxThreads uint `short:"j" long:"threads" description:"Max number of downloader threads" default:"10"`
Directory string `short:"d" long:"directory" description:"download directory" default:"."`
Extractor string `short:"e" long:"engine" description:"Site engine to use" choice:"4chan" choice:"8kun" default:"4chan"`
Profile *string `short:"p" long:"profile" description:"enable profile data"`
}
type extractor func(io.ReadCloser, chan extractors.ImageFile) error
func imgWorker(
images chan extractors.ImageFile,
errors chan extractors.ImageError,
dir string,
wg *sync.WaitGroup) {
for img := range images {
resp, err := http.Get(img.FileURL.String())
if err != nil {
errors <- extractors.ImageError{
Err: err,
FileURL: img.FileURL,
}
continue
}
defer resp.Body.Close()
err = util.ValidateHttpResponse(resp, nil)
if err != nil {
errors <- extractors.ImageError{
Err: err,
FileURL: img.FileURL,
}
continue
}
filename := filepath.Join(dir, img.FileName)
err = util.SaveFile(filename, resp.Body)
if err != nil {
errors <- extractors.ImageError{
Err: err,
FileURL: img.FileURL,
}
continue
}
fmt.Printf("Downloaded %s\n", filename)
}
wg.Done()
}
func proccessErrors(errChan chan extractors.ImageError, wg *sync.WaitGroup) {
for err := range errChan {
fmt.Fprintf(os.Stderr, "Failed to get file %s: %v", err.FileURL.String(), err.Err)
}
wg.Done()
}
func processThread(urlStr string, maxThreads uint, dir string, extract extractor) error {
fmt.Printf("Downloading thread %s\n", urlStr)
url, err := url.ParseRequestURI(urlStr)
if err != nil {
return err
}
resp, err := http.Get(url.String())
if err != nil {
return err
}
defer resp.Body.Close()
requiredType := "text/html"
err = util.ValidateHttpResponse(resp, &requiredType)
if err != nil {
return err
}
var workerWg sync.WaitGroup
var errorWg sync.WaitGroup
files := make(chan extractors.ImageFile, 200)
errors := make(chan extractors.ImageError, 200)
var i uint
for i = 0; i < maxThreads; i++ {
go imgWorker(files, errors, dir, &workerWg)
workerWg.Add(1)
}
go proccessErrors(errors, &errorWg)
errorWg.Add(1)
err = extract(resp.Body, files)
if err != nil {
return err
}
close(files)
workerWg.Wait()
close(errors)
errorWg.Wait()
return nil
}
func main() {
var opts options
// TODO change getopt lib for better help management
args, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
log.Fatal(err)
}
if opts.Profile != nil {
f, err := os.Create(*opts.Profile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
isDir, err := util.DirExists(opts.Directory)
if err != nil {
log.Fatalf("Failed to get valid directory: %v\n", err)
}
if isDir == false {
log.Fatalf("Error: %s is not a valid directory", opts.Directory)
}
var extractor extractor
switch opts.Extractor {
case "4chan":
extractor = extractors.Extract4Chan
case "8kun":
extractor = extractors.Extract8Kun
default:
log.Fatalf("Invalid extractor type: %s\n", opts.Extractor)
}
threads := args[1:]
for _, item := range threads {
err := processThread(item, opts.MaxThreads, opts.Directory, extractor)
if err != nil {
log.Printf("Failed to extract url %s: %v", item, err)
}
}
fmt.Printf("Done\n")
}