rrpx 0.1.0

Pixiv client writen in Rust
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# API Endpoints Reference

## Overview

PixivPy implements over 50 API endpoints across four main categories: Illustrations, Users, Novels, and Search/Discovery. Each endpoint returns structured data using Pydantic models for type safety.

## Base API Configuration

### Base URL
- Primary: `https://app-api.pixiv.net`
- All endpoints require authentication via Bearer token

### Common Headers
```http
Authorization: Bearer {access_token}
User-Agent: PixivIOSApp/7.13.3 (iOS 14.6; iPhone13,2)
Accept-Language: en-us (optional, for tag translations)
```

### Common Parameters
- `filter`: `"for_ios"` (default) or `""` (no filter)
- `restrict`: `"public"` or `"private"` (for bookmarks/following)

## 1. Illustration APIs

### 1.1 Get Illustration Details
```python
api.illust_detail(illust_id: int) -> IllustrationInfo
```
- **Endpoint**: `GET /v1/illust/detail`
- **Parameters**:
  - `illust_id`: Illustration ID
- **Returns**: Complete illustration metadata

### 1.2 Illustration Rankings
```python
api.illust_ranking(
    mode: str = "day",
    filter: str = "for_ios"
) -> RankingInfo
```
- **Endpoint**: `GET /v1/illust/ranking`
- **Modes**:
  - `day`: Daily ranking
  - `week`: Weekly ranking
  - `month`: Monthly ranking
  - `day_male`: Daily (male)
  - `day_female`: Daily (female)
  - `week_original`: Weekly (original)
  - `week_rookie`: Weekly (rookie)
  - `day_r18`: Daily (R-18)
  - `day_male_r18`, `day_female_r18`, `week_r18`, etc.
- **Returns**: Ranked illustrations with pagination

### 1.3 Recommended Illustrations
```python
api.illust_recommended(
    content_type: str = "illust",
    include_ranking_label: bool = True,
    filter: str = "for_ios",
    max_bookmark_id_for_recommend: int = None,
    min_bookmark_id_for_recent_illust: int = None,
    offset: int = None,
    user_id: int = None,
    view_records: List[int] = None
) -> RecommendedInfo
```
- **Endpoint**: `GET /v1/illust/recommended`
- **Features**: Personalized recommendations based on user preferences
- **Returns**: Recommended illustrations with variety

### 1.4 Related Illustrations
```python
api.illust_related(
    illust_id: int,
    filter: str = "for_ios",
    offset: int = None,
    seed_illust_ids: List[int] = None,
    view_records: List[int] = None
) -> RelatedInfo
```
- **Endpoint**: `GET /v2/illust/related`
- **Features**: Illustrations similar to the given one
- **Returns**: Related works

### 1.5 Following Users' New Works
```python
api.illust_follow(
    restrict: str = "public",
    filter: str = "for_ios",
    offset: int = None
) -> FollowingWorksInfo
```
- **Endpoint**: `GET /v2/illust/follow`
- **Parameters**:
  - `restrict`: `public` (from public follows) or `private` (from private follows)
- **Returns**: New illustrations from followed users

### 1.6 Latest Illustrations
```python
api.illust_new(
    content_type: str = "illust",
    filter: str = "for_ios",
    max_illust_id: int = None
) -> NewWorksInfo
```
- **Endpoint**: `GET /v1/illust/new`
- **Parameters**:
  - `content_type`: `illust` or `manga`
  - `max_illust_id`: Get works older than this ID
- **Returns**: Latest public illustrations

### 1.7 Illustration Comments
```python
api.illust_comments(
    illust_id: int,
    offset: int = None,
    include_total_comments: bool = True
) -> CommentsInfo
```
- **Endpoint**: `GET /v3/illust/comments`
- **Features**: Pagination support for comments
- **Returns**: Comment tree structure

## 2. User APIs

### 2.1 User Profile
```python
api.user_detail(
    user_id: int,
    filter: str = "for_ios"
) -> UserInfoDetailed
```
- **Endpoint**: `GET /v1/user/detail`
- **Returns**: Complete user profile including stats

