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

@@ -1,6 +1,9 @@
import { program } from 'commander';
import './query/add-comment.ts';
import './query/get-components.ts';
import './query/get-note.ts';
import './query/get-sign.ts'
import './query/mention.ts'
import './query/unread.ts';
import './query/get-userinfo.ts';

View File

@@ -1,8 +1,4 @@
import { XhsClient, xhsServices } from '../index.ts';
import { xhsServices } from '../index.ts';
import { program } from 'commander';
export const cookie =
'a1=1968ba02ff4xrt6hfrzdiz7ubs82j9y3vx11vfw9c40000317680;abRequestId=0a794332-4561-5f49-93f7-780b8b028e1f;access-token-creator.xiaohongshu.com=customer.creator.AT-68c517498636784561614544frjvxzj7yu8iewie;agora_session=6a0031373435393132333637323735343733393437313634000000000000;customerClientId=536706778174172;galaxy_creator_session_id=OhpHDDSoADhNEhnH5LLnQpletFLApu1fd91f;galaxy.creator.beaker.session.id=1745912429847011598150;gid=yjKqYfK0qDyYyj2DDSqd4ujxyW9kvxIuT62ddkMWhElyuxq8yDd6hl888q2WYy88j8i80yYD;loadts=1746020512562;sec_poison_id=441c932e-a6ac-4d8d-97ae-beb14adb1929;unread={%22ub%22:%2267eaf1fe000000001202c3ea%22%2C%22ue%22:%226803aa37000000001c0319d8%22%2C%22uc%22:35};web_session=040069b2e9c511ca302086ca253a4bde8b1cd1;webBuild=4.62.3;webId=97e5f097499594cad49aa0bd1a8ed83f;websectiga=3633fe24d49c7dd0eb923edc8205740f10fdb18b25d424d2a2322c6196d2a4ad;x-user-id-creator.xiaohongshu.com=639d86590000000026006076;xsecappid=xhs-pc-web;acw_tc=0a00df6217460205042195762e721fba339a0dbe8e4738b961a5ff15e74619;';
export const client = new XhsClient({ cookie } as any);
export { program, xhsServices };

View File

@@ -0,0 +1,24 @@
import { Comment } from '@/services/xhs-db/comment.ts';
import { Sequelize, Model, DataTypes, InferAttributes, InferCreationAttributes } from 'sequelize';
import path from 'path';
const dbPath = path.join(process.cwd(), './test2.db');
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: dbPath,
logging: false,
});
const commentTest = async () => {
await Comment.initModel(sequelize, null, { alter: true });
const comment = await Comment.create({
content: 'test',
note_id: '1',
user_id: '1',
comment_id: '1',
});
console.log(comment.toJSON());
const comments = await Comment.findAll();
console.log(comments.map((item) => item.toJSON()));
};
commentTest();

View File

@@ -0,0 +1,40 @@
import { Sequelize, Model, DataTypes, InferAttributes, InferCreationAttributes } from 'sequelize';
import path from 'path';
const dbPath = path.join(process.cwd(), './test2.db');
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: dbPath,
});
console.log('dbPath', dbPath);
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch((err) => {
console.error('Unable to connect to the database:', err);
});
// Define a User model
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
declare username: string | null;
declare birthday: Date | null;
}
User.init(
{
username: DataTypes.STRING,
birthday: DataTypes.DATE,
},
{ sequelize, modelName: 'user' },
);
(async () => {
await sequelize.sync({ alter: true });
const jane = await User.create({
username: 'janedoe',
birthday: new Date(1980, 6, 20),
});
console.log(jane.toJSON());
const users = await User.findAll();
console.log(users.map((item) => item.toJSON()));
})();

View File

@@ -0,0 +1,20 @@
import sqlite3 from 'sqlite3';
const db = new sqlite3.Database('./test.db');
db.serialize(() => {
db.run('CREATE TABLE lorem (info TEXT)');
const stmt = db.prepare('INSERT INTO lorem VALUES (?)');
for (let i = 0; i < 10; i++) {
stmt.run('Ipsum ' + i);
}
stmt.finalize();
db.each('SELECT rowid AS id, info FROM lorem', (err, row) => {
// @ts-ignore
console.log(row.id + ': ' + row.info);
});
});
db.close();

View File

