diff --git a/public/source/UI/video_play_bili.png b/public/source/UI/video_play_bili.png new file mode 100644 index 00000000..bdba73cb Binary files /dev/null and b/public/source/UI/video_play_bili.png differ diff --git a/src-tauri/capabilities/SubWindow.json b/src-tauri/capabilities/SubWindow.json index e4fa1bfe..0092a4dd 100644 --- a/src-tauri/capabilities/SubWindow.json +++ b/src-tauri/capabilities/SubWindow.json @@ -35,31 +35,59 @@ "core:window:default", { "identifier": "fs:allow-exists", - "allow": [{ "path": "**" }] + "allow": [ + { + "path": "**" + } + ] }, { "identifier": "fs:allow-mkdir", - "allow": [{ "path": "**" }] + "allow": [ + { + "path": "**" + } + ] }, { "identifier": "fs:allow-read-dir", - "allow": [{ "path": "**" }] + "allow": [ + { + "path": "**" + } + ] }, { "identifier": "fs:allow-read-text-file", - "allow": [{ "path": "**" }] + "allow": [ + { + "path": "**" + } + ] }, { "identifier": "fs:allow-remove", - "allow": [{ "path": "**" }] + "allow": [ + { + "path": "**" + } + ] }, { "identifier": "fs:allow-write-file", - "allow": [{ "path": "**" }] + "allow": [ + { + "path": "**" + } + ] }, { "identifier": "fs:allow-write-text-file", - "allow": [{ "path": "**" }] + "allow": [ + { + "path": "**" + } + ] }, { "identifier": "http:default", @@ -76,6 +104,9 @@ { "url": "https://*.bilibili.com/*" }, + { + "url": "http://*.hdslb.com/*" + }, { "url": "https://*.hoyoverse.com/*" }, diff --git a/src-tauri/gen/schemas/capabilities.json b/src-tauri/gen/schemas/capabilities.json index 49b4c401..98c66ff0 100644 --- a/src-tauri/gen/schemas/capabilities.json +++ b/src-tauri/gen/schemas/capabilities.json @@ -124,6 +124,7 @@ { "url": "https://*.mihoyo.com/*" }, { "url": "https://*.mihoyogift.com/*" }, { "url": "https://*.bilibili.com/*" }, + { "url": "http://*.hdslb.com/*" }, { "url": "https://*.hoyoverse.com/*" }, { "url": "https://*.genshinnet.com/*" } ] diff --git a/src/components/viewPost/tp-video.vue b/src/components/viewPost/tp-video.vue index db5953b7..2579a565 100644 --- a/src/components/viewPost/tp-video.vue +++ b/src/components/viewPost/tp-video.vue @@ -1,6 +1,5 @@ - diff --git a/src/plugins/Bili/request/getNav.ts b/src/plugins/Bili/request/getNav.ts index 3db64b40..3d594e19 100644 --- a/src/plugins/Bili/request/getNav.ts +++ b/src/plugins/Bili/request/getNav.ts @@ -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} Bili 插件导航请求返回 + * @since Beta v0.5.7 + * @return {Promise} Bili 插件导航请求返回 */ -async function getNav(): Promise { +async function getNav(): Promise { const url = "https://api.bilibili.com/x/web-interface/nav"; - const resp = await TGHttp(url, { method: "GET" }); + const resp = await TGHttp(url, { + method: "GET", + headers: headerBili, + }); return resp.data; } diff --git a/src/plugins/Bili/request/getVideoUrl.ts b/src/plugins/Bili/request/getVideoUrl.ts index ca32207d..4d08ca55 100644 --- a/src/plugins/Bili/request/getVideoUrl.ts +++ b/src/plugins/Bili/request/getVideoUrl.ts @@ -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 { const url = "https://api.bilibili.com/x/player/playurl"; - let params: Record = { - bvid, - cid: cid.toString(), - fnval: "16", - platform: "pc", - }; + let params: Record = { 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(url, { method: "GET", query: params, - headers: { referer: "https://www.bilibili.com/" }, + headers: headerBili, }); return resp.data; } diff --git a/src/plugins/Bili/request/getVideoView.ts b/src/plugins/Bili/request/getVideoView.ts index 17c6fec3..7f4b390a 100644 --- a/src/plugins/Bili/request/getVideoView.ts +++ b/src/plugins/Bili/request/getVideoView.ts @@ -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 { const url = "https://api.bilibili.com/x/web-interface/wbi/view"; - const params: Record = { - need_view: 1, - isGaiaAoided: true, - }; - if (aid) { - params.aid = aid; - } else if (bvid) { - params.bvid = bvid; - } else { - throw new Error("参数错误"); + let params: Record = { 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(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(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; diff --git a/src/plugins/Bili/types/Base.d.ts b/src/plugins/Bili/types/Base.d.ts index dc9a3f1a..70065d70 100644 --- a/src/plugins/Bili/types/Base.d.ts +++ b/src/plugins/Bili/types/Base.d.ts @@ -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 }; } diff --git a/src/plugins/Bili/types/Nav.d.ts b/src/plugins/Bili/types/Nav.d.ts index ac579e5c..d36c7ed5 100644 --- a/src/plugins/Bili/types/Nav.d.ts +++ b/src/plugins/Bili/types/Nav.d.ts @@ -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 } }; } diff --git a/src/plugins/Bili/types/Video.d.ts b/src/plugins/Bili/types/Video.d.ts index 49091b72..efdbae2e 100644 --- a/src/plugins/Bili/types/Video.d.ts +++ b/src/plugins/Bili/types/Video.d.ts @@ -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; diff --git a/src/plugins/Bili/utils/getHeader.ts b/src/plugins/Bili/utils/getHeader.ts new file mode 100644 index 00000000..ba212fa7 --- /dev/null +++ b/src/plugins/Bili/utils/getHeader.ts @@ -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; diff --git a/src/plugins/Bili/utils/getWrid.ts b/src/plugins/Bili/utils/getWrid.ts index f2a5aa6b..6806e53a 100644 --- a/src/plugins/Bili/utils/getWrid.ts +++ b/src/plugins/Bili/utils/getWrid.ts @@ -43,13 +43,12 @@ async function getMixinKey(): Promise { * @param {Record} params 请求参数 * @returns {Promise<[string|string]>} wrid */ -async function getWrid(params: Record): Promise<[string, string]> { +async function getWrid( + params: Record, +): Promise<[string, string]> { const mixin_key = await getMixinKey(); const wts = Math.floor(Date.now() / 1000); - const obj: Record = { - ...params, - wts, - }; + const obj: Record = { ...params, wts }; const keys = Object.keys(obj).sort(); let md5Str = ""; for (let i = 0; i < keys.length; i++) { diff --git a/src/utils/toolFunc.ts b/src/utils/toolFunc.ts index 68794ea1..92f2acb8 100644 --- a/src/utils/toolFunc.ts +++ b/src/utils/toolFunc.ts @@ -232,3 +232,20 @@ export function getZhElement(element: string): string { return "未知"; } } + +/** + * @description 获取视频时长 + * @since Beta v0.5.7 + * @param {number} duration - 视频时长(秒) + * @returns {string} 视频时长 + */ +export function getVideoDuration(duration: number): string { + const seconds = duration % 60; + const minutes = Math.floor(duration / 60) % 60; + const hours = Math.floor(duration / 3600); + let result = ""; + if (hours > 0) result += `${hours.toString().padStart(2, "0")}:`; + result += `${minutes.toString().padStart(2, "0")}:`; + result += `${seconds.toString().padStart(2, "0")}`; + return result; +}