-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
115 lines (98 loc) · 2.25 KB
/
user.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
package pixiv
import (
"compress/gzip"
"fmt"
"io"
"log"
"net/http"
jsoniter "github.com/json-iterator/go"
)
type user struct {
pixiv
userId string
}
// User Constructor of userId of pictures.
//
// userId -id of the user.
func User(userId string) *user {
u := new(user)
u.rname = "user"
u.log = myLog.WithField("place", "user")
u.baseURL = "https://www.pixiv.net/ajax/user/%s/profile/all"
u.userId = userId
u.savePath = globalConfig.GetString("download.user.path")
u.defaultName()
return u
}
// Name Set dir name.
//
// Default is userid
func (u *user) Name(name string) *user {
u.fileDir = name
return u
}
// Num num of pictures to get
func (u *user) Num(num int) *user {
if num <= 0 {
u.log.Fatalln("Please give a number > 0")
}
u.num = num
return u
}
// Download download pictures to loca
func (u *user) Download() {
if u.num == 0 {
u.log.Fatalln("Please set num before download")
}
u.downLoadImg(u.getImgUrls(u.getIds()))
}
// Upload upload pictures to minio server
func (u *user) Upload() {
if u.num == 0 {
u.log.Fatalln("Please set num before download")
}
u.upLoadImg(u.getImgUrls(u.getIds()))
}
func (u *user) defaultName() {
u.fileDir = u.userId
}
func (u *user) getIds() chan string {
ids := make(chan string)
URL := fmt.Sprintf(u.baseURL, u.userId)
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
u.log.WithError(err).Fatalf("Fail to create request, URL=%s", URL)
}
q := req.URL.Query()
q.Add("lang", "zh")
req.URL.RawQuery = q.Encode()
setHeader(req)
res, err := client.Do(req)
if err != nil {
u.log.WithError(err).Fatalln("Fail to get response")
}
if code := res.StatusCode; code != 200 {
u.log.Fatalf("URL Code=%d", res.StatusCode)
}
reader, _ := gzip.NewReader(res.Body)
defer res.Body.Close()
content, err := io.ReadAll(reader)
if err != nil {
u.log.WithError(err).Fatalln("Fail to read response")
}
idNum := jsoniter.Get(content, "body").Get("illusts").Size()
if idNum == 0 {
u.log.Fatalln("Fail to get ids, ids list is null")
}
if u.num > idNum {
log.Fatalf("Total works in id=%s is: %d, while got %d\n", u.userId, idNum, u.num)
}
keys := jsoniter.Get(content, "body").Get("illusts").Keys()
go func() {
for i := 0; i < u.num; i++ {
ids <- keys[i]
}
close(ids)
}()
return ids
}