@@ -0,0 +1,30 @@
const res = await fetch('https://edith.xiaohongshu.com/api/sns/web/v1/you/mentions?num=1&cursor=', {
headers: {
accept: 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
priority: 'u=1, i',
'sec-ch-ua': '"Google Chr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ozdJ/c9aMpC2rSiPoPI/rTAydb7JdD7zbkQ4fRA2BQcydSy4LbQyrTSzBr7q98ppbztqgzat7b7cgmDqrEQc9YT/Sqha7kn4M+Qc94Sy7pFao4l4FzQzL8laLL6qMzQnfSQ2oQ+ag8d8nzl4MH3+7mc2Skwq9z8P9pfqgzmanTw8/+n494lqgzIqopF2rTC87Plp7mSaL+npFSiL/Z6LozzaM87cLDAn0Q6JnzSygb78DSecnpLpdzUaLL3tFSbJnE08fzSyf4CngQ6J7+fqg4OnS468nzPzrzsJ94AySkIcDSha7+DpdzYanT98n8l4MQj/LlQz9GFcDDA+9pL4gz/NM+N8/r7/pmQyBQAaLpbt741+BSQPMSlwBlb8FS3/oY6qg43aL+yp0QDP9pxan4APgp7LDS989LIPo8SPsRMLrSk87PAL9Mr2gp74LSka9pL80mA2BF68/bn4ezPqFkSngb7yrS9zL40PBRS8op7zgm1N7+/qg4dagYzqDS9y9T6pd4o2S87t9bd+bbcpURS8BEd8pzn49RQznRAyDQmq7YPqd8Qz/mAPrIAqA8fz9zQyrEAP7p74DSbJp4F4g4I/7b7cFDAafpLpdzBanVAq9Sl4ApQye+Aydp7+LEl4946pd4MaL+CaLS9LLRzq9DAGDH98pzl4F8QypboaLLM8nzM4r4ynfTFcSLMqMqILLzQyF4PadbFa9bl47pQ40+S+S8F2bbIa9LAaLbSPLl/qDSbad+3cDRSPb87Lf+c49bQcFkApMm7pLSk89p38gbPanVAqAb087+n4g4IanYS8p+n4Bky+FzAanDh4rDA+fL92DkAy9iMq9iI89LI4g46anY3aLSbqnb0Lo40agY0LFDAPBpLp9MQ2nL68pSgO/FjNsQhwaHCP/HhP/LUwerIPjIj2erIH0iUP0LEKc==',
'x-t': '1746181808129',
'x-xray-traceid': 'cb4845eff6cf7d70df0fe575e3d8af06',
cookie:
'acw_tc=0a0b121417461803133571274e75c1c431e8abf9d81133812d3ca340bf8fe2; abRequestId=e1b9d999-8838-528a-9933-5f3ac9134d8c; webBuild=4.62.3; xsecappid=xhs-pc-web; a1=1969088bf22oidhober22hsb74h3qoavpucdvmrbb30000712484; webId=bffbeec4c301c7b3dc284ee35dd742fb; websectiga=8886be45f388a1ee7bf611a69f3e174cae48f1ea02c0f8ec3256031b8be9c7ee; sec_poison_id=d6fbc71a-7538-4ebe-acd6-5e5b79bc78eb; gid=yjKj8YYdSSUqyjKj8YYDi76FJJl3fxlDdFJJSDDxMDW4xfq8qAl0hh888WyJ4Y48ySjjfKfd; web_session=040069b2e9c511ca302098d5213a4b8556ed1d; unread={%22ub%22:%2268077ca2000000001b03bff6%22%2C%22ue%22:%22680e491b000000000b015506%22%2C%22uc%22:30}; loadts=1746181806203',
Referer: 'https://www.xiaohongshu.com/',
'Referrer-Policy': 'strict-origin-when-cross-origin',
},
body: null,
method: 'GET',
});
res.json().then((data) => {
console.log('data', data);
});

View File

@@ -0,0 +1,14 @@
import { xhsServices, program } from '../common.ts';
program
.command('add-comment')
.description('add comment')
.action(async () => {
const client = xhsServices.getClient();
const res = await client.postComment({
note_id: '68136dab0000000007034c46',
content: 'test comment 233',
comment_id: '68136dcf000000000401a8c9',
});
console.log(res);
});

View File

