diff --git a/package.json b/package.json index 58908362..4d2c8248 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "unplugin-auto-import": "^0.16.6", "unplugin-vue-components": "^0.25.1", "vercel": "^31.2.3", - "vite": "^4.3.9", + "vite": "^4.4.9", "vite-plugin-html": "^3.2.0", "vite-plugin-node-polyfills": "^0.8.2", "vitest": "^0.34.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a135dde8..8b18a8e3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -131,7 +131,7 @@ devDependencies: specifier: ^31.2.3 version: 31.2.3 vite: - specifier: ^4.3.9 + specifier: ^4.4.9 version: 4.4.9(@types/node@18.17.4)(stylus@0.59.0) vite-plugin-html: specifier: ^3.2.0 @@ -860,7 +860,7 @@ packages: nopt: 5.0.0 npmlog: 5.0.1 rimraf: 3.0.2 - semver: 7.3.8 + semver: 7.5.4 tar: 6.1.15 transitivePeerDependencies: - encoding @@ -4713,7 +4713,7 @@ packages: resolution: {integrity: sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==} engines: {node: '>=8'} dependencies: - semver: 6.3.1 + semver: 6.1.1 dev: true /make-dir@4.0.0: @@ -5852,8 +5852,8 @@ packages: fsevents: 2.3.2 dev: true - /rollup@3.27.2: - resolution: {integrity: sha512-YGwmHf7h2oUHkVBT248x0yt6vZkYQ3/rvE5iQuVBh3WO8GcJ6BNeOkpoX1yMHIiBm18EMLjBPIoUDkhgnyxGOQ==} + /rollup@3.28.0: + resolution: {integrity: sha512-d7zhvo1OUY2SXSM6pfNjgD5+d0Nz87CUp4mt8l/GgVP3oBsPwzNvSzyu1me6BSG9JIgWNTVcafIXBIyM8yQ3yw==} engines: {node: '>=14.18.0', npm: '>=8.0.0'} hasBin: true optionalDependencies: @@ -5914,11 +5914,6 @@ packages: hasBin: true dev: true - /semver@6.3.1: - resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} - hasBin: true - dev: true - /semver@7.3.8: resolution: {integrity: sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==} engines: {node: '>=10'} @@ -6805,7 +6800,7 @@ packages: '@types/node': 18.17.4 esbuild: 0.18.20 postcss: 8.4.27 - rollup: 3.27.2 + rollup: 3.28.0 stylus: 0.59.0 optionalDependencies: fsevents: 2.3.2 diff --git a/src/adaptors/web/zhihu/zhihuWebAdaptor.ts b/src/adaptors/web/zhihu/zhihuWebAdaptor.ts index 9479979b..2c28e2d8 100644 --- a/src/adaptors/web/zhihu/zhihuWebAdaptor.ts +++ b/src/adaptors/web/zhihu/zhihuWebAdaptor.ts @@ -59,7 +59,7 @@ class ZhihuWebAdaptor extends BaseWebApi { uid: res.uid, title: res.name, avatar: res.avatar_url, - supportTypes: ["html"], + supportTypes: ["markdown"], type: "zhihu", displayName: "知乎", home: "https://www.zhihu.com/settings/account", @@ -68,29 +68,96 @@ class ZhihuWebAdaptor extends BaseWebApi { } public async addPost(post: Post) { - let res + const params = JSON.stringify({ + title: post.title, + content: post.description, + }) + const res = await this.proxyFetch("https://zhuanlan.zhihu.com/api/articles/drafts", [], params, "POST") + this.logger.debug("save zhihu draft res=>", res) + + if (!res.id) { + throw new Error("知乎文章发布失败") + } + + // 目前是存草稿,现在需要把它设置为发布 + const pubParams = JSON.stringify({ + column: null, + commentPermission: "anyone", + disclaimer_type: "none", + disclaimer_status: "close", + table_of_contents_enabled: false, + commercial_report_info: { commercial_types: [] }, + commercial_zhitask_bind_info: null, + }) + const pubRes = await this.proxyFetch( + `https://zhuanlan.zhihu.com/api/articles/${res.id}/publish`, + [], + pubParams, + "PUT" + ) + this.logger.debug("publish zhihu article pubRes=>", pubRes) - throw new Error("开发中") - // var res = await $.ajax({ - // url: 'https://zhuanlan.zhihu.com/api/articles/drafts', - // type: 'POST', - // dataType: 'JSON', - // contentType: 'application/json', - // data: JSON.stringify({ - // title: post.post_title, - // // content: post.post_content - // }), - // }) - // console.log(res) return { status: "success", - post_id: res.id, + post_id: res.id.toString(), } } public async getPreviewUrl(postid: string): Promise { return `https://zhuanlan.zhihu.com/p/${postid}` } + + public async deletePost(postid: string): Promise { + let flag = false + try { + const res = await this.proxyFetch(`https://www.zhihu.com/api/v4/articles/${postid}`, [], {}, "DELETE") + this.logger.debug("delete zhihu article res=>", res) + if (res.success) { + flag = true + } else { + throw new Error(res.error.message) + } + } catch (e) { + this.logger.error("知乎文章删除失败", e) + throw e + } + + return flag + } + + public async editPost(postid: string, post: Post, publish?: boolean): Promise { + // 先更新草稿 + const params = JSON.stringify({ + title: post.title, + content: post.description, + table_of_contents: false, + delta_time: 10, + }) + + try { + await this.proxyFetch(`https://zhuanlan.zhihu.com/api/articles/${postid}/draft`, [], params, "PATCH") + this.logger.debug("updated zhihu draft") + } catch (e) { + throw new Error("知乎文章更新失败") + } + + // 目前是存草稿,现在需要把它设置为发布 + const pubParams = JSON.stringify({ + disclaimer_type: "none", + disclaimer_status: "close", + table_of_contents_enabled: false, + commercial_report_info: { commercial_types: [] }, + commercial_zhitask_bind_info: null, + }) + const pubRes = await this.proxyFetch( + `https://zhuanlan.zhihu.com/api/articles/${postid}/publish`, + [], + pubParams, + "PUT" + ) + this.logger.debug("edit zhihu pubRes=>", pubRes) + return true + } } export { ZhihuWebAdaptor }