### 2.2 User's Illustrations
```python
api.user_illusts(
    user_id: int,
    filter: str = "for_ios",
    offset: int = None,
    type: str = None
) -> UserWorksInfo
```
- **Endpoint**: `GET /v1/user/illusts`
- **Parameters**:
  - `type`: `illust`, `manga`, or `None` (both)
- **Returns**: User's illustration collection

### 2.3 User's Novels
```python
api.user_novels(
    user_id: int,
    filter: str = "for_ios",
    offset: int = None
) -> UserNovelsInfo
```
- **Endpoint**: `GET /v1/user/novels`
- **Returns**: User's novel collection

### 2.4 User's Bookmarked Illustrations
```python
api.user_bookmarks_illust(
    user_id: int,
    restrict: str = "public",
    filter: str = "for_ios",
    max_bookmark_id: int = None,
    tag: str = None
) -> UserBookmarksIllustrationsInfo
```
- **Endpoint**: `GET /v1/user/bookmarks/illust`
- **Parameters**:
  - `restrict`: `public` or `private` bookmarks
  - `tag`: Filter by bookmark tag
- **Returns**: User's bookmarked illustrations

### 2.5 User's Bookmarked Novels
```python
api.user_bookmarks_novel(
    user_id: int,
    restrict: str = "public",
    filter: str = "for_ios",
    max_bookmark_id: int = None
) -> UserBookmarksNovelInfo
```
- **Endpoint**: `GET /v1/user/bookmarks/novel`
- **Returns**: User's bookmarked novels

### 2.6 User's Following List
```python
api.user_following(
    user_id: int,
    restrict: str = "public",
    offset: int = None
) -> UserFollowingInfo
```
- **Endpoint**: `GET /v1/user/following`
- **Returns**: Users that the target user follows

### 2.7 User's Followers
```python
api.user_follower(
    user_id: int,
    filter: str = "for_ios",
    offset: int = None
) -> UserFollowerInfo
```
- **Endpoint**: `GET /v1/user/follower`
- **Returns**: Users that follow the target user

### 2.8 Follow/Unfollow User
```python
api.user_follow_add(user_id: int, restrict: str = "public") -> UserFollowingResult
api.user_follow_delete(user_id: int) -> UserFollowingResult
```
- **Endpoints**:
  - `POST /v1/user/follow/add`
  - `POST /v1/user/follow/delete`
- **Parameters**:
  - `restrict`: `public` or `private` (for follow)

### 2.9 Related Users
```python
api.user_related(
    seed_user_id: int,
    filter: str = "for_ios"
) -> RelatedUsersInfo
```
- **Endpoint**: `GET /v1/user/related`
- **Returns**: Users similar to the seed user

## 3. Novel APIs

### 3.1 Novel Details
```python
api.novel_detail(novel_id: int) -> NovelInfo
```
- **Endpoint**: `GET /v2/novel/detail`
- **Returns**: Complete novel metadata

### 3.2 Novel Content (Deprecated)
```python
api.novel_text(novel_id: int) -> NovelTextInfo
```
- **Endpoint**: `GET /v1/novel/text`
- **Status**: Deprecated, use `webview_novel` instead

### 3.3 Novel Content (Current)
```python
api.webview_novel(novel_id: int) -> WebViewNovelInfo
```
- **Endpoint**: `GET /webview/v2/novel`
- **Returns**: Formatted novel content

### 3.4 Novel Series
```python
api.novel_series(series_id: int) -> NovelSeriesInfo
```
- **Endpoint**: `GET /v2/novel/series`
- **Returns**: Novel series information

### 3.5 Novel Comments
```python
api.novel_comments(
    novel_id: int,
    offset: int = None,
    include_total_comments: bool = True
) -> NovelCommentsInfo
```
- **Endpoint**: `GET /v3/novel/comments`
- **Returns**: Comments on the novel

