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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
"""

bilibili_api.manga



漫画相关操作

"""

import datetime
from enum import Enum
from urllib.parse import urlparse
from typing import Dict, List, Union, Optional

import httpx

from bilibili_api.utils.utils import get_api
from bilibili_api.errors import ArgsException
from bilibili_api.utils.picture import Picture
from bilibili_api.utils.credential import Credential
from bilibili_api.utils.network import HEADERS, Api

API = get_api("manga")


class MangaIndexFilter:
    """

    漫画索引筛选器类。

    """

    class Area(Enum):
        """

        漫画索引筛选器的地区枚举类。



        - ALL: 全部

        - CHINA: 大陆

        - JAPAN: 日本

        - SOUTHKOREA: 韩国

        - OTHER: 其他

        """

        ALL = -1
        CHINA = 1
        JAPAN = 2
        SOUTHKOREA = 6
        OTHER = 5

    class Order(Enum):
        """

        漫画索引筛选器的排序枚举类。



        - HOT: 人气推荐

        - UPDATE: 更新时间

        - RELEASE_DATE: 上架时间

        """

        HOT = 0
        UPDATE = 1
        RELEASE_DATE = 3

    class Status(Enum):
        """

        漫画索引筛选器的状态枚举类。



        - ALL: 全部

        - FINISHED: 完结

        - UNFINISHED: 连载

        """

        ALL = -1
        FINISHED = 1
        UNFINISHED = 0

    class Payment(Enum):
        """

        漫画索引筛选器的付费枚举类。



        - ALL: 全部

        - FREE: 免费

        - PAID: 付费

        - WILL_BE_FREE: 等就免费

        """

        ALL = -1
        FREE = 1
        PAID = 2
        WILL_BE_FREE = 3

    class Style(Enum):
        """

        漫画索引筛选器的风格枚举类。



        - ALL: 全部

        - WARM: 热血

        - ANCIENT: 古风

        - FANTASY: 玄幻

        - IMAGING: 奇幻

        - SUSPENSE: 悬疑

        - CITY: 都市

        - HISTORY: 历史

        - WUXIA: 武侠仙侠

        - GAME: 游戏竞技

        - PARANORMAL: 悬疑灵异

        - ALTERNATE: 架空

        - YOUTH: 青春

        - WEST_MAGIC: 西幻

        - MORDEN: 现代

        - POSITIVE: 正能量

        - SCIENCE_FICTION: 科幻

        """

        ALL = -1
        WARM = 999
        ANCIENT = 997
        FANTASY = 1016
        IMAGING = 998
        SUSPENSE = 1023
        CITY = 1002
        HISTORY = 1096
        WUXIA = 1092
        GAME = 1088
        PARANORMAL = 1081
        ALTERNATE = 1063
        YOUTH = 1060
        WEST_MAGIC = 1054
        MORDEN = 1048
        POSITIVE = 1028
        SCIENCE_FICTION = 1027


