mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-25 15:10:17 +09:00
add support for b23.tv links
This commit is contained in:
parent
aa1035d889
commit
c77c14c69a
@ -36,6 +36,14 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool {
|
||||
// let result = await this.doAcrcloudRecognitionUsingMetaprocAPI(searchQuery);
|
||||
// parse query
|
||||
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(
|
||||
videoAid,
|
||||
parseInt(pid),
|
||||
@ -92,6 +100,5 @@ export class BilibiliMusicRecognitionTool extends Tool implements RequestTool {
|
||||
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.
|
||||
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.`;
|
||||
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.`;
|
||||
}
|
||||
|
@ -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).`;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user