-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
284 lines (255 loc) · 7.47 KB
/
util.js
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
/**
* Collect items and stored them in a proxy
* @param {Array} items Source items
* @param {object} data Other properties
* @param {Function} type Related class
* @returns {Proxy} Collections
*/
function collect(items, data, type) {
const result = (type instanceof Object) ? items.map((item) => {
if (item instanceof type) {
return item
} else {
return Reflect.construct(type, [item, data.api])
}
}) : items
result.api = data.api
result.next = data.next_url
result.limit = data.search_span_limit
result.itemAt = index => result[index - 1]
return new Proxy(result, {
get(target, key) {
const result = Reflect.get(target, key)
return result instanceof Function ? result.bind(target) : result
}
})
}
class PixivUser {
constructor(data, api) {
/** General information */
this.user = data.user
/** Whether the user is muted */
this.is_muted = data.is_muted
/** User profile */
this.profile = data.profile
/** User profile publicity */
this.profile_publicity = data.profile_publicity
/** User workspace */
this.workspace = data.workspace
/** Pixiv API */
this.api = api
/** User illustrations */
this._illusts = collect(data.illusts || [], {api}, PixivIllust)
/** User novels */
this._novels = collect(data.novels || [], {api}, PixivNovel)
}
/** User id */
get id() {
return this.user.id
}
/** Get user detail information */
detail() {
if (this.api.allowCache && this.profile) return Promise.resolve(this)
return this.api.search('user', this.id, 'detail', {}, (data) => {
return Object.assign(this, data)
})
}
/** Get user illustrations */
illusts() {
if (this.api.allowCache && this._illusts.length) return Promise.resolve(this._illusts)
return this.api.search('user', this.id, 'illusts', {}, (data) => {
this._illusts = collect(data.illusts, {...data, api: this.api}, PixivIllust)
return this._illusts
})
}
/** Get user novels */
novels() {
if (this.api.allowCache && this._novels.length) return Promise.resolve(this._novels)
return this.api.search('user', this.id, 'novels', {}, (data) => {
this._novels = collect(data.novels, {...data, api: this.api}, PixivNovel)
return this._novels
})
}
/** Get user followers */
followers() {
if (this.api.allowCache && this._followers) return Promise.resolve(this._followers)
return this.api.search('user', this.id, 'follower', {}, (data) => {
this._followers = collect(data.user_previews, {...data, api: this.api}, PixivUser)
return this._followers
})
}
/** Get user followings */
followings() {
if (this.api.allowCache && this._followings) return Promise.resolve(this._followings)
return this.api.search('user', this.id, 'following', {}, (data) => {
this._followings = collect(data.user_previews, {...data, api: this.api}, PixivUser)
return this._followings
})
}
/**
* Follow user
* @param {string} restrict Restriction
*
* Restriction can be `public` or `private`.
**/
follow(restrict = 'public') {
return this.api.postRequest('/v1/user/follow/add', {
user_id: this.id,
restrict,
})
}
/** Unfollow user */
unfollow() {
return this.api.postRequest('/v1/user/follow/delete', {
user_id: this.id
})
}
}
PixivUser.type = 'illusts'
class PixivIllust {
constructor(data, api) {
Object.assign(this, data)
/** @private Pixiv API */
this.api = api
}
/** Illustrator */
author() {
return Promise.resolve(new PixivUser({user: this.user}, this.api))
}
/** Get illustration detail information */
detail() {
if (this.api.allowCache) return Promise.resolve(this)
return this.api.search('illust', this.id, 'detail', {}, (data) => {
return Object.assign(this, data)
})
}
/** Get illustration bookmark information */
bookmark() {
if (this.api.allowCache && this._bookmark) return Promise.resolve(this._bookmark)
return this.api.search('illust', this.id, 'bookmarkDetail', {}, (data) => {
this._bookmark = data
return data
})
}
/** Get illustration comments */
comments() {
if (this.api.allowCache && this._comments) return Promise.resolve(this._comments)
return this.api.search('illust', this.id, 'comments', {}, (data) => {
this._comments = collect(data.comments, {...data, api: this.api}, PixivComment)
return this._comments
})
}
/** Get related illustrations */
related() {
if (this.api.allowCache && this._related) return Promise.resolve(this._related)
return this.api.search('illust', this.id, 'related', {}, (data) => {
this._related = collect(data.illusts, {...data, api: this.api}, PixivIllust)
return this._related
})
}
/**
* Add a comment
* @param {string} comment Comment
**/
addComment(comment) {
if (!comment) return Promise.reject(new TypeError('comment required'))
return this.api.postRequest('/v1/illust/comment/add', {
illust_id: this.id,
comment
})
}
/**
* Add bookmark
* @param {Array<string>} tags Tags to add
* @param {string} restrict Restriction
*
* Restriction can be `public` or `private`.
**/
addBookmark(tags = [], restrict = 'public') {
if (!(tags instanceof Array)) return Promise.reject(new TypeError('invalid tags'))
return this.api.postRequest('/v2/illust/bookmark/add', {
illust_id: this.id,
restrict,
tags,
})
}
/** Delete bookmark */
deleteBookmark() {
return this.api.postRequest('/v1/illust/bookmark/delete', {
illust_id: this.id
})
}
}
class PixivNovel {
constructor(data, api) {
Object.assign(this, data)
/** @private Pixiv API */
this.api = api
}
/** Author */
author() {
return Promise.resolve(new PixivUser({user: this.user}, this.api))
}
/**
* Add a comment
* @param {string} comment Comment
**/
addComment(comment) {
if (!comment) return Promise.reject(new TypeError('comment required'))
return this.api.postRequest('/v1/novel/comment/add', {
novel_id: this.id,
comment
})
}
/**
* Add bookmark
* @param {Array<string>} tags Tags to add
* @param {string} restrict Restriction
*
* Restriction can be `public` or `private`.
**/
addBookmark(tags = [], restrict = 'public') {
if (!(tags instanceof Array)) return Promise.reject(new TypeError('invalid tags'))
return this.api.postRequest('/v2/novel/bookmark/add', {
novel_id: this.id,
restrict,
tags,
})
}
/** Delete bookmark */
deleteBookmark() {
return this.api.postRequest('/v1/novel/bookmark/delete', {
novel_id: this.id
})
}
}
class PixivComment {
constructor(data, api) {
Object.assign(this, data)
/** @private Pixiv API */
this.api = api
}
/** Author */
author() {
return Promise.resolve(new PixivUser({user: this.user}, this.api))
}
/** Get comment replies */
replies() {
if (this.api.allowCache && this._replies) return Promise.resolve(this._replies)
return this.api.search('comment', this.id, 'replies', {}, (data) => {
this._replies = collect(data.comments, {...data, api: this.api}, PixivComment)
return this._replies
})
}
}
PixivIllust.type = 'illusts'
PixivNovel.type = 'novels'
PixivUser.type = 'user_previews'
PixivComment.type = 'comments'
module.exports = {
PixivIllust,
PixivNovel,
PixivComment,
PixivUser,
collect: type => (data, api) => collect(data[type.type], {...data, api}, type)
}