bilibili 接口的 node 包装库,快速迭代中,不保证接口稳定性,部分接口参数和返回值参考接口说明。
支持 WebQrcode,TvQrcode 登录方式,完整支持视频的上传与下载功能,并支持监听进度以及暂停取消等操作。
示例可参考 example 更新记录参考 CHANGELOG
npm install @renmu/bili-api
需要 node>=18
import { Client } from "@renmu/bili-api";
const client = new Client();
const data = await client.live.getMasterInfo(3927637, false);
console.log(data);
如何登录参考下面的登录文档
import { Client } from "@renmu/bili-api";
// 获取登录用户信息
async function getMyInfo() {
const client = new Client();
await client.loadCookieFile("cookies.json");
const data = await client.user.getMyInfo();
console.log(data);
}
你也可用单独导出对应类
import { User } from "@renmu/bili-api";
const getMyInfo2 = async () => {
const auth = new Auth();
await auth.loadCookieFile("cookies.json");
const user = new User(auth);
const res = await user.getMyInfo();
console.log(res);
};
- biliLive-tools B 站录播一站式工具
- bili-cli b 站命令行工具
绝大部分接口都是 web api,只需要 cookie,目前只有上传稿件且使用client
提交接口需要accessToken
import {
Client,
TvQrcodeLogin,
WebVideoUploader,
Common,
Reply,
Video,
User,
Platform,
Search,
Live,
Auth,
} from "@renmu/bili-api";
// 默认无需登录的接口是不使用cookie,部分接口在登录与未登录态下返回参数不同,也在调用时单独传入是否使用cookie参数
const client = new Client(false);
// 加载cookie,cookie如何获取参考登录,目前仅支持tv平台扫码登录
await client.loadCookieFile("cookies.json");
// 也可以手动设置cookie和accessToken,cookie可在web抓包获取
client.setAuth(
{
bili_jct: "1111",
SESSDATA: "111",
},
"1111"
);
import { TvQrcodeLogin, WebQrcodeLogin } from "@renmu/bili-api";
// 也可以使用WebQrcodeLogin使用web端的扫码登录
const tv = new TvQrcodeLogin();
const url = await tv.login();
console.log(url);
// 找个二维码工具将 url 转换为二维码,使用bilibili app扫码登录
// 扫码完成会触发
tv.on("completed", res => {
console.log("completed", res);
// 如果是tv端,可以直接保存后调用loadCookieFile函数
fs.writeFileSync("cookie.json", JSON.stringify(res.data));
// 以下为web端扫码登录返回的参数,可以尝试使用setAuth进行登录
// {
// DedeUserID: '111',
// DedeUserID__ckMd5: '111',
// Expires: '1718116416',
// SESSDATA: '111',
// bili_jct: '1111',
// gourl: 'https://www.bilibili.com',
// refresh_token: '1111'
// }
});
// 失败会触发比如超时
tv.on("error", res => {
console.log("err", res);
});
// 扫描中会触发
tv.on("scan", res => {
console.log("scan", res);
});
// 完成和失败后都会触发,返回原始参数
tv.on("end", res => {
console.log("end", res);
});
// 可用于中断任务,并清除所有监听器
// tv.interrupt();
import { utils } from "@renmu/bili-api";
// fakeBuvid3, // 生成模拟buivd3
// fakeDmCoverImgStr, // 生成模拟dmcoverimgstr
// protobufDecode, // prorobuf弹幕解析为json
// protoBufToXml, // prorobuf弹幕解析为兼容xml
// 用于wbi接口签名, 返回的参数直接用于url拼接
const query = await utils.WbiSign({
test: "111",
});
登录:必要
client.user.getMyInfo();
client.user.getUserInfo(uid)
client.user.space(uid)
client.user.getVideos({mid: uid})
client.user.getCollectionList()
client.user.getSeriesInfo()
client.user.getSeriesVideos()
client.user.getSeasons()
client.live.getRoomInfo(room_id)
client.live.getMasterInfo(uid)
client.live.getGuardTopList({user_id:1,room_id:1,page:1,page_size:20})
登录:必要
使用client
提交接口需要accessToken
分区可以通过 client.platform.getArchivePre()
获取
api.submit 支持"web" | "client" | "b-cut"
三种参数,各有优劣
const res = await client.platform.onUploadMedia(["test.mp4"], {
title: "测试",
tid: 138,
tag: "测试",
});
或者
const task = await client.platform.addMedia(["test.mp4"], {
title: "测试",
tid: 138,
tag: "测试",
});
task.emitter.on("completed", res => {
console.log("completed upload", res);
});
task.emitter.on("progress", res => {
console.log("progress", res);
});
// 暂停任务
task.pause();
// 继续任务
task.start();
// 取消任务
task.cancel();
使用client
提交接口需要accessToken
分区可以通过 client.platform.getArchivePre()
获取
const client = new Client();
await client.loadCookieFile("cookies.json");
const task = await client.platform.editMedia(aid, ["test.mp4"], {}, mode);
// 其余功能参考新投稿
如果你想动态添加分 p,可以尝试使用WebVideoUploader
来单独处理视频的上传,并调用addMediaClientApi
,addMediaWebApi
,addMediaBCutApi
来进行提交。或调用editMediaClientApi
,editMediaWebApi
来进行编辑
bvid
和aid
任选一个传入
client.platorm.getArchive({
bvid,
aid,
});
client.platorm.getArchives(...)
client.platorm.checkTag(text)
client.platform.uploadCover(filePath)
client.platform.getUploadTemplateList()
client.platform.editUploadTemplate(tid)
client.platform.getRecommendTags()
client.platform.getTopic()
client.platform.searchTopic()
client.platform.getSeasonList()
client.platform.addMedia2Season()
client.platform.getSessionId()
client.platform.editMediaSeason()
client.platform.getSeasonDetail()
client.platform.getSectionDetail()
client.platform.getArchivePre()
client.platform.getTypeDesc()
client.search.all({keyword:"测试"})
具体参数见类型文件
client.search.type({keyword:"测试",search_type:"video"})
视频详情页相关的接口,可以通过以下两种方式创建实例。 如果设置了 aid,那么以下所有接口的 aid 都可以无需再传,你也可以传递 aid 进行覆盖。
const video = client.video;
// const video = client.newVideo(aid)
//
// video.setAid(aid);
video.like({
like: true,
});
video.getInfo()
video.playurl()
下载需要传递ffmpeg
地址,用于合并视频与音频,如果不传,默认使用环境变量。
如果你在 electron 中使用,可能需要修复path
const download = async () => {
const client = new Client();
await client.loadCookieFile("cookies.json");
const task = await client.video.download({
bvid: "BV1wc41127vK",
output: "test.mp4",
});
setTimeout(() => {
task.pause();
}, 3000);
setTimeout(() => {
task.start();
}, 5000);
task.on("progress", p => {
console.log(p);
// p.event = "download"|"merge-start"|"merge-end"
// 只有`p.event`=`download`才有`progress`相关参数,注意判断
// if (p.event === "download") {
// const percentage = Math.floor(p.progress.progress * 100);
// }
});
// console.log(res);
};
import { Client, utils } from "@renmu/bili-api";
const convertDm = async () => {
const client = new Client();
const data = await client.video.getAllDm({
aid: 114108254919086,
cid: 28699528643,
});
const xmlContent = await utils.protoBufToXml(data);
fs.writeFileSync("test.xml", xmlContent);
};
video.setAid(aid)
video.like
video.coin()
video.listFavoriteBox()
video.editFavoriteBox()
video.addShare()
video.likeCoinShare()
const reply = client.video.createReply(rpid)
其他可用接口见评论接口
const reply = client.reply;
// 这样设置后不用每次传递oid与type
// const reply = client.newReply(oid,type)
// reply.setOid(oid);
// reply.setType(type);
reply.list()
reply.count()
reply.add()
reply.like()
reply.hate()
reply.delete()
reply.top()
reply.report()
client.common.getAreas()