B站视频基本信息获取,支持分享图生成

This commit is contained in:
目棃
2024-12-16 16:08:14 +08:00
parent 582d2cffb8
commit c6a9548a43
13 changed files with 195 additions and 148 deletions

View File

@@ -1,19 +1,24 @@
/**
* @file plugins/Bili/request/getNav.ts
* @description Bili 插件导航请求文件
* @since Beta v0.5.0
* @since Beta v0.5.7
*/
import headerBili from "@Bili/utils/getHeader.js";
import TGHttp from "@/utils/TGHttp.js";
/**
* @description Bili 插件导航请求
* @since Beta v0.5.0
* @return {Promise<TGApp.Plugins.Bili.Nav.NavData>} Bili 插件导航请求返回
* @since Beta v0.5.7
* @return {Promise<TGApp.Plugins.Bili.Nav.Data>} Bili 插件导航请求返回
*/
async function getNav(): Promise<TGApp.Plugins.Bili.Nav.NavData> {
async function getNav(): Promise<TGApp.Plugins.Bili.Nav.Data> {
const url = "https://api.bilibili.com/x/web-interface/nav";
const resp = await TGHttp<TGApp.Plugins.Bili.Nav.NavResponse>(url, { method: "GET" });
const resp = await TGHttp<TGApp.Plugins.Bili.Nav.Response>(url, {
method: "GET",
headers: headerBili,
});
return resp.data;
}

View File

@@ -4,6 +4,7 @@
* @since Beta v0.5.0
*/
import headerBili from "@Bili/utils/getHeader.js";
import getWrid from "@Bili/utils/getWrid.js";
import TGHttp from "@/utils/TGHttp.js";
@@ -18,22 +19,13 @@ import TGHttp from "@/utils/TGHttp.js";
*/
async function getVideoUrl(cid: number, bvid: string): Promise<TGApp.Plugins.Bili.Video.UrlData> {
const url = "https://api.bilibili.com/x/player/playurl";
let params: Record<string, string> = {
bvid,
cid: cid.toString(),
fnval: "16",
platform: "pc",
};
let params: Record<string, string> = { bvid, cid: cid.toString(), fnval: "16", platform: "pc" };
const wridRes = await getWrid(params);
params = {
...params,
wts: wridRes[0],
wrid: wridRes[1],
};
params = { ...params, wts: wridRes[0], w_rid: wridRes[1] };
const resp = await TGHttp<TGApp.Plugins.Bili.Video.UrlResponse>(url, {
method: "GET",
query: params,
headers: { referer: "https://www.bilibili.com/" },
headers: headerBili,
});
return resp.data;
}

View File

@@ -4,7 +4,11 @@
* @since Beta v0.5.0
*/
import headerBili from "@Bili/utils/getHeader.js";
import getWrid from "@Bili/utils/getWrid.js";
import TGHttp from "@/utils/TGHttp.js";
import TGLogger from "@/utils/TGLogger.js";
/**
* @description 获取视频基本信息
@@ -18,26 +22,24 @@ async function getVideoView(
bvid?: string,
): Promise<TGApp.Plugins.Bili.Video.ViewData> {
const url = "https://api.bilibili.com/x/web-interface/wbi/view";
const params: Record<string, string | number | boolean> = {
need_view: 1,
isGaiaAoided: true,
};
if (aid) {
params.aid = aid;
} else if (bvid) {
params.bvid = bvid;
} else {
throw new Error("参数错误");
let params: Record<string, string | number | boolean> = { need_view: 1, isGaiaAvoided: true };
if (aid) params.aid = aid;
if (bvid) params.bvid = bvid;
if (!aid && !bvid) throw new Error("aid和bVid不能同时为空");
const wrid = await getWrid(params);
params = { ...params, wts: wrid[0], w_rid: wrid[1] };
try {
const resp = await TGHttp<TGApp.Plugins.Bili.Video.ViewResponse>(url, {
method: "GET",
query: params,
headers: headerBili,
});
return resp.data;
} catch (error) {
if (error instanceof Error) await TGLogger.Error(`获取视频基本信息失败: ${error.message}`);
else await TGLogger.Error(`获取视频基本信息失败: ${error}`);
}
const resp = await TGHttp<TGApp.Plugins.Bili.Video.ViewResponse>(url, {
method: "GET",
query: params,
}).catch((err) => {
console.error(err);
return err;
});
console.warn(resp.data);
return resp.data;
throw new Error("获取视频基本信息失败");
}
export default getVideoView;

View File

@@ -4,12 +4,6 @@
* @since Beta v0.4.0
*/
/**
* @description Bili 插件基础类型
* @since Beta v0.4.0
* @namespace Base
* @memberof TGApp.Plugins.Bili
*/
declare namespace TGApp.Plugins.Bili.Base {
/**
* @description Bili Response 统一接口
@@ -21,10 +15,5 @@ declare namespace TGApp.Plugins.Bili.Base {
* @property {any} data 数据
* @return Response
*/
interface Response {
code: number;
message: string;
ttl: number;
data: any;
}
type Response = { code: number; message: string; ttl: number; data: unknown };
}

View File

@@ -1,42 +1,29 @@
/**
* @file plugins/Bili/types/Nav.d.ts
* @description Bili 插件导航类型定义文件
* @since Beta v0.4.0
* @since Beta v0.5.7
*/
/**
* @description Bili 插件导航类型
* @since Beta v0.4.0
* @namespace Nav
* @memberof TGApp.Plugins.Bili
*/
declare namespace TGApp.Plugins.Bili.Nav {
/**
* @description Bili 导航基本信息返回
* @since Beta v0.4.0
* @interface NavResponse
* @since Beta v0.5.7
* @interface Response
* @extends {TGApp.Plugins.Bili.Base.Response}
* @property {NavData} data 导航基本信息
* @property {Data} data 导航基本信息
* @return NavResponse
*/
interface NavResponse extends TGApp.Plugins.Bili.Base.Response {
data: NavData;
}
type Response = TGApp.Plugins.Bili.Base.Response & { data: Data };
/**
* @description Bili 导航基本信息
* @since Beta v0.4.0
* @interface NavData
* @since Beta v0.5.7
* @interface Data
* @see https://api.bilibili.com/x/web-interface/nav
* @desc 只写了用到的部分
* @property {string} wbi_img.img_url 网站图标
* @property {string} wbi_img.sub_url 网站图标(小)
* @return NavData
* @return Data
*/
interface NavData {
wbi_img: {
img_url: string;
sub_url: string;
};
}
type Data = { wbi_img: { img_url: string; sub_url: string } };
}

View File

@@ -187,7 +187,7 @@ declare namespace TGApp.Plugins.Bili.Video {
* @property {string} seek_type 视频跳转类型
* @property {UrlDash} dash 视频播放地址
* @property {UrlDurl[]} durl 视频播放地址
* @property {UrlFormats} support_formats 视频支持格式
* @property {UrlFormat[]} support_formats 视频支持格式
* @property {unknown} high_format 视频高清格式
* @property {number} last_play_time 视频上次播放时间
* @property {number} last_play_cid 视频上次播放分P号
@@ -208,7 +208,7 @@ declare namespace TGApp.Plugins.Bili.Video {
seek_type: string;
dash: UrlDash; // dash 返回
durl: UrlDurl[]; // mp4 返回
support_formats: UrlFormats;
support_formats: UrlFormat[];
high_format: unknown;
last_play_time: number;
last_play_cid: number;
@@ -331,7 +331,7 @@ declare namespace TGApp.Plugins.Bili.Video {
* @property {unknown} codecs 视频编码
* @return UrlFormats
*/
interface UrlFormats {
interface UrlFormat {
quality: number;
format: string;
new_description: string;

View File

@@ -0,0 +1,13 @@
/**
* @file plugins/Bili/utils/getHeader.ts
* @description 获取请求头
* @since Beta v0.5.7
*/
const headerBili = {
cookie: "",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
origin: "https://www.bilibili.com",
};
export default headerBili;

View File

@@ -43,13 +43,12 @@ async function getMixinKey(): Promise<string> {
* @param {Record<string,string|number>} params 请求参数
* @returns {Promise<[string|string]>} wrid
*/
async function getWrid(params: Record<string, string | number>): Promise<[string, string]> {
async function getWrid(
params: Record<string, string | number | boolean>,
): Promise<[string, string]> {
const mixin_key = await getMixinKey();
const wts = Math.floor(Date.now() / 1000);
const obj: Record<string, string | number> = {
...params,
wts,
};
const obj: Record<string, string | number> = { ...params, wts };
const keys = Object.keys(obj).sort();
let md5Str = "";
for (let i = 0; i < keys.length; i++) {