Skip to content

Let's use AI to Earn!

参数过滤失败

daoyian1c0ee4861a2a
diff --git a/src/api/youtube.ts b/src/api/youtube.ts
index 8e2180a2..f82ada84 100644
--- a/src/api/youtube.ts
+++ b/src/api/youtube.ts
@@ -24,6 +24,15 @@ export const uploadYouTubeVideoApi = (data: FormData) => {
});
};
+
+export const uploadYouTubeVideoSmallApi = (data: FormData) => {
+ return request({
+ url: '/plat/youtube/video/upload/small',
+ method: 'POST',
+ body: data,
+ });
+};
+
export const getYouTubeListApi = (data: any) => {
return request({
url: '/plat/youtube/videos/list',
diff --git a/src/app/[lng]/youtube/page.tsx b/src/app/[lng]/youtube/page.tsx
index 18d5f289..c39a1c4d 100644
--- a/src/app/[lng]/youtube/page.tsx
+++ b/src/app/[lng]/youtube/page.tsx
@@ -129,7 +129,7 @@ const YouTubeAuth: React.FC = () => {
const response = await uploadYouTubeVideoApi(formData);
if (response?.data) {
message.success(t("uploadSuccess"));
- }
+ }
} catch (error) {
console.error("上传视频失败:", error);
message.error(t("uploadFailed"));
diff --git a/src/app/plat/PlatManage.ts b/src/app/plat/PlatManage.ts
index c65a06f0..749534fa 100644
--- a/src/app/plat/PlatManage.ts
+++ b/src/app/plat/PlatManage.ts
@@ -1,6 +1,7 @@
import { PlatType } from "@/app/config/platConfig";
import { KwaiPlat } from "@/app/plat/platChildren/kwai/KwaiPlat";
import { BilibiliPlat } from "@/app/plat/platChildren/bilibili/BilibiliPlat";
+import { YoutubePlat } from "@/app/plat/platChildren/youtube/YoutubePlat";
import { PlatBase } from "@/app/plat/PlatBase";
import {
IPlatConstrParams,
@@ -17,6 +18,8 @@ class PlatManage {
return new KwaiPlat(params);
case PlatType.BILIBILI:
return new BilibiliPlat(params);
+ case PlatType.YouTube:
+ return new YoutubePlat(params);
}
return new KwaiPlat(params);
}
diff --git a/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts b/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts
index 3ee5cdde..e761e9af 100644
--- a/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts
+++ b/src/app/plat/platChildren/bilibili/BilibiliPubCore.ts
@@ -16,18 +16,18 @@ import { apiCreatePublish, AccountType, PubStatus } from "@/api/publish";
import { PubType } from "@/app/config/publishConfig";
export class BilibiliPubCore {
- private kwaiPlat: BilibiliPlat;
+ private thisPlat: BilibiliPlat;
private videoPubParams: IVideoPublishItem;
private fileBlockSize = 4194304;
// 发布进度
private readonly onProgress: PubProgressType;
constructor(
- kwaiPlat: BilibiliPlat,
+ thisPlat: BilibiliPlat,
pubParams: IVideoPublishItem,
onProgress: PubProgressType,
) {
- this.kwaiPlat = kwaiPlat;
+ this.thisPlat = thisPlat;
this.videoPubParams = pubParams;
this.onProgress = onProgress;
}
@@ -61,8 +61,8 @@ export class BilibiliPubCore {
type: PubType.VIDEO,
title: this.videoPubParams.title || "",
desc: this.videoPubParams.describe || "",
- accountId: this.kwaiPlat.account.id,
- uid: this.kwaiPlat.account.uid || "",
+ accountId: this.thisPlat.account.id,
+ uid: this.thisPlat.account.uid || "",
accountType: AccountType.BILIBILI,
videoUrl: result.worksUrl, // 使用构建的视频地址
coverUrl: coverUrl, // 使用实际上传的封面地址
@@ -87,15 +87,15 @@ export class BilibiliPubCore {
}
// 视频发布
- async publishVideo(): Promise<IPublishResult> {
+ async publishVideo(): Promise<IPublishResult> {
console.log("视频发布");
console.log(this.videoPubParams);
- console.log(this.kwaiPlat.account);
+ console.log(this.thisPlat.account);
try {
// 1. 获取上传token
const initRes = await apiInitBilibiliVideo({
- accountId: this.kwaiPlat.account.id,
+ accountId: this.thisPlat.account.id,
name: this.videoPubParams.video.filename,
});
@@ -123,7 +123,7 @@ export class BilibiliPubCore {
console.log(`上传分片 ${i + 1}/${chunkRange.length}`);
const res = await apiUploadBilibiliVideoPart(
- this.kwaiPlat.account.id,
+ this.thisPlat.account.id,
uploadToken,
i + 1,
sliced,
@@ -142,7 +142,7 @@ export class BilibiliPubCore {
console.log("开始合并分片...");
this.onProgress?.(0.7, "开始合并分片");
const completeRes = await apiCompleteBilibiliVideo(
- this.kwaiPlat.account.id,
+ this.thisPlat.account.id,
uploadToken,
);
if (completeRes?.code !== 0) {
@@ -156,7 +156,7 @@ export class BilibiliPubCore {
const formData = new FormData();
formData.append("file", this.videoPubParams.cover.file);
const coverRes = await apiUploadBilibiliCover(
- this.kwaiPlat.account.id,
+ this.thisPlat.account.id,
formData,
);
if (coverRes?.data) {
@@ -168,7 +168,7 @@ export class BilibiliPubCore {
// 4. 提交稿件
const archiveData = {
- accountId: this.kwaiPlat.account.id,
+ accountId: this.thisPlat.account.id,
uploadToken,
title: this.videoPubParams.title || "开心快乐每一天",
cover: coverUrl,
@@ -220,7 +220,7 @@ export class BilibiliPubCore {
// 发起上传
private async _startUploadVideo() {
- const res = await this.kwaiPlat.request({
+ const res = await this.thisPlat.request({
url: "openapi/photo/start_upload",
method: "POST",
});
diff --git a/src/app/plat/platChildren/youtube/YoutubePlat.ts b/src/app/plat/platChildren/youtube/YoutubePlat.ts
new file mode 100644
index 00000000..7a10bc97
--- /dev/null
+++ b/src/app/plat/platChildren/youtube/YoutubePlat.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 { YoutubePubCore } from "@/app/plat/platChildren/youtube/YoutubePubCore";
+
+export class YoutubePlat 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 YoutubePubCore(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/youtube/YoutubePubCore.ts b/src/app/plat/platChildren/youtube/YoutubePubCore.ts
new file mode 100644
index 00000000..d5dff35d
--- /dev/null
+++ b/src/app/plat/platChildren/youtube/YoutubePubCore.ts
@@ -0,0 +1,180 @@
+import { YoutubePlat } from "./YoutubePlat";
+import {
+ IPublishResult,
+ IVideoPublishItem,
+ PubProgressType,
+} from "@/app/plat/plat.type";
+import { calculateChunks } from "@/app/plat/plat.util";
+import {
+ // apiInitBilibiliVideo,
+ // apiUploadBilibiliCover,
+ // apiSubmitBilibiliArchive,
+ // apiUploadBilibiliVideoPart,
+ // apiCompleteBilibiliVideo,
+ uploadYouTubeVideoSmallApi
+} from "@/api/youtube";
+import { apiCreatePublish, AccountType, PubStatus } from "@/api/publish";
+import { PubType } from "@/app/config/publishConfig";
+
+export class YoutubePubCore {
+ private thisPlat: YoutubePlat;
+ private videoPubParams: IVideoPublishItem;
+ private fileBlockSize = 4194304;
+ // 发布进度
+ private readonly onProgress: PubProgressType;
+
+ constructor(
+ thisPlat: YoutubePlat,
+ pubParams: IVideoPublishItem,
+ onProgress: PubProgressType,
+ ) {
+ this.thisPlat = thisPlat;
+ 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();
+ }
+
+ // 创建发布记录
+ private async createPublishRecord(result: any, coverUrl: string) {
+ try {
+ const publishData = {
+ flowId: `youtube_${Date.now()}`, // 生成唯一的流程ID
+ type: PubType.VIDEO,
+ title: this.videoPubParams.title || "",
+ desc: this.videoPubParams.describe || "",
+ accountId: this.thisPlat.account.id,
+ uid: this.thisPlat.account.uid || "",
+ accountType: AccountType.YOUTUBE,
+ videoUrl: result.worksUrl, // 使用构建的视频地址
+ coverUrl: coverUrl, // 使用实际上传的封面地址
+ imgList: [],
+ publishTime: new Date().toISOString(),
+ status: result.success ? PubStatus.RELEASED : PubStatus.FAIL,
+ option: {
+ worksId: result.worksId,
+ worksUrl: result.worksUrl,
+ platform: "youtube",
+ publishResult: result,
+ },
+ };
+
+ const res = await apiCreatePublish(publishData);
+ console.log("发布记录创建成功:", res);
+ return res;
+ } catch (error) {
+ console.error("创建发布记录失败:", error);
+ // 不抛出错误,避免影响主流程
+ }
+ }
+
+ // 视频发布
+ async publishVideo(): Promise<IPublishResult> {
+ console.log("视频发布");
+ console.log(this.videoPubParams);
+ console.log(this.thisPlat.account);
+
+ try {
+
+ const formData = new FormData();
+ formData.append("file", this.videoPubParams.video.file);
+ formData.append("accountId", this.thisPlat.account.id);
+ formData.append("title", this.videoPubParams.title || "开心快乐每一天");
+ formData.append("description", this.videoPubParams.describe || "开心快乐每一天");
+ formData.append("privacyStatus", "private");
+ // if (selectedSection) {
+ // formData.append("sectionId", selectedSection);
+ // }
+
+ const response = await uploadYouTubeVideoSmallApi(formData);
+ console.log('response',response)
+ return {
+ worksId: "",
+ worksUrl: "",
+ success: true,
+ };
+
+ // 4. 提交稿件
+ const archiveData = {
+ accountId: this.thisPlat.account.id,
+ uploadToken,
+ title: this.videoPubParams.title || "开心快乐每一天",
+ cover: coverUrl,
+ tid: 21, // 默认使用日常分区
+ // noReprint: this.videoPubParams.noReprint || 0,
+ noReprint: 0,
+ desc: this.videoPubParams.describe || "开心快乐每一天",
+ tag: this.videoPubParams.topics || [],
+ copyright: 1,
+ };
+
+ this.onProgress?.(0.95, "正在提交稿件");
+ const submitRes: any = await apiSubmitBilibiliArchive(archiveData);
+ console.log("稿件提交结果:", submitRes);
+
+ if (submitRes?.code !== 0) {
+ throw new Error("稿件提交失败");
+ }
+
+ this.onProgress?.(1, "发布成功");
+
+ const result = {
+ worksId: submitRes.data?.data || "",
+ worksUrl: `https://www.bilibili.com/video/${submitRes.data?.data}`,
+ success: true,
+ };
+
+ // 5. 创建发布记录
+ await this.createPublishRecord(result, coverUrl);
+
+ return result;
+ } catch (error: any) {
+ console.error("发布视频失败:", error);
+ this.onProgress?.(0, "发布失败");
+
+ const result = {
+ worksId: "",
+ worksUrl: "",
+ success: false,
+ failMsg: error instanceof Error ? error.message : "发布失败",
+ };
+
+ // 即使发布失败也创建记录
+ await this.createPublishRecord(result, "");
+
+ return result;
+ }
+ }
+
+ // 发起上传
+ private async _startUploadVideo() {
+ const res = await this.thisPlat.request({
+ url: "openapi/photo/start_upload",
+ method: "POST",
+ });
+ return res as {
+ result: number;
+ upload_token: string;
+ endpoint: string;
+ };
+ }
+}