-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixiv_client_test.go
155 lines (138 loc) · 3.76 KB
/
pixiv_client_test.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
package pixiv_api_go
import (
"encoding/json"
"testing"
)
// var Cookie = ""
var userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
func TestPixivID(t *testing.T) {
js := `{"id": 123456789}`
var id struct {
Id PixivID `json:"id"`
}
err := json.Unmarshal([]byte(js), &id)
if err != nil {
t.Fatal(err)
}
t.Log(id.Id)
}
func TestUserBookmarks(t *testing.T) {
//client := NewPixivClient(5000)
//client.SetUserAgent(userAgent)
//client.SetCookiePHPSESSID("")
//
//var testCase = []struct {
// uid string
//}{
// {"4495110"},
//}
//
//for _, tc := range testCase {
// bookmarks, err := client.GetUserBookmarks(tc.uid, 0, 48)
// if err != nil {
// t.Fatal(err)
// }
// j, _ := json.MarshalIndent(bookmarks, "", " ")
// t.Log(string(j))
//}
}
func TestGetIllustInfo(t *testing.T) {
client := NewPixivClient(5000)
client.SetUserAgent(userAgent)
var testCase = []struct {
pid PixivID
expectedPage int
}{
{"107157430", 5},
}
for _, tc := range testCase {
illusts, err := client.GetIllustInfo(tc.pid, false)
if err != nil {
t.Fatal(err)
}
if tc.expectedPage != len(illusts) {
t.Errorf("illust expected page: %d, acture: %d", tc.expectedPage, len(illusts))
}
}
}
func TestIllustRank(t *testing.T) {
client := NewPixivClient(5000)
//client.SetCookiePHPSESSID(Cookie)
//client.SetUserAgent(userAgent)
var testCase = []struct {
illustRankMode IllustRankMode
illustRankContent IllustRankContent
page int
date string
expectedPage RankPageType
expectedPrev RankPageType
expectedNext RankPageType
expectedDate RankDateType
expectedPrevDate RankDateType
expectedNextDate RankDateType
}{
{IllustRankModeDaily, IllustRankContentIllust, 1, "", 1, 0, 2, "", "", "false"},
}
for _, tc := range testCase {
illustRank, err := client.IllustRank(tc.illustRankMode, tc.illustRankContent, tc.date, tc.page)
if err != nil {
t.Fatal(err)
}
if illustRank.Page != tc.expectedPage {
t.Errorf("illust rank expected page: %d, acture: %d", tc.expectedPage, illustRank.Page)
}
if illustRank.Prev != tc.expectedPrev {
t.Errorf("illust rank expected prev: %d, acture: %d", tc.expectedPrev, illustRank.Page)
}
if illustRank.NextDate != tc.expectedNextDate {
t.Errorf("illust rank expected next_date: %s, acture: %s", tc.expectedNextDate, illustRank.NextDate)
}
j, _ := json.MarshalIndent(illustRank, "", " ")
t.Logf("%s", string(j))
}
}
func TestIllustInfo(t *testing.T) {
client := NewPixivClient(5000)
//client.SetCookiePHPSESSID(Cookie)
//client.SetUserAgent(userAgent)
var testCase = []struct {
illustId PixivID
p0 bool
expectedPages int
expectedR18 bool
}{
{"103535277", true, 1, false},
{"103882937", true, 1, false},
//{"103882937", false, 2, false},
}
for _, tc := range testCase {
illusts, err := client.GetIllustInfo(tc.illustId, tc.p0)
if err != nil {
t.Fatal(err)
}
if len(illusts) != tc.expectedPages {
t.Errorf("illust id: %s, expected page: %d, acture: %d", tc.illustId, tc.expectedPages, len(illusts))
}
for _, illust := range illusts {
if illust.R18 != tc.expectedR18 {
t.Errorf("illust id: %s, expect r18: %v, acture: %v", tc.illustId, tc.expectedR18, illust.R18)
}
t.Logf("%s", illust.ToJson(true))
}
}
}
func TestDownload(t *testing.T) {
client := NewPixivClient(60000)
filename := "/tmp/pixiv-api/1.jpg"
_, hash, err := client.DownloadIllust("https://i.pximg.net/img-original/img/2022/12/24/00/00/46/103842593_p0.jpg", filename)
if err != nil {
t.Fatal(err)
}
actualHash, err := FileSha1Sum(filename)
if err != nil {
t.Fatal(err)
}
if hash != actualHash {
t.Errorf("expected: %s, acture: %s", hash, actualHash)
}
}