-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank.go
132 lines (115 loc) · 2.68 KB
/
rank.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
package pixiv
import (
"compress/gzip"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"unsafe"
jsoniter "github.com/json-iterator/go"
)
type rank struct {
pixiv
num int
cookie string
}
// Rank Constructor of rank per month.
func Rank() *rank {
r := new(rank)
r.rname = "rank"
r.log = myLog.WithField("place", "rank")
r.baseURL = "https://www.pixiv.net/ranking.php?mode=monthly&p=1&format=json"
r.savePath = globalConfig.GetString("download.rank.path")
r.cookie = r.getCookie()
return r
}
// Cookie Set cookie (necessary).
//
// cookie -cookie in request header.
func (r *rank) Cookie(cookie string) *rank {
r.cookie = cookie
return r
}
// Num num of picture to get.
//
// default 100
func (r *rank) Num(num int) *rank {
if r.cookie == "" {
r.log.Fatalln("cookie is null, please use Cookie method to set cookie")
}
r.num = num
return r
}
func (r *rank) DownLoad() {
if r.num < 0 {
r.log.Fatalln("Please give a number > 0")
}
if r.num == 0 {
r.num = 100
}
r.downLoadImg(r.getImgUrls(r.getIds()))
}
func (r *rank) Upload() {
if r.num < 0 {
r.log.Fatalln("Please give a number > 0")
}
if r.num == 0 {
r.num = 100
}
if r.num > 49 {
r.log.Fatalln("Please give a number < 50, it's better")
}
r.upLoadImg(r.getImgUrls(r.getIds()))
}
func (r *rank) getIds() chan string {
ids := make(chan string)
req, err := http.NewRequest("GET", r.baseURL, nil)
if err != nil {
r.log.WithError(err).Fatalf("Fail to create request, URL=%s", r.baseURL)
}
setHeader(req)
req.Header.Set("cookie", r.cookie)
res, err := client.Do(req)
if err != nil {
r.log.WithError(err).Fatalln("Fail to get response")
}
if code := res.StatusCode; code != 200 {
if code == 400 {
r.log.Fatalln("Cookie Error, please use Cookie to set cookie")
}
r.log.Fatalf("Response Code=%d", res.StatusCode)
}
reader, _ := gzip.NewReader(res.Body)
defer res.Body.Close()
content, err := io.ReadAll(reader)
if err != nil {
r.log.WithError(err).Fatalln("Fail to read response")
}
rankDate := fmt.Sprintf("%d%d%d", time.Now().Year(), time.Now().Month(), time.Now().Day())
r.log.Infof("Rank date is: %s\n", rankDate)
r.fileDir = rankDate
contents := jsoniter.Get(content, "contents")
go func() {
for s := 0; s < r.num; s++ {
ids <- contents.Get(s).Get("illust_id").ToString()
}
close(ids)
}()
return ids
}
func (r *rank) getCookie() string {
cookieFile := "cookie.txt"
cookieByte, err := os.ReadFile(cookieFile)
if err != nil {
cookieFile = "../cookie.txt"
cookieByte, err = os.ReadFile(cookieFile)
if err != nil {
r.log.WithError(err).Fatalln("Fail to read cookie.txt")
}
}
cookie := *(*string)(unsafe.Pointer(&cookieByte))
cookie = strings.TrimSpace(cookie)
return cookie
}