class Manga:
    """

    漫画类



    Attributes:

        credential (Credential): 凭据类。

    """

    def __init__(self, manga_id: int, credential: Optional[Credential] = None):
        """

        Args:

            manga_id   (int)              : 漫画 id



            credential (Credential | None): 凭据类. Defaults to None.

        """
        credential = credential if credential else Credential()
        self.__manga_id = manga_id
        self.credential = credential
        self.__info: Optional[Dict] = None

    def get_manga_id(self) -> int:
        return self.__manga_id

    async def get_info(self) -> dict:
        """

        获取漫画信息



        Returns:

            dict: 调用 API 返回的结果

        """
        api = API["info"]["detail"]
        params = {"comic_id": self.__manga_id}
        return (
            await Api(
                **api,
                credential=self.credential,
                no_csrf=(
                    False
                    if (
                        self.credential.has_sessdata()
                        and self.credential.has_bili_jct()
                    )
                    else True
                ),
            )
            .update_params(**params)
            .result
        )

    async def __get_info_cached(self) -> dict:
        """

        获取漫画信息,如果有缓存则使用缓存。

        """
        if self.__info == None:
            self.__info = await self.get_info()
        return self.__info

    async def get_episode_info(

        self,

        episode_count: Optional[Union[int, float]] = None,

        episode_id: Optional[int] = None,

    ) -> dict:
        """

        获取某一话的详细信息



        Args:

            episode_count (int | float | None): 第几话.



            episode_id    (int | None)        : 对应的话的 id. 可以通过 `get_episode_id` 获取。



        **注意:episode_count 和 episode_id 中必须提供一个参数。**



        Returns:

            dict: 对应的话的详细信息

        """
        info = await self.__get_info_cached()
        for ep in info["ep_list"]:
            if episode_count == None:
                if ep["id"] == episode_id:
                    return ep
            elif episode_id == None:
                if ep["ord"] == episode_count:
                    return ep
            else:
                raise ArgsException("episode_count 和 episode_id 中必须提供一个参数。")
        raise ArgsException("未找到对应的话")

    async def get_episode_id(

        self, episode_count: Optional[Union[int, float]] = None

    ) -> int:
        """

        获取某一话的 id



        Args:

            episode_count (int | float | None): 第几话.



        Returns:

            int: 对应的话的 id

        """
        return (await self.get_episode_info(episode_count=episode_count))["id"]

    async def get_images_url(

        self,

        episode_count: Optional[Union[int, float]] = None,

        episode_id: Optional[int] = None,

    ) -> dict:
        """

        获取某一话的图片链接。(未经过处理,所有的链接无法直接访问)



        获取的图片 url 请传入 `manga.manga_image_url_turn_to_Picture` 函数以转换为 `Picture` 类。



        Args:

            episode_count (int | float | None): 第几话.



            episode_id    (int | None)        : 对应的话的 id. 可以通过 `get_episode_id` 获取。



        **注意:episode_count 和 episode_id 中必须提供一个参数。**



        Returns:

            dict: 调用 API 返回的结果

        """
        if episode_id == None:
            if episode_count == None:
                raise ArgsException("episode_count 和 episode_id 中必须提供一个参数。")
            episode_id = await self.get_episode_id(episode_count)
        api = API["info"]["episode_images"]
        params = {"ep_id": episode_id}
        return (
            await Api(
                **api,
                credential=self.credential,
                no_csrf=(
                    False
                    if (
                        self.credential.has_sessdata()
                        and self.credential.has_bili_jct()
                    )
                    else True
                ),
            )
            .update_params(**params)
            .result
        )

    async def get_images(

        self,

        episode_count: Optional[Union[int, float]] = None,

        episode_id: Optional[int] = None,

    ) -> List[Dict]:
        """

        获取某一话的所有图片



        # 注意事项:此函数速度非常慢并且失败率高



        Args:

            episode_count (int | float | None): 第几话.



            episode_id    (int | None)        : 对应的话的 id. 可以通过 `get_episode_id` 获取。



        **注意:episode_count 和 episode_id 中必须提供一个参数。**



        Returns:

            List[Picture]: 所有的图片

        """
        data = await self.get_images_url(
            episode_count=episode_count, episode_id=episode_id
        )
        pictures: List[Dict] = []

        async def get_real_image_url(url: str) -> str:
            token_api = API["info"]["image_token"]
            datas = {"urls": f'["{url}"]'}
            token_data = (
                await Api(
                    **token_api,
                    credential=self.credential,
                    no_csrf=(
                        False
                        if (
                            self.credential.has_sessdata()
                            and self.credential.has_bili_jct()
                        )
                        else True
                    ),
                )
                .update_data(**datas)
                .result
            )
            return token_data[0]["url"] + "?token=" + token_data[0]["token"]

        for img in data["images"]:
            url = await get_real_image_url(img["path"])
            pictures.append(
                {
                    "x": img["x"],
                    "y": img["y"],
                    "picture": Picture.from_content(
                        (await httpx.AsyncClient().get(url, headers=HEADERS)).content,
                        "jpg",
                    ),
                }
            )
        return pictures


async def manga_image_url_turn_to_Picture(

    url: str, credential: Optional[Credential] = None

) -> Picture:
    """

    将 Manga.get_images_url 函数获得的图片 url 转换为 Picture 类。



    Args:

        url        (str)               : 未经处理的漫画图片链接。



        credential (Credential | None): 凭据类. Defaults to None.



    Returns:

        Picture: 图片类。

    """
    url = urlparse(url).path
    credential = credential if credential else Credential()

    async def get_real_image_url(url: str) -> str:
        token_api = API["info"]["image_token"]
        datas = {"urls": f'["{url}"]'}
        token_data = (
            await Api(
                **token_api,
                credential=credential,
                no_csrf=(
                    False
                    if (credential.has_sessdata() and credential.has_bili_jct())
                    else True
                ),
            )
            .update_data(**datas)
            .result
        )
        return f'{token_data[0]["url"]}?token={token_data[0]["token"]}'

    url = await get_real_image_url(url)
    return await Picture.async_load_url(url)


async def set_follow_manga(

    manga: Manga, status: bool = True, credential: Optional[Credential] = None

) -> dict:
    """

    设置追漫



    Args:

        manga      (Manga)     : 漫画类。



        status     (bool)      : 设置是否追漫。是为 True,否为 False。Defaults to True.



        credential (Credential): 凭据类。

    """
    if credential == None:
        if manga.credential.has_sessdata() and manga.credential.has_bili_jct():
            credential = manga.credential
        else:
            credential = Credential()
    credential.raise_for_no_sessdata()
    credential.raise_for_no_bili_jct()
    if status == True:
        api = API["operate"]["add_favorite"]
    else:
        api = API["operate"]["del_favorite"]
    data = {"comic_ids": str(manga.get_manga_id())}
    return await Api(**api, credential=credential).update_data(**data).result


