diff --git a/.github/CODING.md b/.github/CODING.md
index bd9d4c11..c4b76c77 100644
--- a/.github/CODING.md
+++ b/.github/CODING.md
@@ -8,9 +8,11 @@
**入门示例:我要贡献一个 API !**
-首先你需要写入你的 API,找到 `bilibili_api/data/api` 目录,会有许多的 `json` 文件,你需要找到对应的 `json` 文件介入你的 API。举个例子:你的 API 如果和用户有关,请写入 `user.json` 文件中。
+首先你需要写入你的 API,找到 `bilibili_api/data/api` 目录,会有许多的 `json` 文件,你需要找到对应的 `json` 文件介入你的 API。举个例子:你的 API 如果和用户有关,请写入 `user.json` 文件中。
+
每一个 `json` 文件会根据 API 的作用大致分类,你只要找到对应的分类就可以了。一般 `json` 文件将分为两个分类:`info` (有关获取信息的 API) 和 `operate` (有关操作用户相关信息的 API)。当然有的时候部分 API 也会被归类到另外一个分类,如 `video.json` 中有一个分类就存放着有关弹幕的 API (`danmaku`)。除非有大量与某个东西相关的 API (如视频 API 有许多是和弹幕有关的),否则千万不要创建在 `info` 和 `operate` 以外的新的分类。
-~~当然有的文件因为懒什么分类都没有弄,在 API 非常少的时候你可以这么做,但是不推荐。~~
+~~当然有的文件因为懒什么分类都没有弄,在 API 非常少的时候你可以这么做,但是不推荐。~~
+
然后你就可以进行编写了。首先找到对应的文件 (如和视频相关的 API 的具体实现要写在 `video.py` 中),然后 (在一个类里面 ( 有的时候不用写在一个类里面 )) 新建一个异步函数,一定要记得完善参数类型和返回值类型。( 建议让你的 IDE 打开检查 `typing` 的模式,就例如 `Visual Studio Code` 里面将 `python.analysis.typeCheckingMode` 设置为 `basic` 或 `strict`)。接着你需要编写注释,像下面那样子编写:
``` python
@@ -27,7 +29,11 @@ async def get_user_real_name(self, uid: int, credential: Union[Credential, None]
"""
```
-首先你要写上函数的用途,然后要写好参数的说明:名称 + 类型 + 用途。其中类型如果是多个的话,那么可以用 `|` 分隔而不是用 `Union` (写代码的时候务必要用 `Union`,需要支持 `Python3.8`),如果你的 API 需要登录的话,你要加上 `credential` 参数,类型是 `Credential | None`,描述可以用 `凭据类。`。(凭据类能传入 cookies 以保证能正常访问需要登录的 API,这里就做到了登录的作用 ) 最后要把返回值写好,一般情况下 API 返回的结果类型都是 `dict`,这个时候你的返回值说明就可以用 `调用 API 返回的结果`。
+首先你要写上函数的用途,然后要写好参数的说明:名称 + 类型 + 用途。其中类型如果是多个的话,那么写注释时可以用 `|` 分隔而不是用 `Union`,但写代码的时候务必要用 `Union`,需要支持 `Python3.8`。
+
+如果你的 API 需要登录的话,你要加上 `credential` 参数,类型是 `Credential | None`,描述可以用 `凭据类。`。(凭据类能传入 cookies 以保证能正常访问需要登录的 API,这里就做到了登录的作用 )
+
+最后要把返回值写好,一般情况下 API 返回的结果类型都是 `dict`,这个时候你的返回值说明就可以用 `调用 API 返回的结果`。
然后就是函数的主体了,你可以按照下面的写法来写:
``` python
@@ -40,14 +46,22 @@ async def ...(...) -> dict:
params = {
...
}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
```
-首先如果函数要传入凭据类,那么你要添加 `credential = credential if credential else Credential()` 以确保 `credential` 参数不会是 `None` 类型,~~当然不这么做也不会报错,但是你好歹要走个形式啊!~~。接着我们声明 `api` 变量,它是字典 `API` 的一部分,这里的字典 `API` 就是你写入 API 的那个 `json` 文件的内容。所以你的 API 也在里面,你只需要补充好字典的键值就可以了。接着就是可选的 `params` 或 `data`,如果你的 API 需要传入参数的话不要忘记创建它们。最后你只需要返回已经封装好了的函数 `request` 得到的结果就可以了。`request` 函数的第一个参数是访问 `API` 的方法,一般是 `GET`/`POST`/`PUT`/`DELETE`/`PATCH`,第二个参数是 `API` 的链接,你只需要填入 `api["url"]` 就可以了 ( 前提是你的键值没有输错 ),下面的参数最好要指定名称传参,最后不要忘记了:如果这个接口需要登录,那么你得要把凭据类传入进去!( 如果你的函数是在一个类里面,通常每一个类都有一个属性 `credential`,就是创建这个类的时候会传入的,这个时候你只需要传入 `self.credential` 就可以了。当然参数里面不用再写 `credential` 参数了!! )
+1. 首先如果函数要传入凭据类,那么你应该创建变量 `credential = credential if credential else Credential()` 以确保 `credential` 参数不会是 `None` 类型,~~当然不这么做也不会报错,但是你好歹要走个形式啊!~~。
+
+2. 接着我们声明 `api` 变量,它是字典 `API` 的一部分,这里的字典 `API` 就是你写入 API 的那个 `json` 文件的内容。
+所以你的 API 也在里面,你只需要补充好字典的键值就可以了。接着就是可选的 `params` 或 `data`,如果你的 API 需要传入参数的话不要忘记传入它们。
+
+3. 最后你只需要返回已经封装好了的函数 `Api` 得到的结果就可以了。`Api` 函数的第一个参数是 `**api`,通常里面包括 HTTP 方法、URL 等信息,`params`/`data`/`headers`/`files`参数应该通过 `Api().update()`/`Api().update_headers()` 等传入。
+
+4. 不要忘记了:如果这个接口需要登录,那么你得把凭据类传入进去!( 如果你的函数是在一个类里面,通常每一个类都有一个属性 `credential`,就是创建这个类的时候会传入的,这个时候你只需要传入 `self.credential` 就可以了。当然参数里面不用再写 `credential` 参数了!! )
+
恭喜你完成了代码的编写!你可以先进行一次新功能的提交:
``` bash
-$ git commit -am "feat(user.User): 新增一个狗都不用的接口"
+$ git commit -am "feat(user.User): 新增一个 ### 的接口"
```
然后你还需要干两件事情:文档 & 测试。这里不详细说明了,因为这里不会提及除了写代码以外的任何事情。SO...
@@ -56,13 +70,7 @@ $ git commit -am "feat(user.User): 新增一个狗都不用的接口"
当然,除了这个指南举的简单 API 样例,你还可以学习更加高级的功能,例如新建一个模块、事件监听器、`protobuf` 数据等等,这里~~因为懒~~先不写了,你可以查看这些功能的样例进行学习。
-所以做完功能不要忘记提交到线上仓库! (**PR 记得向 `dev` 分之提交!!!**)
-
-
-看不见我
+所以做完功能不要忘记提交到 Github 仓库!
-``` bash
-$ git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;git push origin dev;
-```
+**PR 记得向 `dev` 分支提交!!! `git push origin dev`**
-
diff --git a/.github/ISSUE_TEMPLATE/documentation.md b/.github/ISSUE_TEMPLATE/documentation.md
index 51138a9c..5387b9a4 100644
--- a/.github/ISSUE_TEMPLATE/documentation.md
+++ b/.github/ISSUE_TEMPLATE/documentation.md
@@ -1,7 +1,7 @@
---
name: 文档勘误
about: 使用该模板汇报文档错误
-title: "【文档】 这里填写标题"
+title: "[文档] 这里填写标题"
labels: 'documentation'
assignees: ''
diff --git a/.github/ISSUE_TEMPLATE/needs.md b/.github/ISSUE_TEMPLATE/needs.md
index 572991ab..d1b524ca 100644
--- a/.github/ISSUE_TEMPLATE/needs.md
+++ b/.github/ISSUE_TEMPLATE/needs.md
@@ -1,7 +1,7 @@
---
name: 提交需求
about: 使用该模板进行提交需求
-title: "【需求】{这里填写标题}"
+title: "[需求] {这里填写标题}"
labels: 'need'
assignees: ''
diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md
index 7b79b128..bbe95c71 100644
--- a/.github/ISSUE_TEMPLATE/question.md
+++ b/.github/ISSUE_TEMPLATE/question.md
@@ -1,7 +1,7 @@
---
name: 提出疑问
about: 使用该模板进行提问
-title: "【提问】{这里填写标题}"
+title: "[提问] {这里填写标题}"
labels: 'question'
assignees: ''
@@ -13,6 +13,8 @@ assignees: ''
**运行环境:** Windows / Linux / MacOS
+
+
---
在此处写正文
diff --git a/.github/ISSUE_TEMPLATE/suggestion.md b/.github/ISSUE_TEMPLATE/suggestion.md
index 8144c823..09ac939d 100644
--- a/.github/ISSUE_TEMPLATE/suggestion.md
+++ b/.github/ISSUE_TEMPLATE/suggestion.md
@@ -1,7 +1,7 @@
---
name: 建言献策
about: 使用该模板提建议
-title: "【建议】{这里填写标题}"
+title: "[建议] {这里填写标题}"
labels: '建议'
assignees: 'enhancement'
diff --git a/.github/ISSUE_TEMPLATE/troubleshooting.md b/.github/ISSUE_TEMPLATE/troubleshooting.md
index 8e2a65c7..ea226b87 100644
--- a/.github/ISSUE_TEMPLATE/troubleshooting.md
+++ b/.github/ISSUE_TEMPLATE/troubleshooting.md
@@ -1,7 +1,7 @@
---
name: 漏洞反馈
about: 使用该模板进行漏洞反馈
-title: "【漏洞】{这里填写标题}"
+title: "[漏洞] {这里填写标题}"
labels: 'bug'
assignees: ''
@@ -21,6 +21,8 @@ assignees: ''
**报错信息:**
+
+
```
这里填写报错信息
```
diff --git a/CHANGELOGS/v15.md b/CHANGELOGS/v15.md
index e0accefa..3e70b635 100644
--- a/CHANGELOGS/v15.md
+++ b/CHANGELOGS/v15.md
@@ -1,3 +1,16 @@
+# 15.5.2 2023/7/25
+- fix: 修改新请求函数为 Api 方法 by @Drelf2018 in https://github.com/Nemo2011/bilibili-api/pull/364
+- fix: 尝试修复发送图片私信的问题 by @Drelf2018 in https://github.com/Nemo2011/bilibili-api/pull/367
+- fix: 修复 params 中有汉字时计算 w_rid 错误的漏洞以及弹幕用户名打码问题 by @Drelf2018 in https://github.com/Nemo2011/bilibili-api/pull/373
+- fix: update get_followings by @z0z0r4 in https://github.com/Nemo2011/bilibili-api/pull/381
+- rechore: login term and tk by @z0z0r4 in https://github.com/Nemo2011/bilibili-api/pull/383
+* fix: import by @Nemo2011 https://github.com/Nemo2011/bilibili-api/commit/dacefba0d909a5dbbc9213100267b84bc54581d5
+* style: by @Nemo2011 https://github.com/Nemo2011/bilibili-api/commit/9ca10d2bb534a216e6dabadb1fb0c5093548b11d
+* feat: settings.wbi_retry_times by @Nemo2011 https://github.com/Nemo2011/bilibili-api/commit/54087564b9a68cac800664d00846abf35b3d204e
+* fix: testing & login by @Nemo2011 https://github.com/Nemo2011/bilibili-api/commit/b329fe6d16022981c89ffa2bec8738ce96881afd
+* fix: buvid3 by @z0z0r4 in https://github.com/Nemo2011/bilibili-api/pull/391
+* rechore: 将 `request` 换到 `Api` by @z0z0r4 https://github.com/Nemo2011/bilibili-api/pull/403
+
# 15.5.1 2023/6/24
- fix: credential 刷新异步问题 (https://github.com/Nemo2011/bilibili-api/issues/351) (https://github.com/Nemo2011/bilibili-api/issues/358)
diff --git a/bilibili_api/__init__.py b/bilibili_api/__init__.py
index dd6a25a0..4d5c3962 100644
--- a/bilibili_api/__init__.py
+++ b/bilibili_api/__init__.py
@@ -7,83 +7,83 @@
import asyncio
import platform
+from .utils.sync import sync
+from .credential import Credential
+from .utils.picture import Picture
+from .utils.short import get_real_url
+from .utils.parse_link import ResourceType, parse_link
+from .utils.aid_bvid_transformer import aid2bvid, bvid2aid
+from .utils.danmaku import DmMode, Danmaku, DmFontSize, SpecialDanmaku
+from .utils.network_httpx import (
+ HEADERS,
+ Api,
+ retry,
+ enc_wbi,
+ get_nav,
+ request,
+ check_valid,
+ get_session,
+ set_session,
+ get_mixin_key,
+)
+from .errors import (
+ LoginError,
+ ApiException,
+ ArgsException,
+ LiveException,
+ NetworkException,
+ ResponseException,
+ VideoUploadException,
+ ResponseCodeException,
+ DanmakuClosedException,
+ CredentialNoBuvid3Exception,
+ CredentialNoBiliJctException,
+ DynamicExceedImagesException,
+ CredentialNoSessdataException,
+ CredentialNoDedeUserIDException,
+)
from . import (
- album,
app,
- article,
- article_category,
ass,
- audio,
- bangumi,
- black_room,
- channel,
- channel_series,
- cheese,
- client,
- comment,
- creative_center,
- dynamic,
- emoji,
- favorite_list,
- game,
- homepage,
hot,
- interactive_video,
+ game,
live,
- live_area,
- login,
- login_func,
- manga,
note,
rank,
+ user,
+ vote,
+ album,
+ audio,
+ emoji,
+ login,
+ manga,
+ topic,
+ video,
+ cheese,
+ client,
search,
+ article,
+ bangumi,
+ channel,
+ comment,
+ dynamic,
session,
+ homepage,
settings,
- topic,
- user,
- video,
+ live_area,
video_tag,
- video_uploader,
+ black_room,
+ login_func,
video_zone,
- vote,
-)
-from .errors import (
- ApiException,
- ArgsException,
- CredentialNoBiliJctException,
- CredentialNoBuvid3Exception,
- CredentialNoDedeUserIDException,
- CredentialNoSessdataException,
- DanmakuClosedException,
- DynamicExceedImagesException,
- LiveException,
- LoginError,
- NetworkException,
- ResponseCodeException,
- ResponseException,
- VideoUploadException,
-)
-from .credential import Credential
-from .utils.aid_bvid_transformer import aid2bvid, bvid2aid
-from .utils.danmaku import Danmaku, DmFontSize, DmMode, SpecialDanmaku
-from .utils.network_httpx import (
- HEADERS,
- Api,
- check_valid,
- enc_wbi,
- get_mixin_key,
- get_nav,
- get_session,
- request,
- retry,
- set_session,
+ favorite_list,
+ channel_series,
+ video_uploader,
+ creative_center,
+ article_category,
+ interactive_video,
)
-from .utils.parse_link import ResourceType, parse_link
-from .utils.picture import Picture
-from .utils.short import get_real_url
-from .utils.sync import sync
-BILIBILI_API_VERSION = "15.5.1.b0"
+BILIBILI_API_VERSION = "15.5.2"
# 如果系统为 Windows,则修改默认策略,以解决代理报错问题
if "windows" in platform.system().lower():
diff --git a/bilibili_api/_pyinstaller/entry_points.py b/bilibili_api/_pyinstaller/entry_points.py
index 11d42a09..0b1575c7 100644
--- a/bilibili_api/_pyinstaller/entry_points.py
+++ b/bilibili_api/_pyinstaller/entry_points.py
@@ -1,5 +1,5 @@
-from typing import List
import os
+from typing import List
def get_hook_dirs() -> List[str]:
diff --git a/bilibili_api/_pyinstaller/hook-bilibili_api.py b/bilibili_api/_pyinstaller/hook-bilibili_api.py
index eaef3b5a..e6eab940 100644
--- a/bilibili_api/_pyinstaller/hook-bilibili_api.py
+++ b/bilibili_api/_pyinstaller/hook-bilibili_api.py
@@ -1,4 +1,5 @@
-from PyInstaller.utils.hooks import collect_data_files
from typing import List, Tuple
+from PyInstaller.utils.hooks import collect_data_files
+
datas: List[Tuple[str, str]] = collect_data_files("bilibili_api")
diff --git a/bilibili_api/album.py b/bilibili_api/album.py
index 00de231b..70d42e2c 100644
--- a/bilibili_api/album.py
+++ b/bilibili_api/album.py
@@ -4,13 +4,14 @@
相簿相关
"""
+import enum
from typing import Any, List, Union, Optional
+
from .utils.utils import get_api
-from .utils.network_httpx import request
-from .utils.credential import Credential
from .utils.picture import Picture
from .exceptions import ArgsException
-import enum
+from .utils.credential import Credential
+from .utils.network_httpx import Api
API = get_api("album")
@@ -72,7 +73,7 @@ async def get_info(self) -> dict:
"""
api = API["info"]["detail"]
params = {"doc_id": self.get_doc_id()}
- resp = await request("GET", api["url"], params=params)
+ resp = await Api(**api).update_params(**params).result
self.__info = resp
return resp
@@ -141,14 +142,14 @@ async def get_painter() -> dict:
if order != AlbumOrder.RECOMMEND:
raise ArgsException("摄影相簿暂不支持以热度/以时间排序。")
params = {"type": order.value, "page_num": page_num, "page_size": page_size}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_photos() -> dict:
api = API["info"]["homepage_photos_albums_list"]
if order != AlbumOrder.RECOMMEND:
raise ArgsException("摄影相簿暂不支持以热度/以时间排序。")
params = {"type": order.value, "page_num": page_num, "page_size": page_size}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
if category == AlbumCategory.PAINTS:
return await get_painter()
@@ -183,12 +184,12 @@ async def get_homepage_recommend_uppers(
async def get_painters() -> dict:
api = API["info"]["homepage_recommended_painters"]
params = {"num": numbers}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_photos_uppers() -> dict:
api = API["info"]["homepage_recommended_photos_uppers"]
params = {"num": numbers}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
if category == AlbumCategory.PAINTS:
return await get_painters()
@@ -234,4 +235,4 @@ async def get_user_albums(
"page_num": page_num,
"page_size": page_size,
}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
diff --git a/bilibili_api/app.py b/bilibili_api/app.py
index 2d21ea8a..3eae8e86 100644
--- a/bilibili_api/app.py
+++ b/bilibili_api/app.py
@@ -4,13 +4,14 @@
手机 APP 相关
"""
-from .utils.utils import get_api
-from .utils.credential import Credential
-from .utils.network_httpx import request
-from hashlib import md5
import time
+from hashlib import md5
from typing import Union
+from .utils.utils import get_api
+from .utils.credential import Credential
+from .utils.network_httpx import Api
+
API = get_api("app")
@@ -55,7 +56,7 @@ async def get_loading_images(
"width": width,
"birth": birth,
}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_loading_images_special(
@@ -119,4 +120,4 @@ async def get_loading_images_special(
"ts": ts,
"sign": sign,
}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
diff --git a/bilibili_api/article.py b/bilibili_api/article.py
index fc4fef95..d0a0acb0 100644
--- a/bilibili_api/article.py
+++ b/bilibili_api/article.py
@@ -4,26 +4,25 @@
专栏相关
"""
-from copy import copy
+import re
import json
+from copy import copy
from enum import Enum
-from typing import List, overload, Union
-
-import httpx
-from .utils.utils import get_api
-from .utils.credential import Credential
-from .note import Note, NoteType
-import re
from html import unescape
+from datetime import datetime
+from urllib.parse import unquote
+from typing import List, Union, TypeVar, overload
import yaml
+import httpx
from yarl import URL
-from .utils.network_httpx import get_session, request
-from .exceptions.NetworkException import NetworkException, ApiException
from bs4 import BeautifulSoup, element
-from datetime import datetime
-from urllib.parse import unquote
-from typing import TypeVar
+
+from .note import Note, NoteType
+from .utils.utils import get_api
+from .utils.credential import Credential
+from .utils.network_httpx import Api, get_session
+from .exceptions.NetworkException import ApiException, NetworkException
API = get_api("article")
@@ -108,7 +107,7 @@ async def get_article_rank(
"""
api = API["info"]["rank"]
params = {"cid": rank_type.value}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
class ArticleList:
@@ -143,7 +142,7 @@ async def get_content(self) -> dict:
api = API["info"]["list"]
params = {"id": self.__rlid}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
class Article:
@@ -473,7 +472,7 @@ async def parse(el: BeautifulSoup):
elif e.name == "a":
# 超链接
if len(e.contents) == 0:
- from .utils.parse_link import parse_link, ResourceType
+ from .utils.parse_link import ResourceType, parse_link
parse_link_res = await parse_link(e.attrs["href"])
if parse_link_res[1] == ResourceType.VIDEO:
@@ -602,10 +601,7 @@ async def get_info(self) -> dict:
api = API["info"]["view"]
params = {"id": self.__cvid}
- resp = await request(
- "GET", api["url"], params=params, credential=self.credential
- )
- return resp
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_all(self) -> dict:
"""
@@ -648,7 +644,7 @@ async def set_like(self, status: bool = True) -> dict:
api = API["operate"]["like"]
data = {"id": self.__cvid, "type": 1 if status else 2}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def set_favorite(self, status: bool = True) -> dict:
"""
@@ -667,7 +663,7 @@ async def set_favorite(self, status: bool = True) -> dict:
)
data = {"id": self.__cvid}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def add_coins(self) -> dict:
"""
@@ -681,7 +677,7 @@ async def add_coins(self) -> dict:
upid = (await self.get_info())["mid"]
api = API["operate"]["coin"]
data = {"aid": self.__cvid, "multiply": 1, "upid": upid, "avtype": 2}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
# TODO: 专栏上传/编辑/删除
diff --git a/bilibili_api/article_category.py b/bilibili_api/article_category.py
index 3beba09f..09888bdd 100644
--- a/bilibili_api/article_category.py
+++ b/bilibili_api/article_category.py
@@ -4,13 +4,13 @@
专栏分类相关
"""
import os
-from typing import List, Tuple, Optional
import copy
import json
-from .utils.network_httpx import request
-from .utils.utils import get_api
from enum import Enum
+from typing import List, Tuple, Optional
+from .utils.utils import get_api
+from .utils.network_httpx import Api
API = get_api("article-category")
@@ -148,4 +148,4 @@ async def get_category_recommend_articles(
"""
api = API["info"]["recommends"]
params = {"cid": category_id, "sort": order.value, "pn": page_num, "ps": page_size}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
diff --git a/bilibili_api/ass.py b/bilibili_api/ass.py
index 403cdab6..fb787179 100644
--- a/bilibili_api/ass.py
+++ b/bilibili_api/ass.py
@@ -4,19 +4,18 @@
有关 ASS 文件的操作
"""
import os
+from tempfile import gettempdir
from typing import Union, Optional
+from .video import Video
from .bangumi import Episode
from .cheese import CheeseVideo
-from .exceptions.ArgsException import ArgsException
-
+from .utils.srt2ass import srt2ass
+from .utils.json2srt import json2srt
from .utils.credential import Credential
-from .utils.network_httpx import get_session
from .utils.danmaku2ass import Danmaku2ASS
-from .utils.json2srt import json2srt
-from .utils.srt2ass import srt2ass
-from .video import Video
-from tempfile import gettempdir
+from .utils.network_httpx import get_session
+from .exceptions.ArgsException import ArgsException
def export_ass_from_xml(
diff --git a/bilibili_api/audio.py b/bilibili_api/audio.py
index 73b3cbc1..4189c384 100644
--- a/bilibili_api/audio.py
+++ b/bilibili_api/audio.py
@@ -4,11 +4,12 @@
音频相关
"""
+from enum import Enum
+from typing import Union, Optional
+
from .utils.utils import get_api
from .utils.credential import Credential
-from .utils.network_httpx import request
-from typing import Union, Optional
-from enum import Enum
+from .utils.network_httpx import Api
API = get_api("audio")
@@ -44,7 +45,7 @@ async def get_info(self) -> dict:
api = API["audio_info"]["info"]
params = {"sid": self.__auid}
- return await request("GET", api["url"], params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_tags(self) -> dict:
"""
@@ -55,7 +56,7 @@ async def get_tags(self) -> dict:
"""
api = API["audio_info"]["tag"]
params = {"sid": self.__auid}
- return await request("GET", api["url"], params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_download_url(self) -> dict:
"""
@@ -66,7 +67,7 @@ async def get_download_url(self) -> dict:
"""
api = API["audio_info"]["download_url"]
params = {"sid": self.__auid, "privilege": 2, "quality": 2}
- return await request("GET", api["url"], params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def add_coins(self, num: int = 2) -> dict:
"""
@@ -83,7 +84,7 @@ async def add_coins(self, num: int = 2) -> dict:
api = API["audio_operate"]["coin"]
data = {"sid": self.__auid, "multiply": num}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
# TODO: 音频编辑
@@ -119,7 +120,7 @@ async def get_info(self) -> dict:
api = API["list_info"]["info"]
params = {"sid": self.__amid}
- return await request("GET", api["url"], params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_tags(self) -> dict:
"""
@@ -131,7 +132,7 @@ async def get_tags(self) -> dict:
api = API["list_info"]["tag"]
params = {"sid": self.__amid}
- return await request("GET", api["url"], params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_song_list(self, pn: int = 1) -> dict:
"""
@@ -146,7 +147,7 @@ async def get_song_list(self, pn: int = 1) -> dict:
api = API["list_info"]["song_list"]
params = {"sid": self.__amid, "pn": pn, "ps": 100}
- return await request("GET", api["url"], params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
# TODO: 歌单编辑
@@ -166,7 +167,7 @@ async def get_user_stat(uid: int, credential: Union[Credential, None] = None) ->
credential = credential if credential is not None else Credential()
api = API["audio_info"]["user"]
params = {"uid": uid}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_hot_song_list(
@@ -186,4 +187,4 @@ async def get_hot_song_list(
credential = credential if credential is not None else Credential()
api = API["list_info"]["hot"]
params = {"pn": pn, "ps": 100}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
diff --git a/bilibili_api/bangumi.py b/bilibili_api/bangumi.py
index c8c918e1..51bdde75 100644
--- a/bilibili_api/bangumi.py
+++ b/bilibili_api/bangumi.py
@@ -12,21 +12,20 @@
import datetime
from enum import Enum
-from typing import Any, Tuple, Union, List, Optional
+from typing import Any, List, Tuple, Union, Optional
import requests
from bilibili_api.utils.danmaku import Danmaku
from . import settings
-
+from .video import Video
from .utils.utils import get_api
-from .utils.initial_state import get_initial_state, get_initial_state_sync
from .utils.credential import Credential
-from .utils.network_httpx import get_session, request
-from .exceptions.ResponseException import ResponseException
from .exceptions.ApiException import ApiException
-from .video import Video
+from .utils.network_httpx import request, Api, get_session
+from .exceptions.ResponseException import ResponseException
+from .utils.initial_state import get_initial_state, get_initial_state_sync
API = get_api("bangumi")
@@ -71,7 +70,7 @@ async def get_timeline(type_: BangumiType, before: int = 7, after: int = 0) -> d
"""
api = API["info"]["timeline"]
params = {"types": type_.value, "before": before, "after": after}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
class IndexFilter:
@@ -965,7 +964,7 @@ async def get_index_info(
# params["type"] 未知参数,为 1
params["type"] = 1
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
class Bangumi:
@@ -1092,7 +1091,7 @@ async def get_meta(self) -> dict:
api = API["info"]["meta"]
params = {"media_id": self.__media_id}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_short_comment_list(
self,
@@ -1117,7 +1116,7 @@ async def get_short_comment_list(
if next is not None:
params["cursor"] = next
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_long_comment_list(
self,
@@ -1142,7 +1141,7 @@ async def get_long_comment_list(
if next is not None:
params["cursor"] = next
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_episode_list(self) -> dict:
"""
@@ -1166,7 +1165,7 @@ async def get_episode_list(self) -> dict:
)
api = API["info"]["episodes_list"]
params = {"season_id": self.__ssid}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_episodes(self) -> List["Episode"]:
"""
@@ -1204,7 +1203,7 @@ async def get_stat(self) -> dict:
api = API["info"]["season_status"]
params = {"season_id": self.__ssid}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_overview(self) -> dict:
"""
@@ -1219,7 +1218,7 @@ async def get_overview(self) -> dict:
else:
api = API["info"]["collective_info"]
params = {"season_id": self.__ssid}
- return await request("GET", api["url"], params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def set_follow(
@@ -1243,7 +1242,7 @@ async def set_follow(
api = API["operate"]["follow_add"] if status else API["operate"]["follow_del"]
data = {"season_id": bangumi.get_season_id()}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def update_follow_status(
@@ -1266,7 +1265,7 @@ async def update_follow_status(
api = API["operate"]["follow_status"]
data = {"season_id": bangumi.get_season_id(), "status": status}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
class Episode(Video):
@@ -1374,7 +1373,7 @@ async def get_download_url(self) -> dict:
Returns:
dict: 调用 API 返回的结果。
"""
- url = API["info"]["playurl"]["url"]
+ api = API["info"]["playurl"]
if True:
params = {
"avid": self.get_aid(),
@@ -1384,7 +1383,7 @@ async def get_download_url(self) -> dict:
"fnval": 4048,
"fourk": 1,
}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_danmaku_xml(self) -> str:
"""
diff --git a/bilibili_api/black_room.py b/bilibili_api/black_room.py
index 6cb7d63c..5c613868 100644
--- a/bilibili_api/black_room.py
+++ b/bilibili_api/black_room.py
@@ -5,11 +5,11 @@
"""
from enum import Enum
-from typing import Union, Optional, List
+from typing import List, Union, Optional
from .utils.utils import get_api
-from .utils.network_httpx import request
from .utils.credential import Credential
+from .utils.network_httpx import Api
class BlackReasonType(Enum):
@@ -179,7 +179,7 @@ async def get_blocked_list(
params = {"pn": pn, "otype": type_.value}
if from_.value != None:
params["btype"] = from_.value
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
class BlackRoom:
@@ -209,9 +209,7 @@ async def get_details(self) -> dict:
"""
api = API["black_room"]["detail"]
params = {"id": self.__id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_reason(self) -> BlackReasonType:
"""
@@ -249,9 +247,7 @@ async def get_details(self) -> dict:
"""
api = API["jury"]["detail"]
params = {"case_id": self.case_id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_opinions(self, pn: int = 1, ps: int = 20) -> dict:
"""
@@ -267,9 +263,7 @@ async def get_opinions(self, pn: int = 1, ps: int = 20) -> dict:
"""
api = API["jury"]["opinion"]
params = {"case_id": self.case_id, "pn": pn, "ps": ps}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def vote(
self,
@@ -303,7 +297,7 @@ async def vote(
}
if reason:
data["content"] = reason
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_next_jury_case(credential: Credential) -> JuryCase:
@@ -319,7 +313,7 @@ async def get_next_jury_case(credential: Credential) -> JuryCase:
credential.raise_for_no_sessdata()
api = API["jury"]["next_case"]
return JuryCase(
- (await request("GET", api["url"], credential=credential))["case_id"], credential
+ (await Api(**api, credential=credential).result)["case_id"], credential
)
@@ -341,5 +335,5 @@ async def get_jury_case_list(
"""
api = API["jury"]["case_list"]
params = {"pn": pn, "ps": ps}
- info = await request("GET", api["url"], params=params, credential=credential)
+ info = await Api(**api, credential=credential).update_params(**params).result
return [JuryCase(case["case_id"], credential) for case in info["list"]]
diff --git a/bilibili_api/channel.py b/bilibili_api/channel.py
index c41d3c39..8754b90f 100644
--- a/bilibili_api/channel.py
+++ b/bilibili_api/channel.py
@@ -3,15 +3,16 @@
频道相关,与视频分区不互通。
"""
-from bilibili_api.utils.initial_state import get_initial_state
-from bilibili_api.utils.network_httpx import request, get_session
-from bilibili_api.utils.utils import get_api
-from bilibili_api.utils.credential import Credential
-from bilibili_api.errors import ResponseException, ApiException
import re
import json
from enum import Enum
-from typing import Optional, List, Union
+from typing import List, Union, Optional
+
+from bilibili_api.utils.utils import get_api
+from bilibili_api.utils.credential import Credential
+from bilibili_api.utils.initial_state import get_initial_state
+from bilibili_api.errors import ApiException, ResponseException
+from bilibili_api.utils.network_httpx import request, Api
API = get_api("channel")
@@ -24,7 +25,7 @@ async def get_channel_categories() -> dict:
dict: 调用 API 返回的结果
"""
api = API["categories"]["list"]
- return await request("GET", api["url"])
+ return await Api(**api).result
async def get_channel_category_detail(
@@ -41,7 +42,7 @@ async def get_channel_category_detail(
credential = credential if credential else Credential()
api = API["categories"]["sub_channels"]
params = {"id": category_id, "offset": offset}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
class ChannelVideosOrder(Enum):
@@ -160,7 +161,7 @@ async def get_featured_list(
if offset is not None:
params["offset"] = offset
params["page_size"] = page_size
- info = await request("GET", api["url"], params=params)
+ info = await Api(**api).update_params(**params).result
# 如果频道与番剧有关,会有番剧信息,需要排除掉 card_type=season 的数据
info["list"] = self.only_achieve(info["list"])
@@ -198,7 +199,7 @@ async def get_raw_list(
params["offset"] = offset
params["page_size"] = page_size
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_list(
self,
@@ -278,7 +279,7 @@ async def get_self_subscribe_channels(credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["self_subscribes"]["list"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def subscribe_channel(channel: Channel, credential: Credential) -> dict:
@@ -294,8 +295,8 @@ async def subscribe_channel(channel: Channel, credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["channel"]["subscribe"]
- params = {"id": channel.get_channel_id()}
- return await request("POST", api["url"], data=params, credential=credential)
+ data = {"id": channel.get_channel_id()}
+ return await Api(**api, credential=credential).update_data(data).result
async def unsubscribe_channel(channel: Channel, credential: Credential) -> dict:
@@ -311,5 +312,5 @@ async def unsubscribe_channel(channel: Channel, credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["channel"]["unsubscribe"]
- params = {"id": channel.get_channel_id()}
- return await request("POST", api["url"], data=params, credential=credential)
+ data = {"id": channel.get_channel_id()}
+ return await Api(**api, credential=credential).update_data(data).result
diff --git a/bilibili_api/channel_series.py b/bilibili_api/channel_series.py
index acbfc61b..497d734f 100644
--- a/bilibili_api/channel_series.py
+++ b/bilibili_api/channel_series.py
@@ -3,14 +3,16 @@
用户合集与列表相关
"""
-from enum import Enum
-from typing import Union, List, Optional
-from .utils.credential import Credential
-from .utils.utils import get_api
-from .utils.network_httpx import request
import json
+from enum import Enum
+from typing import List, Union, Optional
+
import httpx
+from .utils.utils import get_api
+from .utils.credential import Credential
+from .utils.network_httpx import Api
+
API_USER = get_api("user")
API = get_api("channel-series")
@@ -166,7 +168,7 @@ async def create_channel_series(
"keywords": ",".join(keywords),
"description": description,
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def del_channel_series(series_id: int, credential: Credential) -> dict:
@@ -181,7 +183,7 @@ async def del_channel_series(series_id: int, credential: Credential) -> dict:
Returns:
dict: 调用 API 返回的结果
"""
- from .user import get_self_info, User
+ from .user import User, get_self_info
credential.raise_for_no_sessdata()
credential.raise_for_no_bili_jct()
@@ -203,7 +205,7 @@ async def del_channel_series(series_id: int, credential: Credential) -> dict:
"series_id": series_id,
"aids": ",".join(map(lambda x: str(x), aids)),
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def add_aids_to_series(
@@ -233,7 +235,7 @@ async def add_aids_to_series(
"series_id": series_id,
"aids": ",".join(map(lambda x: str(x), aids)),
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def del_aids_from_series(
@@ -263,7 +265,7 @@ async def del_aids_from_series(
"series_id": series_id,
"aids": ",".join(map(lambda x: str(x), aids)),
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def set_follow_channel_season(
@@ -279,4 +281,4 @@ async def set_follow_channel_season(
"""
api = API["operate"]["fav"] if status else API["operate"]["unfav"]
data = {"season_id": season_id}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
diff --git a/bilibili_api/cheese.py b/bilibili_api/cheese.py
index 5b5178a7..1dd73724 100644
--- a/bilibili_api/cheese.py
+++ b/bilibili_api/cheese.py
@@ -12,23 +12,20 @@
还有,课程的 season_id 和 ep_id 不与番剧相通,井水不犯河水,请不要错用!
"""
-import datetime
import json
-from . import settings
+import datetime
+from typing import Any, List, Union
import requests
-from .exceptions import (
- DanmakuClosedException,
- NetworkException,
- ResponseException,
-)
-from .exceptions.ArgsException import ArgsException
-from .utils.BytesReader import BytesReader
+
+from . import settings
+from .utils.utils import get_api
from .utils.danmaku import Danmaku
from .utils.credential import Credential
-from .utils.utils import get_api
-from .utils.network_httpx import get_session, request
-from typing import Any, List, Union
+from .utils.BytesReader import BytesReader
+from .exceptions.ArgsException import ArgsException
+from .utils.network_httpx import Api, get_session
+from .exceptions import NetworkException, ResponseException, DanmakuClosedException
API = get_api("cheese")
API_video = get_api("video")
@@ -95,9 +92,7 @@ async def get_meta(self) -> dict:
"""
api = API["info"]["meta"]
params = {"season_id": self.__season_id, "ep_id": self.__ep_id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_list_raw(self):
"""
@@ -108,9 +103,7 @@ async def get_list_raw(self):
"""
api = API["info"]["list"]
params = {"season_id": self.__season_id, "pn": 1, "ps": 1000}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_list(self) -> List["CheeseVideo"]:
"""
@@ -122,9 +115,7 @@ async def get_list(self) -> List["CheeseVideo"]:
global cheese_video_meta_cache
api = API["info"]["list"]
params = {"season_id": self.__season_id, "pn": 1, "ps": 1000}
- lists = await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ lists = await Api(**api, credential=self.credential).update_params(**params).result
cheese_videos = []
for c in lists["items"]:
c["ssid"] = self.get_season_id()
@@ -223,9 +214,7 @@ async def get_download_url(self) -> dict:
"fnval": 4048,
"fourk": 1,
}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_stat(self) -> dict:
"""
@@ -234,9 +223,9 @@ async def get_stat(self) -> dict:
Returns:
dict: 调用 API 返回的结果。
"""
- url = API_video["info"]["stat"]["url"]
+ api = API_video["info"]["stat"]
params = {"aid": self.get_aid()}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_pages(self) -> dict:
"""
@@ -245,9 +234,9 @@ async def get_pages(self) -> dict:
Returns:
dict: 调用 API 返回的结果。
"""
- url = API_video["info"]["pages"]["url"]
+ api = API_video["info"]["pages"]
params = {"aid": self.get_aid()}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_danmaku_view(self) -> dict:
"""
@@ -637,9 +626,7 @@ async def send_danmaku(self, danmaku: Union[Danmaku, None] = None):
"mode": danmaku.mode,
"plat": 1,
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(data).result
async def has_liked(self):
"""
@@ -650,9 +637,9 @@ async def has_liked(self):
"""
self.credential.raise_for_no_sessdata()
- url = get_api("video")["info"]["has_liked"]["url"]
+ api =API_video["info"]["has_liked"]
params = {"aid": self.get_aid()}
- return await request("GET", url, params=params, credential=self.credential) == 1
+ return await Api(**api, credential=self.credential).update_params(**params).result == 1
async def get_pay_coins(self):
"""
@@ -663,9 +650,9 @@ async def get_pay_coins(self):
"""
self.credential.raise_for_no_sessdata()
- url = get_api("video")["info"]["get_pay_coins"]["url"]
+ api =API_video["info"]["get_pay_coins"]
params = {"aid": self.get_aid()}
- return (await request("GET", url, params=params, credential=self.credential))[
+ return (await Api(**api, credential=self.credential).update_params(**params).result)[
"multiply"
]
@@ -678,9 +665,9 @@ async def has_favoured(self):
"""
self.credential.raise_for_no_sessdata()
- url = get_api("video")["info"]["has_favoured"]["url"]
+ api =API_video["info"]["has_favoured"]
params = {"aid": self.get_aid()}
- return (await request("GET", url, params=params, credential=self.credential))[
+ return (await Api(**api, credential=self.credential).update_params(**params).result)[
"favoured"
]
@@ -697,11 +684,9 @@ async def like(self, status: bool = True):
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
- api = get_api("video")["operate"]["like"]
+ api = API_video["operate"]["like"]
data = {"aid": self.get_aid(), "like": 1 if status else 2}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(data).result
async def pay_coin(self, num: int = 1, like: bool = False):
"""
@@ -721,15 +706,13 @@ async def pay_coin(self, num: int = 1, like: bool = False):
if num not in (1, 2):
raise ArgsException("投币数量只能是 1 ~ 2 个。")
- api = get_api("video")["operate"]["coin"]
+ api = API_video["operate"]["coin"]
data = {
"aid": self.get_aid(),
"multiply": num,
"like": 1 if like else 0,
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(data).result
async def set_favorite(
self, add_media_ids: List[int] = [], del_media_ids: List[int] = []
@@ -751,16 +734,14 @@ async def set_favorite(
self.credential.raise_for_no_sessdata()
self.credential.raise_for_no_bili_jct()
- api = get_api("video")["operate"]["favorite"]
+ api = API_video["operate"]["favorite"]
data = {
"rid": self.get_aid(),
"type": 2,
"add_media_ids": ",".join(map(lambda x: str(x), add_media_ids)),
"del_media_ids": ",".join(map(lambda x: str(x), del_media_ids)),
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(data).result
async def get_danmaku_xml(self):
"""
diff --git a/bilibili_api/client.py b/bilibili_api/client.py
index a97b035d..ed8a8384 100644
--- a/bilibili_api/client.py
+++ b/bilibili_api/client.py
@@ -4,8 +4,8 @@
IP 终端相关
"""
-from .utils.network_httpx import request
from .utils.utils import get_api
+from .utils.network_httpx import request, Api
API = get_api("client")
@@ -18,7 +18,7 @@ async def get_zone() -> dict:
dict: 调用 API 返回的结果
"""
api = API["zone"]
- return await request("GET", api["url"])
+ return await Api(**api).result
async def get_client_info() -> dict:
@@ -29,4 +29,4 @@ async def get_client_info() -> dict:
dict: 调用 API 返回的结果
"""
api = API["info"]
- return await request("GET", api["url"])
+ return await Api(**api).result
diff --git a/bilibili_api/comment.py b/bilibili_api/comment.py
index 193489b4..86a4e2a4 100644
--- a/bilibili_api/comment.py
+++ b/bilibili_api/comment.py
@@ -16,11 +16,10 @@
from typing import Union
from .utils.utils import get_api
-from .utils.network_httpx import request
from .utils.credential import Credential
+from .utils.network_httpx import Api
from .exceptions.ArgsException import ArgsException
-
API = get_api("common")
@@ -133,9 +132,7 @@ async def like(self, status: bool = True) -> dict:
self.credential.raise_for_no_bili_jct()
api = API["comment"]["like"]
- return await request(
- "POST", api["url"], data=self.__get_data(status), credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**self.__get_data(status)).result
async def hate(self, status: bool = True) -> dict:
"""
@@ -152,9 +149,7 @@ async def hate(self, status: bool = True) -> dict:
self.credential.raise_for_no_bili_jct()
api = API["comment"]["hate"]
- return await request(
- "POST", api["url"], data=self.__get_data(status), credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**self.__get_data(status)).result
async def pin(self, status: bool = True) -> dict:
"""
@@ -170,9 +165,7 @@ async def pin(self, status: bool = True) -> dict:
self.credential.raise_for_no_bili_jct()
api = API["comment"]["pin"]
- return await request(
- "POST", api["url"], data=self.__get_data(status), credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**self.__get_data(status)).result
async def delete(self) -> dict:
"""
@@ -187,7 +180,7 @@ async def delete(self) -> dict:
api = API["comment"]["del"]
data = self.__get_data(True)
del data["action"]
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_sub_comments(self, page_index: int = 1) -> dict:
"""
@@ -211,9 +204,7 @@ async def get_sub_comments(self, page_index: int = 1) -> dict:
"root": self.__rpid,
}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def send_comment(
@@ -280,7 +271,7 @@ async def send_comment(
raise ArgsException("root=None 时,parent 不得设置")
api = API["comment"]["send"]
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def get_comments(
@@ -289,7 +280,7 @@ async def get_comments(
page_index: int = 1,
order: OrderType = OrderType.TIME,
credential: Union[Credential, None] = None,
-):
+) -> dict:
"""
获取资源评论列表。
@@ -312,4 +303,4 @@ async def get_comments(
api = API["comment"]["get"]
params = {"pn": page_index, "type": type_.value, "oid": oid, "sort": order.value}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
diff --git a/bilibili_api/creative_center.py b/bilibili_api/creative_center.py
index d65fec7f..2fb7b833 100644
--- a/bilibili_api/creative_center.py
+++ b/bilibili_api/creative_center.py
@@ -7,11 +7,11 @@
"""
from enum import Enum
-from typing import Union, List
+from typing import List, Union
from .utils.utils import get_api
-from .utils.network_httpx import request
from .utils.credential import Credential
+from .utils.network_httpx import Api
API = get_api("creative_center")
@@ -139,7 +139,7 @@ async def get_compare(credential: Credential) -> dict:
dict: 视频对比数据。
"""
api = API["overview"]["compare"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_graph(
@@ -166,7 +166,7 @@ async def get_graph(
"s_locale": "zh_CN",
"type": graph_type.value,
}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_overview(
@@ -186,7 +186,7 @@ async def get_overview(
api = API["overview"]["num"]
# 不知道 tab 的作用是什么,但是不传会报错
params = {"period": period.value, "s_locale": "zh_CN", "tab": 0}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_video_survey(credential: Credential) -> dict:
@@ -201,7 +201,7 @@ async def get_video_survey(credential: Credential) -> dict:
"""
api = API["video"]["survey"]
params = {"type": 1}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_video_playanalysis(
@@ -220,7 +220,7 @@ async def get_video_playanalysis(
"""
api = API["video"]["playanalysis"]
params = {"copyright": copyright.value}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_video_source(credential: Credential) -> dict:
@@ -235,7 +235,7 @@ async def get_video_source(credential: Credential) -> dict:
"""
api = API["video"]["source"]
params = {"s_locale": "zh_CN"}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_fan_overview(
@@ -254,7 +254,7 @@ async def get_fan_overview(
"""
api = API["fan"]["overview"]
params = {"period": period.value}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_fan_graph(
@@ -277,7 +277,7 @@ async def get_fan_graph(
"""
api = API["fan"]["graph"]
params = {"period": period.value, "type": graph_type.value}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_article_overview(credential: Credential) -> dict:
@@ -291,7 +291,7 @@ async def get_article_overview(credential: Credential) -> dict:
dict: 文章概览数据。
"""
api = API["article"]["overview"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_article_graph(
@@ -311,7 +311,7 @@ async def get_article_graph(
api = API["article"]["graph"]
params = {"type": graph_type.value}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_article_rank(
@@ -331,7 +331,7 @@ async def get_article_rank(
api = API["article"]["rank"]
params = {"type": rank_type.value}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_article_source(credential: Credential) -> dict:
@@ -346,4 +346,4 @@ async def get_article_source(credential: Credential) -> dict:
"""
api = API["article"]["source"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
diff --git a/bilibili_api/credential.py b/bilibili_api/credential.py
index 8b86cc96..5dec7847 100644
--- a/bilibili_api/credential.py
+++ b/bilibili_api/credential.py
@@ -4,16 +4,18 @@
凭据操作类
"""
-from typing import Union
-from .utils.credential import Credential as _Credential
-from .utils.network_httpx import get_api, request, get_session, Api
-from Cryptodome.Cipher import PKCS1_OAEP
-from Cryptodome.PublicKey import RSA
-from Cryptodome.Hash import SHA256
-import binascii
+import re
import time
import uuid
-import re
+import binascii
+from typing import Union
+
+from Cryptodome.Hash import SHA256
+from Cryptodome.PublicKey import RSA
+from Cryptodome.Cipher import PKCS1_OAEP
+
+from .utils.credential import Credential as _Credential
+from .utils.network_httpx import Api, get_api, get_session
key = RSA.importKey(
"""\
@@ -85,7 +87,7 @@ async def check_cookies(credential: Credential) -> bool:
bool: 是否需要刷新 Cookies
"""
api = API["info"]["check_cookies"]
- return (await request("GET", api["url"], credential=credential))["refresh"]
+ return (await Api(**api, credential=credential).result)["refresh"]
def getCorrespondPath() -> str:
@@ -176,4 +178,4 @@ async def confirm_refresh(
"csrf": new_credential.bili_jct,
"refresh_token": old_credential.ac_time_value,
}
- await request("POST", api["url"], data=data, credential=new_credential)
+ await Api(**api, credential=new_credential).update_data(**data).result
diff --git a/bilibili_api/data/api/user.json b/bilibili_api/data/api/user.json
index 6045cf00..5b365bdb 100644
--- a/bilibili_api/data/api/user.json
+++ b/bilibili_api/data/api/user.json
@@ -52,7 +52,7 @@
"params": {
"vmid": "int: uid",
"pn": "int: 页码",
- "ps":"int: 每页项数"
+ "ps": "int: 每页项数"
},
"comment": "用户关注的 TAG / 话题,认证方式:SESSDATA"
},
@@ -61,7 +61,7 @@
"method": "GET",
"verify": false,
"params": {
- "vmid":"int: uid"
+ "vmid": "int: uid"
},
"comment": "用户代表作"
},
@@ -227,7 +227,7 @@
"order": "str: desc 倒序, asc 正序",
"order_type": "str: 按照关注顺序排列:留空 按照最常访问排列:attention",
"pn": "int: 页码",
- "ps":"const int: 100"
+ "ps": "const int: 100"
},
"comment": "获取用户关注"
},
@@ -305,7 +305,7 @@
"channel_list": {
"url": "https://api.bilibili.com/x/polymer/space/seasons_series_list",
"method": "GET",
- "verity": false,
+ "verify": false,
"params": {
"mid": "int: uid",
"page_num": "int: 开始项",
@@ -316,7 +316,7 @@
"channel_video_series": {
"url": "https://api.bilibili.com/x/series/archives",
"method": "GET",
- "verity": false,
+ "verify": false,
"params": {
"mid": "int: uid",
"series_id": "int: series_id",
@@ -328,7 +328,7 @@
"channel_video_season": {
"url": "https://api.bilibili.com/x/polymer/space/seasons_archives_list",
"method": "GET",
- "verity": false,
+ "verify": false,
"params": {
"mid": "int: uid",
"season_id": "int: season_id",
@@ -341,7 +341,7 @@
"pugv": {
"url": "https://api.bilibili.com/pugv/app/web/season/page",
"method": "GET",
- "verity": false,
+ "verify": false,
"params": {
"mid": "int: uid"
},
@@ -387,7 +387,7 @@
"method": "GET",
"params": {
"pn": "int: 页码",
- "ps":"int: 每页项数"
+ "ps": "int: 每页项数"
},
"verify": true,
"comment": "获取自己的特别关注"
@@ -397,7 +397,7 @@
"method": "GET",
"params": {
"pn": "int: 页码",
- "ps":"int: 每页项数"
+ "ps": "int: 每页项数"
},
"verify": true,
"comment": "获取自己的悄悄关注"
@@ -413,7 +413,7 @@
"method": "GET",
"params": {
"pn": "int: 页码",
- "ps":"int: 每页项数"
+ "ps": "int: 每页项数"
},
"verify": true,
"comment": "获取自己的黑名单列表"
@@ -425,7 +425,7 @@
"params": {
"vmid": "int: uid",
"pn": "int: 页码",
- "ps":"int: 每页项数"
+ "ps": "int: 每页项数"
},
"comment": "获取指定用户和自己共同关注的 up 主"
},
@@ -528,7 +528,7 @@
"del_channel_aids_series": {
"url": "https://api.bilibili.com/x/series/series/delArchives",
"method": "POST",
- "verity": true,
+ "verify": true,
"params": {
"mid": "int: uid",
"series_id": "int: series_id",
@@ -538,7 +538,7 @@
"add_channel_aids_series": {
"url": "https://api.bilibili.com/x/series/series/addArchives",
"method": "POST",
- "verity": true,
+ "verify": true,
"params": {
"mid": "int: uid",
"series_id": "int: series_id",
@@ -548,7 +548,7 @@
"del_channel_series": {
"url": "https://api.bilibili.com/x/series/series/delete",
"method": "POST",
- "verity": true,
+ "verify": true,
"query": {
"mid": "int: uid",
"series_id": "int: series_id",
@@ -569,4 +569,4 @@
}
}
}
-}
+}
\ No newline at end of file
diff --git a/bilibili_api/data/api/video.json b/bilibili_api/data/api/video.json
index 30c14806..3f24feb7 100644
--- a/bilibili_api/data/api/video.json
+++ b/bilibili_api/data/api/video.json
@@ -197,6 +197,12 @@
"ps": "int: 每页项数"
},
"comment": "列出稿件公开笔记列表"
+ },
+ "video_online_broadcast_servers": {
+ "method": "GET",
+ "verify": true,
+ "url": "https://api.bilibili.com/x/web-interface/broadcast/servers?platform=pc",
+ "comment": "获取视频在线人数实时监测服务器列表"
}
},
"operate": {
diff --git a/bilibili_api/dynamic.py b/bilibili_api/dynamic.py
index 47b2d91c..39330da8 100644
--- a/bilibili_api/dynamic.py
+++ b/bilibili_api/dynamic.py
@@ -4,25 +4,24 @@
动态相关
"""
-import json
-import re
import os
+import re
import sys
-from typing import Any, List, Tuple, Union, Optional
+import json
+import asyncio
from enum import Enum
+from datetime import datetime
+from typing import Any, List, Tuple, Union, Optional
import httpx
-from .exceptions.DynamicExceedImagesException import DynamicExceedImagesException
-from .utils.network_httpx import request
-from .utils.credential import Credential
-from .utils.sync import sync
-from . import user, exceptions
from .utils import utils
+from .utils.sync import sync
from .utils.picture import Picture
-from . import vote
-from datetime import datetime
-import asyncio
+from . import user, vote, exceptions
+from .utils.credential import Credential
+from .utils.network_httpx import Api
+from .exceptions.DynamicExceedImagesException import DynamicExceedImagesException
API = utils.get_api("dynamic")
@@ -201,14 +200,8 @@ async def upload_image(image: Picture, credential: Credential, data: dict = None
if data is None:
data = {"biz": "new_dyn", "category": "daily"}
- return_info = await request(
- "POST",
- url=api["url"],
- data=data,
- files={"file_up": raw},
- credential=credential,
- )
-
+ files={"file_up": raw}
+ return_info = await Api(**api, credential=credential).update_data(**data).request(files=files)
return return_info
@@ -612,7 +605,7 @@ async def schedule(type_: int):
"publish_time": int(send_time.timestamp()), # type: ignore
"request": json.dumps(request_data, ensure_ascii=False),
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
if info.time != None:
return await schedule(2 if len(info.pics) == 0 else 4)
@@ -637,14 +630,8 @@ async def schedule(type_: int):
data["dyn_req"]["attach_card"]["common_card"] = info.get_attach_card()
else:
data["dyn_req"]["attach_card"] = None
- send_result = await request(
- "POST",
- api["url"],
- data=data,
- credential=credential,
- params={"csrf": credential.bili_jct},
- json_body=True,
- )
+ params = {"csrf": credential.bili_jct}
+ send_result = await Api(**api, credential=credential, json_body=True).update_data(**data).update_params(**params).result
return send_result
@@ -664,7 +651,7 @@ async def get_schedules_list(credential: Credential) -> dict:
credential.raise_for_no_sessdata()
api = API["schedule"]["list"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def send_schedule_now(draft_id: int, credential: Credential) -> dict:
@@ -683,7 +670,7 @@ async def send_schedule_now(draft_id: int, credential: Credential) -> dict:
api = API["schedule"]["publish_now"]
data = {"draft_id": draft_id}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def delete_schedule(draft_id: int, credential: Credential) -> dict:
@@ -702,7 +689,7 @@ async def delete_schedule(draft_id: int, credential: Credential) -> dict:
api = API["schedule"]["delete"]
data = {"draft_id": draft_id}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
class Dynamic:
@@ -741,9 +728,7 @@ async def get_info(self) -> dict:
"timezone_offset": -480,
"features": "itemOpusStyle",
}
- data = await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ data = await Api(**api, credential=self.credential).update_params(**params).result
return data
async def get_reposts(self, offset: str = "0") -> dict:
@@ -760,9 +745,7 @@ async def get_reposts(self, offset: str = "0") -> dict:
params: dict[str, Any] = {"dynamic_id": self.__dynamic_id}
if offset != "0":
params["offset"] = offset
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_likes(self, pn: int = 1, ps: int = 30) -> dict:
"""
@@ -778,9 +761,7 @@ async def get_likes(self, pn: int = 1, ps: int = 30) -> dict:
"""
api = API["info"]["likes"]
params = {"dynamic_id": self.__dynamic_id, "pn": pn, "ps": ps}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def set_like(self, status: bool = True) -> dict:
"""
@@ -805,7 +786,7 @@ async def set_like(self, status: bool = True) -> dict:
"up": 1 if status else 2,
"uid": self_uid,
}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def delete(self) -> dict:
"""
@@ -818,7 +799,7 @@ async def delete(self) -> dict:
api = API["operate"]["delete"]
data = {"dynamic_id": self.__dynamic_id}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def repost(self, text: str = "转发动态") -> dict:
"""
@@ -835,7 +816,7 @@ async def repost(self, text: str = "转发动态") -> dict:
api = API["operate"]["repost"]
data = await _get_text_data(text)
data["dynamic_id"] = self.__dynamic_id
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_new_dynamic_users(credential: Union[Credential, None] = None) -> dict:
@@ -851,7 +832,7 @@ async def get_new_dynamic_users(credential: Union[Credential, None] = None) -> d
credential = credential if credential else Credential()
credential.raise_for_no_sessdata()
api = API["info"]["attention_new_dynamic"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_live_users(
@@ -872,7 +853,7 @@ async def get_live_users(
credential.raise_for_no_sessdata()
api = API["info"]["attention_live"]
params = {"size": size}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_dynamic_page_UPs_info(credential: Credential) -> dict:
@@ -886,7 +867,7 @@ async def get_dynamic_page_UPs_info(credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["info"]["dynamic_page_UPs_info"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_dynamic_page_info(
@@ -930,9 +911,7 @@ async def get_dynamic_page_info(
elif host_mid: # 指定 UP 主动态
params["host_mid"] = host_mid
- dynmaic_data = await request(
- "GET", api["url"], credential=credential, params=params
- )
+ dynmaic_data = await Api(**api, credential=credential).update_params(**params).result
return [
Dynamic(dynamic_id=int(dynamic["id_str"]), credential=credential)
for dynamic in dynmaic_data["items"]
diff --git a/bilibili_api/emoji.py b/bilibili_api/emoji.py
index 8d1d179a..fa19c6b3 100644
--- a/bilibili_api/emoji.py
+++ b/bilibili_api/emoji.py
@@ -4,8 +4,8 @@
表情包相关
"""
-from .utils.network_httpx import request
from .utils.utils import get_api
+from .utils.network_httpx import Api
API = get_api("emoji")
@@ -22,4 +22,4 @@ async def get_emoji_list(business: str = "reply") -> dict:
"""
api = API["list"]
params = {"business": business}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
diff --git a/bilibili_api/exceptions/__init__.py b/bilibili_api/exceptions/__init__.py
index ba5c6982..4ecd7b4a 100644
--- a/bilibili_api/exceptions/__init__.py
+++ b/bilibili_api/exceptions/__init__.py
@@ -4,17 +4,17 @@
错误
"""
+from .LoginError import *
from .ApiException import *
-from .ResponseCodeException import *
-from .ResponseException import *
-from .NetworkException import *
from .ArgsException import *
-from .CredentialNoSessdataException import *
-from .CredentialNoBiliJctException import *
-from .CredentialNoBuvid3Exception import *
-from .CredentialNoDedeUserIDException import *
-from .DanmakuClosedException import *
+from .NetworkException import *
+from .ResponseException import *
from .VideoUploadException import *
-from .LoginError import *
+from .ResponseCodeException import *
+from .DanmakuClosedException import *
from .LiveException import LiveException
+from .CredentialNoBuvid3Exception import *
+from .CredentialNoBiliJctException import *
+from .CredentialNoSessdataException import *
+from .CredentialNoDedeUserIDException import *
from .DynamicExceedImagesException import DynamicExceedImagesException
diff --git a/bilibili_api/favorite_list.py b/bilibili_api/favorite_list.py
index 1f183e46..49395be7 100644
--- a/bilibili_api/favorite_list.py
+++ b/bilibili_api/favorite_list.py
@@ -5,14 +5,14 @@
"""
from enum import Enum
+from typing import List, Union, Optional
-from .video import Video
from . import user
-from .utils.network_httpx import request
-from .exceptions.ArgsException import ArgsException
+from .video import Video
+from .utils.utils import join, get_api
from .utils.credential import Credential
-from .utils.utils import get_api, join
-from typing import List, Union, Optional
+from .utils.network_httpx import Api
+from .exceptions.ArgsException import ArgsException
API = get_api("favorite-list")
@@ -110,9 +110,7 @@ async def get_info(self):
api = API["info"]["info"]
params = {"media_id": self.__media_id}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_content_video(
self,
@@ -177,9 +175,7 @@ async def get_content_ids_info(self) -> dict:
api = API["info"]["list_content_id_list"]
params = {"media_id": self.__media_id}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_video_favorite_list(
@@ -206,7 +202,7 @@ async def get_video_favorite_list(
if video is not None:
params["rid"] = video.get_aid()
- return await request("GET", url=api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_video_favorite_list_content(
@@ -254,7 +250,7 @@ async def get_video_favorite_list_content(
if keyword is not None:
params["keyword"] = keyword
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_topic_favorite_list(
@@ -279,7 +275,7 @@ async def get_topic_favorite_list(
api = API["info"]["list_topics"]
params = {"page_num": page, "page_size": 16}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_article_favorite_list(
@@ -304,7 +300,7 @@ async def get_article_favorite_list(
api = API["info"]["list_articles"]
params = {"pn": page, "ps": 16}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_album_favorite_list(
@@ -329,7 +325,7 @@ async def get_album_favorite_list(
api = API["info"]["list_albums"]
params = {"page": page, "pagesize": 30, "biz_type": 2}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_course_favorite_list(
@@ -355,7 +351,7 @@ async def get_course_favorite_list(
self_info = await user.get_self_info(credential)
params = {"pn": page, "ps": 10, "mid": self_info["mid"]}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_note_favorite_list(
@@ -380,7 +376,7 @@ async def get_note_favorite_list(
api = API["info"]["list_notes"]
params = {"pn": page, "ps": 16}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def create_video_favorite_list(
@@ -418,7 +414,7 @@ async def create_video_favorite_list(
"cover": "",
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def modify_video_favorite_list(
@@ -461,7 +457,7 @@ async def modify_video_favorite_list(
"media_id": media_id,
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def delete_video_favorite_list(
@@ -485,7 +481,7 @@ async def delete_video_favorite_list(
data = {"media_ids": join(",", media_ids)}
api = API["operate"]["delete"]
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def copy_video_favorite_list_content(
@@ -519,7 +515,7 @@ async def copy_video_favorite_list_content(
"resources": ",".join(map(lambda x: f"{str(x)}:2", aids)),
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def move_video_favorite_list_content(
@@ -550,7 +546,7 @@ async def move_video_favorite_list_content(
"resources": ",".join(map(lambda x: f"{str(x)}:2", aids)),
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def delete_video_favorite_list_content(
@@ -578,7 +574,7 @@ async def delete_video_favorite_list_content(
"resources": ",".join(map(lambda x: f"{str(x)}:2", aids)),
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def clean_video_favorite_list_content(
@@ -601,7 +597,7 @@ async def clean_video_favorite_list_content(
data = {"media_id": media_id}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def get_favorite_collected(
@@ -624,4 +620,4 @@ async def get_favorite_collected(
"""
api = API["info"]["collected"]
params = {"up_mid": uid, "platform": "web", "pn": pn, "ps": ps}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
diff --git a/bilibili_api/game.py b/bilibili_api/game.py
index 8ad71400..09cc7338 100644
--- a/bilibili_api/game.py
+++ b/bilibili_api/game.py
@@ -4,11 +4,12 @@
游戏相关
"""
-from .utils.network_httpx import request
-from .utils.credential import Credential
-from .utils.utils import get_api
from typing import Union
+from .utils.utils import get_api
+from .utils.credential import Credential
+from .utils.network_httpx import Api
+
API = get_api("game")
@@ -42,9 +43,7 @@ async def get_info(self) -> dict:
"""
api = API["info"]["info"]
params = {"game_base_id": self.__game_id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_up_info(self) -> dict:
"""
@@ -55,9 +54,7 @@ async def get_up_info(self) -> dict:
"""
api = API["info"]["UP"]
params = {"game_base_id": self.__game_id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_detail(self) -> dict:
"""
@@ -68,9 +65,7 @@ async def get_detail(self) -> dict:
"""
api = API["info"]["detail"]
params = {"game_base_id": self.__game_id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_wiki(self) -> dict:
"""
@@ -81,9 +76,7 @@ async def get_wiki(self) -> dict:
"""
api = API["info"]["wiki"]
params = {"game_base_id": self.__game_id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_videos(self) -> dict:
"""
@@ -94,9 +87,7 @@ async def get_videos(self) -> dict:
"""
api = API["info"]["videos"]
params = {"game_base_id": self.__game_id}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
# async def get_score(self) -> dict:
# """
diff --git a/bilibili_api/homepage.py b/bilibili_api/homepage.py
index bdf2c04b..b7e5e45d 100644
--- a/bilibili_api/homepage.py
+++ b/bilibili_api/homepage.py
@@ -5,9 +5,10 @@
"""
from typing import Union
+
from .utils.utils import get_api
-from .utils.network_httpx import request
from .utils.credential import Credential
+from .utils.network_httpx import Api
API = get_api("homepage")
@@ -22,7 +23,7 @@ async def get_top_photo() -> dict:
"""
api = API["info"]["top_photo"]
params = {"resource_id": 142}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_links(credential: Union[Credential, None] = None):
@@ -38,7 +39,7 @@ async def get_links(credential: Union[Credential, None] = None):
"""
api = API["info"]["links"]
params = {"pf": 0, "ids": 4694}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_popularize(credential: Union[Credential, None] = None):
@@ -54,7 +55,7 @@ async def get_popularize(credential: Union[Credential, None] = None):
"""
api = API["info"]["popularize"]
params = {"pf": 0, "ids": 34}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_videos(credential: Union[Credential, None] = None):
@@ -68,4 +69,4 @@ async def get_videos(credential: Union[Credential, None] = None):
调用 API 返回的结果
"""
api = API["info"]["videos"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
diff --git a/bilibili_api/hot.py b/bilibili_api/hot.py
index 5401dd57..46665530 100644
--- a/bilibili_api/hot.py
+++ b/bilibili_api/hot.py
@@ -4,8 +4,8 @@
热门相关 API
"""
-from .utils.network_httpx import request
from .utils.utils import get_api
+from .utils.network_httpx import Api
API_rank = get_api("rank")
API = get_api("hot")
@@ -25,7 +25,7 @@ async def get_hot_videos(pn: int = 1, ps: int = 20) -> dict:
"""
api = API_rank["info"]["hot"]
params = {"ps": ps, "pn": pn}
- return await request("GET", url=api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_weakly_hot_videos_list() -> dict:
@@ -36,7 +36,7 @@ async def get_weakly_hot_videos_list() -> dict:
调用 API 返回的结果
"""
api = API_rank["info"]["weakly_series"]
- return await request("GET", url=api["url"])
+ return await Api(**api).result
async def get_weakly_hot_videos(week: int = 1) -> dict:
@@ -51,7 +51,7 @@ async def get_weakly_hot_videos(week: int = 1) -> dict:
"""
api = API_rank["info"]["weakly_details"]
params = {"number": week}
- return await request("GET", url=api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_history_popular_videos() -> dict:
@@ -63,7 +63,7 @@ async def get_history_popular_videos() -> dict:
"""
api = API_rank["info"]["history_popular"]
params = {"page_size": 85, "page": 1}
- return await request("GET", url=api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_hot_buzzwords(page_num: int = 1, page_size: int = 20) -> dict:
@@ -80,4 +80,4 @@ async def get_hot_buzzwords(page_num: int = 1, page_size: int = 20) -> dict:
"""
api = API["buzzwords"]
params = {"pn": page_num, "ps": page_size, "type_id": 4}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
diff --git a/bilibili_api/interactive_video.py b/bilibili_api/interactive_video.py
index ae3e0d04..25d550c3 100644
--- a/bilibili_api/interactive_video.py
+++ b/bilibili_api/interactive_video.py
@@ -6,25 +6,26 @@
# pylint: skip-file
-from asyncio import CancelledError, create_task
+import os
import copy
import enum
import json
-import os
-import shutil
import time
-from typing import Callable, List, Tuple, Union
-from .utils.credential import Credential
-from .utils.utils import get_api
-from .utils.network_httpx import request
-from .video import Video
+import shutil
+import zipfile
from urllib import parse
from random import randint as rand
+from asyncio import CancelledError, create_task
+from typing import List, Tuple, Union, Callable, Coroutine
+
import requests
+
from . import settings
-import zipfile
+from .video import Video
+from .utils.utils import get_api
from .utils.AsyncEvent import AsyncEvent
-from typing import Coroutine
+from .utils.credential import Credential
+from .utils.network_httpx import Api
API = get_api("interactive_video")
@@ -475,9 +476,9 @@ async def up_get_ivideo_pages(self) -> dict:
dict: 调用 API 返回的结果
"""
credential = self.credential if self.credential else Credential()
- url = API["info"]["videolist"]["url"]
+ api = API["info"]["videolist"]
params = {"bvid": self.get_bvid()}
- return await request("GET", url=url, params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def up_submit_story_tree(self, story_tree: str) -> dict:
"""
@@ -490,7 +491,7 @@ async def up_submit_story_tree(self, story_tree: str) -> dict:
dict: 调用 API 返回的结果
"""
credential = self.credential if self.credential else Credential()
- url = API["operate"]["savestory"]["url"]
+ api = API["operate"]["savestory"]
form_data = {"preview": "0", "data": story_tree, "csrf": credential.bili_jct}
headers = {
"User-Agent": "Mozilla/5.0",
@@ -500,15 +501,9 @@ async def up_submit_story_tree(self, story_tree: str) -> dict:
"Accept": "application/json, text/plain, */*",
}
data = parse.urlencode(form_data)
- return await request(
- "POST",
- url=url,
- data=data,
- headers=headers,
- no_csrf=True,
- credential=credential,
- )
-
+ return await Api(**api, credential=credential, no_csrf=True
+ ).update_data(**data).update_headers(**headers).result
+
async def get_graph_version(self) -> int:
"""
获取剧情图版本号,仅供 `get_edge_info()` 使用。
@@ -527,7 +522,7 @@ async def get_graph_version(self) -> int:
api = "https://api.bilibili.com/x/player/v2"
params = {"bvid": bvid, "cid": cid}
- resp = await request("GET", api, params, credential=credential)
+ resp = await Api(**api, credential=credential).update_params(**params).result
return resp["interaction"]["graph_version"]
async def get_edge_info(self, edge_id: Union[int, None] = None):
@@ -543,13 +538,13 @@ async def get_edge_info(self, edge_id: Union[int, None] = None):
bvid = self.get_bvid()
credential = self.credential if self.credential is not None else Credential()
- url = API["info"]["edge_info"]["url"]
+ api = API["info"]["edge_info"]
params = {"bvid": bvid, "graph_version": (await self.get_graph_version())}
if edge_id is not None:
params["edge_id"] = edge_id
- return await request("GET", url, params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def mark_score(self, score: int = 5):
"""
@@ -565,7 +560,7 @@ async def mark_score(self, score: int = 5):
self.credential.raise_for_no_bili_jct()
api = API["operate"]["mark_score"]
data = {"mark": score, "bvid": self.get_bvid()}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_cid(self) -> int:
"""
diff --git a/bilibili_api/live.py b/bilibili_api/live.py
index 7f744ef3..92a42069 100644
--- a/bilibili_api/live.py
+++ b/bilibili_api/live.py
@@ -3,25 +3,25 @@
直播相关
"""
-import base64
-import time
-from enum import Enum
-import logging
import json
+import time
+import base64
import struct
import asyncio
+import logging
+from enum import Enum
from typing import Any, List, Union
-import aiohttp
-import brotli
+import brotli
+import aiohttp
from aiohttp.client_ws import ClientWebSocketResponse
-from .utils.credential import Credential
-from .utils.network_httpx import request
-from .utils.network import get_session
from .utils.utils import get_api
from .utils.danmaku import Danmaku
+from .utils.network import get_session
from .utils.AsyncEvent import AsyncEvent
+from .utils.credential import Credential
+from .utils.network_httpx import Api
from .exceptions.LiveException import LiveException
API = get_api("live")
@@ -141,9 +141,7 @@ async def start(self, area_id: int) -> dict:
"room_id": self.room_display_id,
"platform": "pc",
}
- resp = await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ resp = await Api(**api, credential=self.credential).update_params(**params).result
return resp
async def stop(self) -> dict:
@@ -157,9 +155,7 @@ async def stop(self) -> dict:
params = {
"room_id": self.room_display_id,
}
- resp = await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ resp = await Api(**api, credential=self.credential).update_params(**params).result
return resp
async def get_room_play_info(self) -> dict:
@@ -173,9 +169,7 @@ async def get_room_play_info(self) -> dict:
params = {
"room_id": self.room_display_id,
}
- resp = await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ resp = await Api(**api, credential=self.credential).update_params(**params).result
# 缓存真实房间 ID
self.__ruid = resp["uid"]
@@ -205,9 +199,7 @@ async def get_chat_conf(self) -> dict:
"""
api = API["info"]["chat_conf"]
params = {"room_id": self.room_display_id}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_room_info(self) -> dict:
"""
@@ -218,9 +210,7 @@ async def get_room_info(self) -> dict:
"""
api = API["info"]["room_info"]
params = {"room_id": self.room_display_id}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_fan_model(
self,
@@ -256,9 +246,7 @@ async def get_fan_model(
params["roomId"] = roomId
if target_id:
params["target_id"] = target_id
- return await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_user_info_in_room(self) -> dict:
"""
@@ -271,9 +259,7 @@ async def get_user_info_in_room(self) -> dict:
api = API["info"]["user_info_in_room"]
params = {"room_id": self.room_display_id}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_dahanghai(self, page: int = 1) -> dict:
"""
@@ -292,9 +278,7 @@ async def get_dahanghai(self, page: int = 1) -> dict:
"page_size": 30,
"page": page,
}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_gaonengbang(self, page: int = 1) -> dict:
"""
@@ -313,9 +297,7 @@ async def get_gaonengbang(self, page: int = 1) -> dict:
"pageSize": 50,
"page": page,
}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_seven_rank(self) -> dict:
"""
@@ -329,9 +311,7 @@ async def get_seven_rank(self) -> dict:
"roomid": self.room_display_id,
"ruid": await self.__get_ruid(),
}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_fans_medal_rank(self) -> dict:
"""
@@ -342,9 +322,7 @@ async def get_fans_medal_rank(self) -> dict:
"""
api = API["info"]["fans_medal_rank"]
params = {"roomid": self.room_display_id, "ruid": await self.__get_ruid()}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_black_list(self) -> dict:
"""
@@ -356,9 +334,7 @@ async def get_black_list(self) -> dict:
api = API["info"]["black_list"]
params = {"room_id": self.room_display_id, "ps": 1}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_room_play_url(
self, screen_resolution: ScreenResolution = ScreenResolution.ORIGINAL
@@ -380,9 +356,7 @@ async def get_room_play_url(
"https_url_req": "1",
"ptype": "16",
}
- return await request(
- api["method"], api["url"], params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_room_play_info_v2(
self,
@@ -416,9 +390,7 @@ async def get_room_play_info_v2(
"codec": live_codec.value,
"qn": live_qn.value,
}
- return await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def ban_user(self, uid: int) -> dict:
"""
@@ -439,9 +411,7 @@ async def ban_user(self, uid: int) -> dict:
"mobile_app": "web",
"visit_id": "",
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def unban_user(self, block_id: int) -> dict:
"""
@@ -460,9 +430,7 @@ async def unban_user(self, block_id: int) -> dict:
"id": block_id,
"visit_id": "",
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def send_danmaku(self, danmaku: Danmaku) -> dict:
"""
@@ -489,9 +457,7 @@ async def send_danmaku(self, danmaku: Danmaku) -> dict:
"fontsize": danmaku.font_size,
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def sign_up_dahanghai(self, task_id: int = 1447) -> dict:
"""
@@ -511,9 +477,7 @@ async def sign_up_dahanghai(self, task_id: int = 1447) -> dict:
"task_id": task_id,
"uid": await self.__get_ruid(),
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def send_gift_from_bag(
self,
@@ -560,9 +524,7 @@ async def send_gift_from_bag(
"biz_id": self.room_display_id,
"ruid": await self.__get_ruid(),
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def receive_reward(self, receive_type: int = 2) -> dict:
"""
@@ -581,9 +543,7 @@ async def receive_reward(self, receive_type: int = 2) -> dict:
"ruid": await self.__get_ruid(),
"receive_type": receive_type,
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_general_info(self, act_id: int = 100061) -> dict:
"""
@@ -603,9 +563,7 @@ async def get_general_info(self, act_id: int = 100061) -> dict:
"roomId": self.room_display_id,
"uid": await self.__get_ruid(),
}
- return await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_gift_common(self) -> dict:
"""
@@ -618,12 +576,10 @@ async def get_gift_common(self) -> dict:
params_room_info = {
"room_id": self.room_display_id,
}
- res_room_info = await request(
- api_room_info["method"],
- api_room_info["url"],
- params=params_room_info,
- credential=self.credential,
- )
+ res_room_info = await Api(
+ **api_room_info,
+ credential=self.credential
+ ).update_params(**params_room_info).result
area_id, area_parent_id = (
res_room_info["room_info"]["area_id"],
res_room_info["room_info"]["parent_area_id"],
@@ -637,9 +593,7 @@ async def get_gift_common(self) -> dict:
"platform": "pc",
"source": "live",
}
- return await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_gift_special(self, tab_id: int) -> dict:
"""
@@ -655,12 +609,10 @@ async def get_gift_special(self, tab_id: int) -> dict:
params_room_info = {
"room_id": self.room_display_id,
}
- res_room_info = await request(
- api_room_info["method"],
- api_room_info["url"],
- params=params_room_info,
- credential=self.credential,
- )
+ res_room_info = await Api(
+ **api_room_info,
+ credential=self.credential
+ ).update_params(**params_room_info).result
area_id, area_parent_id = (
res_room_info["room_info"]["area_id"],
res_room_info["room_info"]["parent_area_id"],
@@ -677,9 +629,7 @@ async def get_gift_special(self, tab_id: int) -> dict:
"platform": "pc",
"build": 1,
}
- return await request(
- api["method"], api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def send_gift_gold(
self, uid: int, gift_id: int, gift_num: int, price: int, storm_beat_id: int = 0
@@ -721,9 +671,7 @@ async def send_gift_gold(
"rnd": int(time.time()),
"visit_id": "",
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def send_gift_silver(
self,
@@ -770,9 +718,7 @@ async def send_gift_silver(
"rnd": int(time.time()),
"visit_id": "",
}
- return await request(
- api["method"], api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
class LiveDanmaku(AsyncEvent):
@@ -1097,9 +1043,7 @@ async def __heartbeat(self, ws: ClientWebSocketResponse) -> None:
base64.b64encode(f"60|{self.room_display_id}|1|0".encode("utf-8")),
"utf-8",
)
- await request(
- "GET", heartbeat_url, params={"hb": hb, "pf": "web"}, json_body=True
- )
+ await Api(method="GET", url=heartbeat_url, json_body=True).update_params(**{"hb": hb, "pf": "web"}).result
elif self.__heartbeat_timer <= -30:
# 视为已异常断开连接,发布 TIMEOUT 事件
self.dispatch("TIMEOUT")
@@ -1198,7 +1142,7 @@ async def get_self_info(credential: Credential) -> dict:
credential.raise_for_no_sessdata()
api = API["info"]["user_info"]
- return await request(api["method"], api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_self_live_info(credential: Credential) -> dict:
@@ -1212,7 +1156,7 @@ async def get_self_live_info(credential: Credential) -> dict:
credential.raise_for_no_sessdata()
api = API["info"]["live_info"]
- return await request(api["method"], api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_self_dahanghai_info(
@@ -1245,9 +1189,7 @@ async def get_self_dahanghai_info(
api = API["info"]["user_guards"]
params = {"page": page, "page_size": page_size}
- return await request(
- api["method"], api["url"], params=params, credential=credential
- )
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_bag(credential: Credential) -> dict:
@@ -1261,7 +1203,7 @@ async def get_self_bag(credential: Credential) -> dict:
credential.raise_for_no_sessdata()
api = API["info"]["bag_list"]
- return await request(api["method"], api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_gift_config(
@@ -1292,7 +1234,7 @@ async def get_gift_config(
"area_id": area_id if area_id is not None else "",
"area_parent_id": area_parent_id if area_parent_id is not None else "",
}
- return await request(api["method"], api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_area_info() -> dict:
@@ -1303,7 +1245,7 @@ async def get_area_info() -> dict:
dict: 调用 API 返回的结果
"""
api = API["info"]["area_info"]
- return await request(api["method"], api["url"])
+ return await Api(**api).result
async def get_live_followers_info(
@@ -1325,9 +1267,7 @@ async def get_live_followers_info(
api = API["info"]["followers_live_info"]
params = {"need_recommend": int(need_recommend), "filterRule": 0}
- return await request(
- api["method"], api["url"], params=params, credential=credential
- )
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_unlive_followers_info(
@@ -1354,9 +1294,7 @@ async def get_unlive_followers_info(
"page": page,
"pagesize": page_size,
}
- return await request(
- api["method"], api["url"], params=params, credential=credential
- )
+ return await Api(**api, credential=credential).update_params(**params).result
async def create_live_reserve(
@@ -1383,4 +1321,4 @@ async def create_live_reserve(
"stime": None,
"from": 1,
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
diff --git a/bilibili_api/live_area.py b/bilibili_api/live_area.py
index 1c9130ca..66af2ff4 100644
--- a/bilibili_api/live_area.py
+++ b/bilibili_api/live_area.py
@@ -4,14 +4,14 @@
直播间分区相关操作。
"""
-import json
import os
import copy
-from typing import Tuple, Union, List, Dict
+import json
from enum import Enum
-from .utils.utils import get_api
-from .utils.network_httpx import request
+from typing import Dict, List, Tuple, Union
+from .utils.utils import get_api
+from .utils.network_httpx import Api
API = get_api("live-area")
@@ -149,4 +149,4 @@ async def get_list_by_area(
"page": page,
"sort_type": order.value,
}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
diff --git a/bilibili_api/login.py b/bilibili_api/login.py
index c5fbbd13..939ef559 100644
--- a/bilibili_api/login.py
+++ b/bilibili_api/login.py
@@ -8,36 +8,32 @@
**login_with_qrcode 用到了 tkinter,linux 的小伙伴请注意安装**
"""
+import os
+import sys
import json
-import httpx
-from typing import Union
+import time
import uuid
-from yarl import URL
+import base64
+import tempfile
import webbrowser
-import sys
+from typing import Dict, List, Union
-from .exceptions.LoginError import LoginError
+import rsa
+import httpx
+import qrcode
+from yarl import URL
-from .utils.credential import Credential
-from .utils.utils import get_api
+from . import settings
from .utils.sync import sync
-from .utils.network_httpx import get_session, HEADERS
+from .utils.utils import get_api
+from .utils.credential import Credential
+from .exceptions.LoginError import LoginError
from .utils.network import to_form_urlencoded
-from .utils.captcha import start_server, close_server, get_result
-from .utils.safecenter_captcha import (
- start_server as safecenter_start_server,
- close_server as safecenter_close_server,
- get_result as safecenter_get_result,
-)
-from . import settings
-import qrcode
-import os
-import tempfile
-import time
-import base64
-import rsa
-
-from typing import List, Dict
+from .utils.network_httpx import HEADERS, get_session
+from .utils.captcha import get_result, close_server, start_server
+from .utils.safecenter_captcha import get_result as safecenter_get_result
+from .utils.safecenter_captcha import close_server as safecenter_close_server
+from .utils.safecenter_captcha import start_server as safecenter_start_server
API = get_api("login")
@@ -106,6 +102,7 @@ def login_with_qrcode(root=None) -> Credential:
global id_
import tkinter
import tkinter.font
+
from PIL.ImageTk import PhotoImage
if root == None:
diff --git a/bilibili_api/login_func.py b/bilibili_api/login_func.py
index 60104abe..f0f578d5 100644
--- a/bilibili_api/login_func.py
+++ b/bilibili_api/login_func.py
@@ -4,17 +4,19 @@
登录功能相关
"""
+import enum
+import json
+import uuid
import threading
from typing import Tuple, Union
+
+import requests
+
from . import login
-from .utils.credential import Credential
from .utils.utils import get_api
-from .utils.picture import Picture
-import json
-import requests
-import uuid
from .exceptions import LoginError
-import enum
+from .utils.picture import Picture
+from .utils.credential import Credential
API = get_api("login")
@@ -40,8 +42,9 @@ def get_qrcode() -> Tuple[Picture, str]:
Returns:
Tuple[dir, str]: 第一项是二维码图片地址(本地缓存)和登录密钥。登录密钥需要保存。
"""
- img = login.update_qrcode_image()
- login_key = login.login_key
+ login_data = login.update_qrcode_data()
+ login_key = login_data["qrcode_key"]
+ img = login.make_qrcode(login_data["url"])
return (Picture.from_file(img), login_key)
diff --git a/bilibili_api/manga.py b/bilibili_api/manga.py
index 6f41981c..aad729f5 100644
--- a/bilibili_api/manga.py
+++ b/bilibili_api/manga.py
@@ -4,16 +4,18 @@
漫画相关操作
"""
-from bilibili_api.utils.network_httpx import request, HEADERS
+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.utils.credential import Credential
from bilibili_api.errors import ArgsException
from bilibili_api.utils.picture import Picture
-from typing import Optional, List, Dict, Union
-from urllib.parse import urlparse
-import httpx
-from enum import Enum
-import datetime
+from bilibili_api.utils.credential import Credential
+from bilibili_api.utils.network_httpx import HEADERS, request, Api
API = get_api("manga")
@@ -155,17 +157,15 @@ async def get_info(self) -> dict:
"""
api = API["info"]["detail"]
params = {"comic_id": self.__manga_id}
- return await request(
- "POST",
- api["url"],
- params=params,
+ 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:
"""
@@ -245,17 +245,15 @@ async def get_images_url(
episode_id = await self.get_episode_id(episode_count)
api = API["info"]["episode_images"]
params = {"ep_id": episode_id}
- return await request(
- "POST",
- api["url"],
- params=params,
+ return await Api(
+ **api,
+ credential=self.credential,
no_csrf=(
False
if (self.credential.has_sessdata() and self.credential.has_bili_jct())
else True
),
- credential=self.credential,
- )
+ ).update_params(**params).result
async def get_images(
self,
@@ -285,20 +283,15 @@ async def get_images(
async def get_real_image_url(url: str) -> str:
token_api = API["info"]["image_token"]
datas = {"urls": f'["{url}"]'}
- token_data = await request(
- "POST",
- token_api["url"],
- data=datas,
- no_csrf=(
- False
- if (
- self.credential.has_sessdata()
- and self.credential.has_bili_jct()
- )
- else True
- ),
- credential=self.credential,
- )
+ 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"]:
@@ -336,16 +329,15 @@ async def manga_image_url_turn_to_Picture(
async def get_real_image_url(url: str) -> str:
token_api = API["info"]["image_token"]
datas = {"urls": f'["{url}"]'}
- token_data = await request(
- "POST",
- token_api["url"],
- data=datas,
+ 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)
@@ -377,7 +369,7 @@ async def set_follow_manga(
else:
api = API["operate"]["del_favorite"]
data = {"comic_ids": str(manga.get_manga_id())}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def get_raw_manga_index(
@@ -421,8 +413,7 @@ async def get_raw_manga_index(
"page_num": pn,
"page_size": ps,
}
- return await request("POST", api["url"], data=data, params=params, no_csrf=True)
-
+ return await Api(**api, no_csrf=True).update_data(**data).update_params(**params).result
async def get_manga_index(
area: MangaIndexFilter.Area = MangaIndexFilter.Area.ALL,
@@ -479,9 +470,7 @@ async def get_manga_update(
if isinstance(date, datetime.datetime):
date = date.strftime("%Y-%m-%d")
data = {"date": date, "page_num": pn, "page_size": ps}
- manga_data = await request(
- "POST", api["url"], no_csrf=True, params=params, data=data
- )
+ manga_data = await Api(**api, no_csrf=True).update_data(**data).update_params(**params).result
return [Manga(manga["comic_id"]) for manga in manga_data["list"]]
@@ -502,7 +491,5 @@ async def get_manga_home_recommend(
api = API["info"]["home_recommend"]
params = {"device": "pc", "platform": "web"}
data = {"page_num": pn, "seed": seed}
- manga_data = await request(
- "POST", api["url"], no_csrf=True, params=params, data=data
- )
+ manga_data = await Api(**api, no_csrf=True).update_data(**data).update_params(**params).result
return [Manga(manga["comic_id"]) for manga in manga_data["list"]]
diff --git a/bilibili_api/music.py b/bilibili_api/music.py
index 0dcbcaf6..f255c9f7 100644
--- a/bilibili_api/music.py
+++ b/bilibili_api/music.py
@@ -6,11 +6,12 @@
注意: 目前 B 站的音频并不和 B 站的音乐相关信息互通。这里的 Music 类的数据来源于视频下面的 bgm 标签和全站音乐榜中的每一个 bgm/音乐。get_homepage_recommend 和 get_music_index_info 来源于 https://www.bilibili.com/v/musicplus/
"""
-from .utils.network_httpx import request
-from .utils.credential import Credential
-from .utils.utils import get_api
-from typing import Optional
from enum import Enum
+from typing import Optional
+
+from .utils.utils import get_api
+from .utils.credential import Credential
+from .utils.network_httpx import Api
API_audio = get_api("audio")
API = get_api("music")
@@ -117,7 +118,7 @@ async def get_homepage_recommend(credential: Optional[Credential] = None):
"""
credential = credential if credential else Credential()
api = API_audio["audio_info"]["homepage_recommend"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_music_index_info(
@@ -153,7 +154,7 @@ async def get_music_index_info(
"pn": page_num,
"ps": page_size,
}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
class Music:
@@ -184,7 +185,7 @@ async def get_info(self):
"""
api = API["info"]["detail"]
params = {"music_id": self.__music_id}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_music_videos(self):
"""
@@ -195,4 +196,4 @@ async def get_music_videos(self):
"""
api = API["info"]["video_recommend_list"]
params = {"music_id": self.__music_id}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
diff --git a/bilibili_api/note.py b/bilibili_api/note.py
index 0ca644a6..5d36bd70 100644
--- a/bilibili_api/note.py
+++ b/bilibili_api/note.py
@@ -4,20 +4,21 @@
笔记相关
"""
-from html import unescape
+import re
import json
from enum import Enum
+from html import unescape
+from typing import List, Union, overload
+import yaml
+import httpx
from yarl import URL
+
from .utils.utils import get_api
-from .utils.credential import Credential
-from .utils.network_httpx import request, get_session
-from .exceptions import ArgsException, ApiException
from .utils.picture import Picture
-from typing import List, Union, overload
-import httpx
-import re
-import yaml
+from .utils.credential import Credential
+from .exceptions import ApiException, ArgsException
+from .utils.network_httpx import Api, get_session
API = get_api("note")
API_ARTICLE = get_api("article")
@@ -129,9 +130,7 @@ async def get_private_note_info(self) -> dict:
api = API["private"]["detail"]
# oid 为 0 时指 avid
params = {"oid": self.get_aid(), "note_id": self.get_note_id(), "oid_type": 0}
- resp = await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ resp = await Api(**api, credential=self.credential).update_params(**params).result
# 存入 self.__info 中以备后续调用
self.__info = resp
return resp
@@ -148,9 +147,7 @@ async def get_public_note_info(self) -> dict:
api = API["public"]["detail"]
params = {"cvid": self.get_cvid()}
- resp = await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ resp = await Api(**api, credential=self.credential).update_params(**params).result
# 存入 self.__info 中以备后续调用
self.__info = resp
return resp
@@ -228,7 +225,7 @@ async def set_like(self, status: bool = True) -> dict:
api = API_ARTICLE["operate"]["like"]
data = {"id": self.__cvid, "type": 1 if status else 2}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def set_favorite(self, status: bool = True) -> dict:
"""
@@ -253,7 +250,7 @@ async def set_favorite(self, status: bool = True) -> dict:
)
data = {"id": self.__cvid}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def add_coins(self) -> dict:
"""
@@ -271,7 +268,7 @@ async def add_coins(self) -> dict:
upid = (await self.get_info())["mid"]
api = API_ARTICLE["operate"]["coin"]
data = {"aid": self.__cvid, "multiply": 1, "upid": upid, "avtype": 2}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def fetch_content(self) -> None:
"""
diff --git a/bilibili_api/rank.py b/bilibili_api/rank.py
index 019660c9..4d7acb50 100644
--- a/bilibili_api/rank.py
+++ b/bilibili_api/rank.py
@@ -4,11 +4,12 @@
和哔哩哔哩视频排行榜相关的 API
"""
+from enum import Enum
from typing import Union
-from .utils.network_httpx import request
+
from .utils.utils import get_api
from .utils.credential import Credential
-from enum import Enum
+from .utils.network_httpx import Api
API = get_api("rank")
@@ -201,7 +202,7 @@ async def get_rank(
else:
raise Exception("Unknown RankType")
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_music_rank_list() -> dict:
@@ -213,7 +214,7 @@ async def get_music_rank_list() -> dict:
"""
api = API["info"]["music_weakly_series"]
params = {"list_type": 1}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_music_rank_weakly_detail(week: int = 1) -> dict:
@@ -228,7 +229,7 @@ async def get_music_rank_weakly_detail(week: int = 1) -> dict:
"""
api = API["info"]["music_weakly_details"]
params = {"list_id": week}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_music_rank_weakly_musics(week: int = 1) -> dict:
@@ -243,7 +244,7 @@ async def get_music_rank_weakly_musics(week: int = 1) -> dict:
"""
api = API["info"]["music_weakly_content"]
params = {"list_id": week}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_vip_rank(type_: VIPRankType = VIPRankType.VIP) -> dict:
@@ -258,7 +259,7 @@ async def get_vip_rank(type_: VIPRankType = VIPRankType.VIP) -> dict:
"""
api = API["info"]["VIP_rank"]
params = {"rank_id": type_.value}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_manga_rank(type_: MangeRankType = MangeRankType.NEW) -> dict:
@@ -271,7 +272,7 @@ async def get_manga_rank(type_: MangeRankType = MangeRankType.NEW) -> dict:
api = API["info"]["manga_rank"]
params = {"device": "pc", "platform": "web"}
data = {"id": type_.value}
- return await request("POST", api["url"], params=params, data=data, no_csrf=True)
+ return await Api(**api, no_csrf=True).update_data(**data).update_params(**params).result
async def get_live_hot_rank() -> dict:
@@ -282,7 +283,7 @@ async def get_live_hot_rank() -> dict:
dict: 调用 API 返回的结果
"""
api = API["info"]["live_hot_rank"]
- return await request("GET", api["url"])
+ return await Api(**api).result
async def get_live_sailing_rank() -> dict:
@@ -293,7 +294,7 @@ async def get_live_sailing_rank() -> dict:
dict: 调用 API 返回的结果
"""
api = API["info"]["live_sailing_rank"]
- return await request("GET", api["url"])
+ return await Api(**api).result
async def get_live_energy_user_rank(
@@ -314,7 +315,7 @@ async def get_live_energy_user_rank(
"""
api = API["info"]["live_energy_user_rank"]
params = {"date": date.value, "page": pn, "page_size": ps}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_live_rank(
@@ -341,7 +342,7 @@ async def get_live_rank(
"is_trend": 1,
"area_id": None,
}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_live_user_medal_rank(pn: int = 1, ps: int = 20) -> dict:
@@ -358,12 +359,12 @@ async def get_live_user_medal_rank(pn: int = 1, ps: int = 20) -> dict:
"""
api = API["info"]["live_medal_level_rank"]
params = {"page": pn, "page_size": ps}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def subscribe_music_rank(
status: bool = True, credential: Union[Credential, None] = None
-):
+) -> dict:
"""
设置关注全站音乐榜
@@ -377,10 +378,10 @@ async def subscribe_music_rank(
credential.raise_for_no_bili_jct()
api = API["operate"]["subscribe"]
data = {"list_id": 1, "state": (1 if status else 2)}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
-async def get_playlet_rank_phases():
+async def get_playlet_rank_phases() -> dict:
"""
获取全站短剧榜期数
@@ -388,10 +389,10 @@ async def get_playlet_rank_phases():
dict: 调用 API 返回的结果
"""
api = API["info"]["playlet_rank_phase"]
- return await request("POST", api["url"], data={}, json_body=True, no_csrf=True)
+ return await Api(**api, json_body=True, no_csrf=True).result
-async def get_playlet_rank_info(phase_id: int):
+async def get_playlet_rank_info(phase_id: int) -> dict:
"""
获取全站短剧榜
@@ -405,4 +406,4 @@ async def get_playlet_rank_info(phase_id: int):
"""
api = API["info"]["playlet_rank_info"]
data = {"phaseID": phase_id}
- return await request("POST", api["url"], data=data, json_body=True, no_csrf=True)
+ return await Api(**api, json_body=True, no_csrf=True).update_data(**data).result
diff --git a/bilibili_api/search.py b/bilibili_api/search.py
index a831ea6e..6ff8843f 100644
--- a/bilibili_api/search.py
+++ b/bilibili_api/search.py
@@ -3,12 +3,13 @@
搜索
"""
-from enum import Enum
-from typing import Callable, Union, List
import json
+from enum import Enum
+from typing import List, Union, Callable
+
from .utils.utils import get_api
-from .utils.network_httpx import request, get_session
from .video_zone import VideoZoneTypes
+from .utils.network_httpx import Api, get_session
API = get_api("search")
@@ -163,7 +164,7 @@ async def search(keyword: str, page: int = 1) -> dict:
"""
api = API["search"]["web_search"]
params = {"keyword": keyword, "page": page}
- return await request("GET", url=api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def search_by_type(
@@ -249,7 +250,7 @@ async def search_by_type(
if debug_param_func:
debug_param_func(params)
api = API["search"]["web_search_by_type"]
- return await request("GET", url=api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_default_search_keyword() -> dict:
@@ -260,7 +261,7 @@ async def get_default_search_keyword() -> dict:
dict: 调用 API 返回的结果
"""
api = API["search"]["default_search_keyword"]
- return await request("GET", api["url"])
+ return await Api(**api).result
async def get_hot_search_keywords() -> dict:
@@ -308,7 +309,7 @@ async def search_games(keyword: str) -> dict:
"""
api = API["search"]["game"]
params = {"keyword": keyword}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def search_manga(keyword: str, page_num: int = 1, page_size: int = 9):
@@ -327,7 +328,7 @@ async def search_manga(keyword: str, page_num: int = 1, page_size: int = 9):
"""
api = API["search"]["manga"]
data = {"key_word": keyword, "page_num": page_num, "page_size": page_size}
- return await request("POST", api["url"], data=data, no_csrf=True)
+ return await Api(**api, no_csrf=True).update_data(**data).result
async def search_cheese(
@@ -358,4 +359,4 @@ async def search_cheese(
"page_size": page_size,
"sort_type": order.value,
}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
diff --git a/bilibili_api/session.py b/bilibili_api/session.py
index aa241e7b..01255b1e 100644
--- a/bilibili_api/session.py
+++ b/bilibili_api/session.py
@@ -4,24 +4,24 @@
消息相关
"""
-import asyncio
-import datetime
import json
-import logging
import time
+import asyncio
+import logging
+import datetime
from typing import Union
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from bilibili_api.exceptions import ApiException
+from .video import Video
from .user import get_self_info
+from .utils.utils import get_api
+from .utils.picture import Picture
from .utils.AsyncEvent import AsyncEvent
from .utils.credential import Credential
-from .utils.network_httpx import request
-from .utils.picture import Picture
-from .utils.utils import get_api
-from .video import Video
+from .utils.network_httpx import Api
API = get_api("session")
@@ -53,7 +53,7 @@ async def fetch_session_msgs(
}
api = API["session"]["fetch"]
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def new_sessions(
@@ -75,7 +75,7 @@ async def new_sessions(
params = {"begin_ts": begin_ts, "build": 0, "mobi_app": "web"}
api = API["session"]["new"]
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_sessions(credential: Credential, session_type: int = 4) -> dict:
@@ -102,7 +102,7 @@ async def get_sessions(credential: Credential, session_type: int = 4) -> dict:
}
api = API["session"]["get"]
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_session_detail(
@@ -126,7 +126,7 @@ async def get_session_detail(
params = {"talker_id": talker_id, "session_type": session_type}
api = API["session"]["get_session_detail"]
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_likes(credential: Credential) -> dict:
@@ -140,7 +140,7 @@ async def get_likes(credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["session"]["likes"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_unread_messages(credential: Credential) -> dict:
@@ -154,7 +154,7 @@ async def get_unread_messages(credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["session"]["unread"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_replies(credential: Credential) -> dict:
@@ -168,7 +168,7 @@ async def get_replies(credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["session"]["replies"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_system_messages(credential: Credential) -> dict:
@@ -182,7 +182,7 @@ async def get_system_messages(credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["session"]["system_msg"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_session_settings(credential: Credential) -> dict:
@@ -196,7 +196,7 @@ async def get_session_settings(credential: Credential) -> dict:
dict: 调用 API 返回的结果
"""
api = API["session"]["session_settings"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def send_msg(
@@ -260,7 +260,7 @@ async def send_msg(
"build": 0,
"mobi_app": "web",
}
- return await request("POST", url=api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
class Event:
diff --git a/bilibili_api/settings.py b/bilibili_api/settings.py
index d5b193be..27f1ffa8 100644
--- a/bilibili_api/settings.py
+++ b/bilibili_api/settings.py
@@ -32,6 +32,11 @@
请求 Api 时是否打印 Api 信息
"""
+wbi_retry_times: int = 3
+"""
+WBI请求重试次数上限设置, 默认为3次
+"""
+
logger = logging.getLogger("request")
if not logger.handlers:
logger.setLevel(logging.INFO)
diff --git a/bilibili_api/tools/ivitools/__main__.py b/bilibili_api/tools/ivitools/__main__.py
index 2612a4c4..7ffc0646 100644
--- a/bilibili_api/tools/ivitools/__main__.py
+++ b/bilibili_api/tools/ivitools/__main__.py
@@ -10,15 +10,17 @@
__author__ = "Nemo2011 "
__license__ = "GPLv3+"
-from typing import List
import sys
-import warnings
import platform
+import warnings
+from typing import List
+
+from colorama import Fore
+
+from .touch import touch_ivi
from .scan import scan_ivi_file
from .extract import extract_ivi
-from .touch import touch_ivi
from .download import download_interactive_video
-from colorama import Fore
def run_args(command: str, args: List[str]):
diff --git a/bilibili_api/tools/ivitools/download.py b/bilibili_api/tools/ivitools/download.py
index 66b20af5..45a3e13b 100644
--- a/bilibili_api/tools/ivitools/download.py
+++ b/bilibili_api/tools/ivitools/download.py
@@ -5,10 +5,11 @@
"""
import os
-from colorama import Fore
-from bilibili_api import interactive_video, sync
import tqdm
import requests
+from colorama import Fore
+
+from bilibili_api import sync, interactive_video
async def download_file(url: str, out: str):
diff --git a/bilibili_api/tools/ivitools/extract.py b/bilibili_api/tools/ivitools/extract.py
index eaab9df1..b9e671ac 100644
--- a/bilibili_api/tools/ivitools/extract.py
+++ b/bilibili_api/tools/ivitools/extract.py
@@ -3,8 +3,8 @@
拆解 ivi 文件相关
"""
-import zipfile
import os
+import zipfile
def extract_ivi(path: str, dest: str):
diff --git a/bilibili_api/tools/ivitools/player.py b/bilibili_api/tools/ivitools/player.py
index 18afaad4..fc1bff5d 100644
--- a/bilibili_api/tools/ivitools/player.py
+++ b/bilibili_api/tools/ivitools/player.py
@@ -1,16 +1,16 @@
+import os
+import sys
import copy
import enum
import json
-import os
+import time
import random
-from random import random as rand
import shutil
-import sys
-import time
import zipfile
-from typing import List, Union, Tuple
+from random import random as rand
+from typing import List, Tuple, Union
-from PyQt6 import QtCore, QtGui, QtMultimedia, QtMultimediaWidgets, QtWidgets
+from PyQt6 import QtGui, QtCore, QtWidgets, QtMultimedia, QtMultimediaWidgets
class InteractiveVariable:
diff --git a/bilibili_api/tools/ivitools/scan.py b/bilibili_api/tools/ivitools/scan.py
index 48e018dd..23efe8a4 100644
--- a/bilibili_api/tools/ivitools/scan.py
+++ b/bilibili_api/tools/ivitools/scan.py
@@ -4,14 +4,16 @@
扫描 ivi 文件相关
"""
import os
-from .touch import touch_ivi
-from colorama import Fore, Cursor
-from colorama.ansi import clear_line
+import json
+import time
import zipfile
import tempfile
-import time
-import json
+
from tqdm import tqdm
+from colorama import Fore, Cursor
+from colorama.ansi import clear_line
+
+from .touch import touch_ivi
def scan_ivi_file(path: str):
diff --git a/bilibili_api/tools/ivitools/touch.py b/bilibili_api/tools/ivitools/touch.py
index 677a50c2..824411bf 100644
--- a/bilibili_api/tools/ivitools/touch.py
+++ b/bilibili_api/tools/ivitools/touch.py
@@ -3,8 +3,8 @@
获取 ivi 文件信息
"""
-import zipfile
import json
+import zipfile
def touch_ivi(path: str):
diff --git a/bilibili_api/tools/opendocs/__main__.py b/bilibili_api/tools/opendocs/__main__.py
index f1c5cc05..41a34def 100644
--- a/bilibili_api/tools/opendocs/__main__.py
+++ b/bilibili_api/tools/opendocs/__main__.py
@@ -1,10 +1,10 @@
# Bibibili API Documentions
-import webbrowser
import sys
+import webbrowser
try:
- from PyQt5 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets
+ from PyQt5 import QtGui, QtCore, QtWidgets, QtWebEngineWidgets
except:
PYQT5 = False
else:
diff --git a/bilibili_api/tools/parser/__init__.py b/bilibili_api/tools/parser/__init__.py
index 6af52dbf..1cf6e411 100644
--- a/bilibili_api/tools/parser/__init__.py
+++ b/bilibili_api/tools/parser/__init__.py
@@ -1,5 +1,5 @@
-from .app import bilibili_api_web, get_fastapi
from .parser import Parser
+from .app import get_fastapi, bilibili_api_web
__all__ = [
"bilibili_api_web",
diff --git a/bilibili_api/tools/parser/parser.py b/bilibili_api/tools/parser/parser.py
index 03cf295b..4f445d2e 100644
--- a/bilibili_api/tools/parser/parser.py
+++ b/bilibili_api/tools/parser/parser.py
@@ -1,9 +1,9 @@
import re
from enum import Enum
from inspect import isclass
-from inspect import iscoroutinefunction as isAsync
from inspect import isfunction as isFn
-from typing import Any, Dict, List, Optional, Tuple
+from inspect import iscoroutinefunction as isAsync
+from typing import Any, Dict, List, Tuple, Optional
import bilibili_api
diff --git a/bilibili_api/topic.py b/bilibili_api/topic.py
index 7805237e..9c7925ea 100644
--- a/bilibili_api/topic.py
+++ b/bilibili_api/topic.py
@@ -4,13 +4,14 @@
话题相关
"""
-from typing import Union, Optional
from enum import Enum
-from .utils.network_httpx import request
+from typing import Union, Optional
+
+from . import dynamic
+from .user import get_self_info
from .utils.utils import get_api
from .utils.credential import Credential
-from .user import get_self_info
-from . import dynamic
+from .utils.network_httpx import Api
API = get_api("topic")
@@ -41,7 +42,7 @@ async def get_hot_topics(numbers: int = 33) -> dict:
"""
api = API["info"]["dynamic_page_topics"]
params = {"page_size": numbers}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def search_topic(keyword: str, ps: int = 20, pn: int = 1) -> dict:
@@ -62,7 +63,7 @@ async def search_topic(keyword: str, ps: int = 20, pn: int = 1) -> dict:
"""
api = API["info"]["search"]
params = {"keywords": keyword, "page_size": ps, "page_num": pn}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
class Topic:
@@ -101,9 +102,7 @@ async def get_info(self) -> dict:
"""
api = API["info"]["info"]
params = {"topic_id": self.get_topic_id()}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_raw_cards(
self,
@@ -133,9 +132,7 @@ async def get_raw_cards(
"sort_by": sort_by.value,
"offset": offset,
}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_cards(
self,
@@ -190,7 +187,7 @@ async def like(self, status: bool = True) -> dict:
"business": "topic",
"up_mid": (await get_self_info(self.credential))["mid"],
}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def set_favorite(self, status: bool = True) -> dict:
"""
@@ -204,4 +201,4 @@ async def set_favorite(self, status: bool = True) -> dict:
"""
api = API["operate"]["add_favorite" if status else "cancel_favorite"]
data = {"topic_id": self.get_topic_id()}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
diff --git a/bilibili_api/user.py b/bilibili_api/user.py
index 6ad5eaa2..edfbf9a7 100644
--- a/bilibili_api/user.py
+++ b/bilibili_api/user.py
@@ -4,18 +4,17 @@
用户相关
"""
-from enum import Enum
import json
-from json.decoder import JSONDecodeError
import time
+from enum import Enum
+from typing import List, Union, Tuple
+from json.decoder import JSONDecodeError
-from .exceptions import ResponseCodeException
-
-from .utils.network_httpx import get_session, request
from .utils.utils import get_api, join
from .utils.credential import Credential
+from .exceptions import ResponseCodeException
+from .utils.network_httpx import get_session, Api
from .channel_series import ChannelOrder, ChannelSeries, ChannelSeriesType
-from typing import List, Union
API = get_api("user")
@@ -190,13 +189,12 @@ async def name2uid(names: Union[str, List[str]]):
Returns:
dict: 调用 API 返回的结果
"""
- api = API["info"]["name_to_uid"]
if isinstance(names, str):
n = names
else:
n = ",".join(names)
params = {"names": n}
- return await request("GET", api["url"], params=params)
+ return await Api(**API["info"]["name_to_uid"]).update_params(**params).result
class User:
@@ -224,14 +222,13 @@ async def get_user_info(self) -> dict:
Returns:
dict: 调用接口返回的内容。
+
+ [用户空间详细信息](https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/user/info.md#%E7%94%A8%E6%88%B7%E7%A9%BA%E9%97%B4%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF)
"""
- api = API["info"]["info"]
params = {
"mid": self.__uid,
}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**API["info"]["info"], credential=self.credential).update_params(**params).result
async def __get_self_info(self) -> dict:
"""
@@ -267,8 +264,8 @@ async def get_user_fav_tag(self, pn: int = 1, ps: int = 20) -> dict:
dict: 调用接口返回的内容。
"""
api = API["info"]["user_tag"]
- params = {"vmid": self.__uid, "pn": pn, "ps": ps}
- return await request("GET", url=api["url"], params=params, credential=self.credential)
+ params = {"vmid": self.__uid}#, "pn": pn, "ps": ps}
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_space_notice(self) -> dict:
"""
@@ -279,9 +276,7 @@ async def get_space_notice(self) -> dict:
"""
api = API["info"]["space_notice"]
params = {"mid": self.__uid}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def set_space_notice(self, content: str = "") -> dict:
"""
@@ -299,9 +294,7 @@ async def set_space_notice(self, content: str = "") -> dict:
api = API["operate"]["set_space_notice"]
data = {"notice": content}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_relation_info(self) -> dict:
"""
@@ -312,9 +305,7 @@ async def get_relation_info(self) -> dict:
"""
api = API["info"]["relation"]
params = {"vmid": self.__uid}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_up_stat(self) -> dict:
"""
@@ -327,9 +318,7 @@ async def get_up_stat(self) -> dict:
api = API["info"]["upstat"]
params = {"mid": self.__uid}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_top_videos(self) -> dict:
"""
@@ -340,9 +329,7 @@ async def get_top_videos(self) -> dict:
"""
api = API["info"]["user_top_videos"]
params = {"vmid": self.get_uid()}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_user_medal(self) -> dict:
"""
@@ -355,9 +342,7 @@ async def get_user_medal(self) -> dict:
# self.credential.raise_for_no_bili_jct()
api = API["info"]["user_medal"]
params = {"target_id": self.__uid}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_live_info(self) -> dict:
"""
@@ -368,9 +353,7 @@ async def get_live_info(self) -> dict:
"""
api = API["info"]["live"]
params = {"mid": self.__uid}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_videos(
self,
@@ -406,9 +389,7 @@ async def get_videos(
"keyword": keyword,
"order": order.value,
}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_audios(
self, order: AudioOrder = AudioOrder.PUBDATE, pn: int = 1, ps: int = 30
@@ -426,9 +407,7 @@ async def get_audios(
"""
api = API["info"]["audio"]
params = {"uid": self.__uid, "ps": ps, "pn": pn, "order": order.value}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_album(
self, biz: AlbumType = AlbumType.ALL, page_num: int = 1, page_size: int = 30
@@ -453,9 +432,7 @@ async def get_album(
"page_size": page_size,
"biz": biz.value,
}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_articles(
self, pn: int = 1, order: ArticleOrder = ArticleOrder.PUBDATE, ps: int = 30
@@ -475,9 +452,7 @@ async def get_articles(
"""
api = API["info"]["article"]
params = {"mid": self.__uid, "ps": ps, "pn": pn, "sort": order.value}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential, wbi=True).update_params(**params).result
async def get_article_list(
self, order: ArticleListOrder = ArticleListOrder.LATEST
@@ -493,9 +468,7 @@ async def get_article_list(
"""
api = API["info"]["article_lists"]
params = {"mid": self.__uid, "sort": order.value}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_dynamics(self, offset: int = 0, need_top: bool = False) -> dict:
"""
@@ -524,9 +497,7 @@ async def get_dynamics(self, offset: int = 0, need_top: bool = False) -> dict:
"offset_dynamic_id": offset,
"need_top": 1 if need_top else 0,
}
- data = await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ data = await Api(**api, credential=self.credential).update_params(**params).result
# card 字段自动转换成 JSON。
if "cards" in data:
for card in data["cards"]:
@@ -564,9 +535,7 @@ async def get_subscribed_bangumi(
"type": type_.value,
"follow_status": follow_status.value,
}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_followings(
self, pn: int = 1, ps: int = 100, attention: bool = False, order: OrderType = OrderType.desc
@@ -594,9 +563,7 @@ async def get_followings(
"order_type": "attention" if attention else "",
"order": order.value
}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_all_followings(self) -> dict:
"""
@@ -640,9 +607,7 @@ async def get_followers(
"pn": pn,
"order": "desc" if desc else "asc",
}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_self_same_followers(self, pn: int = 1, ps: int = 50) -> dict:
"""
@@ -659,9 +624,7 @@ async def get_self_same_followers(self, pn: int = 1, ps: int = 50) -> dict:
self.credential.raise_for_no_sessdata()
api = API["info"]["get_same_followings"]
params = {"vmid": self.get_uid(), "pn": pn, "ps": ps}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def top_followers(self, since=None) -> dict:
"""
@@ -676,9 +639,7 @@ async def top_followers(self, since=None) -> dict:
params = {}
if since:
params["t"] = int(since)
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_overview_stat(self) -> dict:
"""
@@ -689,9 +650,7 @@ async def get_overview_stat(self) -> dict:
"""
api = API["info"]["overview"]
params = {"mid": self.__uid, "jsonp": "jsonp"}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
# 操作用户
@@ -711,9 +670,7 @@ async def modify_relation(self, relation: RelationType) -> dict:
api = API["operate"]["modify"]
data = {"fid": self.__uid, "act": relation.value, "re_src": 11}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
# 有关合集与列表
@@ -738,16 +695,14 @@ async def get_channel_videos_series(
dict: 调用接口返回的内容
"""
api = API["info"]["channel_video_series"]
- param = {
+ params = {
"mid": self.__uid,
"series_id": sid,
"pn": pn,
"ps": ps,
"sort": "asc" if ChannelOrder.CHANGE else "desc",
}
- return await request(
- "GET", url=api["url"], params=param, credential=self.credential
- )
+ return await Api(**api, wbi=True, credential=self.credential).update_params(**params).result
async def get_channel_videos_season(
self,
@@ -772,16 +727,14 @@ async def get_channel_videos_season(
dict: 调用接口返回的内容
"""
api = API["info"]["channel_video_season"]
- param = {
+ params = {
"mid": self.__uid,
"season_id": sid,
"sort_reverse": sort.value,
"page_num": pn,
"page_size": ps,
}
- return await request(
- "GET", url=api["url"], params=param, credential=self.credential
- )
+ return await Api(**api, wbi=True,credential=self.credential).update_params(**params).result
async def get_channel_list(self) -> dict:
"""
@@ -795,18 +748,14 @@ async def get_channel_list(self) -> dict:
dict: 调用接口返回的结果
"""
api = API["info"]["channel_list"]
- param = {"mid": self.__uid, "page_num": 1, "page_size": 1}
- res = await request(
- "GET", url=api["url"], params=param, credential=self.credential
- )
+ params = {"mid": self.__uid, "page_num": 1, "page_size": 1}
+ res = await Api(**api, wbi=True, credential=self.credential).update_params(**params).result
items = res["items_lists"]["page"]["total"]
time.sleep(0.5)
if items == 0:
items = 1
- param["page_size"] = items
- return await request(
- "GET", url=api["url"], params=param, credential=self.credential
- )
+ params["page_size"] = items
+ return await Api(**api, wbi=True,credential=self.credential).update_params(**params).result
async def get_channels(self) -> List["ChannelSeries"]:
"""
@@ -852,7 +801,7 @@ async def get_cheese(self) -> dict:
"""
api = API["info"]["pugv"]
params = {"mid": self.__uid}
- return await request("GET", api["url"], params=params)
+ return await Api(**api, wbi=True, credential=self.credential).update_params(**params).result
async def get_reservation(self) -> dict:
"""
@@ -863,9 +812,7 @@ async def get_reservation(self) -> dict:
"""
api = API["info"]["reservation"]
params = {"vmid": self.get_uid()}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_elec_user_monthly(self) -> dict:
"""
@@ -876,9 +823,7 @@ async def get_elec_user_monthly(self) -> dict:
"""
api = API["info"]["elec_user_monthly"]
params = {"up_mid": self.get_uid()}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_self_info(credential: Credential) -> dict:
@@ -891,7 +836,7 @@ async def get_self_info(credential: Credential) -> dict:
api = API["info"]["my_info"]
credential.raise_for_no_sessdata()
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def edit_self_info(
@@ -918,7 +863,7 @@ async def edit_self_info(
api = API["info"]["edit_my_info"]
data = {"birthday": birthday, "sex": sex, "uname": uname, "usersign": usersign}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def create_subscribe_group(name: str, credential: Credential) -> dict:
@@ -939,7 +884,7 @@ async def create_subscribe_group(name: str, credential: Credential) -> dict:
api = API["operate"]["create_subscribe_group"]
data = {"tag": name}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def delete_subscribe_group(group_id: int, credential: Credential) -> dict:
@@ -960,7 +905,7 @@ async def delete_subscribe_group(group_id: int, credential: Credential) -> dict:
api = API["operate"]["del_subscribe_group"]
data = {"tagid": group_id}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def rename_subscribe_group(
@@ -985,7 +930,7 @@ async def rename_subscribe_group(
api = API["operate"]["rename_subscribe_group"]
data = {"tagid": group_id, "name": new_name}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def set_subscribe_group(
@@ -1010,7 +955,7 @@ async def set_subscribe_group(
api = API["operate"]["set_user_subscribe_group"]
data = {"fids": join(",", uids), "tagids": join(",", group_ids)}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
async def get_self_history(
@@ -1039,7 +984,7 @@ async def get_self_history(
api = API["info"]["history"]
params = {"pn": page_num, "ps": per_page_item}
- return await request("GET", url=api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_history_new(
@@ -1080,7 +1025,7 @@ async def get_self_history_new(
"max": max,
"business": business if business is None else business.value,
}
- return await request("GET", url=api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_coins(credential: Credential) -> int:
@@ -1095,7 +1040,7 @@ async def get_self_coins(credential: Credential) -> int:
credential.raise_for_no_sessdata()
credential.raise_for_no_dedeuserid()
api = API["info"]["get_coins"]
- return (await request("GET", url=api["url"], credential=credential))["money"]
+ return (await Api(**api, credential=credential).result)["money"]
async def get_self_special_followings(
@@ -1114,7 +1059,7 @@ async def get_self_special_followings(
credential.raise_for_no_sessdata()
api = API["info"]["get_special_followings"]
params = {"pn": pn, "ps": ps}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_whisper_followings(
@@ -1133,7 +1078,7 @@ async def get_self_whisper_followings(
credential.raise_for_no_sessdata()
api = API["info"]["get_whisper_followings"]
params = {"pn": pn, "ps": ps}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_friends(credential: Credential) -> dict:
@@ -1145,7 +1090,7 @@ async def get_self_friends(credential: Credential) -> dict:
"""
credential.raise_for_no_sessdata()
api = API["info"]["get_friends"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def get_self_black_list(
@@ -1164,7 +1109,7 @@ async def get_self_black_list(
credential.raise_for_no_sessdata()
api = API["info"]["get_black_list"]
params = {"pn": pn, "ps": ps}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_toview_list(credential: Credential):
@@ -1179,7 +1124,7 @@ async def get_toview_list(credential: Credential):
"""
api = get_api("toview")["info"]["list"]
credential.raise_for_no_sessdata()
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def clear_toview_list(credential: Credential):
@@ -1195,7 +1140,7 @@ async def clear_toview_list(credential: Credential):
api = get_api("toview")["operate"]["clear"]
credential.raise_for_no_sessdata()
credential.raise_for_no_bili_jct()
- return await request("POST", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
async def delete_viewed_videos_from_toview(credential: Credential):
@@ -1212,10 +1157,10 @@ async def delete_viewed_videos_from_toview(credential: Credential):
credential.raise_for_no_sessdata()
credential.raise_for_no_bili_jct()
datas = {"viewed": "true"}
- return await request("POST", api["url"], credential=credential, data=datas)
+ return await Api(**api, credential=credential).update_data(**datas).result
-async def check_nickname(nick_name: str):
+async def check_nickname(nick_name: str) -> Tuple[bool, str]:
"""
检验昵称是否可用
@@ -1228,7 +1173,7 @@ async def check_nickname(nick_name: str):
api = get_api("common")["nickname"]["check_nickname"]
params = {"nickName": nick_name}
try:
- resp = await request("GET", api["url"], params=params)
+ resp = await Api(**api).update_params(**params).result
except ResponseCodeException as e:
return False, str(e)
else:
@@ -1250,7 +1195,7 @@ async def get_self_events(ts: int = 0, credential: Union[Credential, None] = Non
credential = credential if credential else Credential()
api = API["info"]["events"]
params = {"ts": ts}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_notes_info(
@@ -1277,7 +1222,7 @@ async def get_self_notes_info(
api = API["info"]["all_notes"]
params = {"pn": page_num, "ps": page_size}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_public_notes_info(
@@ -1304,7 +1249,7 @@ async def get_self_public_notes_info(
api = API["info"]["public_notes"]
params = {"pn": page_num, "ps": page_size}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
async def get_self_jury_info(credential: Credential) -> dict:
@@ -1313,4 +1258,4 @@ async def get_self_jury_info(credential: Credential) -> dict:
"""
credential.raise_for_no_sessdata()
api = API["info"]["jury"]
- return await request("GET", api["url"], credential=credential)
+ return await Api(**api, credential=credential).result
diff --git a/bilibili_api/utils/AsyncEvent.py b/bilibili_api/utils/AsyncEvent.py
index 2ef285fd..aa362112 100644
--- a/bilibili_api/utils/AsyncEvent.py
+++ b/bilibili_api/utils/AsyncEvent.py
@@ -4,8 +4,8 @@
发布-订阅模式异步事件类支持。
"""
-from typing import Coroutine, Callable
import asyncio
+from typing import Callable, Coroutine
class AsyncEvent:
diff --git a/bilibili_api/utils/captcha.py b/bilibili_api/utils/captcha.py
index 53f23098..126c7d0b 100644
--- a/bilibili_api/utils/captcha.py
+++ b/bilibili_api/utils/captcha.py
@@ -3,12 +3,14 @@
人机测试
"""
+import os
import copy
import json
-import os
import time
import webbrowser
+
import requests
+
from .utils import get_api
validate = None
@@ -128,10 +130,10 @@ def _start_server(urlhandler, hostname, port):
>>> print(serverthread.error)
None
"""
- import http.server
- import email.message
import select
import threading
+ import http.server
+ import email.message
class DocHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
diff --git a/bilibili_api/utils/credential.py b/bilibili_api/utils/credential.py
index cb31a2bc..eca74aed 100644
--- a/bilibili_api/utils/credential.py
+++ b/bilibili_api/utils/credential.py
@@ -4,14 +4,15 @@
凭据类,用于各种请求操作的验证。
"""
+import uuid
+from typing import Union
+
from ..exceptions import (
+ CredentialNoBuvid3Exception,
CredentialNoBiliJctException,
CredentialNoSessdataException,
- CredentialNoBuvid3Exception,
CredentialNoDedeUserIDException,
)
-import uuid
-from typing import Union
class Credential:
diff --git a/bilibili_api/utils/danmaku.py b/bilibili_api/utils/danmaku.py
index 405cc6f6..6b972b59 100644
--- a/bilibili_api/utils/danmaku.py
+++ b/bilibili_api/utils/danmaku.py
@@ -6,9 +6,10 @@
import time
from enum import Enum
-from .utils import crack_uid as _crack_uid
from typing import Union
+from .utils import crack_uid as _crack_uid
+
class DmFontSize(Enum):
"""
diff --git a/bilibili_api/utils/danmaku2ass.py b/bilibili_api/utils/danmaku2ass.py
index 293ae755..1043add2 100644
--- a/bilibili_api/utils/danmaku2ass.py
+++ b/bilibili_api/utils/danmaku2ass.py
@@ -14,21 +14,20 @@
# pylint: skip-file
# type: ignore
-import argparse
-import calendar
-import gettext
import io
-import json
-import logging
-import math
import os
-import random
import re
import sys
+import json
+import math
import time
+import random
+import gettext
+import logging
+import argparse
+import calendar
import xml.dom.minidom
-
if sys.version_info < (3,):
raise RuntimeError("at least Python 3.0 is required")
diff --git a/bilibili_api/utils/initial_state.py b/bilibili_api/utils/initial_state.py
index d381a2f5..755667fe 100644
--- a/bilibili_api/utils/initial_state.py
+++ b/bilibili_api/utils/initial_state.py
@@ -3,14 +3,16 @@
用于获取页码的初始化信息
"""
-from .credential import Credential
-from ..exceptions import *
-from .network_httpx import get_session
-from .short import get_real_url
-import httpx
import re
import json
+import httpx
+
+from ..exceptions import *
+from .short import get_real_url
+from .credential import Credential
+from .network_httpx import get_session
+
async def get_initial_state(url: str, credential: Credential = Credential()) -> dict:
'''
diff --git a/bilibili_api/utils/json2srt.py b/bilibili_api/utils/json2srt.py
index e33b08c3..94b1f9ba 100644
--- a/bilibili_api/utils/json2srt.py
+++ b/bilibili_api/utils/json2srt.py
@@ -2,9 +2,9 @@
# author: 皓空Fly
# 此文件采用 CC 4.0 BY-SA 协议开源。
+import os
import json
import math
-import os
def json2srt(doc: str, out: str):
diff --git a/bilibili_api/utils/network.py b/bilibili_api/utils/network.py
index 296e1c40..542a181b 100644
--- a/bilibili_api/utils/network.py
+++ b/bilibili_api/utils/network.py
@@ -4,17 +4,18 @@
与网络请求相关的模块。能对会话进行管理(复用 TCP 连接)。
"""
+import re
+import json
+import atexit
+import asyncio
from typing import Any, Union
from urllib.parse import quote
+
import aiohttp
-import json
-import re
-import asyncio
-import atexit
-from ..exceptions import ResponseCodeException, ResponseException, NetworkException
from .. import settings
from .credential import Credential
+from ..exceptions import NetworkException, ResponseException, ResponseCodeException
__session_pool = {}
diff --git a/bilibili_api/utils/network_httpx.py b/bilibili_api/utils/network_httpx.py
index 56f92860..9b58ac2b 100644
--- a/bilibili_api/utils/network_httpx.py
+++ b/bilibili_api/utils/network_httpx.py
@@ -4,27 +4,27 @@
复写了 .utils.network,使用 httpx
"""
-import asyncio
-import atexit
-import hashlib
-import json
import re
-import threading
+import json
import time
import uuid
-from dataclasses import dataclass, field
+import atexit
+import asyncio
+import hashlib
+import threading
from functools import reduce
-from inspect import iscoroutinefunction as isAsync
-from typing import Any, Coroutine, Dict, Union
from urllib.parse import urlencode
+from dataclasses import field, dataclass
+from typing import Any, Dict, Union, Coroutine
+from inspect import iscoroutinefunction as isAsync
import httpx
-from .. import settings
-from ..exceptions import ApiException, ResponseCodeException
-from .credential import Credential
from .sync import sync
+from .. import settings
from .utils import get_api
+from .credential import Credential
+from ..exceptions import ApiException, ResponseCodeException
__session_pool = {}
last_proxy = ""
@@ -120,14 +120,18 @@ class Api:
ignore_code: bool = False
data: dict = field(default_factory=dict)
params: dict = field(default_factory=dict)
+ files: dict = field(default_factory=dict)
+ headers: dict = field(default_factory=dict)
credential: Credential = field(default_factory=Credential)
- def __post_init__(self):
+ def __post_init__(self) -> None:
self.method = self.method.upper()
self.original_data = self.data.copy()
self.original_params = self.params.copy()
self.data = {k: "" for k in self.data.keys()}
self.params = {k: "" for k in self.params.keys()}
+ self.files = {k: "" for k in self.files.keys()}
+ self.headers = {k: "" for k in self.headers.keys()}
if self.credential is None:
self.credential = Credential()
self.__result = None
@@ -183,7 +187,7 @@ def thread_result(self) -> Union[None, dict]:
time.sleep(0.0167)
return self.__result
- def update_data(self, **kwargs):
+ def update_data(self, **kwargs) -> "Api":
"""
毫无亮点的更新 data
"""
@@ -191,7 +195,7 @@ def update_data(self, **kwargs):
self.__result = None
return self
- def update_params(self, **kwargs):
+ def update_params(self, **kwargs) -> "Api":
"""
毫无亮点的更新 params
"""
@@ -199,16 +203,32 @@ def update_params(self, **kwargs):
self.__result = None
return self
- def update(self, **kwargs):
+ def update_files(self, **kwargs) -> "Api":
"""
- 毫无亮点的自动选择更新
+ 毫无亮点的更新 files
+ """
+ self.files.update(kwargs)
+ self.__result = None
+ return self
+
+ def update_headers(self, **kwargs) -> "Api":
+ """
+ 毫无亮点的更新 headers
+ """
+ self.headers.update(kwargs)
+ self.__result = None
+ return self
+
+ def update(self, **kwargs) -> "Api":
+ """
+ 毫无亮点的自动选择更新,不包括 files, headers
"""
if self.method == "GET":
return self.update_params(**kwargs)
else:
return self.update_data(**kwargs)
- @retry(times=3)
+ @retry(times=settings.wbi_retry_times)
async def request(self, **kwargs) -> Any:
"""
向接口发送请求。
@@ -239,7 +259,9 @@ async def request(self, **kwargs) -> Any:
self.data["csrf_token"] = self.credential.bili_jct
cookies = self.credential.get_cookies()
- cookies["buvid3"] = str(uuid.uuid1())
+ # cookies["buvid3"] = str(uuid.uuid1())
+ # 直接定值,随机字符串部分接口 -412
+ cookies["buvid3"] = "931081E9D-AE3E-9F5F-9109F-6E1521591018836102infoc"
cookies["Domain"] = ".bilibili.com"
config = {
@@ -247,8 +269,9 @@ async def request(self, **kwargs) -> Any:
"method": self.method,
"data": self.data,
"params": self.params,
+ "files": self.files,
"cookies": cookies,
- "headers": HEADERS.copy(),
+ "headers": HEADERS.copy() if len(self.headers) == 0 else self.headers,
}
config.update(kwargs)
@@ -372,6 +395,7 @@ def enc_wbi(params: dict, mixin_key: str):
params["w_rid"] = hashlib.md5(
(Ae + mixin_key).encode(encoding="utf-8")
).hexdigest()
+ params["web_location"] = 1550101
@atexit.register
@@ -464,7 +488,9 @@ async def request_old(
params["callback"] = "callback"
cookies = credential.get_cookies()
- cookies["buvid3"] = str(uuid.uuid1())
+ # cookies["buvid3"] = str(uuid.uuid1())
+ # 直接定值,随机字符串部分接口 -412
+ cookies["buvid3"] = "931081E9D-AE3E-9F5F-9109F-6E1521591018836102infoc"
cookies["Domain"] = ".bilibili.com"
config = {
@@ -579,51 +605,7 @@ async def wrapper(*args, **kwargs):
return wrapper
-def retry(times: int = 3):
- """
- 重试装饰器
-
- Args:
- times (int): 最大重试次数 默认 3 次 负数则一直重试直到成功
-
- Returns:
- Any: 原函数调用结果
- """
- def wrapper(func: Coroutine):
- async def inner(*args, **kwargs):
- # 这里必须新建一个变量用来计数!!不能直接对 times 操作!!!
- nonlocal times
- loop = times
- while loop != 0:
- if loop != times and settings.request_log:
- settings.logger.info(f"第 {times - loop} 次重试")
- loop -= 1
- try:
- return await func(*args, **kwargs)
- except json.decoder.JSONDecodeError:
- # json 解析错误 说明数据获取有误 再给次机会
- continue
- except ResponseCodeException as e:
- # -403 时尝试重新获取 wbi_mixin_key 可能过期了
- if e.code == -403:
- global wbi_mixin_key
- wbi_mixin_key = ""
- continue
- # 不是 -403 错误直接报错
- raise
- raise ApiException("重试达到最大次数")
- return inner
-
- if isAsync(times):
- # 防呆不防傻 防止有人 @retry() 不打括号
- func = times
- times = 3
- return wrapper(func)
-
- return wrapper
-
-
-@retry()
+@retry(times = settings.wbi_retry_times)
@rollback
async def request(api: Api, url: str = "", params: dict = None, **kwargs) -> Any:
"""
diff --git a/bilibili_api/utils/parse_link.py b/bilibili_api/utils/parse_link.py
index a0ee901f..973da329 100644
--- a/bilibili_api/utils/parse_link.py
+++ b/bilibili_api/utils/parse_link.py
@@ -4,35 +4,35 @@
链接资源解析。
"""
-import json
import re
+import json
from enum import Enum
-from typing import Literal, Tuple, Union
+from typing import Tuple, Union, Literal
import httpx
from yarl import URL
-from ..article import Article, ArticleList
-from ..audio import Audio, AudioList
-from ..bangumi import Bangumi, Episode
-from ..black_room import BlackRoom
-from ..cheese import CheeseVideo, CheeseList
-from ..dynamic import Dynamic
+from ..game import Game
+from ..album import Album
+from ..manga import Manga
+from ..topic import Topic
+from ..video import Video
from ..exceptions import *
-from ..favorite_list import FavoriteList, FavoriteListType
-from ..interactive_video import InteractiveVideo
+from .utils import get_api
from ..live import LiveRoom
-from ..user import ChannelSeries, ChannelSeriesType, User, get_self_info
+from ..dynamic import Dynamic
+from .short import get_real_url
from ..note import Note, NoteType
-from ..video import Video
-from ..game import Game
+from ..black_room import BlackRoom
from .credential import Credential
-from .short import get_real_url
-from .utils import get_api
-from ..topic import Topic
-from ..manga import Manga
-from ..album import Album
+from ..audio import Audio, AudioList
+from ..bangumi import Bangumi, Episode
+from ..article import Article, ArticleList
+from ..cheese import CheeseList, CheeseVideo
from .initial_state import get_initial_state
+from ..interactive_video import InteractiveVideo
+from ..favorite_list import FavoriteList, FavoriteListType
+from ..user import User, ChannelSeries, ChannelSeriesType, get_self_info
class ResourceType(Enum):
diff --git a/bilibili_api/utils/picture.py b/bilibili_api/utils/picture.py
index 83fe17ea..d3e9e951 100644
--- a/bilibili_api/utils/picture.py
+++ b/bilibili_api/utils/picture.py
@@ -1,12 +1,14 @@
-from dataclasses import dataclass
-from PIL import Image
-from typing import Any
-import tempfile
import os
+import tempfile
+from typing import Any
+from dataclasses import dataclass
+
import httpx
from yarl import URL
-from .credential import Credential
+from PIL import Image
+
from .utils import get_api
+from .credential import Credential
@dataclass
diff --git a/bilibili_api/utils/safecenter_captcha.py b/bilibili_api/utils/safecenter_captcha.py
index 0c9afe1c..d8fbf6d6 100644
--- a/bilibili_api/utils/safecenter_captcha.py
+++ b/bilibili_api/utils/safecenter_captcha.py
@@ -3,11 +3,13 @@
验证人机测试
"""
+import os
import copy
import json
-import os
import time
+
import requests
+
from .utils import get_api
validate = None
@@ -133,10 +135,10 @@ def _start_server(urlhandler, hostname, port):
>>> print(serverthread.error)
None
"""
- import http.server
- import email.message
import select
import threading
+ import http.server
+ import email.message
class DocHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
diff --git a/bilibili_api/utils/short.py b/bilibili_api/utils/short.py
index 6032419b..8745d9ef 100644
--- a/bilibili_api/utils/short.py
+++ b/bilibili_api/utils/short.py
@@ -3,11 +3,13 @@
一个很简单的处理短链接的模块,主要是读取跳转链接。
"""
+from typing import Optional
+
import httpx
-from .network_httpx import get_session
-from .credential import Credential
+
from .. import settings
-from typing import Optional
+from .credential import Credential
+from .network_httpx import get_session
async def get_real_url(short_url: str, credential: Optional[Credential] = None) -> str:
diff --git a/bilibili_api/utils/srt2ass.py b/bilibili_api/utils/srt2ass.py
index 1ff3492f..4752a8dd 100644
--- a/bilibili_api/utils/srt2ass.py
+++ b/bilibili_api/utils/srt2ass.py
@@ -1,13 +1,12 @@
# type: ignore
# pylint: skip-file
-import sys
+import io
import os
import re
-import io
+import sys
import getopt
-
DEBUG = False
diff --git a/bilibili_api/utils/sync.py b/bilibili_api/utils/sync.py
index 8bfff3d3..03622213 100644
--- a/bilibili_api/utils/sync.py
+++ b/bilibili_api/utils/sync.py
@@ -5,7 +5,7 @@
"""
import asyncio
-from typing import Any, Coroutine, TypeVar
+from typing import Any, TypeVar, Coroutine
T = TypeVar("T")
diff --git a/bilibili_api/utils/utils.py b/bilibili_api/utils/utils.py
index 4d6cc2f8..b2e51adc 100644
--- a/bilibili_api/utils/utils.py
+++ b/bilibili_api/utils/utils.py
@@ -4,8 +4,8 @@
通用工具库。
"""
-import json
import os
+import json
from typing import List, TypeVar
diff --git a/bilibili_api/video.py b/bilibili_api/video.py
index 0bef4eab..e0a64350 100644
--- a/bilibili_api/video.py
+++ b/bilibili_api/video.py
@@ -6,33 +6,36 @@
注意,同时存在 page_index 和 cid 的参数,两者至少提供一个。
"""
-from enum import Enum
import re
-import datetime
-import asyncio
-import logging
import json
import struct
-import aiohttp
-import httpx
-from typing import Any, List, Union
+import asyncio
+import logging
+import datetime
+from enum import Enum
+from inspect import isfunction
from functools import cmp_to_key
+from dataclasses import dataclass
+from typing import Any, List, Union
-from .exceptions import ResponseException
-from .exceptions import NetworkException
-from .exceptions import ArgsException, DanmakuClosedException
+import httpx
+import aiohttp
-from .utils.credential import Credential
-from .utils.aid_bvid_transformer import aid2bvid, bvid2aid
+from . import settings
+from .utils.aid_bvid_transformer import bvid2aid, aid2bvid
from .utils.utils import get_api
-from .utils.network_httpx import request, get_session
-from .utils.network import get_session as get_session_aiohttp
-from .utils.danmaku import Danmaku, SpecialDanmaku
-from .utils.BytesReader import BytesReader
from .utils.AsyncEvent import AsyncEvent
-from dataclasses import dataclass
-from . import settings
-from inspect import isfunction
+from .utils.credential import Credential
+from .utils.BytesReader import BytesReader
+from .utils.danmaku import Danmaku, SpecialDanmaku
+from .utils.network_httpx import Api, get_session
+from .utils.network import get_session as get_session_aiohttp
+from .exceptions import (
+ ArgsException,
+ NetworkException,
+ ResponseException,
+ DanmakuClosedException,
+)
API = get_api("video")
@@ -46,7 +49,7 @@ async def get_cid_info(cid: int):
"""
api = API["info"]["cid_info"]
params = {"cid": cid}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
class DanmakuOperatorType(Enum):
@@ -212,9 +215,7 @@ async def get_info(self) -> dict:
"""
api = API["info"]["detail"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
- resp = await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ resp = await Api(**api, credential=self.credential).update_params(**params).result
# 存入 self.__info 中以备后续调用
self.__info = resp
return resp
@@ -237,9 +238,9 @@ async def get_stat(self) -> dict:
Returns:
dict: 调用 API 返回的结果。
"""
- url = API["info"]["stat"]["url"]
+ api = API["info"]["stat"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_tags(
self, page_index: Union[int, None] = 0, cid: Union[int, None] = None
@@ -260,9 +261,9 @@ async def get_tags(
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await self.get_cid(page_index=page_index)
- url = API["info"]["tags"]["url"]
+ api = API["info"]["tags"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid(), "cid": cid}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_chargers(self) -> dict:
"""
@@ -273,9 +274,9 @@ async def get_chargers(self) -> dict:
"""
info = await self.__get_info_cached()
mid = info["owner"]["mid"]
- url = API["info"]["chargers"]["url"]
+ api = API["info"]["chargers"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid(), "mid": mid}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_pages(self) -> List[dict]:
"""
@@ -284,9 +285,9 @@ async def get_pages(self) -> List[dict]:
Returns:
dict: 调用 API 返回的结果。
"""
- url = API["info"]["pages"]["url"]
+ api = API["info"]["pages"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid()}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def __get_page_id_by_index(self, page_index: int) -> int:
"""
@@ -332,15 +333,15 @@ async def get_video_snapshot(
"""
params: dict[str, Any] = {"aid": self.get_aid()}
if pvideo:
- url = API["info"]["video_snapshot_pvideo"]["url"]
+ api = API["info"]["video_snapshot_pvideo"]
else:
params["bvid"] = self.get_bvid()
if json_index:
params["index"] = 1
if cid:
params["cid"] = cid
- url = API["info"]["video_snapshot"]["url"]
- return await request("GET", url, params=params)
+ api = API["info"]["video_snapshot"]
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_cid(self, page_index: int) -> int:
"""
@@ -383,7 +384,7 @@ async def get_download_url(
cid = await self.__get_page_id_by_index(page_index)
- url = API["info"]["playurl"]["url"]
+ api = API["info"]["playurl"]
if html5:
params = {
"avid": self.get_aid(),
@@ -403,7 +404,7 @@ async def get_download_url(
"fnval": 4048,
"fourk": 1,
}
- result = await request("GET", url, params=params, credential=self.credential)
+ result = await Api(**api, credential=self.credential).update_params(**params).result
result.update({"is_html5": True} if html5 else {})
return result
@@ -414,9 +415,9 @@ async def get_related(self) -> dict:
Returns:
dict: 调用 API 返回的结果。
"""
- url = API["info"]["related"]["url"]
+ api = API["info"]["related"]
params = {"aid": self.get_aid(), "bvid": self.get_bvid()}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def has_liked(self) -> bool:
"""
@@ -427,9 +428,9 @@ async def has_liked(self) -> bool:
"""
self.credential.raise_for_no_sessdata()
- url = API["info"]["has_liked"]["url"]
+ api = API["info"]["has_liked"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
- return await request("GET", url, params=params, credential=self.credential) == 1
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_pay_coins(self) -> int:
"""
@@ -440,9 +441,9 @@ async def get_pay_coins(self) -> int:
"""
self.credential.raise_for_no_sessdata()
- url = API["info"]["get_pay_coins"]["url"]
+ api = API["info"]["get_pay_coins"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
- return (await request("GET", url, params=params, credential=self.credential))[
+ return (await Api(**api, credential=self.credential).update_params(**params).result)[
"multiply"
]
@@ -455,9 +456,9 @@ async def has_favoured(self) -> bool:
"""
self.credential.raise_for_no_sessdata()
- url = API["info"]["has_favoured"]["url"]
+ api = API["info"]["has_favoured"]
params = {"bvid": self.get_bvid(), "aid": self.get_aid()}
- return (await request("GET", url, params=params, credential=self.credential))[
+ return (await Api(**api, credential=self.credential).update_params(**params).result)[
"favoured"
]
@@ -468,9 +469,9 @@ async def is_forbid_note(self) -> bool:
Returns:
bool: 是否禁止笔记。
"""
- url = API["info"]["is_forbid"]["url"]
+ api = API["info"]["is_forbid"]
params = {"aid": self.get_aid()}
- return (await request("GET", url, params=params, credential=self.credential))[
+ return (await Api(**api, credential=self.credential).update_params(**params).result)[
"forbid_note_entrance"
]
@@ -483,9 +484,9 @@ async def get_private_notes_list(self) -> list:
"""
self.credential.raise_for_no_sessdata()
- url = API["info"]["private_notes"]["url"]
+ api = API["info"]["private_notes"]
params = {"oid": self.get_aid(), "oid_type": 0}
- return (await request("GET", url, params=params, credential=self.credential))[
+ return (await Api(**api, credential=self.credential).update_params(**params).result)[
"noteIds"
]
@@ -502,9 +503,9 @@ async def get_public_notes_list(self, pn: int, ps: int) -> dict:
dict: 调用 API 返回的结果。
"""
- url = API["info"]["public_notes"]["url"]
+ api = API["info"]["public_notes"]
params = {"oid": self.get_aid(), "oid_type": 0, "pn": pn, "ps": ps}
- return await request("GET", url, params=params, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def get_danmaku_view(
self, page_index: Union[int, None] = None, cid: Union[int, None] = None
@@ -961,9 +962,7 @@ async def get_history_danmaku_index(
api = API["danmaku"]["get_history_danmaku_index"]
params = {"oid": cid, "month": date.strftime("%Y-%m"), "type": 1}
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def has_liked_danmakus(
self,
@@ -997,9 +996,7 @@ async def has_liked_danmakus(
api = API["danmaku"]["has_liked_danmaku"]
params = {"oid": cid, "ids": ",".join(ids)} # type: ignore
- return await request(
- "GET", url=api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def send_danmaku(
self,
@@ -1052,9 +1049,7 @@ async def send_danmaku(
"mode": danmaku.mode,
"plat": 1,
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_danmaku_xml(
self, page_index: Union[int, None] = None, cid: Union[int, None] = None
@@ -1126,9 +1121,7 @@ async def like_danmaku(
"op": 1 if status else 2,
"platform": "web_player",
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def operate_danmaku(
self,
@@ -1177,9 +1170,7 @@ async def operate_danmaku(
"state": type_.value,
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def like(self, status: bool = True) -> dict:
"""
@@ -1196,9 +1187,7 @@ async def like(self, status: bool = True) -> dict:
api = API["operate"]["like"]
data = {"aid": self.get_aid(), "like": 1 if status else 2}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def pay_coin(self, num: int = 1, like: bool = False) -> dict:
"""
@@ -1225,9 +1214,7 @@ async def pay_coin(self, num: int = 1, like: bool = False) -> dict:
"multiply": num,
"like": 1 if like else 0,
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def triple(self) -> dict:
"""
@@ -1238,7 +1225,7 @@ async def triple(self) -> dict:
"""
api = API["operate"]["yjsl"]
data = {"bvid": self.get_bvid(), "aid": self.get_aid()}
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def add_tag(self, name: str) -> dict:
"""
@@ -1255,9 +1242,7 @@ async def add_tag(self, name: str) -> dict:
api = API["operate"]["add_tag"]
data = {"aid": self.get_aid(), "bvid": self.get_bvid(), "tag_name": name}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def delete_tag(self, tag_id: int) -> dict:
"""
@@ -1275,9 +1260,7 @@ async def delete_tag(self, tag_id: int) -> dict:
api = API["operate"]["del_tag"]
data = {"tag_id": tag_id, "aid": self.get_aid(), "bvid": self.get_bvid()}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def appeal(self, reason: Any, detail: str):
"""
@@ -1299,7 +1282,7 @@ async def appeal(self, reason: Any, detail: str):
reason = {"tid": reason}
data.update(reason)
# XXX: 暂不支持上传附件
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def set_favorite(
self, add_media_ids: List[int] = [], del_media_ids: List[int] = []
@@ -1328,9 +1311,7 @@ async def set_favorite(
"add_media_ids": ",".join(map(lambda x: str(x), add_media_ids)),
"del_media_ids": ",".join(map(lambda x: str(x), del_media_ids)),
}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_subtitle(
self,
@@ -1375,9 +1356,7 @@ async def get_player_info(
"cid": cid,
"ep_id": epid,
}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def submit_subtitle(
self,
@@ -1449,9 +1428,7 @@ async def submit_subtitle(
"bvid": self.get_bvid(),
}
- return await request(
- "POST", api["url"], data=payload, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_danmaku_snapshot(self) -> dict:
"""
@@ -1464,9 +1441,7 @@ async def get_danmaku_snapshot(self) -> dict:
params = {"aid": self.get_aid()}
- return await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_params(**params).result
async def recall_danmaku(
self,
@@ -1498,9 +1473,7 @@ async def recall_danmaku(
api = API["danmaku"]["recall"]
data = {"dmid": dmid, "cid": cid}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def get_pbp(
self, page_index: Union[int, None] = None, cid: Union[int, None] = None
@@ -1549,7 +1522,7 @@ async def add_to_toview(self) -> dict:
datas = {
"aid": self.get_aid(),
}
- return await request("POST", api["url"], data=datas, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**datas).result
async def delete_from_toview(self) -> dict:
"""
@@ -1562,7 +1535,7 @@ async def delete_from_toview(self) -> dict:
self.credential.raise_for_no_bili_jct()
api = get_api("toview")["operate"]["del"]
datas = {"viewed": "false", "aid": self.get_aid()}
- return await request("POST", api["url"], data=datas, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**datas).result
class VideoOnlineMonitor(AsyncEvent):
@@ -1694,11 +1667,9 @@ async def __main(self):
# 获取服务器信息
self.logger.debug(f"准备连接:{self.__video.get_bvid()}")
self.logger.debug(f"获取服务器信息中...")
- resp = await request(
- "GET",
- "https://api.bilibili.com/x/web-interface/broadcast/servers?platform=pc",
- credential=self.credential,
- )
+
+ api = API["video"]["info"]["video_online_broadcast_servers"]
+ resp = await Api(**api, credential=self.credential).result
uri = f"wss://{resp['domain']}:{resp['wss_port']}/sub"
self.__heartbeat_interval = resp["heartbeat"]
diff --git a/bilibili_api/video_tag.py b/bilibili_api/video_tag.py
index 9e100cd3..0b950e9d 100644
--- a/bilibili_api/video_tag.py
+++ b/bilibili_api/video_tag.py
@@ -4,13 +4,15 @@
视频标签相关,部分的标签的 id 与同名的频道的 id 一模一样。
"""
-from .utils.credential import Credential
-from .utils.network_httpx import request
-from .utils.utils import get_api
-from .errors import *
-import httpx
from typing import Optional
+import httpx
+
+from .errors import *
+from .utils.utils import get_api
+from .utils.credential import Credential
+from .utils.network_httpx import Api
+
API = get_api("video_tag")
API_video = get_api("video")
@@ -60,7 +62,7 @@ async def get_tag_info(self) -> dict:
"""
api = API["info"]["tag_info"]
params = {"tag_id": self.get_tag_id()}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_similar_tags(self) -> dict:
"""
@@ -71,7 +73,7 @@ async def get_similar_tags(self) -> dict:
"""
api = API["info"]["get_similar"]
params = {"tag_id": self.get_tag_id()}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_cards(self) -> dict:
"""
@@ -82,7 +84,7 @@ async def get_cards(self) -> dict:
"""
api = API["info"]["get_list"]
params = {"topic_id": self.get_tag_id()}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_history_cards(self, offset_dynamic_id: int) -> dict:
"""
@@ -93,7 +95,7 @@ async def get_history_cards(self, offset_dynamic_id: int) -> dict:
"""
api = API["info"]["get_history_list"]
params = {"topic_id": self.get_tag_id(), "offset_dynamic_id": offset_dynamic_id}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def subscribe_tag(self) -> dict:
"""
@@ -108,9 +110,7 @@ async def subscribe_tag(self) -> dict:
api = API_video["operate"]["subscribe_tag"]
data = {"tag_id": self.__tag_id}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(data).result
async def unsubscribe_tag(self) -> dict:
"""
@@ -125,6 +125,4 @@ async def unsubscribe_tag(self) -> dict:
api = API_video["operate"]["unsubscribe_tag"]
data = {"tag_id": self.__tag_id}
- return await request(
- "POST", url=api["url"], data=data, credential=self.credential
- )
+ return await Api(**api, credential=self.credential).update_data(data).result
diff --git a/bilibili_api/video_uploader.py b/bilibili_api/video_uploader.py
index 8a473424..7a220727 100644
--- a/bilibili_api/video_uploader.py
+++ b/bilibili_api/video_uploader.py
@@ -3,32 +3,29 @@
视频上传
"""
-import asyncio
-from asyncio.exceptions import CancelledError
-from asyncio.tasks import Task, create_task
import os
-import time
-import random
-
-from .video import Video
-from .utils.aid_bvid_transformer import bvid2aid
-from .utils.credential import Credential
-from .utils.picture import Picture
-from copy import copy, deepcopy
-from .exceptions.ResponseCodeException import ResponseCodeException
import json
+import time
import base64
+import random
+import asyncio
from enum import Enum
-
-from .exceptions.ApiException import ApiException
-from .exceptions.NetworkException import NetworkException
from typing import List, Union
+from copy import copy, deepcopy
+from asyncio.tasks import Task, create_task
+from asyncio.exceptions import CancelledError
-from .utils.AsyncEvent import AsyncEvent
-from .utils.network_httpx import get_session, request
+from .video import Video
from .utils.utils import get_api
-
from .dynamic import upload_image
+from .utils.picture import Picture
+from .utils.AsyncEvent import AsyncEvent
+from .utils.credential import Credential
+from .utils.aid_bvid_transformer import bvid2aid
+from .exceptions.ApiException import ApiException
+from .utils.network_httpx import Api, get_session
+from .exceptions.NetworkException import NetworkException
+from .exceptions.ResponseCodeException import ResponseCodeException
# import ffmpeg
@@ -41,7 +38,7 @@ async def _upload_cover(cover: Picture, credential: Credential):
data = {
"cover": f'data:image/png;base64,{base64.b64encode(cover.content).decode("utf-8")}'
}
- return await request("POST", api["url"], data=data, credential=credential)
+ return await Api(**api, credential=credential).update_data(**data).result
class VideoUploaderPage:
@@ -741,16 +738,13 @@ async def _submit(self, videos: list, cover_url: str = "") -> dict:
api = _API["submit"]
try:
- resp = await request(
- "POST",
- api["url"],
- params={"csrf": self.credential.bili_jct},
- data=json.dumps(meta),
- headers={"content-type": "application/json"},
- credential=self.credential,
- no_csrf=True,
- )
-
+ params = {"csrf": self.credential.bili_jct}
+ data = json.dumps(meta)
+ headers = {"content-type": "application/json"}
+ resp = await Api(**api, credential=self.credential, no_csrf=True
+ ).update_params(**params
+ ).update_data(**data
+ ).update_headers(**headers).result
self.dispatch(VideoUploaderEvents.AFTER_SUBMIT.value, resp)
return resp
@@ -784,7 +778,7 @@ async def get_missions(tid: int = 0, credential: Union[Credential, None] = None)
params = {"tid": tid}
- return await request("GET", api["url"], params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
class VideoEditorEvents(Enum):
@@ -894,9 +888,7 @@ async def _fetch_configs(self):
try:
api = _API["upload_args"]
params = {"bvid": self.bvid}
- self.__old_configs = await request(
- "GET", api["url"], params=params, credential=self.credential
- )
+ self.__old_configs = await Api(**api, credential=self.credential).update_params(**params).result
except Exception as e:
self.dispatch(VideoEditorEvents.PRELOAD_FAILED.value, {"err", e})
raise e
@@ -934,19 +926,17 @@ async def _submit(self):
datas["csrf"] = self.credential.bili_jct
self.dispatch(VideoEditorEvents.PRE_SUBMIT.value)
try:
- resp = await request(
- "POST",
- api["url"],
- params={"csrf": self.credential.bili_jct, "t": int(time.time())},
- data=json.dumps(datas),
- headers={
- "content-type": "application/json;charset=UTF-8",
- "referer": "https://member.bilibili.com",
- "user-agent": "Mozilla/5.0",
- },
- credential=self.credential,
- no_csrf=True,
- )
+ params={"csrf": self.credential.bili_jct, "t": int(time.time())},
+ data=json.dumps(datas),
+ headers={
+ "content-type": "application/json;charset=UTF-8",
+ "referer": "https://member.bilibili.com",
+ "user-agent": "Mozilla/5.0",
+ }
+ resp = await Api(**api, credential=self.credential, no_csrf=True
+ ).update_params(**params
+ ).update_data(**data
+ ).update_headers(**headers).result
self.dispatch(VideoEditorEvents.AFTER_SUBMIT.value, resp)
except Exception as e:
self.dispatch(VideoEditorEvents.SUBMIT_FAILED.value, {"err", e})
diff --git a/bilibili_api/video_zone.py b/bilibili_api/video_zone.py
index 6cb725e0..266aa479 100644
--- a/bilibili_api/video_zone.py
+++ b/bilibili_api/video_zone.py
@@ -4,16 +4,16 @@
分区相关操作,与频道不互通。
"""
-import json
import os
import copy
import enum
-from typing import Tuple, Union, List, Dict
+import json
+from typing import Dict, List, Tuple, Union
-from .exceptions import ArgsException
from .utils.utils import get_api
-from .utils.network_httpx import request
+from .exceptions import ArgsException
from .utils.credential import Credential
+from .utils.network_httpx import Api
API = get_api("video_zone")
@@ -97,9 +97,9 @@ async def get_zone_top10(
if day not in (3, 7):
raise ArgsException("参数 day 只能是 3,7。")
- url = API["ranking"]["get_top10"]["url"]
+ api = API["ranking"]["get_top10"]
params = {"rid": tid, "day": day}
- return await request("GET", url, params=params, credential=credential)
+ return await Api(**api, credential=credential).update_params(**params).result
def get_zone_list() -> List[Dict]:
@@ -155,7 +155,7 @@ async def get_zone_videos_count_today(
"""
credential = credential if credential else Credential()
api = API["count"]
- return (await request("GET", api["url"], credential=credential))["region_count"]
+ return (await Api(**api, credential=credential).result)["region_count"]
async def get_zone_new_videos(tid: int, page_num: int = 1, page_size: int = 10) -> dict:
@@ -174,7 +174,7 @@ async def get_zone_new_videos(tid: int, page_num: int = 1, page_size: int = 10)
"""
api = API["new"]
params = {"rid": tid, "pn": page_num, "ps": page_size}
- return await request("GET", api["url"], params=params)
+ return await Api(**api).update_params(**params).result
async def get_zone_hot_tags(tid: int) -> List[dict]:
@@ -190,7 +190,7 @@ async def get_zone_hot_tags(tid: int) -> List[dict]:
api = API["get_hot_tags"]
params = {"rid": tid}
- return (await request("GET", api["url"], params=params))[0]
+ return (await Api(**api).update_params(**params).result)[0]
class VideoZoneTypes(enum.Enum):
diff --git a/bilibili_api/vote.py b/bilibili_api/vote.py
index c6ca13e3..df631cfc 100644
--- a/bilibili_api/vote.py
+++ b/bilibili_api/vote.py
@@ -5,12 +5,13 @@
需要 vote_id,获取 vote_id: https://nemo2011.github.io/bilibili-api/#/vote_id
"""
-from typing import Optional, Union
from enum import Enum
-from .utils.picture import Picture
+from typing import Union, Optional
+
from .utils.utils import get_api
-from .utils.network_httpx import request
+from .utils.picture import Picture
from .utils.credential import Credential
+from .utils.network_httpx import Api
API = get_api("vote")
@@ -109,7 +110,7 @@ async def get_info(self) -> dict:
"""
api = API["info"]["vote_info"]
params = {"vote_id": self.get_vote_id()}
- info = await request("GET", api["url"], params=params)
+ info = await Api(**api).update_params(**params).result
self.title = info["info"]["title"] # 为 dynmaic.BuildDnamic.add_vote 缓存 title
return info
@@ -169,7 +170,7 @@ async def update_vote(
data.update(choices.get_choices())
if choice_cnt > len(choices.choices):
raise ValueError("choice_cnt 大于 choices 选项数")
- return await request("POST", api["url"], data=data, credential=self.credential)
+ return await Api(**api, credential=self.credential).update_data(**data).result
async def create_vote(
@@ -213,7 +214,7 @@ async def create_vote(
data.update(choices.get_choices())
if choice_cnt > len(choices.choices):
raise ValueError("choice_cnt 大于 choices 选项数")
- vote_id = (await request("POST", api["url"], data=data, credential=credential))[
+ vote_id = (await Api(**api, credential=credential).update_data(**data).result)[
"vote_id"
]
return Vote(vote_id=vote_id, credential=credential)
diff --git a/requirements.txt b/requirements.txt
index 20ac0857..34221f60 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,8 +1,8 @@
-urllib3~=2.0.3
-aiohttp~=3.8.4
+urllib3~=2.0.4
+aiohttp~=3.8.5
beautifulsoup4~=4.12.2
colorama~=0.4.6
-lxml~=4.9.2
+lxml~=4.9.3
pyyaml~=6.0
brotli~=1.0.9
httpx~=0.24.1
diff --git a/scripts/get_audio_search_tags.py b/scripts/get_audio_search_tags.py
index 1439c91f..0c859c1f 100644
--- a/scripts/get_audio_search_tags.py
+++ b/scripts/get_audio_search_tags.py
@@ -1,6 +1,7 @@
-import requests
import json
+import requests
+
print(
json.dumps(
requests.get("https://api.bilibili.com/x/mv/tag").json(),
diff --git a/scripts/get_bangumi_index_params.py b/scripts/get_bangumi_index_params.py
index 72418d85..8034387f 100644
--- a/scripts/get_bangumi_index_params.py
+++ b/scripts/get_bangumi_index_params.py
@@ -1,8 +1,8 @@
-from bilibili_api.utils.initial_state import get_initial_state
+import json
+
from bilibili_api import sync
from bilibili_api.exceptions import *
-
-import json
+from bilibili_api.utils.initial_state import get_initial_state
async def main() -> None:
diff --git a/setup.py b/setup.py
index 6f7b5eb8..4d54f2c4 100644
--- a/setup.py
+++ b/setup.py
@@ -8,7 +8,7 @@
setuptools.setup(
name="bilibili-api-python",
- version="15.5.1.b0",
+ version="15.5.2",
license="GPLv3+",
author="Nemo2011",
author_email="yimoxia@outlook.com",
@@ -37,7 +37,15 @@
"Programming Language :: Python :: 3.11",
],
package_data={
- "": ["data/**/*.*", "py.typed", "requirements.txt", "data/*.*", "data/article/*.*", "data/geetest/*.*", "data/corerespond/*.*"]
+ "": [
+ "data/**/*.*",
+ "py.typed",
+ "requirements.txt",
+ "data/*.*",
+ "data/article/*.*",
+ "data/geetest/*.*",
+ "data/corerespond/*.*",
+ ]
},
install_requires=requires.splitlines(),
url="https://github.com/nemo2011/bilibili-api",
@@ -48,7 +56,7 @@
],
"console_scripts": [
"bilibili-api-docs = bilibili_api.tools.opendocs.__main__:main",
- "ivitools = bilibili_api.tools.ivitools.__main__:main"
+ "ivitools = bilibili_api.tools.ivitools.__main__:main",
],
},
)
diff --git a/tests/__init__.py b/tests/__init__.py
index e6e2ae36..fe98cb25 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,3 +1,4 @@
-import sys
import os
+import sys
+
sys.path.insert(0, os.path.join(os.path.abspath(os.path.pardir), "bilibili_api"))
diff --git a/tests/__main__.py b/tests/__main__.py
index ffd3f99b..a66fc485 100644
--- a/tests/__main__.py
+++ b/tests/__main__.py
@@ -1,4 +1,5 @@
-from .main import main
import asyncio
+from .main import main
+
asyncio.run(main())
diff --git a/tests/common.py b/tests/common.py
index 0b191713..188839b1 100644
--- a/tests/common.py
+++ b/tests/common.py
@@ -1,6 +1,9 @@
-from bilibili_api import Credential
+
+from bilibili_api import Credential, login
import os
+from bilibili_api import Credential
+
def get_credential():
BILI_SESSDATA = os.getenv("BILI_SESSDATA")
@@ -10,5 +13,15 @@ def get_credential():
if not BILI_SESSDATA or not BILI_CSRF or not BILI_BUVID3 or not BILI_DEDEUSERID:
print(Exception("缺少环境变量"))
-
+ cred = login.login_with_qrcode_term()
+ os.environ["BILI_SESSDATA"] = cred.sessdata
+ os.environ["BILI_CSRF"] = cred.bili_jct
+ os.environ["BILI_BUVID3"] = cred.buvid3
+ os.environ["BILI_DEDEUSERID"] = cred.dedeuserid
+ BILI_SESSDATA = os.getenv("BILI_SESSDATA")
+ BILI_CSRF = os.getenv("BILI_CSRF")
+ BILI_BUVID3 = os.getenv("BILI_BUVID3")
+ BILI_DEDEUSERID = os.getenv("BILI_DEDEUSERID")
+
return Credential(BILI_SESSDATA, BILI_CSRF, BILI_BUVID3, BILI_DEDEUSERID)
+
\ No newline at end of file
diff --git a/tests/main.py b/tests/main.py
index f037823d..182f0739 100644
--- a/tests/main.py
+++ b/tests/main.py
@@ -17,16 +17,18 @@
BILI_PASSWORD
BILI_RATELIMIT
"""
-import asyncio
-from colorama import Fore, init, Style
-from bilibili_api import settings
import os
import sys
+import time
import getopt
-import datetime, time
-import traceback
-import os
+import asyncio
+import datetime
import importlib
+import traceback
+
+from colorama import Fore, Style, init
+
+from bilibili_api import settings
def collect_test_function(module):
diff --git a/tests/test_app.py b/tests/test_app.py
index e252d5d9..2be6fc37 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -1,6 +1,7 @@
# bilibili_api.app
from bilibili_api import app
+
from .common import get_credential
credential = get_credential()
diff --git a/tests/test_article.py b/tests/test_article.py
index f1d98ef7..bf1f80a2 100644
--- a/tests/test_article.py
+++ b/tests/test_article.py
@@ -1,6 +1,7 @@
# bilibili_api.article
-from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
from bilibili_api import article
+from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
+
from .common import get_credential
ar = article.Article(17973349, get_credential())
diff --git a/tests/test_ass.py b/tests/test_ass.py
index 02d16511..2311d528 100644
--- a/tests/test_ass.py
+++ b/tests/test_ass.py
@@ -1,6 +1,7 @@
# bilibili_api.ass
from bilibili_api import ass, video
+
from .common import get_credential
v = video.Video("BV1or4y1u7fk")
diff --git a/tests/test_audio.py b/tests/test_audio.py
index 005e7748..c17d8039 100644
--- a/tests/test_audio.py
+++ b/tests/test_audio.py
@@ -1,8 +1,9 @@
# bilibili_api.audio
+from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
from bilibili_api.audio import Audio, AudioList, get_user_stat, get_hot_song_list
+
from .common import get_credential
-from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
credential = get_credential()
diff --git a/tests/test_black_room.py b/tests/test_black_room.py
index e1c7187b..adefe354 100644
--- a/tests/test_black_room.py
+++ b/tests/test_black_room.py
@@ -1,6 +1,7 @@
# bilibili_api.black_room
from bilibili_api import black_room
+
from . import common
room = black_room.BlackRoom(2656141)
diff --git a/tests/test_comment.py b/tests/test_comment.py
index f79f8435..0f9c1706 100644
--- a/tests/test_comment.py
+++ b/tests/test_comment.py
@@ -1,11 +1,12 @@
# bilibili_api.comment
-from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
-from bilibili_api import comment
import random
import asyncio
-from . import common
+from bilibili_api import comment
+from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
+
+from . import common
BVID = "BV1xx411c7Xg"
AID = 271
diff --git a/tests/test_creative_center.py b/tests/test_creative_center.py
index 4e1d3810..36e92f3d 100644
--- a/tests/test_creative_center.py
+++ b/tests/test_creative_center.py
@@ -1,4 +1,5 @@
from bilibili_api import creative_center
+
from . import common
credential = common.get_credential()
diff --git a/tests/test_dynamic.py b/tests/test_dynamic.py
index d98a8e1b..26b5d057 100644
--- a/tests/test_dynamic.py
+++ b/tests/test_dynamic.py
@@ -1,8 +1,10 @@
# bilibili_api.dynamic
-from datetime import datetime
from time import time, sleep
-from bilibili_api import dynamic, user, Picture
+from datetime import datetime
+
+from bilibili_api import Picture, user, dynamic
+
from . import common
credential = common.get_credential()
diff --git a/tests/test_favorite_list.py b/tests/test_favorite_list.py
index e0ff6fdc..62de17c6 100644
--- a/tests/test_favorite_list.py
+++ b/tests/test_favorite_list.py
@@ -2,7 +2,8 @@
import random
-from bilibili_api import favorite_list, video, bvid2aid
+from bilibili_api import video, bvid2aid, favorite_list
+
from . import common
media_id = None
@@ -67,14 +68,15 @@ async def test_h_create_video_favorite_list():
return data
-async def test_o_favorite_list_info():
- data = await favorite_list.FavoriteList(media_id=media_id).get_info()
- return data
+# async def test_o_favorite_list_info():
+# data = await favorite_list.FavoriteList(media_id=media_id).get_info()
+# return data
-async def test_p_favorite_list_content_ids():
- data = await favorite_list.FavoriteList(media_id=media_id).get_content_ids_info()
- return data
+# async def test_p_favorite_list_content_ids():
+# data = await favorite_list.FavoriteList(media_id=media_id).get_content_ids_info()
+# return data
+# FIXME: Github上运行失败
async def test_i_modify_video_favorite_list():
diff --git a/tests/test_homepage.py b/tests/test_homepage.py
index f645f56c..55a6b2e7 100644
--- a/tests/test_homepage.py
+++ b/tests/test_homepage.py
@@ -1,6 +1,7 @@
# bilibili_api.homepage
from bilibili_api import homepage
+
from .common import get_credential
diff --git a/tests/test_interactive_video.py b/tests/test_interactive_video.py
index 762282d6..bfda07df 100644
--- a/tests/test_interactive_video.py
+++ b/tests/test_interactive_video.py
@@ -1,9 +1,11 @@
# bilibili_api.interactive_video
from typing import List
-from .common import get_credential
+
from bilibili_api import interactive_video
+from .common import get_credential
+
v = interactive_video.InteractiveVideo("BV1Dt411N7LY", credential=get_credential())
graph_version = None
diff --git a/tests/test_live.py b/tests/test_live.py
index 47665e00..34c87b73 100644
--- a/tests/test_live.py
+++ b/tests/test_live.py
@@ -1,11 +1,13 @@
# bilibili_api.live
-from bilibili_api.utils.danmaku import Danmaku
+import time
+import random
+
from bilibili_api import live
+from bilibili_api.utils.danmaku import Danmaku
from bilibili_api.exceptions import ResponseCodeException
+
from .common import get_credential
-import random
-import time
l = live.LiveRoom(22544798, get_credential())
diff --git a/tests/test_login.py b/tests/test_login.py
index 3dac1997..dd60c557 100644
--- a/tests/test_login.py
+++ b/tests/test_login.py
@@ -1,6 +1,7 @@
# bilibili_api.login
import os
+
from bilibili_api import login
username = os.getenv("BILI_PHONE")
diff --git a/tests/test_manga.py b/tests/test_manga.py
index 3a15ec44..c9612faa 100644
--- a/tests/test_manga.py
+++ b/tests/test_manga.py
@@ -1,8 +1,9 @@
# bilibili_api.manga
-from .common import get_credential
from bilibili_api import manga
+from .common import get_credential
+
comic = manga.Manga(manga_id=30023, credential=get_credential())
diff --git a/tests/test_note.py b/tests/test_note.py
index d29c3a1c..23cb7262 100644
--- a/tests/test_note.py
+++ b/tests/test_note.py
@@ -1,6 +1,7 @@
# bilibili_api.note
from bilibili_api import note, bvid2aid
+
from .common import get_credential
public_note = note.Note(
diff --git a/tests/test_rank.py b/tests/test_rank.py
index 964f9ecd..0f5fe250 100644
--- a/tests/test_rank.py
+++ b/tests/test_rank.py
@@ -2,6 +2,7 @@
from bilibili_api import rank
from bilibili_api.rank import RankType, RankDayType, VIPRankType, MangeRankType
+
from .common import get_credential
diff --git a/tests/test_root_functions.py b/tests/test_root_functions.py
index bad255e6..c7ac0dc5 100644
--- a/tests/test_root_functions.py
+++ b/tests/test_root_functions.py
@@ -1,6 +1,7 @@
# bilibili_api.__init__
from bilibili_api import parse_link, get_real_url
+
from .common import get_credential
parse_link_urls = [
diff --git a/tests/test_session.py b/tests/test_session.py
index 58e9acff..7fc346c9 100644
--- a/tests/test_session.py
+++ b/tests/test_session.py
@@ -1,6 +1,7 @@
# bilibili_api.session
-from bilibili_api import session, ResponseCodeException
+from bilibili_api import ResponseCodeException, session
+
from .common import get_credential
diff --git a/tests/test_topic.py b/tests/test_topic.py
index 90538efe..fd63a556 100644
--- a/tests/test_topic.py
+++ b/tests/test_topic.py
@@ -1,4 +1,5 @@
from bilibili_api import topic
+
from .common import get_credential
t = topic.Topic(66571, get_credential())
diff --git a/tests/test_user.py b/tests/test_user.py
index 8369e367..3b51f90f 100644
--- a/tests/test_user.py
+++ b/tests/test_user.py
@@ -1,6 +1,7 @@
# bilibili_api.user
import asyncio
+
from bilibili_api import user, bvid2aid
try:
@@ -12,11 +13,13 @@
print("导入凭据未成功")
import time
import random
+
from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
UID = 660303135
UID2 = 1033942996
UID3 = 7949629
+UID4 = 166311555
UID_Model_Test = 12344667
u = user.User(UID_Model_Test, credential=credential)
@@ -190,7 +193,7 @@ async def test_zg_get_album():
async def test_zh_get_user_fav_tag():
try:
- return await u.get_user_fav_tag()
+ return await user.User(UID4).get_user_fav_tag()
except ResponseCodeException as e:
if e.code == 53013:
# 用户隐私未公开
diff --git a/tests/test_video.py b/tests/test_video.py
index 8c18726c..5c24291e 100644
--- a/tests/test_video.py
+++ b/tests/test_video.py
@@ -1,11 +1,13 @@
# bilibili_api.video
+import time
import asyncio
from sqlite3 import DatabaseError
-import time
+
+from bilibili_api import exceptions
+from bilibili_api import video as video_m
from bilibili_api.utils.danmaku import Danmaku
from bilibili_api.exceptions.ResponseCodeException import ResponseCodeException
-from bilibili_api import video as video_m, exceptions
# 调试兼容
try:
diff --git a/tests/test_video_tag.py b/tests/test_video_tag.py
index 629d2552..14eaaa3a 100644
--- a/tests/test_video_tag.py
+++ b/tests/test_video_tag.py
@@ -1,4 +1,5 @@
from bilibili_api import video_tag
+
from .common import get_credential
tag = video_tag.Tag(tag_name="真白花音", credential=get_credential())