libpixiv 0.2.5

pixiv.net API client library
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use strum::Display;

/// Some comment on content
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Comment {
    pub id: i64,
    pub comment: String,
    pub date: String,
    pub user: User,
    pub has_replies: bool,
    pub stamp: Option<Stamp>,
}

/// Some comment stamp
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Stamp {
    pub stamp_id: i64,
    pub stamp_url: String,
}

/// Some pixiv novel
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Novel {
    pub id: i64,
    pub title: String,
    pub caption: String,
    pub restrict: i64,
    pub x_restrict: i64,
    pub is_original: bool,
    pub image_urls: IllustrationURLs,
    pub create_date: String,
    pub tags: Vec<Tag>,
    pub page_count: i64,
    pub text_length: i64,
    pub user: User,
    #[serde(deserialize_with = "Novel::deserialize_series")]
    pub series: Option<Series>,
    pub is_bookmarked: bool,
    pub total_bookmarks: i64,
    pub total_view: i64,
    pub visible: bool,
    pub total_comments: i64,
    pub is_muted: bool,
    pub is_mypixiv_only: bool,
    pub is_x_restricted: bool,
    pub novel_ai_type: i64,
    pub request: Value,
}

// based on https://users.rust-lang.org/t/serde-deserialize-empty-string-as-option-none/116201/2
impl Novel {
    fn deserialize_series<'de, D>(d: D) -> Result<Option<Series>, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[non_exhaustive]
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum Helper {
            R(Series),
            #[allow(dead_code)] // value is still required for "validation"
            S(Value),
        }

        match Helper::deserialize(d) {
            Ok(Helper::S(_)) => Ok(None),
            Ok(Helper::R(s)) => Ok(Some(s)),
            Err(e) => Err(e),
        }
    }
}

/// Some pixiv user response.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PixivUser {
    pub id: i64,
    pub name: String,
    pub account: String,
    pub profile_image_urls: ProfileImageUrls,
    pub comment: Option<String>,
    pub is_followed: bool,
    pub is_access_blocking_user: Option<bool>,
}

/// Some user profile image.
///
/// Only one resolution is currently provided by the API.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProfileImageUrls {
    pub medium: String,
}

/// Some full user profile
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Profile {
    pub webpage: Value,
    pub gender: String,
    pub birth: String,
    pub birth_day: String,
    pub birth_year: i64,
    pub region: String,
    pub address_id: i64,
    pub country_code: String,
    pub job: String,
    pub job_id: i64,
    pub total_follow_users: i64,
    pub total_mypixiv_users: i64,
    pub total_illusts: i64,
    pub total_manga: i64,
    pub total_novels: i64,
    pub total_illust_bookmarks_public: i64,
    pub total_illust_series: i64,
    pub total_novel_series: i64,
    pub background_image_url: String,
    pub twitter_account: String,
    pub twitter_url: String,
    pub pawoo_url: Value,
    pub is_premium: bool,
    pub is_using_custom_profile_image: bool,
}

/// Private data provided by some user.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProfilePublicity {
    pub gender: String,
    pub region: String,
    pub birth_day: String,
    pub birth_year: String,
    pub job: String,
    pub pawoo: bool,
}

/// Some user's workspace metadata.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Workspace {
    pub pc: String,
    pub monitor: String,
    pub tool: String,
    pub scanner: String,
    pub tablet: String,
    pub mouse: String,
    pub printer: String,
    pub desktop: String,
    pub music: String,
    pub desk: String,
    pub chair: String,
    pub comment: String,
    pub workspace_image_url: Value,
}

/// Some ugoira metadata.
///
/// Ugoiras are pixivs "lossless" GIFs provided as zipped image sequences.
/// There is currently no helper that handles GIF encoding, so you will need
/// to do that yourself for now.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UgoiraMetadata {
    pub zip_urls: ZipUrls,
    pub frames: Vec<Frame>,
}

/// Wrapper type for some zip url used for ugoiras.
///
/// There is currently only one ugoira resoluton is available.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ZipUrls {
    pub medium: String,
}

/// An ugoira frame.
///
/// Yes, the delay may differ per frame and its thus a good idea to not only check
/// the first frame when building the Ugoira.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Frame {
    pub file: String,
    pub delay: i64,
}

/// Content type
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[non_exhaustive]
#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Display, Clone)]
#[serde(rename_all = "lowercase")]
pub enum ContentType {
    #[strum(to_string = "illust")]
    Illust,
    #[strum(to_string = "manga")]
    Manga,
    #[strum(to_string = "ugoira")]
    Ugoira,
}

