mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-15 13:33:43 +08:00
add support for b23.tv links
This commit is contained in:
@@ -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).`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user