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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
import json
import re
from datetime import date, timedelta
from enum import Enum
from typing import Any, Literal, Optional, Union, cast, overload

from hibiapi.api.pixiv.constants import PixivConstants
from hibiapi.api.pixiv.net import NetRequest as PixivNetClient
from hibiapi.utils.cache import cache_config
from hibiapi.utils.decorators import enum_auto_doc
from hibiapi.utils.net import catch_network_error
from hibiapi.utils.routing import BaseEndpoint, dont_route, request_headers


@enum_auto_doc
class IllustType(str, Enum):
    """็”ปไฝœ็ฑปๅž‹"""

    illust = "illust"
    """ๆ’็”ป"""
    manga = "manga"
    """ๆผซ็”ป"""


@enum_auto_doc
class RankingType(str, Enum):
    """ๆŽ’่กŒๆฆœๅ†…ๅฎน็ฑปๅž‹"""

    day = "day"
    """ๆ—ฅๆฆœ"""
    week = "week"
    """ๅ‘จๆฆœ"""
    month = "month"
    """ๆœˆๆฆœ"""
    day_male = "day_male"
    """็”ทๆ€งๅ‘"""
    day_female = "day_female"
    """ๅฅณๆ€งๅ‘"""
    week_original = "week_original"
    """ๅŽŸๅˆ›ๅ‘จๆฆœ"""
    week_rookie = "week_rookie"
    """ๆ–ฐไบบๅ‘จๆฆœ"""
    day_ai = "day_ai"
    """AIๆ—ฅๆฆœ"""
    day_manga = "day_manga"
    """ๆผซ็”ปๆ—ฅๆฆœ"""
    week_manga = "week_manga"
    """ๆผซ็”ปๅ‘จๆฆœ"""
    month_manga = "month_manga"
    """ๆผซ็”ปๆœˆๆฆœ"""
    week_rookie_manga = "week_rookie_manga"
    """ๆผซ็”ปๆ–ฐไบบๅ‘จๆฆœ"""
    day_r18 = "day_r18"
    day_male_r18 = "day_male_r18"
    day_female_r18 = "day_female_r18"
    week_r18 = "week_r18"
    week_r18g = "week_r18g"
    day_r18_ai = "day_r18_ai"
    day_r18_manga = "day_r18_manga"
    week_r18_manga = "week_r18_manga"


@enum_auto_doc
class SearchModeType(str, Enum):
    """ๆœ็ดขๅŒน้…็ฑปๅž‹"""

    partial_match_for_tags = "partial_match_for_tags"
    """ๆ ‡็ญพ้ƒจๅˆ†ไธ€่‡ด"""
    exact_match_for_tags = "exact_match_for_tags"
    """ๆ ‡็ญพๅฎŒๅ…จไธ€่‡ด"""
    title_and_caption = "title_and_caption"
    """ๆ ‡้ข˜่ฏดๆ˜Žๆ–‡"""


@enum_auto_doc
class SearchNovelModeType(str, Enum):
    """ๆœ็ดขๅŒน้…็ฑปๅž‹"""

    partial_match_for_tags = "partial_match_for_tags"
    """ๆ ‡็ญพ้ƒจๅˆ†ไธ€่‡ด"""
    exact_match_for_tags = "exact_match_for_tags"
    """ๆ ‡็ญพๅฎŒๅ…จไธ€่‡ด"""
    text = "text"
    """ๆญฃๆ–‡"""
    keyword = "keyword"
    """ๅ…ณ้”ฎ่ฏ"""


@enum_auto_doc
class SearchSortType(str, Enum):
    """ๆœ็ดขๆŽ’ๅบ็ฑปๅž‹"""

    date_desc = "date_desc"
    """ๆŒ‰ๆ—ฅๆœŸๅ€’ๅบ"""
    date_asc = "date_asc"
    """ๆŒ‰ๆ—ฅๆœŸๆญฃๅบ"""
    popular_desc = "popular_desc"
    """ๅ—ๆฌข่ฟŽ้™ๅบ(PremiumๅŠŸ่ƒฝ)"""


@enum_auto_doc
class SearchDurationType(str, Enum):
    """ๆœ็ดขๆ—ถๆฎต็ฑปๅž‹"""

    within_last_day = "within_last_day"
    """ไธ€ๅคฉๅ†…"""
    within_last_week = "within_last_week"
    """ไธ€ๅ‘จๅ†…"""
    within_last_month = "within_last_month"
    """ไธ€ไธชๆœˆๅ†…"""


