-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimages.go
84 lines (69 loc) · 1.85 KB
/
images.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"mime"
"path"
"strconv"
"time"
)
func (d *Downloader) GetChaptersImages(chapterUrls []string, titles []string, imageUrls [][]string, update func(int)) error {
// Should probably use a struct for this
if !(len(chapterUrls) == len(titles) && len(titles) == len(imageUrls)) {
return errors.New("parameter length mismatch")
}
for i := 0; i < len(titles); i++ {
err := d.GetChapterImages(chapterUrls[i], titles[i], imageUrls[i], update)
if err != nil {
return err
}
}
return nil
}
func (d *Downloader) GetChapterImages(chapterUrl string, title string, imageUrls []string, update func(int)) error {
// Headers
headers := make(map[string] string)
headers["Referer"] = chapterUrl
// Create padded format string for image names
nameFmtStr := fmt.Sprintf("%%0%dd%%s", len(strconv.Itoa(len(imageUrls))))
// Get images
for i, imageUrl := range imageUrls {
res, err := d.Client.GetPageRaw(imageUrl, headers)
if err != nil {
return err
}
// Get extension (if extension corresponding to content-type exists
extension := ""
if content_type, exists := res.Header["Content-Type"]; exists {
if len(content_type) > 0 {
extensions, err := mime.ExtensionsByType(content_type[0])
if err != nil {
return err
}
if extensions != nil && len(extensions) > 0 {
extension = extensions[0]
}
}
}
// I'm lazy sue me
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
// Make sure subfolder exists
err = DirExists(path.Join(d.OutputDir, title), false)
if err != nil {
return err
}
// Write file
err = ioutil.WriteFile(path.Join(d.OutputDir, title, fmt.Sprintf(nameFmtStr, i + 1, extension)), data, 0644)
if err != nil {
return err
}
// Don't trigger DDOS protection or something
time.Sleep(d.Delay)
update(0)
}
return nil
}