-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixiv.go
184 lines (164 loc) · 4.42 KB
/
pixiv.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
// Package pixiv
//
// a pixiv spider in go
package pixiv
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"regexp"
"runtime"
"strconv"
"sync"
"github.com/minio/minio-go"
"github.com/sirupsen/logrus"
)
type pixiv struct {
baseURL string
num int
log *logrus.Entry
savePath string
fileDir string
// real struct name
rname string
}
func (p *pixiv) getImgUrls(ids chan string) chan string {
imgUrls := make(chan string, p.num)
var urlsCount int
urlsChan := make(chan int)
for id := range ids {
urlsCount++
go func(id string) {
URL := fmt.Sprintf("https://www.pixiv.net/artworks/%s", id)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
p.log.WithError(err).Fatalf("pixiv: Fail to create request, URL=%s", URL)
}
res, err := client.Do(req)
if err != nil {
p.log.WithError(err).Fatalln("pixiv: Fail to get response")
}
if res.StatusCode != 200 {
p.log.Fatalf("pixiv: Response Code=%d", res.StatusCode)
}
defer res.Body.Close()
htmlByte, err := io.ReadAll(res.Body)
if err != nil {
p.log.WithError(err).Fatalln("pixiv: Fail to read response")
}
html := string(htmlByte)
reg := regexp.MustCompile(`(?s)"original":"(.*?)"}`)
u := reg.FindStringSubmatch(html)[1]
if u == "" {
p.log.WithError(err).Fatalf("pixiv: Fail to get url of id=%s", id)
}
p.log.Infof("Got work that id is %s", id)
imgUrls <- u
urlsChan <- 1
}(id)
}
go func(urlsCount int, urlsChan chan int) {
for ; urlsCount > 0; urlsCount-- {
<-urlsChan
}
close(imgUrls)
}(urlsCount, urlsChan)
return imgUrls
}
func (p *pixiv) upLoadImg(imgUrls chan string) {
minioClient := initMinio()
bucketName := globalConfig.GetString("upload.bucketName")
mu := sync.Mutex{}
var count int
var imgsCount int
imgsChan := make(chan int)
for imgUrl := range imgUrls {
imgsCount++
go func(url string) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
p.log.WithError(err).Fatalf("pixiv: Fail to send request, URL=%s", url)
}
setHeader(req)
res, _ := client.Do(req)
if res.StatusCode != 200 {
p.log.Fatalf("pixiv: URL Code=%d", res.StatusCode)
}
defer res.Body.Close()
name := "/" + "pixiv" + "/" + p.rname + "/" + strconv.Itoa(count) + url[len(url)-4:]
contentType := res.Header.Get("content-type")
n, err := minioClient.PutObject(bucketName, name, res.Body, res.ContentLength, minio.PutObjectOptions{ContentType: contentType})
if n == 0 || err != nil {
p.log.WithError(err).Fatalln("pixiv: Fail to upload to minio")
}
p.log.Infoln(name + "upload succeded")
mu.Lock()
count++
mu.Unlock()
imgsChan <- 1
}(imgUrl)
}
for ; imgsCount > 0; imgsCount-- {
<-imgsChan
}
p.log.Infof("Total Uploaded: %d pictures", count)
}
func (p *pixiv) downLoadImg(imgUrls chan string) {
var wg sync.WaitGroup
var count int
var sep string // seperator -depends on os(windows/linux)
mu := sync.Mutex{}
sysType := runtime.GOOS
if sysType == "linux" {
sep = "/"
} else if sysType == "windows" {
sep = "\\"
}
if ok, _ := pathExists(p.savePath + sep + p.fileDir); !ok {
err := os.Mkdir(p.savePath + sep + p.fileDir, 0766)
if err != nil {
p.log.WithError(err).Fatalf("pixiv: Fail to create dir: %s", p.savePath+p.fileDir)
}
}
for imgUrl := range imgUrls {
wg.Add(1)
count++
go func(url string, count int) {
defer wg.Done()
req, err := http.NewRequest("GET", url, nil)
if err != nil {
p.log.WithError(err).Fatalf("pixiv: pixiv: Fail to create request, URL=%s", url)
}
setHeader(req)
res, _ := client.Do(req)
if res.StatusCode != 200 {
p.log.Fatalf("pixiv: pixiv: Response Code=%d", res.StatusCode)
}
defer res.Body.Close()
imgByte, err := io.ReadAll(res.Body)
if err != nil {
p.log.WithError(err).Fatalln("pixiv: Fail to read response")
}
fileName := p.savePath + sep + p.fileDir + sep + strconv.Itoa(count) + url[len(url)-4:]
if ok, _ := pathExists(fileName); !ok {
file, err := os.Create(fileName)
if err != nil {
p.log.WithError(err).Fatalf("pixiv: Fail to create file: %s", fileName)
}
writer := bufio.NewWriter(file)
nn, err := writer.Write(imgByte)
if nn == 0 || err != nil {
p.log.WithError(err).Fatalf("pixiv: Fail to write picture: %s", fileName)
}
p.log.Infoln("pixiv " + fileName + " save successfully")
}
mu.Lock()
count++
mu.Unlock()
}(imgUrl, count)
wg.Wait()
p.log.Infof("pixiv: Total: %d", count)
}
}