class RankingDate(date):
    @classmethod
    def yesterday(cls) -> "RankingDate":
        yesterday = cls.today() - timedelta(days=1)
        return cls(yesterday.year, yesterday.month, yesterday.day)

    def toString(self) -> str:
        return self.strftime(r"%Y-%m-%d")

    @classmethod
    def new(cls, date: date) -> "RankingDate":
        return cls(date.year, date.month, date.day)


class PixivEndpoints(BaseEndpoint):
    @staticmethod
    def _parse_accept_language(accept_language: str) -> str:
        first_language, *_ = accept_language.partition(",")
        language_code, *_ = first_language.partition(";")
        return language_code.lower().strip()

    @overload
    async def request(
        self,
        endpoint: str,
        *,
        params: Optional[dict[str, Any]] = None,
        return_text: Literal[False] = False,
    ) -> dict[str, Any]: ...

    @overload
    async def request(
        self,
        endpoint: str,
        *,
        params: Optional[dict[str, Any]] = None,
        return_text: Literal[True],
    ) -> str: ...

    @dont_route
    @catch_network_error
    async def request(
        self,
        endpoint: str,
        *,
        params: Optional[dict[str, Any]] = None,
        return_text: bool = False,
    ) -> Union[dict[str, Any], str]:
        headers = self.client.headers.copy()

        net_client = cast(PixivNetClient, self.client.net_client)
        async with net_client.auth_lock:
            auth, token = net_client.get_available_user()
            if auth is None:
                auth = await net_client.auth(token)
        headers["Authorization"] = f"Bearer {auth.access_token}"

        if language := request_headers.get().get("Accept-Language"):
            language = self._parse_accept_language(language)
            headers["Accept-Language"] = language

        response = await self.client.get(
            self._join(
                base=PixivConstants.APP_HOST,
                endpoint=endpoint,
                params=params or {},
            ),
            headers=headers,
        )
        if return_text:
            return response.text
        return response.json()

    @cache_config(ttl=timedelta(days=3))
    async def illust(self, *, id: int):
        return await self.request("v1/illust/detail", params={"illust_id": id})

    @cache_config(ttl=timedelta(days=1))
    async def member(self, *, id: int):
        return await self.request("v1/user/detail", params={"user_id": id})

    async def member_illust(
        self,
        *,
        id: int,
        illust_type: IllustType = IllustType.illust,
        page: int = 1,
        size: int = 30,
    ):
        return await self.request(
            "v1/user/illusts",
            params={
                "user_id": id,
                "type": illust_type,
                "offset": (page - 1) * size,
            },
        )

    async def favorite(
        self,
        *,
        id: int,
        tag: Optional[str] = None,
        max_bookmark_id: Optional[int] = None,
    ):
        return await self.request(
            "v1/user/bookmarks/illust",
            params={
                "user_id": id,
                "tag": tag,
                "restrict": "public",
                "max_bookmark_id": max_bookmark_id or None,
            },
        )

    # ็”จๆˆทๆ”ถ่—็š„ๅฐ่ฏด
    async def favorite_novel(
        self,
        *,
        id: int,
        tag: Optional[str] = None,
    ):
        return await self.request(
            "v1/user/bookmarks/novel",
            params={
                "user_id": id,
                "tag": tag,
                "restrict": "public",
            },
        )

    async def following(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "v1/user/following",
            params={
                "user_id": id,
                "offset": (page - 1) * size,
            },
        )

    async def follower(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "v1/user/follower",
            params={
                "user_id": id,
                "offset": (page - 1) * size,
            },
        )

    @cache_config(ttl=timedelta(hours=12))
    async def rank(
        self,
        *,
        mode: RankingType = RankingType.week,
        date: Optional[RankingDate] = None,
        page: int = 1,
        size: int = 30,
    ):
        return await self.request(
            "v1/illust/ranking",
            params={
                "mode": mode,
                "date": RankingDate.new(date or RankingDate.yesterday()).toString(),
                "offset": (page - 1) * size,
            },
        )

    async def search(
        self,
        *,
        word: str,
        mode: SearchModeType = SearchModeType.partial_match_for_tags,
        order: SearchSortType = SearchSortType.date_desc,
        duration: Optional[SearchDurationType] = None,
        page: int = 1,
        size: int = 30,
        include_translated_tag_results: bool = True,
        search_ai_type: bool = True,  # ๆœ็ดข็ป“ๆžœๆ˜ฏๅฆๅŒ…ๅซAIไฝœๅ“
    ):
        return await self.request(
            "v1/search/illust",
            params={
                "word": word,
                "search_target": mode,
                "sort": order,
                "duration": duration,
                "offset": (page - 1) * size,
                "include_translated_tag_results": include_translated_tag_results,
                "search_ai_type": 1 if search_ai_type else 0,
            },
        )

    # ็ƒญ้—จๆ’็”ปไฝœๅ“้ข„่งˆ
    async def popular_preview(
        self,
        *,
        word: str,
        mode: SearchModeType = SearchModeType.partial_match_for_tags,
        merge_plain_keyword_results: bool = True,
        include_translated_tag_results: bool = True,
        filter: str = "for_ios",
    ):
        return await self.request(
            "v1/search/popular-preview/illust",
            params={
                "word": word,
                "search_target": mode,
                "merge_plain_keyword_results": merge_plain_keyword_results,
                "include_translated_tag_results": include_translated_tag_results,
                "filter": filter,
            },
        )

    async def search_user(
        self,
        *,
        word: str,
        page: int = 1,
        size: int = 30,
    ):
        return await self.request(
            "v1/search/user",
            params={"word": word, "offset": (page - 1) * size},
        )

    async def tags_autocomplete(
        self,
        *,
        word: str,
        merge_plain_keyword_results: bool = True,
    ):
        return await self.request(
            "/v2/search/autocomplete",
            params={
                "word": word,
                "merge_plain_keyword_results": merge_plain_keyword_results,
            },
        )

    @cache_config(ttl=timedelta(hours=12))
    async def tags(self):
        return await self.request("v1/trending-tags/illust")

    @cache_config(ttl=timedelta(minutes=15))
    async def related(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "v2/illust/related",
            params={
                "illust_id": id,
                "offset": (page - 1) * size,
            },
        )

    @cache_config(ttl=timedelta(days=3))
    async def ugoira_metadata(self, *, id: int):
        return await self.request(
            "v1/ugoira/metadata",
            params={
                "illust_id": id,
            },
        )

    # ๅคงๅฎถ็š„ๆ–ฐไฝœๅ“๏ผˆๆ’็”ป๏ผ‰
    async def illust_new(
        self,
        *,
        content_type: str = "illust",
    ):
        return await self.request(
            "v1/illust/new",
            params={
                "content_type": content_type,
                "filter": "for_ios",
            },
        )

    # pixivision(ไบฎ็‚น/็‰น่พ‘) ๅˆ—่กจ
    async def spotlights(
        self,
        *,
        category: str = "all",
        page: int = 1,
        size: int = 10,
    ):
        return await self.request(
            "v1/spotlight/articles",
            params={
                "filter": "for_ios",
                "category": category,
                "offset": (page - 1) * size,
            },
        )

    # ๆ’็”ป่ฏ„่ฎบ
    async def illust_comments(
        self,
        *,
        id: int,
        page: int = 1,
        size: int = 30,
    ):
        return await self.request(
            "v3/illust/comments",
            params={
                "illust_id": id,
                "offset": (page - 1) * size,
            },
        )

    # ๆ’็”ป่ฏ„่ฎบๅ›žๅค
    async def illust_comment_replies(
        self,
        *,
        id: int,
    ):
        return await self.request(
            "v2/illust/comment/replies",
            params={
                "comment_id": id,
            },
        )

    # ๅฐ่ฏด่ฏ„่ฎบ
    async def novel_comments(
        self,
        *,
        id: int,
        page: int = 1,
        size: int = 30,
    ):
        return await self.request(
            "v3/novel/comments",
            params={
                "novel_id": id,
                "offset": (page - 1) * size,
            },
        )

    # ๅฐ่ฏด่ฏ„่ฎบๅ›žๅค
    async def novel_comment_replies(
        self,
        *,
        id: int,
    ):
        return await self.request(
            "v2/novel/comment/replies",
            params={
                "comment_id": id,
            },
        )

    # ๅฐ่ฏดๆŽ’่กŒๆฆœ
    async def rank_novel(
        self,
        *,
        mode: str = "day",
        date: Optional[RankingDate] = None,
        page: int = 1,
        size: int = 30,
    ):
        return await self.request(
            "v1/novel/ranking",
            params={
                "mode": mode,
                "date": RankingDate.new(date or RankingDate.yesterday()).toString(),
                "offset": (page - 1) * size,
            },
        )

    async def member_novel(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "/v1/user/novels",
            params={
                "user_id": id,
                "offset": (page - 1) * size,
            },
        )

    async def novel_series(self, *, id: int):
        return await self.request("/v2/novel/series", params={"series_id": id})

    async def novel_detail(self, *, id: int):
        return await self.request("/v2/novel/detail", params={"novel_id": id})

    # ๅทฒ่ขซๅฎ˜ๆ–น็งป้™ค๏ผŒ่ฐƒ็”จ webview/v2/novel ไฝœๅ…ผๅฎนๅค„็†
    async def novel_text(self, *, id: int):
        # return await self.request("/v1/novel/text", params={"novel_id": id})
        response = await self.webview_novel(id=id)
        return {"novel_text": response["text"] or ""}

    # ่Žทๅ–ๅฐ่ฏด HTML ๅŽ่งฃๆž JSON
    async def webview_novel(self, *, id: int):
        response = await self.request(
            "webview/v2/novel",
            params={
                "id": id,
                "viewer_version": "20221031_ai",
            },
            return_text=True,
        )

        novel_match = re.search(r"novel:\s+(?P<data>{.+?}),\s+isOwnWork", response)
        return json.loads(novel_match["data"] if novel_match else response)

    @cache_config(ttl=timedelta(hours=12))
    async def tags_novel(self):
        return await self.request("v1/trending-tags/novel")

    async def search_novel(
        self,
        *,
        word: str,
        mode: SearchNovelModeType = SearchNovelModeType.partial_match_for_tags,
        sort: SearchSortType = SearchSortType.date_desc,
        merge_plain_keyword_results: bool = True,
        include_translated_tag_results: bool = True,
        duration: Optional[SearchDurationType] = None,
        page: int = 1,
        size: int = 30,
        search_ai_type: bool = True,  # ๆœ็ดข็ป“ๆžœๆ˜ฏๅฆๅŒ…ๅซAIไฝœๅ“
    ):
        return await self.request(
            "/v1/search/novel",
            params={
                "word": word,
                "search_target": mode,
                "sort": sort,
                "merge_plain_keyword_results": merge_plain_keyword_results,
                "include_translated_tag_results": include_translated_tag_results,
                "duration": duration,
                "offset": (page - 1) * size,
                "search_ai_type": 1 if search_ai_type else 0,
            },
        )

    # ็ƒญ้—จๅฐ่ฏดไฝœๅ“้ข„่งˆ
    async def popular_preview_novel(
        self,
        *,
        word: str,
        mode: SearchNovelModeType = SearchNovelModeType.partial_match_for_tags,
        merge_plain_keyword_results: bool = True,
        include_translated_tag_results: bool = True,
        filter: str = "for_ios",
    ):
        return await self.request(
            "v1/search/popular-preview/novel",
            params={
                "word": word,
                "search_target": mode,
                "merge_plain_keyword_results": merge_plain_keyword_results,
                "include_translated_tag_results": include_translated_tag_results,
                "filter": filter,
            },
        )

    async def novel_new(self, *, max_novel_id: Optional[int] = None):
        return await self.request(
            "/v1/novel/new", params={"max_novel_id": max_novel_id}
        )

    # ไบบๆฐ”็›ดๆ’ญๅˆ—่กจ
    async def live_list(self, *, page: int = 1, size: int = 30):
        params = {"list_type": "popular", "offset": (page - 1) * size}
        if not params["offset"]:
            del params["offset"]
        return await self.request("v1/live/list", params=params)

    # ็›ธๅ…ณๅฐ่ฏดไฝœๅ“
    async def related_novel(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "v1/novel/related",
            params={
                "novel_id": id,
                "offset": (page - 1) * size,
            },
        )

    # ็›ธๅ…ณ็”จๆˆท
    async def related_member(self, *, id: int):
        return await self.request("v1/user/related", params={"seed_user_id": id})

    # ๆผซ็”ป็ณปๅˆ—
    async def illust_series(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "v1/illust/series",
            params={"illust_series_id": id, "offset": (page - 1) * size},
        )

    # ็”จๆˆท็š„ๆผซ็”ป็ณปๅˆ—
    async def member_illust_series(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "v1/user/illust-series",
            params={"user_id": id, "offset": (page - 1) * size},
        )

    # ็”จๆˆท็š„ๅฐ่ฏด็ณปๅˆ—
    async def member_novel_series(self, *, id: int, page: int = 1, size: int = 30):
        return await self.request(
            "v1/user/novel-series", params={"user_id": id, "offset": (page - 1) * size}
        )