{"id":24148938,"url":"https://github.com/api-scraper/bilibili-api","last_synced_at":"2026-04-25T12:31:48.950Z","repository":{"id":236331279,"uuid":"791747368","full_name":"api-scraper/bilibili-api","owner":"api-scraper","description":"type-safe nodejs/browser library for bilibili api based on axios and mitt, accorded from Bilibili-API-Collect","archived":false,"fork":false,"pushed_at":"2024-04-27T16:12:56.000Z","size":82,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-29T02:43:05.724Z","etag":null,"topics":["bilibili","bilibili-api","http-api","nodejs","type-safe","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/api-scraper.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-25T09:42:42.000Z","updated_at":"2024-12-06T02:24:56.000Z","dependencies_parsed_at":"2024-04-26T16:29:44.987Z","dependency_job_id":"da1488f2-578a-4ca8-a951-988e492dc546","html_url":"https://github.com/api-scraper/bilibili-api","commit_stats":null,"previous_names":["eric2788/bilibili-api","api-scraper/bilibili-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/api-scraper/bilibili-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api-scraper%2Fbilibili-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api-scraper%2Fbilibili-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api-scraper%2Fbilibili-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api-scraper%2Fbilibili-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/api-scraper","download_url":"https://codeload.github.com/api-scraper/bilibili-api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api-scraper%2Fbilibili-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32262800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-25T09:15:33.318Z","status":"ssl_error","status_checked_at":"2026-04-25T09:15:31.997Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bilibili","bilibili-api","http-api","nodejs","type-safe","typescript"],"created_at":"2025-01-12T08:27:20.189Z","updated_at":"2026-04-25T12:31:48.933Z","avatar_url":"https://github.com/api-scraper.png","language":"TypeScript","readme":"\u003e [!IMPORTANT]\n\u003e 该项目为概念半成品，主要用于测试日后维护和扩展API的可行性。\n\u003e \n\u003e 假设你觉得这个项目可行，欢迎贡献代码，让这个项目更加完善！\n\u003e 当进度足够完善时，将会发布到 npm 上。\n\n# Bilibili-API\n\n适用于 nodejs/browser 的 bilibili api 类型安全库\n\n基于 `mitt` 和 `axios`\n\n\u003e [!NOTE]\n\u003e 这些 API 都是根据 [bilibili-API-collect](https://github.com/SocialSisterYi/bilibili-API-collect/) 来的，如果有任何不匹配，欢迎开 issue 或 PR。\n\n## 特性\n\n- 类型安全\n- 开箱即用\n- 易于扩展/维护\n\n## 安装\n\n（尚未发布）\n\n```bash\nnpm install bilibili-api\n```\n\n## 使用\n\n简单示例\n\n```ts\nimport { ApiClient } from \"bilibili-api\";\n\nconst client = await ApiClient.create()\n\nclient.live.emit(\"info/room\", { room_id: 1 }).then((res) =\u003e {\n  console.log(res)\n})\n\nclient.video.emit(\"action/like/web\", { bvid: \"1mK4y1C7Bz\", like: 1, csrf: \"\" }).then((res) =\u003e {\n  console.log(res)\n})\n\n// 监听事件\nclient.live.on(\"info/room\", (res) =\u003e {\n    // res 是类型安全的\n    console.log(res)\n})\n```\n\n部分引入\n\n```ts\nimport { LiveClient, VideoClient } from \"bilibili-api\"\nimport axiosFactory from 'axios'\n\n// 在 nodejs 中，你需要使用外部库来支持 axios 中的 cookie\nconst axios = axiosFactory.create({ withCredentials: true })\nconst live = new LiveClient(axios)\nconst video = new VideoClient(axios)\n\n// emit 键是类型安全的\nlive.emit(\"info/room\", { room_id: 1 }).then((res) =\u003e {\n    // res 是类型安全的\n    console.log(res)\n})\n\nvideo\n    .emit(\"action/like/web\", { // 输入是类型安全的\n        bvid: \"1mK4y1C7Bz\",\n        like: 1,\n        csrf: \"\",\n    })\n    .then((res) =\u003e {\n        // res 是类型安全的\n        console.log(res)\n    })\n// 监听事件\nlive.on(\"info/room\", (res) =\u003e {\n    // res 是类型安全的\n    console.log(res)\n})\n```\n\n\n## 维护/扩展/贡献\n\n### 架构\n\n参考：[src/schema](./src/schemas/)\n\n以 `live/info/user` 为例：\n\n文件：`live/info/user.ts`\n\n```ts\n// 信息来源于 https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/live/info.md#获取用户对应的直播间状态\nexport namespace User {\n\n    export type Schema = HttpSchema\u003cInput, Output\u003e\n\n    // 定义\n    export const defines: HttpDefine = {\n        method: 'GET',\n        url: 'https://api.live.bilibili.com/room/v1/Room/getRoomInfoOld',\n        params: ['mid']\n    }\n\n    // 输入架构\n    type Input = {\n        mid: number\n    }\n\n    // 输出架构\n    type Output = V1Response\u003c{\n        roomStatus: number\n        roundStatus: number\n        live_status: number\n        url: string\n        title: string\n        cover: string\n        online: number\n        roomid: number\n        broadcast_type: number\n        online_hidden: number\n    }\u003e\n\n}\n```\n\n文件：`live/info/index.ts`\n```ts\nexport * from './init'\nexport * from './room'\nexport * from './stream'\nexport * from './streamer'\nexport * from './user' // 已添加\n```\n\n文件：`live/index.ts`\n```ts\nimport * as info from './info'\nimport { Area } from './area'\nimport { Stream } from './stream'\nimport { HttpDefine } from '@schemas/common'\n\nexport type Schema = {\n    'info/init': info.Init.Schema\n    'info/room': info.Room.Schema\n    'info/stream': info.Stream.Schema\n    'info/streamer': info.Streamer.Schema\n    'info/user': info.User.Schema // 已添加\n    'area': Area.Schema\n    'stream': Stream.Schema\n}\n\nexport type SchemaKey = keyof Schema\nexport const defines: Record\u003cSchemaKey, HttpDefine\u003e = {\n    'info/init': info.Init.defines,\n    'info/room': info.Room.defines,\n    'info/stream': info.Stream.defines,\n    'info/streamer': info.Streamer.defines,\n    'info/user': info.User.defines, // 已添加\n    'area': Area.defines,\n    'stream': Stream.defines\n}\n```\n\n添加后，类型安全现已就绪\n\n```ts\nimport { LiveClient } from \"bilibili-api\"\n\nconst live = new LiveClient(axios)\n\nlive.emit(\"info/user\", { mid: 1 }).then((res) =\u003e {\n    // res 是类型安全的\n    console.log(res)\n})\n```\n\n### 客户端\n\n参考：[src/clients](./src/clients/)\n\n新增一个新的客户端类别时使用。例如 `schames/live`\n\n文件：`clients/live.ts`\n\n```ts\nimport { HttpDefine } from \"@schemas/common\"\nimport { defines, Schema, SchemaKey } from \"@schemas/live\"\nimport { BaseClient, RequestConfig } from \"./base\"\n\n// Schema 是基于 该文件夹的架构定义的 (这里的例子为 live )\nexport class LiveClient extends BaseClient\u003cSchema\u003e {\n\n    async emit\u003cK extends SchemaKey, Input extends Schema[K]['input'], Output extends Schema[K]['output']\u003e(key: K, data: Input = undefined, options: RequestConfig = {}): Promise\u003cOutput\u003e {\n        return super.emit(key, data, options)\n    }\n\n    on\u003cK extends SchemaKey, Data extends Schema[K]['output']\u003e(key: K, handler: (data: Data) =\u003e void) {\n        super.on(key, handler)\n    }\n\n    off\u003cK extends SchemaKey, Data extends Schema[K]['output']\u003e(key: K, handler: (data: Data) =\u003e void) {\n        super.off(key, handler)\n    }\n\n    protected get defines(): Record\u003ckeyof Schema, HttpDefine\u003e {\n        return defines\n    }\n\n}\n```\n\n文件：`index.ts`\n\n```ts\nimport { LiveClient } from \"@clients/live\" // 已添加\nimport { LoginClient } from \"@clients/login\"\nimport { VideoClient } from \"@clients/video\"\nimport { AxiosInstance } from 'axios'\nexport class ApiClient {\n\n    /**\n     * 根据环境创建 ApiClient 实例。\n     * \n     * @returns {ApiClient} 创建的 ApiClient 实例。\n     */\n    static create(): ApiClient {\n        let create: Function\n        if (typeof window === 'undefined') {\n            const fromNodeJS = require('@factories/node').default\n            create = fromNodeJS\n        } else {\n            const fromBrowser = require('@factories/browser').default\n            create = fromBrowser\n        }\n        return create()\n    }\n\n    public constructor(axios: AxiosInstance) {\n        this.login = new LoginClient(axios)\n        this.video = new VideoClient(axios)\n        this.live = new LiveClient(axios) // 已添加\n    }\n\n    public readonly login: LoginClient\n    public readonly video: VideoClient\n    public readonly live: LiveClient // 已添加\n\n}\n\nexport {\n    LiveClient, // 为部分引入而添加\n    LoginClient,\n    VideoClient\n}\n```\n\n使用方法\n\n```ts\nimport { ApiClient, LiveClient } from \"bilibili-api\"\n\n// 使用 ApiClient\nconst api = ApiClient.create()\napi.live.emit(\"info/user\", { mid: 1 }).then((res) =\u003e {\n    // res 是类型安全的\n    console.log(res)\n})\n\n// 使用 LiveClient\nconst axios = axios.create()\nconst live = new LiveClient(axios)\nlive.emit(\"info/user\", { mid: 1 }).then((res) =\u003e {\n    // res 是类型安全的\n    console.log(res)\n})\n```\n\n\n注意事项：\n\n- 所有实现都是从 BaseClient 预设的\n- 重新声明函数只是为了类型安全\n\n\u003e [!NOTE] \n\u003e 目前每个客户端的实现似乎都是一样的，只是为了类型安全，如果你有更好的想法，欢迎 PR","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapi-scraper%2Fbilibili-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapi-scraper%2Fbilibili-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapi-scraper%2Fbilibili-api/lists"}