-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
59 lines (48 loc) · 1.09 KB
/
fetch.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
package main
import (
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"github.com/cheggaaa/pb"
)
func fetch() {
fmt.Println("Fetch image ...")
count := len(media.Media)
// create and start new bar
bar := pb.StartNew(count)
// bar.Prefix("Image")
for i := 0; i < count; i++ {
id := media.Media[i].Id
url := media.Media[i].Path
ext := filepath.Ext(url)
// don't worry about errors
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: INSECURE_SKIP_VERIFY},
MaxIdleConns: MAX_IDLE_CONNS,
MaxIdleConnsPerHost: MAX_IDLE_CONNS_PER_HOST,
}
client := &http.Client{Transport: tr}
response, e := client.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
//open a file for writing
file, err := os.Create(FOLDER_IN + id + ext)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Use io.Copy to just dump the response body to the file. This supports huge files
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
bar.Increment()
}
bar.FinishPrint("Done")
}