-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
354 lines (296 loc) · 10 KB
/
model.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package pixiv_api_go
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
// PixivID is the illust id / user id, the pixiv ajax api return a number
// type illust id when the illust has been deleted
type PixivID string
func (w *PixivID) UnmarshalJSON(data []byte) (err error) {
if zip, err := strconv.Atoi(string(data)); err == nil {
str := strconv.Itoa(zip)
*w = PixivID(str)
return nil
}
var str string
err = json.Unmarshal(data, &str)
if err != nil {
return err
}
return json.Unmarshal([]byte(str), w)
}
type PixivResponse struct {
Error bool `json:"error"`
Message string `json:"message"`
Body json.RawMessage `json:"body"`
}
type UserInfo struct {
UserId PixivID `json:"userId"`
UserName string `json:"userName"`
UserAccount string `json:"userAccount"`
}
// IllustDigest is the illust basic info get from bookmarks or artist work
type IllustDigest struct {
Id PixivID `json:"id"`
Title string `json:"title"`
PageCount int32 `json:"pageCount"`
BookmarkDate *BookmarkDate `json:"bookmarkData"`
UserInfo
}
func (bi *IllustDigest) DigestString() string {
return fmt.Sprintf("[id: %s, title: %s, uid: %s, uname: %s, pages: %d]", bi.Id, bi.Title, bi.UserId, bi.UserName, bi.PageCount)
}
// BookmarksInfo is the response body of bookmarks api
type BookmarksInfo struct {
Total int32 `json:"total"`
Works []*IllustDigest `json:"works"`
}
// FollowingInfo is the response body of following api
type FollowingInfo struct {
Users []*UserInfo `json:"users"`
Total int32 `json:"total"`
}
type Urls struct {
Mini string `json:"mini"`
Thumb string `json:"thumb"`
Small string `json:"small"`
Regular string `json:"regular"`
Original string `json:"original"`
}
type BookmarkDate struct {
Id PixivID `json:"id"`
Private bool `json:"private"`
}
type IllustTypeCode int
const (
IllustTypeIllust IllustTypeCode = 0
IllustTypeManga IllustTypeCode = 1
IllustTypeUgoira IllustTypeCode = 2
)
var illustName = map[IllustTypeCode]string{
IllustTypeIllust: "Illust",
IllustTypeManga: "Manga",
IllustTypeUgoira: "Ugoira",
}
func IllustTypeName(code IllustTypeCode) string {
if v, ok := illustName[code]; ok {
return v
}
return "UNKNOWN"
}
func (i IllustTypeCode) MarshalJSON() ([]byte, error) {
name := IllustTypeName(i)
return []byte(`"` + name + `"`), nil
}
type AITypeCode int
const (
AITypeUndefined AITypeCode = 0
AITypeNotAiGenerate AITypeCode = 1
AITypeAiGenerate AITypeCode = 2
)
var aiTypeCodeName = map[AITypeCode]string{
AITypeUndefined: "Undefined",
AITypeNotAiGenerate: "NotAiGenerate",
AITypeAiGenerate: "AiGenerate",
}
func AITypeCodeName(code AITypeCode) string {
if v, ok := aiTypeCodeName[code]; ok {
return v
}
return "UNKNOWN"
}
func (a AITypeCode) MarshalJSON() ([]byte, error) {
name := AITypeCodeName(a)
return []byte(`"` + name + `"`), nil
}
//================================================================
type RestrictLevel int
const (
RestrictLevelPublic RestrictLevel = 0
RestrictLevelMypixiv RestrictLevel = 1 // illust will only be visible to people who are added to your My pixiv
RestrictLevelPrivate RestrictLevel = 2
)
var restrictLevelName = map[RestrictLevel]string{
RestrictLevelPublic: "Public",
RestrictLevelMypixiv: "Mypixiv",
RestrictLevelPrivate: "Private",
}
func RestrictName(level RestrictLevel) string {
if v, ok := restrictLevelName[level]; ok {
return v
}
return "UNKNOWN"
}
func (r RestrictLevel) MarshalJSON() ([]byte, error) {
name := RestrictName(r)
return []byte(`"` + name + `"`), nil
}
//================================================================
type XRestrictLevel int
const (
XRestrictLevelSafe XRestrictLevel = 0
XRestrictLevelR18 XRestrictLevel = 1
XRestrictLevelR18G XRestrictLevel = 2
)
var xRestrictLevelName = map[XRestrictLevel]string{
XRestrictLevelSafe: "Safe",
XRestrictLevelR18: "R18",
XRestrictLevelR18G: "R18G",
}
func XRestrictName(level XRestrictLevel) string {
if v, ok := xRestrictLevelName[level]; ok {
return v
}
return "UNKNOWN"
}
func (xr XRestrictLevel) MarshalJSON() ([]byte, error) {
name := XRestrictName(xr)
return []byte(`"` + name + `"`), nil
}
//================================================================
type SanityLevelCode int
const (
SanityLevelUnchecked SanityLevelCode = 0
SanityLevelGray SanityLevelCode = 1
SanityLevelWhite SanityLevelCode = 2
SanityLevelSemiBlack SanityLevelCode = 4
SanityLevelBlack SanityLevelCode = 6
SanityLevelIllegal SanityLevelCode = 7
)
var sanityLevelCodeName = map[SanityLevelCode]string{
SanityLevelUnchecked: "Unchecked",
SanityLevelGray: "Gray",
SanityLevelWhite: "White",
SanityLevelSemiBlack: "SemiBlack",
SanityLevelBlack: "Black",
SanityLevelIllegal: "Illegal",
}
func SanityLevelName(code SanityLevelCode) string {
if v, ok := sanityLevelCodeName[code]; ok {
return v
}
return "UNKNOWN"
}
func (c SanityLevelCode) MarshalJSON() ([]byte, error) {
name := SanityLevelName(c)
return []byte(`"` + name + `"`), nil
}
type IllustInfo struct {
Id PixivID `json:"id"`
PageIdx int `json:"curPage"`
Title string `json:"title"`
Description string `json:"description"`
IllustType IllustTypeCode `json:"illustType"`
CreateDate time.Time `json:"createDate"`
UploadDate time.Time `json:"uploadDate"`
Restrict RestrictLevel `json:"restrict"`
XRestrict XRestrictLevel `json:"XRestrict"`
SanityLevel SanityLevelCode `json:"sl"`
Urls Urls `json:"urls"`
R18 bool `json:"r18"`
Tags []string `json:"string_tags"`
TransTags []string `json:"trans_tags"` // the tag translation for your specified language
Width int `json:"width"`
Height int `json:"height"`
PageCount int `json:"pageCount"`
BookmarkCount int `json:"bookmarkCount"`
LikeCount int `json:"likeCount"`
CommentCount int `json:"commentCount"`
ViewCount int `json:"viewCount"`
IsOriginal bool `json:"isOriginal"`
BookmarkDate *BookmarkDate `json:"bookmarkData"` // nil if you don't bookmark this illust
AiType AITypeCode `json:"aiType"`
UserInfo
}
func (i *IllustInfo) DigestString() string {
return fmt.Sprintf("[id: %s, page: %d, title: %s, uid: %s, uname: %s, pageCnt: %d, R18: %v, bookmarkCnt: %d, likeCnt: %d]",
i.Id, i.PageIdx, i.Title, i.UserId, i.UserName, i.PageCount, i.R18, i.BookmarkCount, i.LikeCount)
}
func (i *IllustInfo) DigestStringWithUrl() string {
return fmt.Sprintf("[id: %s, page: %d, title: %s, uid: %s, uname: %s, pageCnt: %d, R18: %v, bookmarkCnt: %d, likeCnt: %d, width: %d, height: %d, URL: %s]",
i.Id, i.PageIdx, i.Title, i.UserId, i.UserName, i.PageCount, i.R18, i.BookmarkCount, i.LikeCount, i.Width, i.Height, i.Urls.Original)
}
func (i *IllustInfo) ToJson(ident bool) string {
var j []byte
if ident {
j, _ = json.MarshalIndent(i, "", " ")
} else {
j, _ = json.Marshal(i)
}
return string(j)
}
type IllustRankMode string
type IllustRankContent string
type IllustRankItem struct {
Title string `json:"title"`
Date string `json:"date"`
Tags []string `json:"tags"`
Url string `json:"url"`
IllustType string `json:"illust_type"`
IllustBookStyle string `json:"illust_book_style"`
IllustPageCount string `json:"illust_page_count"`
UserName string `json:"user_name"`
ProfileImg string `json:"profile_img"`
IllustContentType map[string]interface{} `json:"illust_content_type"`
IllustSeries bool `json:"illust_series"`
IllustId PixivID `json:"illust_id"`
Width int `json:"width"`
Height int `json:"height"`
UserId PixivID `json:"user_id"`
Rank int `json:"rank"`
YesRank int `json:"yes_rank"`
RatingCount int `json:"rating_count"`
ViewCount int `json:"view_count"`
IllustUploadTimestamp int `json:"illust_upload_timestamp"`
Attr string `json:"attr"`
IsBookmarked bool `json:"is_bookmarked"`
Bookmarkable bool `json:"bookmarkable"`
}
type RankPageType int
func (pt *RankPageType) UnmarshalJSON(data []byte) (err error) {
if string(data) == "false" {
*pt = 0
return nil
}
var p int
_ = json.Unmarshal(data, &p)
*pt = RankPageType(p)
return nil
}
type RankDateType string
func (dt *RankDateType) UnmarshalJSON(data []byte) (err error) {
if string(data) == "false" {
*dt = "false"
return nil
}
var date string
_ = json.Unmarshal(data, &date)
*dt = RankDateType(date)
return nil
}
type IllustRankInfo struct {
Contents []*IllustRankItem `json:"contents"`
Mode IllustRankMode `json:"mode"`
Content IllustRankContent `json:"content"`
// Page is the current page in get request
Page RankPageType `json:"page"`
// Prev is the Page - 1, if current page is the first page, Prev will be 0
Prev RankPageType `json:"prev"`
// Next is the Page + 1, if it has no next page, Next will be 0
Next RankPageType `json:"next"`
// Date is the current date in get request
Date RankDateType `json:"date"`
// PrevDate is the Date - 1day
PrevDate RankDateType `json:"prev_date"`
// NextDate is the Date + 1day, if current date is today, the NextDate will be 'false'
NextDate RankDateType `json:"next_date"`
RankTotal int `json:"rank_total"`
}
func (r *IllustRankInfo) HasNextDate() bool {
return r.NextDate == "false"
}
func (r *IllustRankInfo) HasNextPage() bool {
return r.Next != 0
}