-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
204 lines (171 loc) · 4.07 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"github.com/gedex/imgdownloader/provider"
)
var (
tag = flag.String("tag", "", "Image tag")
n = flag.Uint("n", 10, "Number of images to download")
providerString = flag.String("from", "flickr", "Image provider (flickr or instagram)")
out = flag.String("out", "./", "Path to downloaded images")
worker = flag.Uint("worker", 10, "Number of workers downloading the images")
// Active config is stored here
activeConf config
)
type config map[string]string
func main() {
flag.Parse()
err := checkParams()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v.\n", err)
printUsage(1)
}
activeConf, err := getConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v.\n", err)
os.Exit(1)
}
p, err := provider.Get(*providerString)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v.\n", err)
os.Exit(1)
}
p.Configure(activeConf)
listToDownload, err := p.Request(*tag, *n)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v.\n", err)
os.Exit(1)
}
downloads := make(chan *provider.ProviderItem, len(listToDownload))
results := make(chan string, len(listToDownload))
numberOfDownloaders := int(*worker)
if numberOfDownloaders > len(listToDownload) {
numberOfDownloaders = len(listToDownload)
}
for d := 1; d <= numberOfDownloaders; d++ {
go downloader(downloads, results)
}
for _, item := range listToDownload {
downloads <- item
}
close(downloads)
for r := 1; r <= len(listToDownload); r++ {
fmt.Println(<-results)
}
}
func checkParams() error {
if *tag == "" {
return fmt.Errorf("Tag is not supplied")
}
_, err := ioutil.ReadDir(*out)
if err != nil {
switch {
case os.IsNotExist(err):
// Try to make the dir first
err = os.Mkdir(*out, os.ModeDir|os.ModePerm)
if err != nil {
return err
}
default:
return err
}
}
return nil
}
func printUsage(code int) {
format := fmt.Sprintf("Usage of %s:\n", os.Args[0])
if code > 0 {
fmt.Fprintf(os.Stderr, format)
} else {
fmt.Fprintf(os.Stdout, format)
}
flag.PrintDefaults()
os.Exit(code)
}
func getConfig() (config, error) {
file, err := getFileConfig("./imgdownloader.json")
if err != nil {
file, err = getFileConfig("~/imgdownloader.json")
return nil, err
}
c, err2 := readFileConfig(file)
if err2 != nil {
return nil, err2
}
return c, nil
}
func getFileConfig(path string) (file *os.File, err error) {
file, err = os.Open(path)
if err != nil {
return nil, err
}
return
}
func readFileConfig(file *os.File) (config, error) {
defer file.Close()
var data []byte
buf := make([]byte, 1024)
for {
n, err := file.Read(buf)
if err != nil && err != io.EOF {
return nil, err
}
if n == 0 {
break
}
data = buf[:n]
}
var v interface{}
err := json.Unmarshal(data, &v)
if err != nil {
return nil, fmt.Errorf("unmarshal error: %v", err)
}
vv := v.(map[string]interface{})
if _, ok := vv[*providerString]; !ok {
return nil, fmt.Errorf("%v provider is not found in imgdownaloder.json", *providerString)
}
vvv := vv[*providerString].(map[string]interface{})
c := config{}
for key, val := range vvv {
vstr, ok := val.(string)
if !ok {
return nil, fmt.Errorf("invalid %v provider config", *providerString)
}
c[key] = vstr
}
return c, nil
}
func downloader(downloads <-chan *provider.ProviderItem, results chan<- string) {
for d := range downloads {
resp, err := http.Get(d.Link)
if err != nil {
results <- fmt.Sprintf("Error GET %v: %v", d.Link, err)
continue
}
defer resp.Body.Close()
outFile := getOutFilePath(d.Filename)
out, err := os.Create(outFile)
if err != nil {
results <- fmt.Sprintf("Error creating %v: %v", out, err)
continue
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
results <- fmt.Sprintf("Error copying %v to %v", d.Filename, outFile)
continue
}
results <- fmt.Sprintf("Successfully downloading %v to %v", d.Link, outFile)
}
}
func getOutFilePath(filename string) string {
fileOut := path.Join(*out, filename)
return fileOut
}