### 3.6 Following Users' New Novels
```python
api.novel_follow(
    restrict: str = "public",
    filter: str = "for_ios",
    offset: int = None
) -> NovelFollowInfo
```
- **Endpoint**: `GET /v1/novel/follow`
- **Returns**: New novels from followed users

## 4. Search and Discovery APIs

### 4.1 Search Illustrations
```python
api.search_illust(
    word: str,
    search_target: str = "partial_match_for_tags",
    sort: str = "date_desc",
    duration: str = None,
    filter: str = "for_ios",
    offset: int = None,
    merge_plain_keyword_results: bool = True,
    include_translated_tag_results: bool = True,
    search_ai_type: int = None
) -> SearchIllustrationsInfo
```
- **Endpoint**: `GET /v1/search/illust`
- **Parameters**:
  - `word`: Search keyword
  - `search_target`:
    - `partial_match_for_tags`: Partial tag match
    - `exact_match_for_tags`: Exact tag match
    - `title_and_caption`: Title/caption search
    - `keyword`: Keyword search
  - `sort`: `date_desc`, `date_asc`, `popular_desc`
  - `duration`: `within_last_day`, `within_last_week`, `within_last_month`
  - `search_ai_type`:
    - `None`: All content
    - `1`: Exclude AI content
    - `2`: Include AI content only

### 4.2 Search Novels
```python
api.search_novel(
    word: str,
    search_target: str = "partial_match_for_tags",
    sort: str = "date_desc",
    merge_plain_keyword_results: bool = True,
    include_translated_tag_results: bool = True,
    duration: str = None,
    filter: str = "for_ios",
    offset: int = None
) -> SearchNovelInfo
```
- **Endpoint**: `GET /v1/search/novel`
- **Similar parameters to `search_illust`**

### 4.3 Search Users
```python
api.search_user(
    word: str,
    filter: str = "for_ios",
    offset: int = None
) -> SearchUserInfo
```
- **Endpoint**: `GET /v1/search/user`
- **Returns**: Users matching the search term

### 4.4 Trending Tags for Illustrations
```python
api.trending_tags_illust(filter: str = "for_ios") -> TrendingTagsInfo
```
- **Endpoint**: `GET /v1/trending-tags/illust`
- **Returns**: Currently trending illustration tags

### 4.5 Trending Tags for Novels
```python
api.trending_tags_novel(filter: str = "for_ios") -> NovelTrendingTagsInfo
```
- **Endpoint**: `GET /v1/trending-tags/novel`
- **Returns**: Currently trending novel tags

## 5. Utility APIs

### 5.1 Parse Next URL
```python
api.parse_qs(next_url: str) -> Dict[str, Any]
```
- **Utility**: Extract query parameters from `next_url` for pagination
- **Usage**:
```python
result = api.search_illust("watercolor")
while result.next_url:
    next_qs = api.parse_qs(result.next_url)
    result = api.search_illust(**next_qs)
```

### 5.2 Download Illustration
```python
api.download(
    url: str,
    path: str = None,
    prefix: str = None,
    ext: str = None,
    replace: bool = False
) -> str
```
- **Features**:
  - Automatic referer header for Pixiv images
  - Progress tracking
  - File extension detection
  - Resume support
- **Returns**: Downloaded file path

## 6. Response Models

### 6.1 Common Response Structure
```python
class BaseResponse:
    # All API responses follow this pattern
    class Meta:
        status: int
        error: Optional[str]
    class Data:
        # Actual data varies by endpoint
```

### 6.2 Pagination Information
```python
class PaginatedResponse:
    next_url: Optional[str]  # URL for next page
    # Data items
```

## 7. Request Patterns

### 7.1 GET Requests
Most endpoints use GET with query parameters:
```python
api.illust_detail(59580629)
# -> GET /v1/illust/detail?illust_id=59580629
```

### 7.2 POST Requests
State-changing operations use POST:
```python
api.user_follow_add(12345)
# -> POST /v1/user/follow/add with form data
```

