Skip to content

Let's use AI to Earn!

feat: 快手api发布初版添加,还差参数

gaoSir5571a3e1b829
diff --git a/commont/utils.ts b/commont/utils.ts
new file mode 100644
index 00000000..393ac65e
--- /dev/null
+++ b/commont/utils.ts
@@ -0,0 +1,14 @@
+// 根据文件路径获取文件名和后缀
+export function getFilePathNameCommon(path: string) {
+ if (!path)
+ return {
+ filename: '',
+ suffix: '',
+ };
+ const path1 = path.split('\\')[path.split('\\').length - 1];
+ const filename = path1.split('/')[path1.split('/').length - 1];
+ return {
+ filename,
+ suffix: filename.split('.')[filename.split('.').length - 1],
+ };
+}
\ No newline at end of file
diff --git a/electron/main/plat/platforms/Kwai/index.ts b/electron/main/plat/platforms/Kwai/index.ts
index 7d34f10e..b4c23221 100644
--- a/electron/main/plat/platforms/Kwai/index.ts
+++ b/electron/main/plat/platforms/Kwai/index.ts
@@ -84,25 +84,39 @@ export class Kwai extends PlatformBase {
params: VideoModel,
callback: VideoCallbackType,
): Promise<PublishVideoResult> {
- const publishVideoResult = new PublishVideoResult();
- const res = await kwaiPub.pubVideo({
- videoPath: params.videoPath || '',
- coverPath: params.coverPath || '',
- cookies: params.cookies!,
- desc: params.desc + params.topics.map((v) => `#${v}`).join(' '),
- callback,
- visibleType: VisibleTypeEnum.Public
- ? KwaiVisibleTypeEnum.Public
- : VisibleTypeEnum.Friend
- ? KwaiVisibleTypeEnum.Friend
- : KwaiVisibleTypeEnum.Private,
+ return new Promise(async (resolve) => {
+ const result = await kwaiPub
+ .pubVideo({
+ videoPath: params.videoPath || '',
+ coverPath: params.coverPath || '',
+ cookies: params.cookies!,
+ desc: params.desc + params.topics.map((v) => `#${v}`).join(' '),
+ callback,
+ visibleType: VisibleTypeEnum.Public
+ ? KwaiVisibleTypeEnum.Public
+ : VisibleTypeEnum.Friend
+ ? KwaiVisibleTypeEnum.Friend
+ : KwaiVisibleTypeEnum.Private,
+ })
+ .catch((e) => {
+ resolve({
+ code: 0,
+ msg: e,
+ });
+ });
+ if (!result || !result.publishId)
+ return resolve({
+ code: 0,
+ msg: '网络繁忙,请稍后重试',
+ });
+
+ return resolve({
+ code: 1,
+ msg: '发布成功',
+ dataId: result.publishId,
+ previewVideoLink: result.shareLink,
+ });
});
- // 发布失败
- if (!res.success) {
- publishVideoResult.code = 0;
- publishVideoResult.msg = res.msg;
- }
- return publishVideoResult;
}
async getStatistics(account: AccountModel) {
diff --git a/electron/plat/Kwai/index.ts b/electron/plat/Kwai/index.ts
index b5a7890d..a0d515b0 100644
--- a/electron/plat/Kwai/index.ts
+++ b/electron/plat/Kwai/index.ts
@@ -12,10 +12,15 @@ import {
IKwaiPubVideoParams,
IKwaiUserInfoResponse,
ILoginResponse,
+ KwaiSubmitResponse,
+ UploadFinishResponse,
+ UploadPpreResponse,
} from './kwai.type';
import { CookieToString } from '../utils';
import { BrowserWindow } from 'electron';
import kwaiSign from './sign/KwaiSign';
+import { FileUtils } from '../../util/file';
+import { getFilePathNameCommon } from '../../../commont/utils';
interface IRequestApiParams extends IRequestNetParams {
cookie: Electron.Cookie[];
@@ -23,13 +28,132 @@ interface IRequestApiParams extends IRequestNetParams {
}
class KwaiPub {
+ fileBlockSize = 4194304;
+
// 发布视频
async pubVideo(params: IKwaiPubVideoParams): Promise<{
- success: boolean;
- msg: string;
+ publishId: string;
+ shareLink: string;
}> {
- return new Promise(async (resolve) => {
- console.log('快手图文发布');
+ return new Promise(async (resolve, reject) => {
+ try {
+ const callback = params.callback;
+ callback(5, '正在视频分片...');
+ const { filename, suffix } = getFilePathNameCommon(params.videoPath);
+ const filePartInfo = await FileUtils.getFilePartInfo(
+ params.videoPath,
+ this.fileBlockSize,
+ );
+ callback(10, '正在创建视频...');
+ const preRes = await this.requestApi<UploadPpreResponse>({
+ url: '/rest/cp/works/v2/video/pc/upload/pre',
+ cookie: params.cookies,
+ method: 'POST',
+ body: {
+ uploadType: 1,
+ },
+ });
+ console.log('创建视频:', preRes.data);
+
+ for (const i in filePartInfo.blockInfo) {
+ callback(40, `上传视频(${i}/${filePartInfo.blockInfo.length})`);
+ const chunkStart =
+ i === '0' ? 0 : filePartInfo.blockInfo[parseInt(i) - 1];
+ const chunkEnd = filePartInfo.blockInfo[i] - 1;
+ const chunkContent = await FileUtils.getFilePartContent(
+ params.videoPath,
+ chunkStart,
+ chunkEnd,
+ );
+ const uploadVideoRes = await requestNet({
+ method: 'POST',
+ url: `https://upload.kuaishouzt.com/api/upload/fragment?upload_token=${preRes.data.data.token}&fragment_id=${i}`,
+ isFile: true,
+ body: chunkContent,
+ });
+ console.log(`分片上传:(${i}/${filePartInfo.blockInfo.length})`);
+ console.log(uploadVideoRes.data);
+ }
+
+ callback(60, `查看分片上传结果...`);
+ const completeRes = await this.requestApi({
+ apiUrl: `https://upload.kuaishouzt.com/api/upload/complete?upload_token=${preRes.data.data.token}&fragment_count=${filePartInfo.blockInfo.length}`,
+ method: 'POST',
+ cookie: params.cookies,
+ body: {
+ uploadType: 1,
+ },
+ });
+ console.log(`分片上传完成:`, completeRes.data);
+
+ callback(70, `分片上传完成,视频上传结束...`);
+ const finishRes = await this.requestApi<UploadFinishResponse>({
+ url: `/rest/cp/works/v2/video/pc/upload/finish`,
+ method: 'POST',
+ cookie: params.cookies,
+ body: {
+ token: preRes.data.data.token,
+ fileName: filename,
+ fileType: `video/${suffix}`,
+ fileLength: filePartInfo.fileSize,
+ },
+ });
+ console.log(`视频上传结束:`, finishRes.data);
+ callback(80, `视频上传结束,正发布视频...`);
+ const submitParams = {
+ ...finishRes.data.data,
+ coverTimeStamp: 0,
+ caption: '',
+ photoStatus: 2,
+ coverType: 1,
+ coverTitle: '',
+ photoType: 0,
+ collectionId: '',
+ publishTime: 0,
+ longitude: '',
+ latitude: '',
+ poiId: 0,
+ notifyResult: 0,
+ domain: '',
+ secondDomain: '',
+ coverCropped: false,
+ pkCoverKey: '',
+ profileCoverKey: '',
+ downloadType: 1,
+ disableNearbyShow: false,
+ allowSameFrame: true,
+ movieId: '',
+ openPrePreview: false,
+ declareInfo: {},
+ activityIds: [],
+ riseQuality: false,
+ chapters: [],
+ projectId: '',
+ recTagIdList: [],
+ videoInfoMeta: '',
+ previewUrlErrorMessage: '',
+ triggerH265: false,
+ };
+ console.log('发布参数:', submitParams);
+ const submitRes = await this.requestApi<KwaiSubmitResponse>({
+ url: `/rest/cp/works/v2/video/pc/submit`,
+ method: 'POST',
+ cookie: params.cookies,
+ body: submitParams,
+ });
+ console.log(`视频发布完成:`, submitRes.data);
+ console.log(submitRes.data.result);
+ if (submitRes.data.result !== 1) {
+ return reject(submitRes.data.message);
+ }
+ console.log('发布成功!');
+ resolve({
+ shareLink: ``,
+ publishId: '1',
+ });
+ } catch (e: any) {
+ reject(`发布失败,失败原因:${e.message}`);
+ }
});
}
@@ -46,11 +170,6 @@ class KwaiPub {
};
}
- console.log(
- '----------- CookieToString(cookie) --- cookies: ',
- CookieToString(cookie),
- );
-
params['headers'] = {
...(params['headers'] ? params['headers'] : {}),
cookie: CookieToString(cookie),
@@ -62,9 +181,6 @@ class KwaiPub {
url: `${apiUrl}${params.url || ''}`,
});
- console.log('signUrl:', signUrl);
- console.log('apiUrl:', apiUrl);
-
return await requestNet<T>({
...params,
url: `${apiUrl == 'https://cp.kuaishou.com' ? signUrl : apiUrl}`,
diff --git a/electron/plat/Kwai/kwai.type.ts b/electron/plat/Kwai/kwai.type.ts
index 99e1cbdc..137b92f4 100644
--- a/electron/plat/Kwai/kwai.type.ts
+++ b/electron/plat/Kwai/kwai.type.ts
@@ -19,6 +19,32 @@ export interface IKwaiUserCommonResponse<T> {
message: string;
}
+// video/pc/upload/pre
+export type UploadPpreResponse = IKwaiUserCommonResponse<{
+ token: string;
+ fileId: number;
+}>;
+
+// video/pc/upload/finish
+export type UploadFinishResponse = IKwaiUserCommonResponse<{
+ coverKey: string;
+ coverMediaId: string;
+ duration: number;
+ fileId: number;
+ height: number;
+ mediaId: string;
+ photoIdStr: string;
+ videoDuration: string;
+ videoFrameRate: string;
+ width: string;
+}>;
+
+// /v2/video/pc/submit
+export type KwaiSubmitResponse = IKwaiUserCommonResponse<{
+ result: number;
+ message: string;
+}>;
+
export interface IGetHomeOverviewItem {
name: string;
tab: string;
diff --git a/src/components/Choose/ImgChoose.tsx b/src/components/Choose/ImgChoose.tsx
index 4c5e7f44..67b8d301 100644
--- a/src/components/Choose/ImgChoose.tsx
+++ b/src/components/Choose/ImgChoose.tsx
@@ -57,10 +57,10 @@ export const formatImg = async ({
blob?: Blob;
}): Promise<IImgFile> => {
return new Promise((resolve) => {
+ const { filename, suffix } = getFilePathName(path);
if (!blob) {
- const filename = getFilePathName(path);
blob = new Blob([file!], {
- type: `image/${filename.split('.')[filename.split('.').length - 1]}`,
+ type: `image/${suffix}`,
});
}
const imgUrl = URL.createObjectURL(blob);
@@ -73,7 +73,7 @@ export const formatImg = async ({
height: img.height,
imgPath: path,
size: blob!.size,
- filename: getFilePathName(path),
+ filename,
file: blob!,
imgUrl,
});
diff --git a/src/components/Choose/VideoChoose.tsx b/src/components/Choose/VideoChoose.tsx
index c3e6bf81..a33df0ae 100644
--- a/src/components/Choose/VideoChoose.tsx
+++ b/src/components/Choose/VideoChoose.tsx
@@ -115,9 +115,9 @@ export const formatVideo = async (
path: string,
file: Uint8Array,
): Promise<IVideoFile> => {
- const blob = new Blob([file], { type: 'video/mp4' });
+ const { filename, suffix } = getFilePathName(path);
+ const blob = new Blob([file], { type: `video/${suffix}` });
const videoUrl = URL.createObjectURL(blob);
- const filename = getFilePathName(path);
const videoInfo = await getVideoInfo(videoUrl, filename);
return {
diff --git a/src/utils/index.ts b/src/utils/index.ts
index db0a183f..2d61cd94 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -1,4 +1,5 @@
import dayjs from 'dayjs';
+import { getFilePathNameCommon } from '../../commont/utils';
/**
* 生成唯一ID
*/
@@ -10,12 +11,8 @@ export function generateUUID(): string {
});
}
-// 获取文件路径中的文件名
-export function getFilePathName(path: string) {
- if (!path) return '';
- const path1 = path.split('\\')[path.split('\\').length - 1];
- return path1.split('/')[path1.split('/').length - 1];
-}
+// 获取文件路径中的文件名和后缀
+export const getFilePathName = getFilePathNameCommon;
// 格式化时间
export function formatTime(
diff --git a/src/views/publish/children/pubRecord/page.tsx b/src/views/publish/children/pubRecord/page.tsx
index 852cd6fa..2385ce6c 100644
--- a/src/views/publish/children/pubRecord/page.tsx
+++ b/src/views/publish/children/pubRecord/page.tsx
@@ -27,17 +27,19 @@ export const ImageView = ({
setImgFile(res);
});
}, []);
+
+ const filename = useMemo(() => {
+ return getFilePathName(prm.videoPath!).filename;
+ }, []);
+
return (
<div
className={styles['pubRecord-pubCon']}
style={{ minHeight: height + 'px' }}
>
{imgFile && <Image src={imgFile.imgUrl} height={height} width={width} />}
- <span
- title={getFilePathName(prm.videoPath!)}
- className="pubRecord-pubCon-name"
- >
- {getFilePathName(prm.videoPath!)}
+ <span title={filename} className="pubRecord-pubCon-name">
+ {filename}
</span>
</div>
);