From 168ed753319bc86f36e528fd0f94f730e3f5e14f Mon Sep 17 00:00:00 2001 From: BTMuli Date: Wed, 29 Mar 2023 00:57:14 +0800 Subject: [PATCH] =?UTF-8?q?feat(Mys):=20=E6=B7=BB=E5=8A=A0=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E5=8C=96=E5=86=85=E5=AE=B9=E6=8E=A5=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E6=98=8E=E5=A4=A9=E8=B5=B7=E6=9D=A5=E5=AE=8C=E5=96=84=E5=90=84?= =?UTF-8?q?=E7=A7=8D=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Home.vue | 4 +- src/pages/News.vue | 9 +-- src/plugins/Mys/interface/post.ts | 76 +++++++++++++++++++ .../Mys/utils/parser.ts} | 9 ++- 4 files changed, 87 insertions(+), 11 deletions(-) rename src/{utils/MysParse.ts => plugins/Mys/utils/parser.ts} (93%) diff --git a/src/pages/Home.vue b/src/pages/Home.vue index e970ed4c..dd5b03e8 100644 --- a/src/pages/Home.vue +++ b/src/pages/Home.vue @@ -64,7 +64,7 @@ import { fs, http } from "@tauri-apps/api"; import useAppStore from "../store/modules/app"; // utils import { createTGWindow } from "../utils/TGWindow"; -import { parseMys } from "../utils/MysParse"; +import { StructuredPostParser } from "../plugins/Mys/utils/parser"; // interface import { GachaResponse, @@ -180,7 +180,7 @@ async function toPost(post_id: string) { return res.data.post.post; }); // 结构化渲染 - const parseDoc = parseMys(post.structured_content); + const parseDoc = StructuredPostParser(post.structured_content); // 将解析后的 doc 保存到 文件 await fs.writeTextFile( `${appStore.dataPath.temp}\\${post_id}_home.html`, diff --git a/src/pages/News.vue b/src/pages/News.vue index dc4c4696..dc462b8e 100644 --- a/src/pages/News.vue +++ b/src/pages/News.vue @@ -134,14 +134,13 @@ import useAppStore from "../store/modules/app"; import "../tools/svg-inject.js"; // utils import { createTGWindow } from "../utils/TGWindow"; -import { parseMys } from "../utils/MysParse"; +import { StructuredPostParser } from "../plugins/Mys/utils/parser"; // interface import { Post, PostResponse, POST_FULL_API, POST_FULL_REFERER, - PostData, } from "../plugins/Mys/interface/post"; import { NewsResponse, @@ -216,7 +215,7 @@ async function toPost(post_id: string) { // 获取渲染模式 if (renderMode.value) { // 结构化渲染 - parseDoc = parseMys(post.structured_content); + parseDoc = StructuredPostParser(post.structured_content); } else { // 原始渲染 parseDoc = new DOMParser().parseFromString(post.content, "text/html"); @@ -231,12 +230,12 @@ async function toPost(post_id: string) { } async function toJson(post_id: string) { const post = await getPost(post_id).then(res => { - return res.data; + return res.data.post.post.structured_content; }); // 将 json 保存到 文件 await fs.writeTextFile( `${appStore.dataPath.temp}\\${post_id}.json`, - JSON.stringify(post, null, 4) + JSON.stringify(JSON.parse(post), null, 4) ); const logUrl = `file:\\\\\\${appStore.dataPath.temp}\\${post_id}.json`; // 打开窗口 diff --git a/src/plugins/Mys/interface/post.ts b/src/plugins/Mys/interface/post.ts index b5e45d29..0010eadd 100644 --- a/src/plugins/Mys/interface/post.ts +++ b/src/plugins/Mys/interface/post.ts @@ -247,3 +247,79 @@ export interface PostStat { bookmark_num: number; forward_num: number; } + +/** + * @description 帖子结构化内容 + * @since Alpha + * @interface PostStructuredContent + * @property {string|object} insert 插入内容 + * @property {string} insert.image 图片 URL + * @property {object} insert.vod 视频信息 + * @property {number} insert.vod.id 视频 ID + * @property {number} insert.vod.duration 时长 + * @property {string} insert.vod.cover 封面图 URL + * @property {object[]} insert.vod.resolutions 分辨率 + * @property {string} insert.vod.resolutions.url URL + * @property {string} insert.vod.resolutions.definition 清晰度 + * @property {number} insert.vod.resolutions.height 高度 + * @property {number} insert.vod.resolutions.width 宽度 + * @property {number} insert.vod.resolutions.bitrate 比特率 + * @property {number} insert.vod.resolutions.size 大小 + * @property {string} insert.vod.resolutions.format 格式 + * @property {string} insert.vod.resolutions.label 标签 + * @property {number} insert.vod.view_num 浏览数 + * @property {number} insert.vod.transcode_status 转码状态 + * @property {number} insert.vod.review_status 审核状态 + * @property {string} insert.backup_text 折叠文本 + * @property {object} insert.fold 折叠内容 + * @property {string} insert.fold.title 折叠标题,反序列化后为 PostStructuredContent[] + * @property {string} insert.fold.content 折叠文本,反序列化后为 PostStructuredContent[] + * @property {object} attributes 属性 + * @property {number} attributes.height 高度 + * @property {number} attributes.width 宽度 + * @property {number} attributes.size 大小 + * @property {string} attributes.ext 扩展名 + * @property {boolean} attributes.bold 是否加粗 + * @property {string} attributes.color 颜色 + * @property {string} attributes.link 链接 + * @return {PostStructuredContent} + */ +export interface PostStructuredContent { + insert: + | { + image?: string; + vod?: { + id: number; + duration: number; + cover: string; + resolutions: { + url: string; + definition: string; + height: number; + width: number; + bitrate: number; + size: number; + format: string; + label: string; + }[]; + view_num: number; + transcoding_status: number; + review_status: number; + }; + backup_text?: string; + fold?: { + title: string; + content: string; + }; + } + | string; + attributes?: { + height?: number; + width?: number; + size?: number; + ext?: string; + bold?: boolean; + color?: string; + link?: string; + }; +} \ No newline at end of file diff --git a/src/utils/MysParse.ts b/src/plugins/Mys/utils/parser.ts similarity index 93% rename from src/utils/MysParse.ts rename to src/plugins/Mys/utils/parser.ts index 4c5ef0d2..00821d4a 100644 --- a/src/utils/MysParse.ts +++ b/src/plugins/Mys/utils/parser.ts @@ -1,20 +1,21 @@ /** - * @file utils MysParse.ts + * @file plugins Mys utils PostParser.ts * @description 用于解析Mys数据的工具 * @author BTMuli * @since Alpha */ +import { PostStructuredContent } from "../interface/post"; + /** * @description 解析Mys数据 * @param {string} data Mys数据 * @description 为了安全考虑,不会解析所有的属性,只会解析几个常用的属性 * @returns {Document} 解析后的 HTML 文档 */ -export function parseMys(data: string): Document { +export function StructuredPostParser(data: string): Document { // Json 化 - let jsonData = JSON.parse(data); - while (typeof jsonData === "string") jsonData = JSON.parse(jsonData); + let jsonData: PostStructuredContent[] = JSON.parse(data); // 创建 HTML 文档 const doc = document.implementation.createHTMLDocument(); // 遍历 Json 数据