Skip to content

Commit

Permalink
feat: 支持发布到知乎
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 9, 2023
1 parent f76e7f4 commit 0a8d119
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 6 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 82 additions & 15 deletions src/adaptors/web/zhihu/zhihuWebAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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<string> {
return `https://zhuanlan.zhihu.com/p/${postid}`
}

public async deletePost(postid: string): Promise<boolean> {
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<boolean> {
// 先更新草稿
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 }

0 comments on commit 0a8d119

Please sign in to comment.