/// An Illustration
///
/// This struct holds most of the available data.
///
/// Please mind that as with all assets hosted by pixiv, you need to use `Referer: https`
///
/// Some of the properties like `restrict` are not yet mapped to some enum,
/// those will be breaking changes and thus only introduced during major releases.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Illustration {
    pub id: u32,
    pub tags: Vec<Tag>,
    pub visible: bool,
    pub title: String,
    pub r#type: ContentType,
    pub caption: String,
    pub height: u32,
    pub width: u32,
    pub page_count: i32,
    pub user: User,
    pub tools: Vec<String>,
    pub series: Option<SeriesStub>,
    pub restrict: i32,
    pub x_restrict: i32,
    pub image_urls: IllustrationURLs,
    pub meta_single_page: Option<IllustrationMetaURL>,
    pub meta_pages: Vec<IllustrationURLsWrapper>,
    pub total_view: u32,
    pub total_bookmarks: u32,
    pub is_bookmarked: bool,
    pub is_muted: bool,
    pub total_comments: Option<u32>,
    pub illust_ai_type: u32,
    pub illust_book_style: u32,
    pub comment_access_control: Option<u32>,
    pub create_date: String,
}

/// Some illustration series.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SeriesStub {
    pub id: u32,
    pub title: String,
}

/// Some user.
///
/// While this is a smaller version of the `PixivUser` that is returned when calling the
/// user specific endpoints, this will be returned on other endpoints like for illustrations.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct User {
    pub id: i32,
    pub name: String,
    pub account: String,
    pub profile_image_urls: UserProfileImages,
    pub is_followed: Option<bool>,
}

/// Some user profile image
///
/// There is currently only one resolution available.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UserProfileImages {
    pub medium: String,
}

/// Wrapper type for image urls.
///
/// Not for direct use, only for its content.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IllustrationURLsWrapper {
    pub image_urls: IllustrationURLs,
}

/// Wrapper type for the (optional) original image url
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IllustrationMetaURL {
    pub original_image_url: Option<String>,
}

/// Collection of image urls
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IllustrationURLs {
    pub square_medium: String,
    pub medium: String,
    pub large: String,
    pub original: Option<String>,
}

/// A tag
///
/// Essentially a key value pair, the `translated_name` is not
/// guarenteed to be filled at all, especially if the `Accept-Language`
/// header is missing.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Tag {
    pub name: String,
    pub translated_name: Option<String>,
}

#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PixivUserResult {
    pub user: PixivUser,
    pub profile: Option<Profile>,
    pub profile_publicity: Option<ProfilePublicity>,
    pub workspace: Option<Workspace>,
}

#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PixivUserStateResult {
    pub user_state: UserState,
    pub profile: StateProfile,
}

#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UserState {
    pub is_mail_authorized: bool,
    pub has_mail_address: bool,
    pub has_changed_pixiv_id: bool,
    pub can_change_pixiv_id: bool,
    pub has_password: bool,
    pub require_policy_agreement: bool,
    pub no_login_method: bool,
    pub is_user_restricted: bool,
    pub is_official_event_notifications_enabled: bool,
}

#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StateProfile {
    pub user_id: i32,
    pub pixiv_id: String,
    pub name: String,
    pub profile_image_urls: ProfileImageUrls,
    pub is_premium: bool,
    pub x_restrict: i64,
}

/// umbrella type for API results
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PixivResult {
    pub comments: Option<Vec<Comment>>,
    pub illust: Option<Illustration>,
    pub illusts: Option<Vec<Illustration>>,
    pub error: Option<PixivError>,
    pub ugoira_metadata: Option<UgoiraMetadata>,
    pub user_previews: Option<Vec<PixivUserResult>>,
    pub tags: Option<Vec<Tag>>,
    pub novels: Option<Vec<Novel>>,
    pub users: Option<Vec<Value>>,
}

#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PixivError {
    user_message: String,
    message: String,
    reason: String,
}

impl std::error::Error for PixivError {}
impl std::fmt::Display for PixivError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}: {}", self.user_message, self.reason)
    }
}

#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionResponseWrapper {
    pub response: SessionResponse,
}

#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionResponse {
    pub access_token: String,
    pub refresh_token: String,
    pub expires_in: i64,
}

/// Some series cover image.
///
/// Only one resolution is currently provided by the API.
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SeriesCoverUrls {
    pub medium: String,
}

/// Series metadata
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct Series {
    pub id: u32,
    pub title: String,
    pub caption: String,
    pub cover_image_urls: SeriesCoverUrls,
    pub series_work_count: u32,
    pub create_date: DateTime<Utc>,
    pub width: u32,
    pub height: u32,
    pub user: User,
    pub watchlist_added: bool,
}

/// A series response containing series metadata + related illustrations
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[non_exhaustive]
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub struct SeriesResponse {
    pub illust_series_detail: Series,
    pub illust_series_first_illust: Illustration,
    pub illusts: Vec<Illustration>,
    pub error: Option<PixivError>,
}