add db module

This commit is contained in:
2025-05-03 04:48:12 +08:00
parent ee483aa87e
commit c2a0623482
32 changed files with 28860 additions and 109 deletions

View File

@@ -0,0 +1,71 @@
export const list = [
{
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
},
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (iPad; CPU OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/87.0.4280.77 Mobile/15E148 Safari/604.1' },
{
'user-agent':
'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36',
},
{ 'user-agent': 'Mozilla/5.0 (Linux; Android) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.109 Safari/537.36 CrKey/1.54.248666' },
{ 'user-agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320' },
{ 'user-agent': 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+' },
{ 'user-agent': 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+' },
{ 'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' },
{ 'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' },
{ 'user-agent': 'Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 7.0; SM-G950U Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G965U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.111 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 8.1.0; SM-T837A) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; U; en-us; KFAPWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.13 Safari/535.19 Silk-Accelerated=true' },
{
'user-agent':
'Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/102.0.0.0 Mobile Safari/537.36',
},
{
'user-agent':
'Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 550) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263',
},
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 10 Build/MOB31T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36' },
{
'user-agent':
'Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR4.170623.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36',
},
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6 Build/N6F26U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36' },
{
'user-agent':
'Mozilla/5.0 (Linux; Android 8.0.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36',
},
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 520)' },
{ 'user-agent': 'Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13' },
{
'user-agent':
'Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36',
},
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 10; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 11; Pixel 3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Mobile Safari/537.36' },
{ 'user-agent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36' },
{
'user-agent':
'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36',
},
{
'user-agent':
'Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Mobile Safari/537.36',
},
{ 'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1' },
{
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
},
{ 'user-agent': 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1' },
];
export const randomUserAgent = () => {
const randomIndex = Math.floor(Math.random() * list.length);
return list[randomIndex]['user-agent'];
};

View File

@@ -0,0 +1,50 @@
type ApiInfo = {
uri: string;
method?: 'GET' | 'POST';
needSign?: boolean;
description?: string;
isCreator?: boolean;
};
export const api: ApiInfo[] = [
{
uri: '/api/sns/web/v1/you/mentions',
method: 'GET',
description: '获取@我的消息',
},
{
uri: '/api/sns/web/v1/you/likes',
method: 'GET',
description: '获取点赞的消息',
},
{
uri: '/api/sns/web/v1/you/connections',
method: 'GET',
description: '获取关注的消息',
needSign: true,
},
{
uri: '/api/sns/web/v1/you/mentions',
method: 'GET',
needSign: true,
description: '获取@我的消息',
},
];
type ReturnApiInfo = {
apiInfo: ApiInfo;
needSign: boolean;
};
export const getApiInfo = (uri: string): ReturnApiInfo | null => {
const apiInfo = api.find((item) => item.uri?.startsWith(uri));
if (apiInfo) {
return {
apiInfo,
needSign: apiInfo.needSign ?? true,
};
} else {
}
return {
apiInfo: null,
needSign: true,
};
};

View File

@@ -1,3 +1,4 @@
import { getApiInfo } from './xhs-api/api.ts';
import { XhsClient as XhsClientBase } from '@kevisual/xhs-core';
import { Mention, CommonentInfo } from './xhs-type/mention.ts';
@@ -13,17 +14,29 @@ type SignInfo = {
a1: string;
web_session?: string;
};
type SignResponse = {
a1: string;
sign: {
b1: string;
b1b1: string;
['x-s']: string;
['x-t']: string;
};
[key: string]: any;
};
type SignOptions = {
signUrl?: string;
};
export const getSign = async (signInfo: SignInfo, options?: SignOptions) => {
export const getSign = async (signInfo: SignInfo, options?: SignOptions): Promise<SignResponse> => {
const { uri, data, a1, web_session } = signInfo;
// console.log('getSign', uri, data, a1, web_session);
// let signUri = new URL(uri, 'http://light.xiongxiao.me:5006').pathname;
// signUri = '/api/sns/web/v2/user/me';
try {
let signUrl = options?.signUrl || 'http://light.xiongxiao.me:5006/sign';
// signUrl = 'http://localhost:5005/sign'
let signUrl = options?.signUrl || 'http://localhost:5005/sign';
// signUrl = 'http://localhost:5005/sign';
// const urlA1 = ''http://light.xiongxiao.me:5006/a1';
// const urlA1 = 'http://localhost:5005/a1';
const signs = await fetch(signUrl, {
method: 'POST',
headers: {
@@ -52,18 +65,27 @@ export class XhsClient extends XhsClientBase {
constructor(opts: XhsOptions) {
super(opts as any);
}
printResult(msg: string, response: any) {
getApiInfo = getApiInfo;
printResult(msg: string, data: any) {
if (msg === 'response') {
console.log('url', response.url);
if (response.response) {
console.log('status', response.response.status);
console.log('data', response.response.data);
console.log('url', data.url);
console.log('status', data?.response?.status);
if (data.response) {
console.log('data', data.response.data);
}
} else if (msg === 'request') {
// console.log('request', response);
console.log('request', data);
} else if (msg === 'html') {
// console.log('html', response);
}
switch (msg) {
case 'get':
console.log('get', data);
break;
case 'sign':
console.log('sign', data);
break;
}
}
/**
* 获取未读消息
@@ -71,7 +93,7 @@ export class XhsClient extends XhsClientBase {
*/
async getUnread(): Promise<Result<UnreadCount>> {
const url = '/api/sns/web/unread_count';
const response = await this.get(url);
const response = await this.get(url, null, { needSign: false });
return response;
}
@@ -103,6 +125,12 @@ export class XhsClient extends XhsClientBase {
const response = await super.getUserInfoFromHtml(userId);
return response as ReturnData;
}
/**
* 这个接口不能多用否则会出现封控406错误
* @param num
* @param cursor
* @returns
*/
async getFollowNotifications(num = 10, cursor = '') {
const url = '/api/sns/web/v1/you/connections';
type Connection = {
@@ -129,18 +157,14 @@ export class XhsClient extends XhsClientBase {
* @uri /api/sns/web/v1/you/mentions
* @returns
*/
async getMention(num = 10): Promise<Result<Mention>> {
async getMention(num = 20): Promise<Result<Mention>> {
const url = '/api/sns/web/v1/you/mentions';
const response = await this.get(
url,
{ num },
{ num: 20, cursor: '' },
{
sign: this.sign.bind(this),
// headers: {
// 'x-s':
// 'XYW_eyJzaWduU3ZuIjoiNTYiLCJzaWduVHlwZSI6IngyIiwiYXBwSWQiOiJsb2dpbiIsInNpZ25WZXJzaW9uIjoiMSIsInBheWxvYWQiOiJiNGJmMGI2MDVkZTlkOWMyY2RlNTI2YmVjNjM2ZmIxMjkxYzUxMTIyYWQyOTk5MzIyMzNjMmU0OTEzMWFmYzgzY2FmOGQzZDIzMTA0Y2RlNWUzZDZlZDczMDg0MmUzYzAxOTNkY2FjZjEyZjk1NTMzZGQzY2ZkMGFmOTg5MGZmMDIwNWI0MmQwOTNiYmJjMGNkZWU3MzdmOGE2MmRkYWVlYjZhMjcxZDViNjZkNGRjYjA1NDg2MGZhNTllN2M5MjE0ZDE2OTJjYWQyZjZmNzE1NThmYWQ3YjQxZjlhZTNiYjA1ZDExN2YzYWI2ZjRjYzY5MzcyMzRhOTY1OTkxYzMwMWY2YjI1MzY4MTZiNzM1YzhmMWEzOTk2ODhkMWU0NDFiODljYTNlNzQ3YWNlN2M2MGIzZDlhZWQwZDVlZDZlNGFhMDE5MmQ5YzZjNDE1M2IxM2RjODAwYjUzZTQxYWEzOTU4MjJhMzYyMmJjODEwYmY4MzA3MjkwMjY2ZDUzNmQwMjdkMTJlOWEwMzhlZmY1YWU4OTM5NDVlNDhmYmY2MCJ9',
// 'x-t': '1746097556685',
// },
needSign: true,
},
);
return response;
@@ -148,15 +172,25 @@ export class XhsClient extends XhsClientBase {
async sign(uri: string, data: any, config: any) {
let headers = config?.headers || {};
const cookieDist = this.getCookieMap();
const apiInfo = this.getApiInfo(uri);
if (apiInfo && !apiInfo?.needSign) {
return config || {};
}
const web_session = cookieDist['web_session'];
const a1 = cookieDist['a1'];
const res = await getSign({ uri, data, a1, web_session }, this.signConfig);
if (res) {
headers['x-s'] = res?.['x-s'];
headers['x-t'] = res?.['x-t'];
const _sign = res.sign;
this.printResult('sign', { uri, apiInfo, res });
const xs = _sign?.['x-s'];
const xt = _sign?.['x-t'];
const b1 = _sign?.['b1'];
if (res && xs) {
headers['x-s'] = xs;
headers['x-t'] = xt;
// headers['x-s-common'] = this.getXCommon(a1, b1, xs, xt);
config.headers = headers;
} else {
console.log('get sign error');
console.log('get sign error', res);
throw new Error('get sign error');
}
return config;