-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
117 lines (103 loc) · 2.44 KB
/
tag.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
package pixiv
import (
"compress/gzip"
"fmt"
"io"
"net/http"
"strconv"
jsoniter "github.com/json-iterator/go"
)
type tag struct {
pixiv
tagName string
}
// Tag Constructor of tag of pictures.
//
// tagName -name of tag, can't be multi.
func Tag(tagName string) *tag {
t := new(tag)
t.rname = "tag"
t.log = myLog.WithField("place", "tag")
t.savePath = globalConfig.GetString("download.tag.path")
t.fileDir = tagName
t.tagName = tagName
t.baseURL = "https://www.pixiv.net/ajax/search/artworks/%s"
return t
}
// Num num of pictures to get.
func (t *tag) Num(num int) *tag {
if num <= 0 {
t.log.Fatalln("Please give a number > 0")
}
t.num = num
return t
}
// Download download pictures to local
func (t *tag) Download() {
if t.num == 0 {
t.log.Fatalln("Please set num before download")
}
t.downLoadImg(t.getImgUrls(t.getIds()))
}
// Upload upload pictures to minio server
func (t *tag) Upload() {
if t.num == 0 {
t.log.Fatalln("Please set num before download")
}
t.upLoadImg(t.getImgUrls(t.getIds()))
}
func (t *tag) getIds() chan string {
ids := make(chan string, t.num)
pageUp := t.num/60 + 1
numLeft := t.num % 60
pageCount := pageUp
// wait chan
idsChan := make(chan int)
URL := fmt.Sprintf(t.baseURL, t.tagName)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
t.log.WithError(err).Fatalf("Fail to create request, URL=%s", URL)
}
setHeader(req)
for ; pageUp > 0; pageUp-- {
go func(pageUp int) {
idNum := 60
if pageUp == pageCount {
idNum = numLeft
}
q := req.URL.Query()
q.Add("word", t.tagName)
q.Add("order", "date_d")
q.Add("mode", "all")
q.Add("p", strconv.Itoa(pageUp))
q.Add("s_mode", "s_tag_full")
q.Add("type", "all")
q.Add("lang", "zh")
req.URL.RawQuery = q.Encode()
res, err := client.Do(req)
if err != nil {
t.log.WithError(err).Fatalln("Fail to get response")
}
if code := res.StatusCode; code != 200 {
t.log.Fatalf("URL Code=%d", res.StatusCode)
}
reader, _ := gzip.NewReader(res.Body)
content, err := io.ReadAll(reader)
defer res.Body.Close()
if err != nil {
t.log.WithError(err).Fatalln("Fail to read response")
}
for ; idNum > 0; idNum-- {
ids <- jsoniter.Get(content, "body").Get("illustManga").Get("data", idNum-1).Get("id").ToString()
}
idsChan <- 1
}(pageUp)
}
go func(pageCount int, idsChan chan int) {
for ; pageCount > 0; pageCount-- {
<-idsChan
}
close(ids)
}(pageCount, idsChan)
return ids
}