async def get_raw_manga_index(

    area: MangaIndexFilter.Area = MangaIndexFilter.Area.ALL,

    order: MangaIndexFilter.Order = MangaIndexFilter.Order.HOT,

    status: MangaIndexFilter.Status = MangaIndexFilter.Status.ALL,

    payment: MangaIndexFilter.Payment = MangaIndexFilter.Payment.ALL,

    style: MangaIndexFilter.Style = MangaIndexFilter.Style.ALL,

    pn: int = 1,

    ps: int = 18,

    credential: Credential = None,

) -> list:
    """

    获取漫画索引



    Args:

        area    (MangaIndexFilter.Area)   : 地区。Defaults to MangaIndexFilter.Area.ALL.



        order   (MangaIndexFilter.Order)  : 排序。Defaults to MangaIndexFilter.Order.HOT.



        status  (MangaIndexFilter.Status) : 状态。Defaults to MangaIndexFilter.Status.ALL.



        payment (MangaIndexFilter.Payment): 支付。Defaults to MangaIndexFilter.Payment.ALL.



        style   (MangaIndexFilter.Style)  : 风格。Defaults to MangaIndexFilter.Style.ALL.



        pn      (int)                     : 页码。Defaults to 1.



        ps      (int)                     : 每页数量。Defaults to 18.



        credential (Credential)           : 凭据类. Defaults to None.



    Returns:

        list: 调用 API 返回的结果

    """
    credential = credential if credential else Credential()
    api = API["info"]["index"]
    params = {"device": "pc", "platform": "web"}
    data = {
        "area_id": area.value,
        "order": order.value,
        "is_finish": status.value,
        "is_free": payment.value,
        "style_id": style.value,
        "page_num": pn,
        "page_size": ps,
    }
    return (
        await Api(**api, credential=credential, no_csrf=True)
        .update_data(**data)
        .update_params(**params)
        .result
    )


async def get_manga_index(

    area: MangaIndexFilter.Area = MangaIndexFilter.Area.ALL,

    order: MangaIndexFilter.Order = MangaIndexFilter.Order.HOT,

    status: MangaIndexFilter.Status = MangaIndexFilter.Status.ALL,

    payment: MangaIndexFilter.Payment = MangaIndexFilter.Payment.ALL,

    style: MangaIndexFilter.Style = MangaIndexFilter.Style.ALL,

    pn: int = 1,

    ps: int = 18,

    credential: Credential = None,

) -> List[Manga]:
    """

    获取漫画索引



    Args:



        area    (MangaIndexFilter.Area)   : 地区。Defaults to MangaIndexFilter.Area.ALL.



        order   (MangaIndexFilter.Order)  : 排序。Defaults to MangaIndexFilter.Order.HOT.



        status  (MangaIndexFilter.Status) : 状态。Defaults to MangaIndexFilter.Status.ALL.



        payment (MangaIndexFilter.Payment): 支付。Defaults to MangaIndexFilter.Payment.ALL.



        style   (MangaIndexFilter.Style)  : 风格。Defaults to MangaIndexFilter.Style.ALL.



        pn      (int)                     : 页码。Defaults to 1.



        ps      (int)                     : 每页数量。Defaults to 18.



        credential (Credential)           : 凭据类. Defaults to None.



    Returns:

        List[Manga]: 漫画索引

    """
    data = await get_raw_manga_index(
        area, order, status, payment, style, pn, ps, credential
    )
    return [Manga(manga_data["season_id"]) for manga_data in data]


async def get_manga_update(

    date: Union[str, datetime.datetime] = datetime.datetime.now(),

    pn: int = 1,

    ps: int = 8,

    credential: Credential = None,

) -> List[Manga]:
    """

    获取更新推荐的漫画



    Args:

        date (Union[str, datetime.datetime]): 日期,默认为今日。



        pn   (int)                          : 页码。Defaults to 1.



        ps   (int)                          : 每页数量。Defaults to 8.



        credential (Credential)           : 凭据类. Defaults to None.



    Returns:

        List[Manga]: 漫画列表

    """
    credential = credential if credential else Credential()
    api = API["info"]["update"]
    params = {"device": "pc", "platform": "web"}
    if isinstance(date, datetime.datetime):
        date = date.strftime("%Y-%m-%d")
    data = {"date": date, "page_num": pn, "page_size": ps}
    manga_data = (
        await Api(**api, credential=credential, no_csrf=True)
        .update_data(**data)
        .update_params(**params)
        .result
    )
    return [Manga(manga["comic_id"]) for manga in manga_data["list"]]


async def get_manga_home_recommend(

    pn: int = 1, seed: Optional[str] = "0", credential: Credential = None

) -> List[Manga]:
    """

    获取首页推荐的漫画



    Args:

        pn   (int)                          : 页码。Defaults to 1.



        seed (Optional[str])                : Unknown param,无需传入.



        credential (Credential)           : 凭据类. Defaults to None.



    Returns:

        List[Manga]: 漫画列表

    """
    credential = credential if credential else Credential()
    api = API["info"]["home_recommend"]
    params = {"device": "pc", "platform": "web"}
    data = {"page_num": pn, "seed": seed}
    manga_data = (
        await Api(**api, credential=credential, no_csrf=True)
        .update_data(**data)
        .update_params(**params)
        .result
    )
    return [Manga(manga["comic_id"]) for manga in manga_data["list"]]