add support for b23.tv links

This commit is contained in:
Sheng Fan
2024-04-08 18:15:26 +08:00
parent aa1035d889
commit c77c14c69a
3 changed files with 407 additions and 268 deletions

View File

@@ -41,14 +41,42 @@ export class BilibiliVideoInfoTool extends Tool implements RequestTool {
}
}
async fetchVideoInfo(prompt: string) {
async fetchVideoInfo(prompt: string): Promise<string> {
const headers = new Headers();
headers.append("User-Agent", getRandomUserAgent());
let video_param = "";
prompt = prompt
.trim()
.replaceAll(/https?:\/\//g, "")
.trim();
// is it bilibili.com video link?
// if so, extract the video ID and use it to fetch video info
if (prompt.startsWith("www.bilibili.com/video/")) {
// prompt = prompt.split("/")[2];
prompt = new URL("https://" + prompt).pathname.split("/")[2];
}
if (prompt.toLowerCase().startsWith("av")) {
video_param = `aid=${prompt.slice(2)}`;
} else if (prompt.toLowerCase().startsWith("bv")) {
video_param = `bvid=${prompt}`;
} else if (prompt.startsWith("b23.tv/")) {
// is it video id (b23.tv/avXXX or b23.tv/BVXXXXXXX)
const suffix = prompt.split("/")[1];
if (suffix.startsWith("av")) {
video_param = `aid=${suffix.slice(2)}`;
} else if (suffix.startsWith("BV")) {
video_param = `bvid=${suffix}`;
} else {
// short links need special handling
const resp = await this.fetchWithTimeout("https://" + prompt, {
redirect: "manual",
});
const location =
resp.headers != null ? resp.headers.get("Location") : resp.url;
if (location) return await this.fetchVideoInfo(location);
else return "FAIL: Unable to resolve b23.tv short link.";
}
} else {
return "FAIL: Invalid video ID or URL.";
}
@@ -60,6 +88,7 @@ export class BilibiliVideoInfoTool extends Tool implements RequestTool {
);
let rawData: { [key: string]: any } = await resp.json();
console.log("response for", prompt, "is", rawData);
let data: { [key: string]: string } = {};
// Keep those: bvid, aid, videos, copyright, tname, title, pubdate, desc, state(values see below), owner, argue_info
@@ -146,5 +175,5 @@ export class BilibiliVideoInfoTool extends Tool implements RequestTool {
}
description = `A tool that fetches video information from Bilibili. It returns a JSON string containing the video title, uploader, and other information.
Input string must be a Bilibili video ID (e.g. av170001, BV17x411w7KC).`;
Input string must be a Bilibili video ID (e.g. av170001, BV17x411w7KC) or a video link (long or short) on Bilibili (e.g. https://www.bilibili.com/video/av170001, https://b23.tv/BV17x411w7KC).`;
}