Skip to content

archibate/dotfiles-opencode / skills/bilibili-api

bilibili-api

Flagged

This skill should be used when the user asks to "use bilibili API", "download bilibili video", "get bilibili user info", "list bilibili favorites", "send bilibili danmaku", "upload video to bilibili", "monitor bilibili live room", "search bilibili", "get bilibili comments", or needs guidance on the bilibili_api Python library usage, authentication, API endpoints, or workflow patterns.

FileKindSize
SKILL.mdskill md8.3 KB
references/api-modules.mdmarkdown6.4 KB
references/common-patterns.mdmarkdown8.6 KB
references/configuration.mdmarkdown4.8 KB
references/credential-setup.mdmarkdown6.1 KB
references/docs/.gitignoreother13 B
references/docs/.nojekyllother0 B
references/docs/DEPLOY_LOCALHOST.mdmarkdown310 B
references/docs/README.mdmarkdown9.9 KB
references/docs/_sidebar.mdmarkdown4.7 KB
references/docs/b23tv.mdmarkdown448 B
references/docs/configuration.mdmarkdown4.3 KB
references/docs/examples/activity.mdmarkdown960 B
references/docs/examples/app.mdmarkdown127 B
references/docs/examples/article.mdmarkdown918 B
references/docs/examples/article_category.mdmarkdown648 B
references/docs/examples/ass.mdmarkdown727 B
references/docs/examples/audio.mdmarkdown1.8 KB
references/docs/examples/audio_uploader.mdmarkdown975 B
references/docs/examples/bangumi.mdmarkdown3.8 KB
references/docs/examples/black_room.mdmarkdown350 B
references/docs/examples/channel_series.mdmarkdown787 B
references/docs/examples/cheese.mdmarkdown152 B
references/docs/examples/client.mdmarkdown183 B
references/docs/examples/comment.mdmarkdown2.2 KB
references/docs/examples/creative_center.mdmarkdown288 B
references/docs/examples/dynamic.mdmarkdown2.0 KB
references/docs/examples/emoji.mdmarkdown802 B
references/docs/examples/favorite_list.mdmarkdown406 B
references/docs/examples/festival.mdmarkdown3.0 KB
references/docs/examples/game.mdmarkdown1.4 KB
references/docs/examples/garb.mdmarkdown3.2 KB
references/docs/examples/homepage.mdmarkdown127 B
references/docs/examples/hot.mdmarkdown263 B
references/docs/examples/interactive_video.mdmarkdown7.9 KB
references/docs/examples/live.mdmarkdown4.0 KB
references/docs/examples/live_area.mdmarkdown349 B
references/docs/examples/login_v2.mdmarkdown5.1 KB
references/docs/examples/manga.mdmarkdown1.2 KB
references/docs/examples/music.mdmarkdown727 B
references/docs/examples/note.mdmarkdown355 B
references/docs/examples/opus.mdmarkdown1.9 KB
references/docs/examples/rank.mdmarkdown122 B
references/docs/examples/search.mdmarkdown1.3 KB
references/docs/examples/session.mdmarkdown778 B
references/docs/examples/show.mdmarkdown255 B
references/docs/examples/topic.mdmarkdown159 B
references/docs/examples/user.mdmarkdown2.5 KB
references/docs/examples/video.mdmarkdown3.8 KB
references/docs/examples/video_tag.mdmarkdown174 B
references/docs/examples/video_uploader.mdmarkdown2.0 KB
references/docs/examples/video_zone.mdmarkdown279 B
references/docs/examples/vote.mdmarkdown133 B
references/docs/examples/watchroom.mdmarkdown662 B
references/docs/get-credential.mdmarkdown3.2 KB
references/docs/history.htmlother1.5 KB
references/docs/history.mdmarkdown123 B
references/docs/index.htmlother2.2 KB
references/docs/ivifile.mdmarkdown3.9 KB
references/docs/logo-small.pngbinary12.2 KB
references/docs/logo.pngbinary88.6 KB
references/docs/modules/README.mdmarkdown432 B
references/docs/modules/activity.mdmarkdown1.0 KB
references/docs/modules/app.mdmarkdown1.3 KB
references/docs/modules/article.mdmarkdown4.8 KB
references/docs/modules/article_category.mdmarkdown2.1 KB
references/docs/modules/ass.mdmarkdown8.2 KB
references/docs/modules/audio.mdmarkdown3.2 KB
references/docs/modules/audio_uploader.mdmarkdown7.5 KB
references/docs/modules/bangumi.mdmarkdown23.6 KB
references/docs/modules/bilibili_api.mdmarkdown38.4 KB
references/docs/modules/black_room.mdmarkdown6.1 KB
references/docs/modules/channel_series.mdmarkdown4.4 KB
references/docs/modules/cheese.mdmarkdown7.3 KB
references/docs/modules/client.mdmarkdown508 B
references/docs/modules/comment.mdmarkdown7.2 KB
references/docs/modules/creative_center.mdmarkdown14.1 KB
references/docs/modules/dynamic.mdmarkdown13.9 KB
references/docs/modules/emoji.mdmarkdown1.6 KB
references/docs/modules/favorite_list.mdmarkdown10.2 KB
references/docs/modules/festival.mdmarkdown771 B
references/docs/modules/game.mdmarkdown3.1 KB
references/docs/modules/garb.mdmarkdown5.8 KB
references/docs/modules/homepage.mdmarkdown2.2 KB
references/docs/modules/hot.mdmarkdown1.6 KB
references/docs/modules/interactive_video.mdmarkdown13.3 KB
references/docs/modules/live.mdmarkdown20.2 KB
references/docs/modules/live_area.mdmarkdown2.2 KB
references/docs/modules/login_v2.mdmarkdown6.0 KB
references/docs/modules/manga.mdmarkdown4.0 KB
references/docs/modules/music.mdmarkdown3.5 KB
references/docs/modules/note.mdmarkdown4.3 KB
references/docs/modules/opus.mdmarkdown3.3 KB
references/docs/modules/rank.mdmarkdown6.8 KB
references/docs/modules/search.mdmarkdown5.9 KB
references/docs/modules/session.mdmarkdown6.9 KB
references/docs/modules/show.mdmarkdown3.2 KB
references/docs/modules/topic.mdmarkdown2.7 KB
references/docs/modules/user.mdmarkdown25.9 KB
references/docs/modules/video.mdmarkdown28.4 KB
references/docs/modules/video_tag.mdmarkdown1.7 KB
references/docs/modules/video_uploader.mdmarkdown9.9 KB
references/docs/modules/video_zone.mdmarkdown6.8 KB
references/docs/modules/vote.mdmarkdown3.2 KB
references/docs/modules/watchroom.mdmarkdown4.4 KB
references/docs/package-lock.jsonmanifest125.8 KB
references/docs/refresh_cookies.mdmarkdown674 B
references/docs/request_client.mdmarkdown11.1 KB
references/docs/search.jsscript6.4 KB
references/docs/sync-executor.mdmarkdown471 B
references/docs/target/npmlist.jsonconfig268 B
references/docs/theme.cssother15.4 KB
references/docs/vote_id.mdmarkdown2.0 KB
references/docs/vote_source.pngbinary41.8 KB
references/live-guide.mdmarkdown5.1 KB
references/self-update.mdmarkdown5.4 KB
references/upload-guide.mdmarkdown5.9 KB
references/user-guide.mdmarkdown4.7 KB
references/video-guide.mdmarkdown6.0 KB
scripts/credential_check.pyscript3.0 KB
scripts/quickstart.pyscript1.2 KB

