-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1549 from terwer/dev
fix halo auth error in some cases
- Loading branch information
Showing
39 changed files
with
307 additions
and
1,024 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* GNU GENERAL PUBLIC LICENSE | ||
* Version 3, 29 June 2007 | ||
* | ||
* Copyright (C) 2024 Terwer, Inc. <https://terwer.space/> | ||
* Everyone is permitted to copy and distribute verbatim copies | ||
* of this license document, but changing it is not allowed. | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* GNU GENERAL PUBLIC LICENSE | ||
* Version 3, 29 June 2007 | ||
* | ||
* Copyright (C) 2024 Terwer, Inc. <https://terwer.space/> | ||
* Everyone is permitted to copy and distribute verbatim copies | ||
* of this license document, but changing it is not allowed. | ||
*/ | ||
|
||
import { BaseWebApi } from "~/src/adaptors/web/base/baseWebApi.ts" | ||
import { StrUtil } from "zhi-common" | ||
import { MockBrowser } from "~/src/utils/MockBrowser.ts" | ||
|
||
/** | ||
* 小红书适配器 | ||
* | ||
* @author terwer | ||
* @since 1.32.0 | ||
*/ | ||
class XiaohongshuWebAdaptor extends BaseWebApi { | ||
public async getMetaData(): Promise<any> { | ||
// "/api/galaxy/user/info" | ||
// "/api/galaxy/user/my-info" | ||
const header = { | ||
"User-Agent": MockBrowser.HEADERS.MACOS_CHROME["User-Agent"], | ||
referer: "https://creator.xiaohongshu.com/publish/publish?from=menu", | ||
} | ||
const res = await this.xiaohongshuFetch("/api/galaxy/user/my-info", header) | ||
// const flag = !!res?.data?.user_id | ||
this.logger.debug("get xiaohongshu metadata finished=>", res) | ||
throw new Error("开发中。。。") | ||
} | ||
|
||
// ================ | ||
// private methods | ||
// ================ | ||
|
||
private async xiaohongshuFetch( | ||
url: string, | ||
headers: any = {}, | ||
params: any = undefined, | ||
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" = "GET", | ||
contentType: string = "application/json" | ||
) { | ||
const apiUrl = StrUtil.pathJoin(this.cfg.apiUrl, url) | ||
const reqHeaderMap = new Map<string, string>() | ||
reqHeaderMap.set("Cookie", this.cfg.password) | ||
|
||
const mergedHeaders = { | ||
...Object.fromEntries(reqHeaderMap), | ||
...headers, | ||
} | ||
|
||
const body = params | ||
|
||
// 构建请求选项 | ||
const requestOptions: RequestInit = { | ||
method: method, | ||
headers: mergedHeaders, | ||
body: params, | ||
} | ||
|
||
// 发送请求并返回响应 | ||
this.logger.debug("xiaohongshu url =>", apiUrl) | ||
this.logger.debug("xiaohongshu requestOptions =>", requestOptions) | ||
const res = await this.webFetch(apiUrl, [mergedHeaders], body, method, contentType, true, "base64", "text") | ||
return res | ||
} | ||
|
||
private async xiaohongshuFormFetch(url: string, formData: BodyInit) { | ||
const apiUrl = StrUtil.pathJoin(this.cfg.apiUrl, url) | ||
const reqHeaderMap = new Map<string, string>() | ||
reqHeaderMap.set("Cookie", this.cfg.password) | ||
|
||
// header | ||
const mergedHeaders = { | ||
Cookie: this.cfg.password, | ||
} | ||
|
||
const options: RequestInit = { | ||
method: "POST", | ||
headers: mergedHeaders, | ||
body: formData, | ||
} | ||
this.logger.debug("xiaohongshu form url =>", apiUrl) | ||
this.logger.debug("xiaohongshu form options =>", options) | ||
const res = await this.webFormFetch(apiUrl, [mergedHeaders], formData, true) | ||
return res | ||
} | ||
} | ||
|
||
export { XiaohongshuWebAdaptor } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* GNU GENERAL PUBLIC LICENSE | ||
* Version 3, 29 June 2007 | ||
* | ||
* Copyright (C) 2024 Terwer, Inc. <https://terwer.space/> | ||
* Everyone is permitted to copy and distribute verbatim copies | ||
* of this license document, but changing it is not allowed. | ||
*/ | ||
|
||
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common" | ||
import { XiaohongshuWebAdaptor } from "./XiaohongshuWebAdaptor" | ||
import { getDynPostidKey } from "~/src/platforms/dynamicConfig.ts" | ||
import { Utils } from "~/src/utils/utils" | ||
import { LEGENCY_SHARED_PROXT_MIDDLEWARE } from "~/src/utils/constants.ts" | ||
import { usePublishSettingStore } from "~/src/stores/usePublishSettingStore.ts" | ||
import { XiaohongshuConfig } from "~/src/adaptors/web/xiaohongshu/xiaohongshuConfig.ts" | ||
import { PublisherAppInstance } from "~/src/publisherAppInstance.ts" | ||
import { createAppLogger } from "~/src/utils/appLogger.ts" | ||
|
||
/** | ||
* 用于获取小红书的API的自定义Hook | ||
* | ||
* @param key - 平台 key | ||
* @param newCfg - 可选参数,用于指定新的配置 | ||
*/ | ||
const useXiaohongshuWeb = async (key?: string, newCfg?: XiaohongshuConfig) => { | ||
// 创建应用日志记录器 | ||
const logger = createAppLogger("use-xiaohongshu-web") | ||
|
||
// 记录开始使用小红书 WebAuth | ||
logger.info("Start using Xiaohongshu WebAuth...") | ||
|
||
// 创建应用实例 | ||
const appInstance = new PublisherAppInstance() | ||
let cfg: XiaohongshuConfig | ||
if (newCfg) { | ||
logger.info("Initialize with the latest newCfg passed in...") | ||
cfg = newCfg | ||
} else { | ||
// 从配置中获取数据 | ||
const { getSetting } = usePublishSettingStore() | ||
const setting = await getSetting() | ||
cfg = JsonUtil.safeParse<XiaohongshuConfig>(setting[key], {} as XiaohongshuConfig) | ||
// 如果配置为空,则使用默认的环境变量值,并记录日志 | ||
if (ObjectUtil.isEmptyObject(cfg)) { | ||
// 从环境变量获取小红书的cookie | ||
const middlewareUrl = Utils.emptyOrDefault(process.env.VITE_MIDDLEWARE_URL, LEGENCY_SHARED_PROXT_MIDDLEWARE) | ||
const xiaohongshuCookie = Utils.emptyOrDefault(process.env.VITE_XIAOHONGSHU_AUTH_TOKEN, "") | ||
cfg = new XiaohongshuConfig(xiaohongshuCookie, middlewareUrl) | ||
logger.debug("Configuration is empty, using default environment variables.") | ||
} else { | ||
logger.info("Using configuration from settings...") | ||
} | ||
const middlewareUrl = Utils.emptyOrDefault(process.env.VITE_MIDDLEWARE_URL, LEGENCY_SHARED_PROXT_MIDDLEWARE) | ||
if (StrUtil.isEmptyString(cfg.middlewareUrl)) { | ||
cfg.middlewareUrl = middlewareUrl | ||
} | ||
// 初始化posidKey | ||
if (StrUtil.isEmptyString(cfg.posidKey)) { | ||
// 默认值 | ||
cfg.posidKey = getDynPostidKey(key) | ||
} | ||
} | ||
|
||
// 小红书其他配置 | ||
|
||
const webApi = new XiaohongshuWebAdaptor(appInstance, cfg) | ||
return { | ||
cfg, | ||
webApi, | ||
} | ||
} | ||
|
||
export { useXiaohongshuWeb } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2024, 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, [email protected] | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { CommonWebPlaceholder } from "~/src/adaptors/web/base/commonWebPlaceholder.ts" | ||
|
||
class XiaohongshuPlaceholder extends CommonWebPlaceholder {} | ||
|
||
export { XiaohongshuPlaceholder } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.