/
jianshuWebAdaptor.ts
312 lines (277 loc) · 9.69 KB
/
jianshuWebAdaptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/
import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts"
import { CategoryInfo, Post, UserBlog } from "zhi-blog-api"
import { ElMessage } from "element-plus"
import { fileToBuffer } from "~/src/utils/polyfillUtils.ts"
import FormDataUtils from "~/src/utils/FormDataUtils.ts"
/**
* 简书网页授权适配器
*
* @see [wechatsync jianshu adaptor](https://github.com/wechatsync/Wechatsync/blob/master/packages/@wechatsync/drivers/src/jianshu.js)
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
class JianshuWebAdaptor extends BaseWebApi {
public async getMetaData(): Promise<any> {
const res = await this.webProxyFetch("https://www.jianshu.com/settings/basic.json")
const avatar = res.data.avatar
const uid = avatar.substring(avatar.lastIndexOf("/") + 1, avatar.lastIndexOf("."))
const flag = !!uid
this.logger.info(`get jianshu metadata finished, flag => ${flag}`)
return {
flag: flag,
uid: uid,
title: res.data.nickname,
avatar: avatar,
type: "jianshu",
displayName: "简书",
supportTypes: ["html"],
home: "https://www.jianshu.com/settings/basic",
icon: "https://www.jianshu.com/favicon.ico",
}
}
public async getUsersBlogs(): Promise<Array<UserBlog>> {
const result: UserBlog[] = []
const header = {
accept: "application/json",
}
const notebooks = await this.webProxyFetch("https://www.jianshu.com/author/notebooks", [header])
this.logger.info(`get jianshu notebooks`, notebooks)
if (notebooks && notebooks.length > 0) {
notebooks.forEach((item: any) => {
const useBlog = new UserBlog()
useBlog.blogid = item.id
useBlog.blogName = item.name
result.push(useBlog)
})
}
this.logger.debug("getUsersBlogs=>", result)
return result
}
public async getCategories(): Promise<CategoryInfo[]> {
const cats = [] as CategoryInfo[]
const header = {
accept: "application/json",
}
const notebooks = await this.webProxyFetch("https://www.jianshu.com/author/notebooks", [header])
this.logger.info(`get jianshu notebooks`, notebooks)
if (notebooks && notebooks.length > 0) {
notebooks.forEach((item: any) => {
const cat = new CategoryInfo()
cat.categoryId = item.id
cat.categoryName = item.name
cats.push(cat)
})
}
return cats
}
public async addPost(post: Post) {
this.logger.debug("start add post =>", { post })
// 1 初始化文章,返回文章ID
// 如果没传递过来就用默认的父页面
const notebookId = post.cate_slugs?.[0]?.toString() ?? this.cfg.blogid.toString()
const initHeader = {
accept: "application/json",
}
const initParams = {
notebook_id: notebookId,
title: post.title,
at_bottom: false,
}
const initRes = await this.webProxyFetch("https://www.jianshu.com/author/notes", [initHeader], initParams, "POST")
this.logger.debug("jianshu addPost initRes =>", initRes)
const pageId = initRes.id
const endUrl = initRes.slug
// 文章更新并发布
await this.updateJianshuArticle(pageId, post.title, post.description, 1)
this.logger.info(`文章发布成功,开始组装简书的ID组合,pageId=${pageId},notebookId=${notebookId},endUrl=${endUrl}`)
const postid = [pageId, notebookId, endUrl].join("_")
return {
status: "success",
post_id: postid,
}
}
public async getPreviewUrl(postid: string): Promise<string> {
const purl = this.cfg.previewUrl ?? ""
const jianshuPostKey = this.getJianshuPostidKey(postid)
// const pageId = jianshuPostKey.pageId
const endUrl = jianshuPostKey.endUrl
const postUrl = purl.replace("[postid]", endUrl)
return postUrl
// return StrUtil.pathJoin(this.cfg.home ?? "", postUrl)
}
public async deletePost(postid: string): Promise<boolean> {
const jianshuPostKey = this.getJianshuPostidKey(postid)
const pageId = jianshuPostKey.pageId
const header = {
accept: "application/json",
}
const res = await this.webProxyFetch(
`https://www.jianshu.com/author/notes/${pageId}/soft_destroy`,
[header],
undefined,
"POST"
)
this.logger.debug("jianshu delete post res =>", res)
return true
}
public async editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
let flag = false
try {
const jianshuPostKey = this.getJianshuPostidKey(postid)
const pageId = jianshuPostKey.pageId
// 查询历史版本
const header = {
accept: "application/json",
}
const res = await this.webProxyFetch(`https://www.jianshu.com/author/notes/${pageId}/note_logs`, [header])
this.logger.debug("jianshu get post version res =>", res)
// 文章更新并发布
const newVersion = res.length + 1
this.logger.debug("jianshu update post with new version =>", newVersion)
await this.updateJianshuArticle(pageId, post.title, post.description, newVersion)
flag = true
} catch (e) {
this.logger.error("简书文章更新失败", e)
}
return flag
}
public async uploadFile(file: File | Blob, filename?: string): Promise<any> {
const { FormData, Blob } = FormDataUtils.getFormData(this.appInstance)
this.logger.debug(`jianshu start uploadFile ${filename}=>`, file)
if (file instanceof Blob) {
// import
const win = this.appInstance.win
if (!win.require) {
throw new Error("非常抱歉,目前仅思源笔记PC客户端支持上传图片")
}
// uploadUrl
const uploadUrl = "https://upload.qiniup.com/"
// 获取图片二进制数据
const bits = await fileToBuffer(file)
const blob = new Blob([bits], { type: file.type })
// formData
const tokenReq = await this.webProxyFetch("https://www.jianshu.com/upload_images/token.json?filename=" + filename)
this.logger.debug("jianshu get picture token res =>", tokenReq)
const formData = new FormData()
formData.append("token", tokenReq.token)
formData.append("key", tokenReq.key)
formData.append("x:protocol", "https")
formData.append("file", blob, filename)
// 发送请求
const resJson = await this.jianshuFormFetch(uploadUrl, formData)
this.logger.debug("jianshu upload success, resJson =>", resJson)
if (!resJson.url) {
throw new Error("简书图片上传失败 =>" + filename)
}
const url = resJson.url
return {
id: tokenReq.key,
object_key: tokenReq.key,
url: url,
}
}
return {}
}
// ================
// private methods
// ================
/**
* 获取封装的postid
*
* @param postid
* @private postid
*/
private getJianshuPostidKey(postid: string): any {
let pageId: string
let notebookId: string
let endUrl: string
if (postid.indexOf("_") > 0) {
const idArr = postid.split("_")
pageId = idArr[0]
notebookId = idArr[1]
endUrl = idArr[2]
} else {
pageId = postid
}
return {
pageId: pageId,
notebookId: notebookId,
endUrl: endUrl,
}
}
/**
*
* @param pageId - 文章ID
* @param title - 标题
* @param conetnt -正文
* @param autosave_control 版本号码,更新必须增加
* @private
*/
private async updateJianshuArticle(pageId: string, title: string, conetnt: string, autosave_control: number = 1) {
// 1 实时保存文章
const saveHeader = {
accept: "application/json",
}
const saveParams = {
id: pageId,
autosave_control: autosave_control,
title: title,
content: conetnt,
}
const saveRes = await this.webProxyFetch(
`https://www.jianshu.com/author/notes/${pageId}`,
[saveHeader],
saveParams,
"PUT"
)
this.logger.debug("jianshu savePost saveRes =>", saveRes)
// 2 文章发布
try {
const publishHeader = {
accept: "application/json",
}
const pubRes = await this.webProxyFetch(
`https://www.jianshu.com/author/notes/${pageId}/publicize`,
[publishHeader],
{},
"POST"
)
this.logger.debug("jianshu publishPost pubRes =>", pubRes)
} catch (e) {
ElMessage.warning("简书文章发布失败,内容已存入草稿 =>" + e)
this.logger.error("简书文章发布失败", e)
}
}
private async jianshuFormFetch(url: string, formData: FormData) {
// header
const header = {}
const resJson = await this.webFormFetch(url, [header], formData)
return resJson
}
}
export { JianshuWebAdaptor }