@nite07/search-core 0.1.0 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/dist/adapters.d.ts +20 -1
- package/dist/adapters.js +21 -4
- package/dist/browser-backends.d.ts +7 -0
- package/dist/browser-backends.js +24 -0
- package/dist/browser.d.ts +46 -0
- package/dist/browser.js +130 -0
- package/dist/catalog.js +1 -0
- package/dist/config.d.ts +8 -0
- package/dist/config.js +50 -17
- package/dist/engine.js +29 -3
- package/dist/index.d.ts +5 -1
- package/dist/index.js +4 -1
- package/dist/media.d.ts +39 -0
- package/dist/media.js +84 -0
- package/dist/reddit/shape.d.ts +20 -0
- package/dist/reddit/shape.js +50 -0
- package/dist/types.d.ts +12 -2
- package/dist/x/parse.d.ts +4 -0
- package/dist/x/parse.js +25 -2
- package/dist/xhs/index.d.ts +94 -0
- package/dist/xhs/index.js +160 -0
- package/dist/xhs/scripts.d.ts +29 -0
- package/dist/xhs/scripts.js +127 -0
- package/package.json +4 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Xiaohongshu page scripts. The site's own webpack HTTP client is the only thing that can make
|
|
3
|
+
* these calls — a plain in-page fetch was refused with 406 — so each script locates that client
|
|
4
|
+
* inside the running page and performs one read. Module ids are never hardcoded: they are found
|
|
5
|
+
* by route literals and the registration call, so a rebuild of the site does not break this.
|
|
6
|
+
*/
|
|
7
|
+
const PRELUDE = `
|
|
8
|
+
const str=(v)=>(typeof v==='string'?v:'');
|
|
9
|
+
const status=(e)=>(e&&e.response&&e.response.status)||0;
|
|
10
|
+
const fail=(code,message)=>({ok:false,code,message:String(message===undefined?'':message).slice(0,300)});
|
|
11
|
+
let captured=null;
|
|
12
|
+
if(!Array.isArray(window.webpackChunkxhs_pc_web)){return fail('NOT_READY','the page has not loaded the site runtime');}
|
|
13
|
+
try{
|
|
14
|
+
window.webpackChunkxhs_pc_web.push([['xhs_bridge_'+Date.now()+'_'+Math.random().toString(36).slice(2)],[],(req)=>{captured=req;}]);
|
|
15
|
+
}catch(e){return fail('NO_RUNTIME',e&&e.message);}
|
|
16
|
+
if(!captured||!captured.m){return fail('NO_RUNTIME','no webpack require was captured');}
|
|
17
|
+
let hitId=null,hitSrc='';
|
|
18
|
+
for(const key of Object.keys(captured.m)){
|
|
19
|
+
let src='';try{src=String(captured.m[key]);}catch{continue;}
|
|
20
|
+
if(src.indexOf('/api/sns/web/v2/user/me')>=0&&src.indexOf('/api/sns/web/v1/feed')>=0){hitId=key;hitSrc=src;break;}
|
|
21
|
+
}
|
|
22
|
+
if(!hitId){return fail('NO_CLIENT','no module registers both the identity and feed routes');}
|
|
23
|
+
const reg=/registerHttp[^]*?\\}\\(\\w+\\((\\d+)\\)\\.([A-Za-z_$][\\w$]*)\\)\\}/.exec(hitSrc);
|
|
24
|
+
if(!reg){return fail('NO_REGISTRATION','the http client registration shape changed');}
|
|
25
|
+
let candidate=null;
|
|
26
|
+
try{const mod=captured(Number(reg[1]));candidate=mod?mod[reg[2]]:null;}catch(e){return fail('NO_CLIENT',e&&e.message);}
|
|
27
|
+
const client=candidate&&typeof candidate.get==='function'?candidate:(candidate&&candidate.default?candidate.default:candidate);
|
|
28
|
+
if(!client||typeof client.get!=='function'||typeof client.post!=='function'){return fail('BAD_CLIENT','the captured export has no get/post');}
|
|
29
|
+
`;
|
|
30
|
+
const wrap = (body) => `async () => {\n${PRELUDE}\ntry{${body}}catch(e){return {ok:false,code:'CALL_FAILED',status:status(e),message:String((e&&e.message)||e).slice(0,300)};}\n}`;
|
|
31
|
+
const literal = (value) => JSON.stringify(value);
|
|
32
|
+
/** Identity probe: is the runtime usable, and is the session signed in? */
|
|
33
|
+
export function healthScript() {
|
|
34
|
+
return wrap(`const me=await client.get('/api/sns/web/v2/user/me');return {ok:true,guest:!!(me&&me.guest),signedIn:!!(me&&me.nickname)};`);
|
|
35
|
+
}
|
|
36
|
+
export function searchScript(input) {
|
|
37
|
+
return wrap(`
|
|
38
|
+
const r=await client.post('/api/sns/web/v2/search/notes',{
|
|
39
|
+
keyword:${literal(input.query)},page:${input.page},page_size:20,sort:${literal(input.sort)},note_type:0,ext_flags:[],image_formats:['jpg','webp','avif'],geo:''
|
|
40
|
+
},{baseURL:'https://so.xiaohongshu.com',withCredentials:true,transform:true});
|
|
41
|
+
const list=Array.isArray(r&&r.items)?r.items:[];
|
|
42
|
+
const items=[];
|
|
43
|
+
for(const item of list){
|
|
44
|
+
if(!item||item.modelType!=='note'){continue;}
|
|
45
|
+
const card=item.noteCard||{};const user=card.user||{};const stats=card.interactInfo||{};
|
|
46
|
+
items.push({
|
|
47
|
+
id:String(item.id||''),
|
|
48
|
+
title:str(card.displayTitle||card.title),
|
|
49
|
+
author:str(user.nickname||user.nickName),
|
|
50
|
+
authorId:str(user.userId),
|
|
51
|
+
likes:stats.likedCount||'0',
|
|
52
|
+
url:'https://www.xiaohongshu.com/explore/'+String(item.id||''),
|
|
53
|
+
cover:str(card.cover&&(card.cover.urlDefault||card.cover.url)),
|
|
54
|
+
xsecToken:str(item.xsecToken)
|
|
55
|
+
});
|
|
56
|
+
if(items.length>=${input.limit}){break;}
|
|
57
|
+
}
|
|
58
|
+
return {ok:true,items:items,hasMore:!!(r&&r.hasMore),raw:list.length};`);
|
|
59
|
+
}
|
|
60
|
+
export function noteScript(input) {
|
|
61
|
+
return wrap(`
|
|
62
|
+
const feed=await client.post('/api/sns/web/v1/feed',{
|
|
63
|
+
source_note_id:${literal(input.id)},
|
|
64
|
+
image_formats:['jpg','webp','avif'],
|
|
65
|
+
extra:{need_head_cursor:true},
|
|
66
|
+
xsec_token:${literal(input.token)}
|
|
67
|
+
});
|
|
68
|
+
const first=feed&&Array.isArray(feed.items)?feed.items[0]:null;
|
|
69
|
+
const card=first&&first.noteCard?first.noteCard:null;
|
|
70
|
+
if(!card){return {ok:false,code:'EMPTY','message':'the feed came back without a note card'};}
|
|
71
|
+
const user=card.user||{};const stats=card.interactInfo||{};
|
|
72
|
+
return {ok:true,note:{
|
|
73
|
+
id:${literal(input.id)},
|
|
74
|
+
title:str(card.title||card.displayTitle),
|
|
75
|
+
desc:str(card.desc),
|
|
76
|
+
author:str(user.nickname||user.nickName),
|
|
77
|
+
authorId:str(user.userId),
|
|
78
|
+
likes:stats.likedCount||'0',
|
|
79
|
+
collects:stats.collectedCount||'0',
|
|
80
|
+
comments:stats.commentCount||'0',
|
|
81
|
+
shares:stats.sharedCount||'0',
|
|
82
|
+
publishedAt:str(card.time||card.lastUpdateTime),
|
|
83
|
+
tags:(card.tagList||[]).map(function(t){return str(t&&t.name);}).filter(Boolean).slice(0,20),
|
|
84
|
+
imageUrls:(card.imageList||[]).map(i=>str(i.urlDefault||i.urlPre||i.url)).filter(Boolean),
|
|
85
|
+
url:'https://www.xiaohongshu.com/explore/'+${literal(input.id)}
|
|
86
|
+
}};`);
|
|
87
|
+
}
|
|
88
|
+
export function commentsScript(input) {
|
|
89
|
+
return wrap(`
|
|
90
|
+
const page=await client.get('/api/sns/web/v2/comment/page',{params:{
|
|
91
|
+
note_id:${literal(input.id)},cursor:${literal(input.cursor)},top_comment_id:'',image_formats:['jpg','webp','avif'],xsec_token:${literal(input.token)}
|
|
92
|
+
}});
|
|
93
|
+
const list=Array.isArray(page&&page.comments)?page.comments:[];
|
|
94
|
+
const out=[];
|
|
95
|
+
for(const c of list){
|
|
96
|
+
if(!c||c.invalid){continue;}
|
|
97
|
+
const u=c.userInfo||{};
|
|
98
|
+
out.push({
|
|
99
|
+
id:String(c.id||''),
|
|
100
|
+
author:str(u.nickname||u.nickName),
|
|
101
|
+
content:str(c.content),
|
|
102
|
+
likes:c.likeCount||'0',
|
|
103
|
+
replies:c.subCommentCount||'0',
|
|
104
|
+
ipLocation:str(c.ipLocation),
|
|
105
|
+
liked:!!c.liked
|
|
106
|
+
});
|
|
107
|
+
if(out.length>=${input.limit}){break;}
|
|
108
|
+
}
|
|
109
|
+
return {ok:true,comments:out,hasMore:!!(page&&page.hasMore),cursor:str(page&&page.cursor)};`);
|
|
110
|
+
}
|
|
111
|
+
/** Set the session cookies inside the page, then prove they work before reporting success. */
|
|
112
|
+
export function sessionScript(input) {
|
|
113
|
+
return wrap(`
|
|
114
|
+
const pairs=${literal(input.cookies)}.split(';');
|
|
115
|
+
let applied=0;
|
|
116
|
+
for(const pair of pairs){
|
|
117
|
+
const idx=pair.indexOf('=');
|
|
118
|
+
if(idx<=0){continue;}
|
|
119
|
+
const name=pair.slice(0,idx).trim();
|
|
120
|
+
const value=pair.slice(idx+1).trim();
|
|
121
|
+
if(!name||!value){continue;}
|
|
122
|
+
document.cookie=name+'='+value+'; path=/; domain=.xiaohongshu.com';
|
|
123
|
+
applied++;
|
|
124
|
+
}
|
|
125
|
+
const me=await client.get('/api/sns/web/v2/user/me');
|
|
126
|
+
return {ok:true,applied:applied,guest:!!(me&&me.guest),signedIn:!!(me&&me.nickname)};`);
|
|
127
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nite07/search-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Provider-agnostic multi-backend search engine: backend catalog, adapters, transport and honest failure reporting. No DSH dependency.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "nite07",
|
|
@@ -50,5 +50,8 @@
|
|
|
50
50
|
"@types/node": "^22.10.2",
|
|
51
51
|
"tsx": "^4.19.2",
|
|
52
52
|
"typescript": "^5.7.2"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"patchright-core": "1.64.3"
|
|
53
56
|
}
|
|
54
57
|
}
|