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

@ -36,6 +36,14 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool {
// let result = await this.doAcrcloudRecognitionUsingMetaprocAPI(searchQuery); // let result = await this.doAcrcloudRecognitionUsingMetaprocAPI(searchQuery);
// parse query // parse query
const [videoAid, pid, targetSec] = query.split(","); const [videoAid, pid, targetSec] = query.split(",");
// check if arguments are valid
// is videoAid a string of numbers?
if (!/^\d+$/.test(videoAid)) {
throw new Error(
"Invalid videoAid: It should be a string of numbers. If a BVid or a short link is given, please convert it to Aid using av{BVid} format using BiliVideoInfo tool.",
);
}
const result = await this.doAcrcloudRecognitionUsingMetaprocAPI( const result = await this.doAcrcloudRecognitionUsingMetaprocAPI(
videoAid, videoAid,
parseInt(pid), parseInt(pid),
@ -92,6 +100,5 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool {
return response; return response;
} }
description = `A tool that recognizes music in Bilibili videos using ACRCloud API. Input string is in this format: video_aid,pid,target_sec. video_aid is extracted from av{video_aid}, e.g. av170001 means video_aid=170001. pid is the page ID of the video(note: pid starts from 1, not 0). and target_sec is the time in seconds where the recognition is expected to start from. description = `A tool that recognizes music in Bilibili videos using ACRCloud API. Input string is in this format: video_aid,pid,target_sec. video_aid is extracted from av{video_aid}, e.g. av170001 means video_aid=170001. pid is the page ID of the video(note: pid starts from 1, not 0). and target_sec is the time in seconds where the recognition is expected to start from. To recognize music that begins with video, use timestamp 0 or 1, which is the most useful case.`;
Trick: If you only have a BVID of the video, you can convert it to video_aid by triggering the video info API and extract the video_aid from the response, before using this tool.`;
} }

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(); const headers = new Headers();
headers.append("User-Agent", getRandomUserAgent()); headers.append("User-Agent", getRandomUserAgent());
let video_param = ""; 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")) { if (prompt.toLowerCase().startsWith("av")) {
video_param = `aid=${prompt.slice(2)}`; video_param = `aid=${prompt.slice(2)}`;
} else if (prompt.toLowerCase().startsWith("bv")) { } else if (prompt.toLowerCase().startsWith("bv")) {
video_param = `bvid=${prompt}`; 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 { } else {
return "FAIL: Invalid video ID or URL."; 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(); let rawData: { [key: string]: any } = await resp.json();
console.log("response for", prompt, "is", rawData);
let data: { [key: string]: string } = {}; let data: { [key: string]: string } = {};
// Keep those: bvid, aid, videos, copyright, tname, title, pubdate, desc, state(values see below), owner, argue_info // 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. 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).`;
} }

631
yarn.lock

File diff suppressed because it is too large Load Diff