generated from tailored/router-db-template
test
This commit is contained in:
@@ -26,6 +26,18 @@ const transformJsonKeys = (jsonData) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class XhsClient {
|
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
|
* Constructor for XhsClient
|
||||||
* @param {Object} options - Configuration options
|
* @param {Object} options - Configuration options
|
||||||
@@ -108,7 +120,7 @@ class XhsClient {
|
|||||||
const cookieDict = this.getCookieMap();
|
const cookieDict = this.getCookieMap();
|
||||||
const newCookieDict = { ...cookieDict, ...data };
|
const newCookieDict = { ...cookieDict, ...data };
|
||||||
const cookieStr = Object.entries(newCookieDict)
|
const cookieStr = Object.entries(newCookieDict)
|
||||||
.map(([key, value]) => {
|
.map(([key, value]: [string, string]) => {
|
||||||
const trimmedKey = key.trim();
|
const trimmedKey = key.trim();
|
||||||
const trimmedValue = value ? value.trim() : '';
|
const trimmedValue = value ? value.trim() : '';
|
||||||
return `${trimmedKey}=${trimmedValue}`;
|
return `${trimmedKey}=${trimmedValue}`;
|
||||||
@@ -119,12 +131,12 @@ class XhsClient {
|
|||||||
this.cookie = cookieStr;
|
this.cookie = cookieStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
async request(method, url, config = {}) {
|
async request(method, url, config: any = {}) {
|
||||||
try {
|
try {
|
||||||
delete config.sign;
|
delete config.sign;
|
||||||
const headers = { ...this._headers, ...(config.headers || {}) };
|
const headers = { ...this._headers, ...(config.headers || {}) };
|
||||||
|
|
||||||
const fetchOptions = {
|
const fetchOptions: any = {
|
||||||
method: method,
|
method: method,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
};
|
};
|
||||||
@@ -145,7 +157,7 @@ class XhsClient {
|
|||||||
if (response.status === 471 || response.status === 461) {
|
if (response.status === 471 || response.status === 461) {
|
||||||
const verifyType = response.headers.get('verifytype');
|
const verifyType = response.headers.get('verifytype');
|
||||||
const verifyUuid = response.headers.get('verifyuuid');
|
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;
|
let data;
|
||||||
@@ -197,7 +209,7 @@ class XhsClient {
|
|||||||
* @param {*} config
|
* @param {*} config
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async requestSign(uri, data = null, config = {}, method = 'GET') {
|
async requestSign(uri, data = null, config: any = {}, method = 'GET') {
|
||||||
const needSign = config.needSign ?? true;
|
const needSign = config.needSign ?? true;
|
||||||
if (needSign && config.sign) {
|
if (needSign && config.sign) {
|
||||||
await config.sign(uri, data, config, method);
|
await config.sign(uri, data, config, method);
|
||||||
@@ -240,7 +252,7 @@ class XhsClient {
|
|||||||
this.printResult('get', { uri, params, config });
|
this.printResult('get', { uri, params, config });
|
||||||
const endpoint = this.getEndpoint(config).endpoint;
|
const endpoint = this.getEndpoint(config).endpoint;
|
||||||
config = await this.requestSign(uri, null, config, 'GET');
|
config = await this.requestSign(uri, null, config, 'GET');
|
||||||
|
|
||||||
return this.request('GET', `${endpoint}${uri}`, config);
|
return this.request('GET', `${endpoint}${uri}`, config);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -324,7 +336,7 @@ class XhsClient {
|
|||||||
if (stateMatch) {
|
if (stateMatch) {
|
||||||
const state = stateMatch[1].replace(/undefined/g, '""');
|
const state = stateMatch[1].replace(/undefined/g, '""');
|
||||||
if (state !== '{}') {
|
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 };
|
return { code: 0, data: noteDict.note.note_detail_map[noteId].note };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -375,32 +387,6 @@ class XhsClient {
|
|||||||
return this.get(uri, params);
|
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
|
* @uri /api/sns/web/v2/comment/page
|
||||||
@@ -504,8 +490,9 @@ class XhsClient {
|
|||||||
if (stateMatch) {
|
if (stateMatch) {
|
||||||
const state = stateMatch[1].replace(/"undefined"/g, '"_"').replace(/\bundefined\b/g, '""');
|
const state = stateMatch[1].replace(/"undefined"/g, '"_"').replace(/\bundefined\b/g, '""');
|
||||||
if (state !== '{}') {
|
if (state !== '{}') {
|
||||||
const parsedState = JSON.parse(state);
|
const parsedState = JSON.parse(state) as any;
|
||||||
const userBasicInfo = transformJsonKeys(parsedState).user.user_page_data.basic_info;
|
const data = transformJsonKeys(parsedState) as any
|
||||||
|
const userBasicInfo =data.user.user_page_data.basic_info;
|
||||||
return userBasicInfo;
|
return userBasicInfo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { getApiInfo } from './xhs-api/api.ts';
|
import { getApiInfo } from './xhs-api/api.ts';
|
||||||
import { XhsClient as XhsClientBase } from './client-base.js';
|
import { XhsClient as XhsClientBase } from './client-base.js';
|
||||||
import { Mention, CommonentInfo, ResponseMession } from './xhs-type/mention.ts';
|
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';
|
import { getNote } from './modules/get-note.ts';
|
||||||
export type Result<T> = {
|
export type Result<T> = {
|
||||||
code: number; // 0: success
|
code: number; // 0: success
|
||||||
@@ -62,7 +62,7 @@ type XhsSign = {
|
|||||||
signUrl?: string;
|
signUrl?: string;
|
||||||
};
|
};
|
||||||
export class XhsClient extends XhsClientBase {
|
export class XhsClient extends XhsClientBase {
|
||||||
signConfig?: XhsSign;
|
declare signConfig?: XhsSign;
|
||||||
constructor(opts: XhsOptions) {
|
constructor(opts: XhsOptions) {
|
||||||
super(opts as any);
|
super(opts as any);
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ export class XhsClient extends XhsClientBase {
|
|||||||
console.log('sign==>', data);
|
console.log('sign==>', data);
|
||||||
break;
|
break;
|
||||||
case 'post':
|
case 'post':
|
||||||
console.log('post==>', data);
|
console.log('post==>', data);
|
||||||
break;
|
break;
|
||||||
case 'error':
|
case 'error':
|
||||||
console.log('error==>', data);
|
console.log('error==>', data);
|
||||||
@@ -181,7 +181,7 @@ export class XhsClient extends XhsClientBase {
|
|||||||
needSign: true,
|
needSign: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return response;
|
return response as any;
|
||||||
}
|
}
|
||||||
async sign(uri: string, data: any, config: any, method?: 'GET' | 'POST') {
|
async sign(uri: string, data: any, config: any, method?: 'GET' | 'POST') {
|
||||||
let headers = config?.headers || {};
|
let headers = config?.headers || {};
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src/**/*.ts", "src/libs/client-base.js",
|
"src/**/*.ts", "src/libs/client-base.ts",
|
||||||
],
|
],
|
||||||
"exclude": [],
|
"exclude": [],
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user