### 7.3 Content Filtering
All data endpoints support filtering:
```python
api.search_illust("tag", filter="for_ios")  # Default
api.search_illust("tag", filter="")        # No filtering
```

## 8. Rate Limits and Best Practices

### 8.1 Rate Limit Headers
```http
X-RateLimit-Limit: 120
X-RateLimit-Remaining: 118
X-RateLimit-Reset: 1640995200
```

### 8.2 Best Practices
1. **Implement delays** between requests (recommended: 1 second)
2. **Use pagination** efficiently with `next_url`
3. **Cache responses** for repeated requests
4. **Handle rate limits** gracefully
5. **Prefer specific endpoints** over generic search when possible

## 9. Example API Calls

### 9.1 Complete Workflow
```python
# Initialize and authenticate
api = AppPixivAPI()
api.auth(refresh_token="TOKEN")

# Search for illustrations
result = api.search_illust("landscape", sort="popular_desc")
for illust in result.illusts:
    print(f"Title: {illust.title}")
    print(f"Author: {illust.user.name}")

    # Get detailed info
    detail = api.illust_detail(illust.id)
    print(f"Views: {detail.illust.total_view}")

# Paginate through results
while result.next_url:
    next_qs = api.parse_qs(result.next_url)
    result = api.search_illust(**next_qs)
    # Process next page...
```

### 9.2 Advanced Search with Filters
```python
# Search recent illustrations, exclude AI content
result = api.search_illust(
    word="original character",
    search_target="partial_match_for_tags",
    sort="date_desc",
    duration="within_last_week",
    search_ai_type=1  # Exclude AI
)

# Filter by popularity
result = api.search_illust(
    word="fantasy art",
    sort="popular_desc",
    filter="for_ios"
)
```

### 9.3 User Data Collection
```python
# Get user profile and all their works
user_detail = api.user_detail(user_id=12345)
print(f"User: {user_detail.user.name}")

# Get all illustrations with pagination
illusts_result = api.user_illusts(user_id=12345)
all_illusts = list(illusts_result.illusts)

while illusts_result.next_url:
    next_qs = api.parse_qs(illusts_result.next_url)
    illusts_result = api.user_illusts(user_id=12345, **next_qs)
    all_illusts.extend(illusts_result.illusts)
```

## 10. Error Responses

### 10.1 Common Error Codes
- `200`: Success
- `400`: Bad Request (invalid parameters)
- `401`: Unauthorized (invalid/expired token)
- `403`: Forbidden (no access)
- `404`: Not Found
- `429`: Rate Limited
- `500`: Internal Server Error

### 10.2 Error Response Format
```json
{
    "has_error": true,
    "errors": {
        "system": {
            "message": "Error description"
        }
    }
}
```

## Rust Implementation Considerations

### 1. Endpoint Organization
```rust
pub mod endpoints {
    pub mod illust;
    pub mod user;
    pub mod novel;
    pub mod search;
}
```

### 2. Common Traits
```rust
pub trait PaginatedResponse {
    fn next_url(&self) -> Option<&str>;
    fn has_more(&self) -> bool;
}

pub trait Endpoint {
    type Response;
    type Parameters;

    fn endpoint() -> &'static str;
    fn method() -> Method;
}
```

### 3. Type-safe Parameters
```rust
#[derive(Debug, Clone, Copy)]
pub enum SearchTarget {
    PartialMatchForTags,
    ExactMatchForTags,
    TitleAndCaption,
    Keyword,
}

#[derive(Debug, Clone, Copy)]
pub enum SortOrder {
    DateDesc,
    DateAsc,
    PopularDesc,
}
```

### 4. Async Implementation
```rust
impl PixivClient {
    pub async fn search_illust(
        &self,
        params: SearchIllustParams,
    ) -> Result<SearchIllustrationsResponse, PixivError> {
        let url = format!("{}/v1/search/illust", self.base_url);
        let response = self
            .client
            .get(&url)
            .query(&params)
            .bearer_auth(&self.access_token)
            .send()
            .await?;

        Ok(response.json().await?)
    }
}
```