references/docs/request_client.md

View on GitHub
# 模块请求库相关

此部分主要是函数 / 用法介绍,将部分用途较多的函数拎出来提一提。这些函数的相关信息文档中均可找到。

[TOC]

## 1、默认支持的第三方请求库

### 1、总表

|           | 优先级 | request | stream | WebSocket | 额外网络请求设置                                                           |
| --------- | ------ | ------- | ------ | --------- | -------------------------------------------------------------------------- |
| curl_cffi | 3      | ✅      | ✅     | ✅        | - `impersonate` defaults to ` ` <br> - `http2` defaults to `False` |
| aiohttp   | 2      | ✅      | ✅     | ✅        |                                                                            |
| httpx     | 1      | ✅      | ✅     | ❌        | `http2` defaults to `False`                                                |

名为 `impersonate` 的设置决定了 curl_cffi 模仿哪个浏览器的指纹,名为 `http2` 的设置决定了 curl_cffi / httpx 是否启用 HTTP2。[这些设置如何启用?](https://nemo2011.github.io/bilibili-api/#/configuration)

### 2、切换请求库

```python
select_client("curl_cffi") # 选择 curl_cffi
select_client("aiohttp") # 选择 aiohttp
select_client("httpx") # 选择 httpx,不支持 WebSocket

print(get_selected_client()) # httpx
```

### 3、把模块的会话拿出来用

```python
select_client("httpx") # 使用 httpx
sess: httpx.AsyncClient = get_session() # 此函数获取的会话仅限于当前事件循环
```

### 4、把自己的会话传给模块

```python
sess: httpx.AsyncClient = httpx.AsyncClient(
    .........
    .........
    .........
)
set_session(sess) # 同样仅限于当前事件循环
```

## 2、模块使用第三方库底层逻辑简析

bilibili_api 支持不同网络请求库的功能基于一个抽象类:`BiliAPIClient`。通过对这个抽象类的改写,可以实现**统一接口样式**的、**基于不同第三方请求库**的不同类,例如模块自带的 `bilibili_api.clients.AioHTTPClient.AioHTTPClient`, `bilibili_api.clients.CurlCFFIClient.CurlCFFIClient`, `bilibili_api.clients.HTTPXClient.HTTPXClient`,这些类会像第三方请求库的会话一样工作,但是它们是**统一接口样式**的。换种说法,这是对第三方请求库的会话对象**进行封装**。

现规定 `session` 表示 **第三方请求库(诸如 `curl_cffi`, `aiohttp`)在模块每次运行中使用可持久化的会话对象**,`client` 表示继承 `BiliAPIClient` 的类。

### 1、模块如何发起请求

我们以 `user.User().get_user_info()` 为例。此处选择的 `User` 实例为站长(`uid=1`)。我们大致简化这个过程,以解释 bilibili_api 发起请求的全过程。

首先,一般地,调用的所有函数会创建一个类的实例,`Api`,可以把它当作请求的载体。这个例子中 `Api` 对象长这样:

- `method`: GET
- `url`: <https://api.bilibili.com/x/space/wbi/acc/info>
- `params`:
  - `mid`: 2
- `wbi`: true
- `wbi2`: true

这里有两个字段,`wbi` 和 `wbi2`,正常网络请求里面没有。其实这是 bilibili 的一种风控方式,通过在请求中使用一整套名为 `wbi` 的加密方法达到反爬虫的目的。这套加密方法是通用的,所以无需在每一个需要使用 `wbi` 的函数中执行一遍,这些工作被安排到了 `Api` 类中。

接下来让我们看 `Api` 类发起请求的几行代码:

```python
config = await self._prepare_request()
client = get_client()
resp = await client.request(**config)
```

第一行中,`_prepare_request` 方法就会对诸如 `wbi` 这类的反爬虫加密方式进行处理,操作完后我们可以得到一个 `config` 字典。`config` 字典长这样:

- `method`: GET
- `url`: <https://api.bilibili.com/x/space/wbi/acc/info>
- `params`:
  - `mid`: 2
  - `w_webid`: eyJhbGciOi...
  - `dm_img_list`: []
  - `dm_img_str`: JC
  - `dm_cover_img_str`: AC
  - `dm_img_inter`: {"ds":[],"wh":[0,0,0],"of":[0,0,0]}
  - `wts`: 1737376069
  - `web_location`: 1550101
  - `w_rid`: bfe6d5df5f...

这就是正常情况下,为了访问这个接口,需要往 `httpx.get`, `aiohttp.get`, `curl_cffi.requests.get` 等第三方库中传入的参数。在浏览器控制台抓取到的真实请求也应该长这样子。

第二行,调用 `get_client`,这个函数会返回一个 `client` 实例。接着在第三行调用这个 `client` 的函数,`client` 会将参数传递到 `session`。

这个例子中,`config` 直接传入 `session` 也是可行的,因为字段够简单,但是不妨多考虑一下,我们现在还需要获得 `session` 的响应文本。`httpx` 只需要 `resp.text`,而 `aiohttp` 需要 `await resp.text()`,此处已经不能统一处理了。

我们再假设,现在需要上传一个文件(`multipart/form-data`),request-like api 中文件会放入 `files` 参数,而 aiohttp api 中文件需要使用 `FormData` 和其他字段结合后传入 `data`。这里该如何统一处理?

这就是 `client` 的作用:作为中间层,**它为模块提供了固定的 api,从而做到只要更换 `client` 就能更换请求库,而不是更换代码**。例如在响应上,它用 `BiliAPIResponse` 统一了各个第三方请求库,在上传文件传参上,它用 `BiliAPIFile` 统一了各个第三方请求库。

### 2、模块如何处理 `client` 类

前面提到模块底层基于 `client` 类,那么是如何用到这个类的?

首先需要把 `client` 的类注册到模块中,以供模块使用。后文将详细描述。

然后是有了这个类之后具体调用方式,首先创建实例,此过程在 `get_client` 中进行。创建实例是有说法的,模块默认有一套设置,它们会在创建实例的过程中给到每一个 `client`,然后 `client` 会将这些设置应用到第三方请求库会话上。也有种情况用户自己要提供会话,即 `set_session`,这时候 `client` 初始化只接受用户提供的会话,模块设置应当被忽略。创建完实例后即可调用。

### 3、如何使用 `client`

前文提到,模块通过 `client` 统一后的接口发送网络请求。事实上,通过 `get_client` 函数就能获取到模块正在使用的 `BiliAPIClient`。因为 `BiliAPIClient` 接口统一,故直接使用 `BiliAPIClient` 相关函数即使更换第三方请求库结果上也不会有影响(理论)。

例如下载文件的功能,在模块提供的示例中有出现,就是基于 `BiliAPIClient` 相关函数实现的。

```python
async def download(url: str, out: str, intro: str):
    dwn_id = await get_client().download_create(url, HEADERS) # 创建下载任务(本质上是建立起连接)
    bts = 0
    tot = get_client().download_content_length(dwn_id) # 获取文件长度
    with open(out, "wb") as file:
        while True:
            bts += file.write(await get_client().download_chunk(dwn_id)) # 每次下载部分
            print(f"{intro} - {out} [{bts} / {tot}]", end="\r")
            if bts == tot:
                break
    print()
# 此函数在 aiohttp / httpx / curl_cffi 下都能正常运行。
```

接下来对 `BiliAPIClient` 内部函数进行介绍。

#### \_\_init\_\_

提供两种传参方式:1、传入所有的设置,字段名称为设置的名字,提供默认值。2、传入一个第三方请求库的会话对象。

```python
def __init__(
    self,
    proxy: str = "",
    timeout: float = 0.0,
    verify_ssl: bool = True,
    trust_env: bool = True,
    session: Optional[object] = None,
) -> None: ...
```

#### set_xxx

当模块设置被修改时会执行的函数

```python
def set_timeout(self, timeout: float = 0.0) -> None: ...
def set_proxy(self, proxy: str = "") -> None: ...
def set_verify_ssl(self, verify_ssl: bool = True) -> None: ...
def set_trust_env(self, trust_env: bool = True) -> None: ...
```

#### get_wrapped_session

获取 `client` 中被包装的 `session`

```python
def get_wrapped_session(self) -> object: ...
```

> `get_session() = get_client().get_wrapped_session()`

#### request

非常正常的 request。

```python
async def request(
    self,
    method: str = "",
    url: str = "",
    params: dict = {},
    data: Union[dict, str, bytes] = {},
    files: Dict[str, BiliAPIFile] = {},
    headers: dict = {},
    cookies: dict = {},
    allow_redirects: bool = True,
) -> BiliAPIResponse: ...
```

`BiliAPIResponse` 和 `BiliAPIFile` 相关可在文档中查阅。

#### WebSocket

WebSocket 为持续连接,然而 `BiliAPIClient` 未提供上下文 api。在创建了 WebSocket 连接后,函数会返回一个整数,表示连接编号,这个连接会以编号的形式保留着。之后相关操作需要传入编号进行。

```python
async def ws_create(
    self, url: str = "", params: dict = {}, headers: dict = {}
) -> int: ...
async def ws_send(self, cnt: int, data: bytes) -> None: ...
async def ws_recv(self, cnt: int) -> Tuple[bytes, BiliWsMsgType]: ...
async def ws_close(self, cnt: int) -> None: ...
```

#### 下载

此处下载为流式下载,设计上和 WebSocket 类似(返回编号表示连接,其他函数传入编号)。

```python
async def download_create(
    self,
    url: str = "",
    headers: dict = {},
) -> int: ...
async def download_chunk(self, cnt: int) -> bytes: ...
def download_content_length(self, cnt: int) -> int: ...
```

## 3、将其他第三方请求库与模块进行适配

### 1、基础

整体思路是新建一个类,继承 `BiliAPIClient`,然后对前文提到的函数进行实现。

此处实现可以参考模块自带的 `client`。见 <https://github.com/nemo2011/bilibili-api/tree/main/bilibili_api/clients>

> 函数中可以调用 `request_log.dispatch` 函数实现日志功能。

### 2、进阶

有时会在自己编写的 `client` 类中添加设置,例如 `curl_cffi` 的 `impersonate` 参数便应当为设置项。如何实现这个功能?

以此处的 `impersonate` 参数为例子,先在 `__init__` 中加上 `xxx` 参数(在 `__init__` 中加上 `impersonate` 参数),然后在 `client` 类中实现 `set_xxx` (在 `CurlCFFIClient` 中实现 `set_impersonate`) 即可。

### 3、注册

前文提到 `client` 使用前需要注册,模块默认携带的 `AioHTTPClient` `CurlCFFIClient` `HTTPXClient` 也有注册的过程。

注册的函数为 `register_client`,第一项参数传入你喜欢的 `client` 的名字,第二项参数传入 `client` 的类,第三项参数需要传入 `client` 中额外使用到的设置项。以 `impersonate` 为例子,首先需要一个字典,然后将字典的 `impersonate` 项设置为你喜欢的 `impersonate` 设置项的默认值,最后将这个字典传入。

```python
class CurlCFFIClient(BiliAPIClient):
    ...

register_client("curl_cffi", CurlCFFIClient, {"impersonate": "chrome131"})
```

注册后会自动切换到注册的 `client`。然后便能使用 `client`。

同时,注册也可以取消。

```python
unregister_client("curl_cffi")
```
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 468kUpdated Claude CodeCodexUnverifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 319kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 319kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 319kUpdated Claude CodeCodexVerifiedInstall