-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageurls.go
111 lines (91 loc) · 1.87 KB
/
imageurls.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
package main
import (
"fmt"
"strconv"
"strings"
"time"
"golang.org/x/net/html"
)
func (d *Downloader) GetImageUrls(chapterUrls [][]string, update func(int)) ([]string, [][]string, error) {
titles := make([]string, 0)
dataUrls := make([][]string, 0)
steps := GetDataUrlSteps()
titleFmtStr := fmt.Sprintf("%%0%dd. %%s", len(strconv.Itoa(len(chapterUrls))))
for i, chapterUrl := range chapterUrls {
page, err := d.Client.GetPage(chapterUrl[1], nil)
if err != nil {
return nil, nil, err
}
doc, err := html.Parse(strings.NewReader(page))
if err != nil {
return nil, nil, err
}
pageDataUrls := step(doc, 0, steps, GetImageUrl)
titles = append(titles, fmt.Sprintf(titleFmtStr, i + 1, chapterUrl[0]))
if len(pageDataUrls) > 0 {
dataUrls = append(dataUrls, pageDataUrls[0])
}
if update != nil {
update(i + 1)
}
time.Sleep(d.Delay)
}
return titles, dataUrls, nil
}
func GetImageUrl(n *html.Node) [][]string {
dataUrls := make([][]string, 1)
dataUrls[0] = make([]string, 0)
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode && c.Data == "img" {
dataUrl := GetAttr(c, "data-url")
if dataUrl != "" {
dataUrls[0] = append(dataUrls[0], dataUrl)
}
}
}
return dataUrls
}
func GetDataUrlSteps() []*Step {
steps := make([]*Step, 8)
steps[0] = &Step {
Element: "html",
Id: "",
Class: "",
}
steps[1] = &Step {
Element: "body",
Id: "",
Class: "",
}
steps[2] = &Step {
Element: "div",
Id: "wrap",
Class: "",
}
steps[3] = &Step {
Element: "div",
Id: "container",
Class: "",
}
steps[4] = &Step {
Element: "div",
Id: "content",
Class: "",
}
steps[5] = &Step {
Element: "div",
Id: "_viewerBox",
Class: "",
}
steps[6] = &Step {
Element: "div",
Id: "",
Class: "viewer_lst",
}
steps[7] = &Step {
Element: "div",
Id: "_imageList",
Class: "",
}
return steps
}