This commit is contained in:
2025-12-25 12:18:42 +08:00
parent a06e12df5a
commit 96e6b6c0e1
3 changed files with 27 additions and 40 deletions

View File

@@ -26,6 +26,18 @@ const transformJsonKeys = (jsonData) => {
};
class XhsClient {
proxies: string | null;
timeout: number;
_host: string;
_creatorHost: string;
_customerHost: string;
home: string;
userAgent: string;
_cookie: string;
_headers: Record<string, string>;
signConfig?: Record<string, any> = {
signUrl: 'http://localhost:5005/sign',
};
/**
* Constructor for XhsClient
* @param {Object} options - Configuration options
@@ -108,7 +120,7 @@ class XhsClient {
const cookieDict = this.getCookieMap();
const newCookieDict = { ...cookieDict, ...data };
const cookieStr = Object.entries(newCookieDict)
.map(([key, value]) => {
.map(([key, value]: [string, string]) => {
const trimmedKey = key.trim();
const trimmedValue = value ? value.trim() : '';
return `${trimmedKey}=${trimmedValue}`;
@@ -119,12 +131,12 @@ class XhsClient {
this.cookie = cookieStr;
}
async request(method, url, config = {}) {
async request(method, url, config: any = {}) {
try {
delete config.sign;
const headers = { ...this._headers, ...(config.headers || {}) };
const fetchOptions = {
const fetchOptions: any = {
method: method,
headers: headers,
};
@@ -145,7 +157,7 @@ class XhsClient {
if (response.status === 471 || response.status === 461) {
const verifyType = response.headers.get('verifytype');
const verifyUuid = response.headers.get('verifyuuid');
throw new NeedVerifyError(`出现验证码请求失败Verifytype: ${verifyType}Verifyuuid: ${verifyUuid}`, response, verifyType, verifyUuid);
throw new NeedVerifyError(`出现验证码请求失败Verifytype: ${verifyType}Verifyuuid: ${verifyUuid}`, response, verifyType);
}
let data;
@@ -197,7 +209,7 @@ class XhsClient {
* @param {*} config
* @returns
*/
async requestSign(uri, data = null, config = {}, method = 'GET') {
async requestSign(uri, data = null, config: any = {}, method = 'GET') {
const needSign = config.needSign ?? true;
if (needSign && config.sign) {
await config.sign(uri, data, config, method);
@@ -324,7 +336,7 @@ class XhsClient {
if (stateMatch) {
const state = stateMatch[1].replace(/undefined/g, '""');
if (state !== '{}') {
const noteDict = transformJsonKeys(JSON.parse(state));
const noteDict = transformJsonKeys(JSON.parse(state)) as any;
return { code: 0, data: noteDict.note.note_detail_map[noteId].note };
}
}
@@ -375,32 +387,6 @@ class XhsClient {
return this.get(uri, params);
}
/**
*
* @uri /api/sns/web/v1/search/notes
* @param {string} keyword
* @param {number} page
* @param {number} pageSize
* @param {string} sort ,分为: general, popularity_descending, time_descending
* @param {number} noteType
* @returns
*/
async getNoteByKeyword(keyword, page = 1, pageSize = 20, sort = SearchSortType.GENERAL, noteType = SearchNoteType.ALL) {
const uri = '/api/sns/web/v1/search/notes';
const data = {
keyword: keyword,
page: page,
page_size: pageSize,
search_id: getSearchId(),
sort: sort.value,
note_type: noteType.value,
image_formats: ['jpg', 'webp', 'avif'],
ext_flags: [],
};
return this.post(uri, data);
}
/**
*
* @uri /api/sns/web/v2/comment/page
@@ -504,8 +490,9 @@ class XhsClient {
if (stateMatch) {
const state = stateMatch[1].replace(/"undefined"/g, '"_"').replace(/\bundefined\b/g, '""');
if (state !== '{}') {
const parsedState = JSON.parse(state);
const userBasicInfo = transformJsonKeys(parsedState).user.user_page_data.basic_info;
const parsedState = JSON.parse(state) as any;
const data = transformJsonKeys(parsedState) as any
const userBasicInfo =data.user.user_page_data.basic_info;
return userBasicInfo;
}
}

View File

@@ -1,7 +1,7 @@
import { getApiInfo } from './xhs-api/api.ts';
import { XhsClient as XhsClientBase } from './client-base.js';
import { Mention, CommonentInfo, ResponseMession } from './xhs-type/mention.ts';
import { pick } from 'lodash-es';
import { pick } from 'lodash-es';
import { getNote } from './modules/get-note.ts';
export type Result<T> = {
code: number; // 0: success
@@ -62,7 +62,7 @@ type XhsSign = {
signUrl?: string;
};
export class XhsClient extends XhsClientBase {
signConfig?: XhsSign;
declare signConfig?: XhsSign;
constructor(opts: XhsOptions) {
super(opts as any);
}
@@ -92,7 +92,7 @@ export class XhsClient extends XhsClientBase {
console.log('sign==>', data);
break;
case 'post':
console.log('post==>', data);
console.log('post==>', data);
break;
case 'error':
console.log('error==>', data);
@@ -181,7 +181,7 @@ export class XhsClient extends XhsClientBase {
needSign: true,
},
);
return response;
return response as any;
}
async sign(uri: string, data: any, config: any, method?: 'GET' | 'POST') {
let headers = config?.headers || {};

View File

@@ -12,7 +12,7 @@
}
},
"include": [
"src/**/*.ts", "src/libs/client-base.js",
"src/**/*.ts", "src/libs/client-base.ts",
],
"exclude": [],
}