{"id":13431566,"url":"https://github.com/CuteReimu/bilibili","last_synced_at":"2025-03-16T11:31:59.126Z","repository":{"id":41846928,"uuid":"460497696","full_name":"CuteReimu/bilibili","owner":"CuteReimu","description":"哔哩哔哩bilibili的API的Go SDK","archived":false,"fork":false,"pushed_at":"2024-05-11T15:04:37.000Z","size":331,"stargazers_count":56,"open_issues_count":1,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-13T12:06:35.378Z","etag":null,"topics":["bilibili","bilibili-api","bilibili-sdk","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CuteReimu.png","metadata":{"files":{"readme":"README.md","changelog":"history.go","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-17T15:38:44.000Z","updated_at":"2024-05-17T08:41:29.025Z","dependencies_parsed_at":"2024-02-26T19:47:42.690Z","dependency_job_id":"a054bf34-205c-4e27-bf6f-84df1e3d4b3d","html_url":"https://github.com/CuteReimu/bilibili","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CuteReimu%2Fbilibili","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CuteReimu%2Fbilibili/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CuteReimu%2Fbilibili/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CuteReimu%2Fbilibili/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CuteReimu","download_url":"https://codeload.github.com/CuteReimu/bilibili/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243863337,"owners_count":20360303,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","bilibili-sdk","golang"],"created_at":"2024-07-31T02:01:04.148Z","updated_at":"2025-03-16T11:31:59.118Z","avatar_url":"https://github.com/CuteReimu.png","language":"Go","funding_links":[],"categories":["开发"],"sub_categories":["直播脚本"],"readme":"\u003cdiv align=\"center\"\u003e\n\n# 哔哩哔哩-API-Go版本\n\n![](https://img.shields.io/github/go-mod/go-version/CuteReimu/bilibili \"语言\")\n[![](https://img.shields.io/github/stars/CuteReimu/bilibili?style=flat\u0026color=yellow)](#star-history \"stars\")\n[![](https://img.shields.io/github/actions/workflow/status/CuteReimu/bilibili/golangci-lint.yml?branch=master)](https://github.com/CuteReimu/bilibili/actions/workflows/golangci-lint.yml \"代码分析\")\n[![](https://img.shields.io/github/contributors/CuteReimu/bilibili)](https://github.com/CuteReimu/bilibili/graphs/contributors \"贡献者\")\n[![](https://img.shields.io/github/license/CuteReimu/bilibili)](https://github.com/CuteReimu/bilibili/blob/master/LICENSE \"许可协议\")\n\u003c/div\u003e\n\n本项目是基于Go语言编写的哔哩哔哩API调用。目前常用的接口已经基本完成。\n\n**本项目不会编写单元测试代码**。一则因为各项数据会频繁变动，难以写成固定的结果；二则因为每次单元测试都要大量请求B站API，会对其产生不必要的压力。\n如果你发现有**接口bug**或者**有你需要但是本库尚未实现的接口**，可以[提交issue](https://github.com/CuteReimu/bilibili/issues/new/choose)或者[提交pull request](.github/CONTRIBUTING.md)。\n如果因为B站修改了接口导致接口突然不可用，不一定能够及时更新，很大程度上需要依赖各位的告知。\n\n\u003e [!IMPORTANT]\n\u003e 现在是v2.1+版本，鉴于`golang.org/x`下面的很多库都已经强制要求Go1.23以上了，我们也同步进行了更新。\n\u003e \n\u003e 如果想使用v2.0版本（支持Go1.19及以上），请执行`go get -u github.com/CuteReimu/bilibili/v2@v2.0.0`获取旧版本。\n\u003e \n\u003e [如果还想使用更早的版本可以点击这里跳转](https://github.com/CuteReimu/bilibili/tree/v1)。\n\n**如果你觉得本项目对你有帮助，点亮右上角的↗ :star: 不迷路**\n\n## 声明\n\n1. 本项目遵守 AGPL 开源协议。\n2. 本项目基于 [SocialSisterYi/bilibili-API-collect](https://github.com/SocialSisterYi/bilibili-API-collect)\n   中描述的接口编写。请尊重该项目作者的努力，遵循该项目的开源要求，禁止一切商业使用。\n3. **请勿滥用，本项目仅用于学习和测试！利用本项目提供的接口、文档等造成不良影响及后果与本人无关。**\n4. 由于本项目的特殊性，可能随时停止开发或删档\n5. 本项目为开源项目，不接受任何形式的催单和索取行为，更不容许存在付费内容\n\nPS：目前，B站调用接口时强制使用 `https` 协议\n\n## 快速开始\n\n### 安装\n\n```bash\ngo get -u github.com/CuteReimu/bilibili/v2 # 定期执行可以更新最新版本\n```\n\n在项目中引用即可使用\n\n```go\nimport \"github.com/CuteReimu/bilibili/v2\"\n\nvar client = bilibili.New()\n```\n\n### 首次登录\n\n\u003e [!TIP]\n\u003e 下文为了篇幅更短，示例中把很多显而易见的`err`校验忽略成了`_`，实际使用请自行校验`err`。\n\n#### 方法一：扫码登录\n\n首先获取二维码：\n\n```go\nqrCode, _ := client.GetQRCode()\nbuf, _ := qrCode.Encode()\nimg, _ := png.Decode(buf) // 或者写入文件 os.WriteFile(\"qrcode.png\", buf, 0644)\n// 也可以调用 qrCode.Print() 将二维码打印在控制台\n```\n\n扫码并确认成功后，发送登录请求：\n\n```go\nresult, err := client.LoginWithQRCode(bilibili.LoginWithQRCodeParam{\n    QrcodeKey: qrCode.QrcodeKey,\n})\nif err == nil \u0026\u0026 result.Code == 0 {\n    log.Println(\"登录成功\")\n}\n```\n\n#### 方法二：账号密码登录\n\n首先获取人机验证参数：\n\n```go\ncaptchaResult, _ := client.Captcha()\n```\n\n将`captchaResult`中的`gt`和`challenge`值保存下来，自行使用 [手动验证器](https://kuresaru.github.io/geetest-validator/) 进行人机验证，并获得`validate`和`seccode`。然后使用账号密码进行登录即可：\n\n```go\nresult, err := client.LoginWithPassword(bilibili.LoginWithPasswordParam{\n    Username:  userName,\n    Password:  password,\n    Token:     captchaResult.Token,\n    Challenge: captchaResult.Geetest.Challenge,\n    Validate:  validate,\n    Seccode:   seccode,\n})\nif err == nil \u0026\u0026 result.Status == 0 {\n    log.Println(\"登录成功\")\n}\n```\n\n#### 方法三：使用短信验证码登录（不推荐）\n\n首先用上述方法二相同的方式获取人机验证参数并进行人机验证。然后获取国际地区代码：\n\n```go\ncountryCrownResult, _ := client.GetCountryCrown()\n```\n\n当然，如果你已经确定`cid`的值，这一步可以跳过。中国大陆的`cid`就是`86`。\n\n然后发送短信验证码：*（[这个接口大概率返回86103错误](https://github.com/SocialSisterYi/bilibili-API-collect/issues/756)）*\n\n```go\nsendSMSResult, _ := client.SendSMS(bilibili.SendSMSParam{\n    Cid:       cid,\n    Tel:       tel,\n    Source:    \"main_web\",\n    Token:     captchaResult.Token,\n    Challenge: captchaResult.Geetest.Challenge,\n    Validate:  validate,\n    Seccode:   seccode,\n})\n```\n\n然后就可以使用手机验证码登录了：\n\n```go\nresult, err := client.LoginWithSMS(bilibili.LoginWithSMSParam{\n    Cid:        cid,\n    Tel:        tel,\n    Code:       123456, // 短信验证码\n    Source:     \"main_web\",\n    CaptchaKey: sendSMSResult.CaptchaKey,\n})\nif err == nil \u0026\u0026 result.Status == 0 {\n    log.Println(\"登录成功\")\n}\n```\n\n### 储存Cookies\n\n使用上述任意方式登录成功后，Cookies值就已经设置好了。你可以保存Cookies值方便下次启动程序时不需要重新登录。\n\n```go\n// 获取cookiesString，自行存储，方便下次启动程序时不需要重新登录\ncookiesString := client.GetCookiesString()\n\n// 下次启动时，把存储的cookiesString设置进来，就不需要登录操作了\nclient.SetCookiesString(cookiesString)\n\n// 如果你是从浏览器request的header中直接复制出来的cookies，则改为调用SetRawCookies\nclient.SetRawCookies(\"cookie1=xxx; cookie2=xxx\")\n```\n\n\u003e [!NOTE]\n\u003e - `GetCookiesString`和`SetCookiesString`使用的字符串是`\"cookie1=xxx; expires=xxx; domain=xxx.com; path=/\\ncookie2=xxx; expires=xxx; domain=xxx.com; path=/\"`，包含过期时间、domain等一些其它信息，以`\"\\n\"`分隔多个cookie\n\u003e - `SetRawCookies`使用的字符串是`\"cookie1=xxx; cookie2=xxx\"`，只包含key=value，以`\"; \"`分隔多个cookie，这和在浏览器F12里复制的一样\n\u003e\n\u003e 请注意不要混用。\n\n### 其它接口\n\n你可以很方便的调用其它接口，以下举个例子：\n\n```go\nvideoInfo, err := client.GetVideoInfo(bilibili.VideoParam{\n    Aid: 12345678,\n})\n```\n\n参数中非必填字段你可以不填（可以通过是否有`omitempty`来判断这个字段是否为非必填字段）。\n\n方法都是按照对应功能的英文翻译命名的，因此你可以方便地使用IDE找到想要的方法，配合注释便能够知道如何使用。\n\n### 对B站返回的错误码进行处理\n\n因为B站的返回内容是这样的格式：\n\n```json\n{\n   \"code\": 0,\n   \"message\": \"错误信息\",\n   \"data\": {}\n}\n```\n\n而我们这个库的接口只会返回`data`数据和一个`error`，若`code`为`0`则`error`为`nil`，否则我们并不会把`code`和`message`字段直接返回。\n\n在一般情况下，调用者不太需要关心`code`和`message`字段，只需要关心是否有`error`即可。\n但如果你实在需要`code`和`message`字段，我们也提供了一个办法：\n\n```go\nvideoInfo, err := client.GetVideoInfo(bilibili.VideoParam{\n    Aid: 12345678,\n})\nif err != nil {\n    var e bilibili.Error\n    if errors.As(err, \u0026e) { // B站返回的错误\n        log.Printf(\"错误码: %d, 错误信息: %s\", e.Code, e.Message)\n    } else { // 其它错误\n        log.Printf(\"%+v\", err)\n    }\n}\n```\n\n\u003e [!TIP]\n\u003e 我们的所有`error`都包含堆栈信息。如有需要，你可以用`log.Printf(\"%+v\", err)`打印出堆栈信息，方便追踪错误。\n\n### 可能用到的工具接口\n\n```go\n// 解析短连接\ntyp, id, err := client.UnwrapShortUrl(\"https://b23.tv/xxxxxx\")\n\n// 获取服务器当前时间\nnow, err := client.Now()\n\n// av号转bv号\nbvid := bilibili.AvToBv(111298867365120)\n\n// bv号转av号\naid := bilibili.BvToAv(\"BV1L9Uoa9EUx\")\n\n// 通过ip确定地理位置\nzoneLocation, err := client.GetZoneLocation()\n\n// 获取分区当日投稿稿件数\nregionDailyCount, err := client.GetRegionDailyCount()\n```\n\n### 设置*resty.Client的一些参数\n\n调用`client.Resty()`就可以获取到`*resty.Client`，然后自行操作即可。**但是不要做一些离谱的操作**~~（比如把Cookies删了）~~\n\n```go\nclient.Resty().SetTimeout(20 * time.Second) // 设置超时时间\nclient.Resty().SetLogger(logger) // 自定义logger\n```\n\n## Star History\n\n\u003ca href=\"https://star-history.com/#CuteReimu/bilibili\u0026Date\"\u003e\n \u003cpicture\u003e\n   \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://api.star-history.com/svg?repos=CuteReimu/bilibili\u0026type=Date\u0026theme=dark\" /\u003e\n   \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://api.star-history.com/svg?repos=CuteReimu/bilibili\u0026type=Date\" /\u003e\n   \u003cimg alt=\"Star History Chart\" src=\"https://api.star-history.com/svg?repos=CuteReimu/bilibili\u0026type=Date\" /\u003e\n \u003c/picture\u003e\n\u003c/a\u003e\n\n## 如何为仓库做贡献？\n\n不知道在哪些方面可以做贡献？[点击这里看看吧！](https://github.com/CuteReimu/bilibili/contribute)\n\n命名规范和编码风格请参考[CONTRIBUTING.md](.github/CONTRIBUTING.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCuteReimu%2Fbilibili","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCuteReimu%2Fbilibili","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCuteReimu%2Fbilibili/lists"}