Skip to content

Commit

Permalink
feat: 新版挂件-支持知乎网页授权
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Jul 30, 2023
1 parent 0ff11a4 commit 044091e
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
dist
dist-ssr
*.local
testdata

# Editor directories and files
.vscode/*
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
"vue-i18n": "^9.2.2",
"vue-router": "^4.2.4",
"xmlbuilder2": "^3.1.1",
"zhi-blog-api": "^1.18.1",
"zhi-blog-api": "^1.20.0",
"zhi-common": "^1.11.5",
"zhi-device": "^2.3.0",
"zhi-fetch-middleware": "^0.1.19",
"zhi-github-middleware": "^0.1.2",
"zhi-lib-base": "^0.4.2",
"zhi-siyuan-api": "^1.27.8",
"zhi-wechatsync-middleware": "^0.1.8",
"zhi-siyuan-api": "^1.28.1",
"zhi-wechatsync-middleware": "^0.1.12",
"zhi-xmlrpc-middleware": "^0.2.19"
}
}
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

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

9 changes: 6 additions & 3 deletions src/adaptors/api/cnblogs/useCnblogsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { CnblogsConfig } from "~/src/adaptors/api/cnblogs/config/cnblogsConfig.t
import { CnblogsApiAdaptor } from "~/src/adaptors/api/cnblogs/adaptor/cnblogsApiAdaptor.ts"
import { AppInstance } from "~/src/appInstance.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { JsonUtil, ObjectUtil } from "zhi-common"
import {JsonUtil, ObjectUtil, StrUtil} from "zhi-common"
import { getDynPostidKey } from "~/src/components/set/publish/platform/dynamicConfig.ts"

/**
Expand Down Expand Up @@ -75,12 +75,15 @@ export const useCnblogsApi = async (key?: string, newCfg?: CnblogsConfig) => {
)

cfg = new CnblogsConfig(cnblogsApiUrl, cnblogsUsername, cnblogsAuthToken, middlewareUrl)
// 默认值
cfg.posidKey = getDynPostidKey(key)
logger.debug("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
}
// 初始化posidKey
if (StrUtil.isEmptyString(cfg.posidKey)) {
// 默认值
cfg.posidKey = getDynPostidKey(key)
}
}

// 创建Cnblogs API适配器
Expand Down
9 changes: 6 additions & 3 deletions src/adaptors/api/typecho/useTypechoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { AppInstance } from "~/src/appInstance.ts"
import { Utils } from "~/src/utils/utils.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { TypechoConfig } from "~/src/adaptors/api/typecho/config/typechoConfig.ts"
import { JsonUtil, ObjectUtil } from "zhi-common"
import {JsonUtil, ObjectUtil, StrUtil} from "zhi-common"
import { getDynPostidKey } from "~/src/components/set/publish/platform/dynamicConfig.ts"
import { TypechoApiAdaptor } from "~/src/adaptors/api/typecho/adaptor/typechoApiAdaptor.ts"

Expand Down Expand Up @@ -71,12 +71,15 @@ export const useTypechoApi = async (key?: string, newCfg?: TypechoConfig) => {
"https://api.terwer.space/api/middleware"
)
cfg = new TypechoConfig(typechoApiUrl, typechoUsername, typechoAuthToken, middlewareUrl)
// 默认值
cfg.posidKey = getDynPostidKey(key)
logger.info("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
}
// 初始化posidKey
if (StrUtil.isEmptyString(cfg.posidKey)) {
// 默认值
cfg.posidKey = getDynPostidKey(key)
}
}

// 创建Typecho API适配器
Expand Down
9 changes: 6 additions & 3 deletions src/adaptors/api/wordpress/useWordpressApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { createAppLogger } from "~/src/utils/appLogger.ts"
import { AppInstance } from "~/src/appInstance.ts"
import { Utils } from "~/src/utils/utils.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { JsonUtil, ObjectUtil } from "zhi-common"
import {JsonUtil, ObjectUtil, StrUtil} from "zhi-common"
import { WordpressConfig } from "~/src/adaptors/api/wordpress/config/wordpressConfig.ts"
import { WordpressApiAdaptor } from "~/src/adaptors/api/wordpress/adaptor/wordpressApiAdaptor.ts"
import { getDynPostidKey } from "~/src/components/set/publish/platform/dynamicConfig.ts"
Expand Down Expand Up @@ -71,12 +71,15 @@ export const useWordpressApi = async (key?: string, newCfg?: WordpressConfig) =>
"https://api.terwer.space/api/middleware"
)
cfg = new WordpressConfig(wordpressApiUrl, wordpressUsername, wordpressAuthToken, middlewareUrl)
// 默认值
cfg.posidKey = getDynPostidKey(key)
logger.info("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
}
// 初始化posidKey
if (StrUtil.isEmptyString(cfg.posidKey)) {
// 默认值
cfg.posidKey = getDynPostidKey(key)
}
}

// 创建Wordpress API适配器
Expand Down
9 changes: 6 additions & 3 deletions src/adaptors/api/yuque/useYuqueApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { AppInstance } from "~/src/appInstance.ts"
import { Utils } from "~/src/utils/utils.ts"
import { YuqueConfig } from "~/src/adaptors/api/yuque/config/yuqueConfig.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { JsonUtil, ObjectUtil } from "zhi-common"
import {JsonUtil, ObjectUtil, StrUtil} from "zhi-common"
import { getDynPostidKey } from "~/src/components/set/publish/platform/dynamicConfig.ts"
import { YuqueApiAdaptor } from "~/src/adaptors/api/yuque/adaptor/yuqueApiAdaptor.ts"

Expand Down Expand Up @@ -62,12 +62,15 @@ const useYuqueApi = async (key: string, newCfg?: YuqueConfig) => {
"https://api.terwer.space/api/middleware"
)
cfg = new YuqueConfig(yuqueUsername, yuqueAuthToken, middlewareUrl)
// 默认值
cfg.posidKey = getDynPostidKey(key)
logger.info("Configuration is empty, using default environment variables.")
} else {
logger.info("Using configuration from settings...")
}
// 初始化posidKey
if (StrUtil.isEmptyString(cfg.posidKey)) {
// 默认值
cfg.posidKey = getDynPostidKey(key)
}
}

// 创建 Yuque API 适配器
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Adaptors {
break
}
case SubPlatformType.Custom_Zhihu: {
const { webApi } = await useZhihuWeb()
const { webApi } = await useZhihuWeb(key, newCfg)
blogAdaptor = webApi
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,9 @@
* questions.
*/

