Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
bili发布
DESKTOP-EMF2SH4\Administrator3ae8e8e4b40f
diff --git a/src/app/plat/PlatManage.ts b/src/app/plat/PlatManage.tsindex 715a2046..e9db611e 100644--- a/src/app/plat/PlatManage.ts+++ b/src/app/plat/PlatManage.ts@@ -1,5 +1,6 @@import { PlatType } from "@/app/config/platConfig";import { KwaiPlat } from "@/app/plat/platChildren/kwai/KwaiPlat";+import { BilibiliPlat } from "@/app/plat/platChildren/bilibili/BilibiliPlat";import { PlatBase } from "@/app/plat/PlatBase";import {IPlatConstrParams,@@ -14,6 +15,8 @@ class PlatManage {switch (type) {case PlatType.KWAI:return new KwaiPlat(params);+ case PlatType.BILIBILI:+ return new BilibiliPlat(params);}return new KwaiPlat(params);}diff --git a/src/app/plat/platChildren/bilibili/BilibiliPlat.ts b/src/app/plat/platChildren/bilibili/BilibiliPlat.tsnew file mode 100644index 00000000..c20d617c--- /dev/null+++ b/src/app/plat/platChildren/bilibili/BilibiliPlat.ts@@ -0,0 +1,57 @@+import { PlatBase } from "@/app/plat/PlatBase";+import { SocialAccount } from "@/api/types/account.type";+import { RequestParams } from "@/utils/FetchService/types";+import { requestPlatApi } from "@/utils/otherRequest";+import { KwaiApiProxyUrl } from "@/constant";+import { kwaiAppId } from "@/app/[lng]/accounts/plat/kwaiLogin";+import { PlatType } from "@/app/config/platConfig";+import {+ IPublishResult,+ IVideoPublishItem,+ PubProgressType,+} from "@/app/plat/plat.type";+import { BilibiliPubCore } from "@/app/plat/platChildren/bilibili/BilibiliPubCore";++export class BilibiliPlat extends PlatBase {+ public async getAccountInfo(): Promise<Partial<SocialAccount> | null> {+ const { user_info } = await this.request({+ url: "openapi/user_info",+ method: "GET",+ });+ return {+ avatar: user_info.bigHead,+ forwardCount: user_info.follow,+ fansCount: user_info.fan,+ nickname: user_info.name,+ type: PlatType.BILIBILI,+ account: "",+ uid: "",+ };+ }++ request(params: RequestParams, isPrefix = true) {+ params.url = `${(isPrefix && KwaiApiProxyUrl) || ""}${params.url}`;+ params.params = {+ access_token: this.access_token,+ app_id: kwaiAppId,+ ...(params.params ?? {}),+ };+ return requestPlatApi(params);+ }++ public async publishVideo(+ videoPubParams: IVideoPublishItem,+ onProgress: PubProgressType,+ ): Promise<IPublishResult> {+ const kwaiPubCore = new BilibiliPubCore(this, videoPubParams, onProgress);+ const pubRes = await kwaiPubCore.publishVideo();++ if (pubRes.success) {+ onProgress(100, "视频发布完成");+ } else {+ onProgress(-1, "视频发布失败");+ }++ return pubRes;+ }+}diff --git a/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts b/src/app/plat/platChildren/bilibili/BilibiliPubCore.tsnew file mode 100644index 00000000..47effc09--- /dev/null+++ b/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts@@ -0,0 +1,157 @@+import { KwaiPlat } from "@/app/plat/platChildren/kwai/KwaiPlat";+import {+ IPublishResult,+ IVideoPublishItem,+ PubProgressType,+} from "@/app/plat/plat.type";+import { calculateChunks, readBlobRange } from "@/app/plat/plat.util";+import { KwaiApiUploadProxyUrl } from "@/constant";++export class BilibiliPubCore {+ private kwaiPlat: KwaiPlat;+ private videoPubParams: IVideoPublishItem;+ private fileBlockSize = 4194304;+ // 发布进度+ private readonly onProgress: PubProgressType;++ constructor(+ kwaiPlat: KwaiPlat,+ pubParams: IVideoPublishItem,+ onProgress: PubProgressType,+ ) {+ this.kwaiPlat = kwaiPlat;+ this.videoPubParams = pubParams;+ this.onProgress = onProgress;+ }++ // 获取caption+ getCaption() {+ const { title, describe, topics } = this.videoPubParams;+ let caption = "";+ if (title) {+ caption += `${title} `;+ }++ if (describe) {+ caption += `${describe} `;+ }++ if (topics?.length !== 0) {+ for (const topic of topics!) {+ caption += `#${topic} `;+ }+ }++ return caption.trim();+ }++ // 视频发布+ async publishVideo(): Promise<IPublishResult> {+ try {+ const startUploadInfo = await this._startUploadVideo();+ this.onProgress(10, "发起上传完成...");++ // 获取分片的大小+ const chunkRange = calculateChunks(+ this.videoPubParams.video.size,+ this.fileBlockSize,+ );+ this.onProgress(20, "分片大小获取完成...");++ // 分片上传+ for (let i = 0; i < chunkRange.length; i++) {+ this.onProgress(50, `正在分片上传视频:${i} / ${chunkRange.length}`);++ const range = chunkRange[i];+ // 获取要上传的视频分片+ const chunk = await readBlobRange(+ this.videoPubParams.video.file,+ range.start,+ range.end,+ );+ // 发起分片上传+ const chunkUploadRes = await this.kwaiPlat.request(+ {+ url: `${KwaiApiUploadProxyUrl}api/upload/fragment`,+ method: "POST",+ headers: {+ "Content-Type": "application/octet-stream",+ },+ params: {+ fragment_id: i,+ upload_token: startUploadInfo.upload_token,+ },+ body: chunk,+ },+ false,+ );+ if (chunkUploadRes.result !== 1) {+ throw new Error("分片上传失败!");+ }+ console.log(+ `${i} / ${chunkRange.length} 分片上传结果:${chunkUploadRes}`,+ );+ }++ // 分片上传完成+ const completeRes = await this.kwaiPlat.request(+ {+ url: `${KwaiApiUploadProxyUrl}api/upload/complete`,+ method: "POST",+ params: {+ fragment_count: chunkRange.length,+ upload_token: startUploadInfo.upload_token,+ },+ },+ false,+ );+ this.onProgress(70, `正在发布视频...`);+ if (completeRes.result !== 1) {+ throw new Error("分片合并失败!");+ }+ console.log("分片上传完成:", completeRes);++ const formData = new FormData();+ formData.append("caption", this.getCaption());+ formData.append("cover", this.videoPubParams.cover!.file);+ const pubRes = await this.kwaiPlat.request({+ url: "/openapi/photo/publish",+ method: "POST",+ params: {+ upload_token: startUploadInfo.upload_token,+ },+ data: formData,+ });+ if (pubRes.result !== 1) {+ throw new Error(`视频发布失败:${pubRes.error}`);+ }++ return {+ worksId: "",+ worksUrl: "",+ success: true,+ };+ } catch (e) {+ console.error(e);+ return {+ worksId: "",+ worksUrl: "",+ success: false,+ failMsg: e instanceof Error ? e.message : "未知错误",+ };+ }+ }++ // 发起上传+ private async _startUploadVideo() {+ const res = await this.kwaiPlat.request({+ url: "openapi/photo/start_upload",+ method: "POST",+ });+ return res as {+ result: number;+ upload_token: string;+ endpoint: string;+ };+ }+}diff --git a/src/app/plat/platChildren/kwai/kwai.type.ts b/src/app/plat/platChildren/kwai/kwai.type.tsdeleted file mode 100644index e69de29b..00000000