mirror of
https://github.com/lyz05/danmaku.git
synced 2026-02-02 17:59:53 +08:00
108 lines
3.6 KiB
JavaScript
108 lines
3.6 KiB
JavaScript
import urlmodule from "url";
|
||
import axios from "axios";
|
||
import BaseSource from "./base.mjs";
|
||
|
||
export default class BilibiliSource extends BaseSource {
|
||
constructor() {
|
||
super();
|
||
this.name = "B站";
|
||
this.domain = "bilibili.com";
|
||
this.example_urls = [
|
||
"https://www.bilibili.com/video/av170001",
|
||
"https://www.bilibili.com/video/av170001?p=2",
|
||
"https://www.bilibili.com/video/BV17x411w7KC?p=3",
|
||
"https://www.bilibili.com/bangumi/play/ep691614",
|
||
"https://www.bilibili.com/bangumi/play/ep2636828"
|
||
];
|
||
}
|
||
|
||
async resolve(url) {
|
||
// 相关API
|
||
const api_video_info = "https://api.bilibili.com/x/web-interface/view";
|
||
const api_epid_cid = "https://api.bilibili.com/pgc/view/web/season";
|
||
const q = urlmodule.parse(url, true);
|
||
const path = q.pathname.split("/");
|
||
// 普通投稿视频
|
||
if (url.indexOf("video/") !== -1) {
|
||
// 获取视频分P信息
|
||
const p = q.query.p || 1;
|
||
// 判断是否为旧版av号
|
||
let params;
|
||
if (url.indexOf("BV") !== -1) {
|
||
params = {"bvid": path[2]};
|
||
} else {
|
||
params = {"aid": path[2].substring(2)};
|
||
}
|
||
const response = await axios.get(api_video_info, {params});
|
||
if (response.data.code !== 0) {
|
||
this.error_msg = "获取普通投稿视频信息失败!"+response.data.message;
|
||
return;
|
||
}
|
||
this.title = response.data.data.title;
|
||
const subtitle = response.data.data.pages[p - 1].part;
|
||
this.title = this.title + "-" + subtitle;
|
||
const cid = response.data.data.pages[p - 1].cid;
|
||
return [`https://comment.bilibili.com/${cid}.xml`];
|
||
} // 番剧
|
||
else if (url.indexOf("bangumi/") !== -1 && url.indexOf("ep") !== -1) {
|
||
const epid = path.slice(-1)[0];
|
||
const params = {"ep_id": epid.slice(2)};
|
||
const response = await axios.get(api_epid_cid, {params});
|
||
if (response.data.code !== 0) {
|
||
this.error_msg = "获取番剧视频信息失败!";
|
||
return;
|
||
}
|
||
// 正片内容
|
||
for (let i = 0; i < response.data.result.episodes.length; i++) {
|
||
if (response.data.result.episodes[i].id == params.ep_id) {
|
||
this.title = response.data.result.episodes[i].share_copy;
|
||
const cid = response.data.result.episodes[i].cid;
|
||
return [`https://comment.bilibili.com/${cid}.xml`];
|
||
}
|
||
}
|
||
// 花絮、PV、番外等非正片内容
|
||
if (Array.isArray(response.data.result.section)) {
|
||
for (let j = 0; j < response.data.result.section.length; j++) {
|
||
for (let i = 0; i < response.data.result.section[j].episodes.length; i++) {
|
||
if (response.data.result.section[j].episodes[i].id == params.ep_id) {
|
||
this.title = response.data.result.section[j].episodes[i].share_copy;
|
||
const cid = response.data.result.section[j].episodes[i].cid;
|
||
return [`https://comment.bilibili.com/${cid}.xml`];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
this.error_msg = "未找到对应的番剧视频信息!";
|
||
} else {
|
||
this.error_msg = "不支持的B站视频网址,仅支持普通视频(av,bv)、剧集视频(ep)";
|
||
}
|
||
|
||
};
|
||
|
||
async work(url) {
|
||
const urls = await this.resolve(url);
|
||
if (!this.error_msg) {
|
||
this.url = urls[0];
|
||
}
|
||
return {
|
||
title: this.title,
|
||
url: this.url,
|
||
msg: this.error_msg? this.error_msg: "ok"
|
||
};
|
||
};
|
||
}
|
||
|
||
// module.exports = Bilibili;
|
||
|
||
// if(!module.parent) {
|
||
// const b = new Bilibili();
|
||
// b.work(b.example_urls[0]).then(() => {
|
||
// console.log(b.content);
|
||
// console.log(b.title);
|
||
// });
|
||
// }
|
||
// 判断是否直接运行当前文件(跨平台)
|
||
// if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
||
// console.log("单独运行")
|
||
// }
|