import { WebApi } from "zhi-blog-api"
import { JsonUtil, StrUtil } from "zhi-common"
import { WebConfig } from "zhi-blog-api"

/**
* 知乎网页授权适配器
*
* @author terwer
* @version 0.9.0
* @since 0.9.0
* 网页授权配置
*/
class ZhihuWebAdaptor extends WebApi {
public async getMetaData(): Promise<object> {
const fetchResponse = await fetch(
"https://www.zhihu.com/api/v4/me?include=account_status%2Cis_bind_phone%2Cis_force_renamed%2Cemail%2Crenamed_fullname"
)
const resText = await fetchResponse.text()
// console.log(res);
const res = JsonUtil.safeParse<any>(resText, {} as any)
const flag = res.uid ? true : false
return {
flag: flag,
uid: res.uid,
title: res.name,
avatar: res.avatar_url,
supportTypes: ["html"],
type: "zhihu",
displayName: "知乎",
home: "https://www.zhihu.com/settings/account",
icon: "https://static.zhihu.com/static/favicon.ico",
}
}
}

export { ZhihuWebAdaptor }
export class CommonWebConfig extends WebConfig {}
31 changes: 31 additions & 0 deletions src/adaptors/web/base/web/config/CommonWebPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { WebPlaceholder } from "zhi-blog-api"

/**
* 网页授权操作提示
*/
export class CommonWebPlaceholder extends WebPlaceholder {}
115 changes: 115 additions & 0 deletions src/adaptors/web/zhihu/adaptor/zhihuWebAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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 { ElectronCookie, WebApi } from "zhi-blog-api"
import { JsonUtil } from "zhi-common"
import { AppInstance } from "~/src/appInstance.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { ZhihuConfig } from "~/src/adaptors/web/zhihu/config/zhihuConfig.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { SiyuanKernelApi } from "zhi-siyuan-api"

/**
* 知乎网页授权适配器
*
* @author terwer
* @version 0.9.0
* @since 0.9.0
*/
class ZhihuWebAdaptor extends WebApi {
private readonly logger
private cfg: ZhihuConfig
private readonly kernelApi: SiyuanKernelApi

/**
* 初始化博客园 API 适配器
*
* @param appInstance 应用实例
* @param cfg 配置项
*/
constructor(appInstance: AppInstance, cfg: ZhihuConfig) {
super()

this.cfg = cfg
this.logger = createAppLogger("cnblogs-api-adaptor")
const { kernelApi } = useSiyuanApi()
this.kernelApi = kernelApi
}

public updateCfg(cfg: ZhihuConfig) {
this.cfg = cfg
}

public async buildCookie(cookies: ElectronCookie[]): Promise<string> {
return cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join(";")
}

public async getMetaData(): Promise<object> {
const res = await this.proxyFetch(
"https://www.zhihu.com/api/v4/me?include=account_status%2Cis_bind_phone%2Cis_force_renamed%2Cemail%2Crenamed_fullname"
)
const flag = !!res.uid
this.logger.info(`get zhihu metadata finished, flag => ${flag}`)
return {
flag: flag,
uid: res.uid,
title: res.name,
avatar: res.avatar_url,
supportTypes: ["html"],
type: "zhihu",
displayName: "知乎",
home: "https://www.zhihu.com/settings/account",
icon: "https://static.zhihu.com/static/favicon.ico",
}
}

public async getPreviewUrl(postid: string): Promise<string> {
return Promise.resolve(`https://zhuanlan.zhihu.com/p/${postid}`)
}

// ================
// private methods
// ================
private async proxyFetch(url: string, params: any = {}): Promise<any> {
const fetchResult = await this.kernelApi.forwardProxy(
url,
[
{
Cookie: this.cfg.password,
},
],
params,
"GET",
"application/json",
7000
)
this.logger.debug("proxyFetch result=>", fetchResult)
const resText = fetchResult?.body
const res = JsonUtil.safeParse<any>(resText, {} as any)
return res
}
}

export { ZhihuWebAdaptor }
Loading

0 comments on commit 044091e

Please sign in to comment.