@@ -1,14 +1,14 @@
import { client } from '../common.ts';
import { program } from 'commander';
import { xhsServices, program } from '../common.ts';
import utils from 'node:util';
program
.command('get-commonents')
.description('get commonents')
.action(async () => {
const res = await client.getNoteComments('6810d722000000002100f139', '', {
const client = xhsServices.getClient();
const res = await client.getNoteComments('68136dab0000000007034c46', '', {
//
xsec_token: 'LBEqTFigLzp41AdwQ-E3hbQScnvrx2flLgHElHpQ8zHWc=',
top_comment_id: '6810e7d80000000012020c7a'
xsec_token: 'LByEmonX8WfJ9ebpAowVbOZX9Xh8T0Qkjil5KRFqDD6LM=',
});
console.log(res);
console.log(utils.inspect(res, { depth: null, colors: true }));
});

View File

@@ -2,8 +2,10 @@ import { xhsServices, program } from '../common.ts';
import util from 'node:util';
const getConnections = async () => {
const client = xhsServices.getClient();
const res = await client.getFollowNotifications(10, '');
console.log(util.inspect(res, { depth: null, colors: true }));
const res = await client.getFollowNotifications(2, '');
if (res.code === 0) {
console.log(util.inspect(res, { depth: null, colors: true }));
}
};
program

View File

@@ -1,5 +1,5 @@
import { client } from '../common.ts';
import { program } from 'commander';
import { xhsServices, program } from '../common.ts';
// client.getNoteComments()
@@ -7,13 +7,15 @@ import { program } from 'commander';
// console.log(res);
// });
const getNoteById = async () => {
const client = xhsServices.getClient();
client.getNoteById('67dcc34e000000000602a8eb', 'ABuYS8Xb1o08DlRmMLIabdqnW0OKnLR9nMpDGq5bVRdvk').then((res) => {
console.log(res);
});
};
const getNote = async () => {
const id = '6810d722000000002100f139';
const x = 'LBEqTFigLzp41AdwQ-E3hbQScnvrx2flLgHElHpQ8zHWc=';
const id = '68136dab0000000007034c46';
const x = 'LByEmonX8WfJ9ebpAowVbOZX9Xh8T0Qkjil5KRFqDD6LM=';
const client = xhsServices.getClient();
client.getNoteByIdFromHtml(id, x).then((res) => {
console.log(res);
});

View File

@@ -1,9 +1,11 @@
import { client, program } from '../common.ts';
import { xhsServices, program } from '../common.ts';
const getSign = async () => {
const uri = '/api/sns/web/v1/you/mentions';
const uri = '/api/sns/web/v1/you/mentions?num=20&cursor=';
const data = null;
// _webmsxyw('/api/sns/web/v1/you/mentions?num=20&cursor=', null);
const config = { headers: {} };
const client = xhsServices.getClient();
const res = await client.sign(uri, data, config);
console.log('getSign', res);
};

View File

@@ -1,14 +1,16 @@
import { client, program } from '../common.ts';
import { xhsServices, program } from '../common.ts';
program
.command('get-userinfo')
.description('获取用户信息')
.action(async () => {
const client = xhsServices.getClient();
const res = await client.getSelfInfoV2();
console.log(res);
});
const getUserById = async (userId: string) => {
const client = xhsServices.getClient();
const res = await client.getUserInfoFromHtml(userId);
console.log(res);
};

View File

@@ -1,10 +1,12 @@
import { client, program } from '../common.ts';
import { xhsServices, program } from '../common.ts';
import util from 'node:util';
const getMentions = async () => {
try {
const res = await client.getMention();
if (res.code === 0) {
console.log('getMentionNotifications', util.inspect(res, { depth: 10 }));
const client = xhsServices.getClient();
const res = await client.getMention(1);
if (res.code) {
const data = res.data || {};
console.log('getMentionNotifications', util.inspect(data, { depth: 10 }));
}
} catch (e) {
console.error('error');
@@ -19,6 +21,7 @@ const getTestMentionNote = async () => {
const title = '在笔记中@了你';
const type = 'mention/item';
const track_type = '2';
const client = xhsServices.getClient();
const note = await client.getNoteByIdFromHtml(id, xsec_token);
console.log('note', note);
};

View File

@@ -1,6 +1,7 @@
import { client, program } from '../common.ts';
import { xhsServices, program } from '../common.ts';
const getUnread = () => {
const client = xhsServices.getClient();
client.getUnread().then((res) => {
console.log(res);
});