publishport-opencli 1.0.68 → 1.0.69
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/cli-manifest.json
CHANGED
|
@@ -52701,6 +52701,13 @@
|
|
|
52701
52701
|
"default": false,
|
|
52702
52702
|
"required": false,
|
|
52703
52703
|
"help": "Include nested replies (楼中楼)"
|
|
52704
|
+
},
|
|
52705
|
+
{
|
|
52706
|
+
"name": "replies-limit",
|
|
52707
|
+
"type": "int",
|
|
52708
|
+
"default": 3,
|
|
52709
|
+
"required": false,
|
|
52710
|
+
"help": "With --with-replies: max nested replies per top-level comment (default 3, 0 = all). Capped threads show replies_complete=false; read a whole thread with `replies <url> <root-id>`"
|
|
52704
52711
|
}
|
|
52705
52712
|
],
|
|
52706
52713
|
"columns": [
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import{cli as
|
|
1
|
+
import{cli as g,Strategy as w}from"@jackwener/opencli/registry";import{AuthRequiredError as x,CommandExecutionError as l,EmptyResultError as b}from"@jackwener/opencli/errors";import{parseNoteId as _,buildNoteUrl as k}from"./note-helpers.js";import{readXhsDetailPage as S}from"./risk-control.js";import{readCommentPage as R}from"./_shared/comment-state.js";const C='.author-wrapper a[href*="/user/profile/"], a.name[href*="/user/profile/"], a.user-name[href*="/user/profile/"], a[href*="/user/profile/"]';export function parseXhsProfileHref(r,t="www.xiaohongshu.com"){const e=typeof r==="string"?r.trim():"";if(!e)return"";const o=String(t||"www.xiaohongshu.com").toLowerCase();let n;try{n=new URL(e,`https://${o}`)}catch{return""}if(n.protocol!=="https:")return"";if(n.hostname.toLowerCase()!==o)return"";return n.pathname.match(/^\/user\/profile\/([a-zA-Z0-9]+)\/?$/)?.[1]??""}export function buildXhsProfileUrl(r,t="www.xiaohongshu.com"){const e=parseXhsProfileHref(r,t);if(!e)return"";return`https://${t}/user/profile/${e}`}export function parseCommentLimit(r,t=20){const e=Number(r);if(!Number.isFinite(e))return t;return Math.max(1,Math.min(Math.floor(e),50))}export function parseRepliesLimit(r,t=3){if(r===void 0||r===null||r==="")return t;const e=Number(r);if(!Number.isFinite(e)||e<0)return t;return Math.floor(e)}export function parseXhsLikeCountText(r){const t=/^(?:\d+|\d{1,3}(?:[,,]\d{3})+)\+?$/u,e=/^((?:\d+|\d{1,3}(?:[,,]\d{3})+)(?:\.\d+)?)([wWkK万千])\+?$/u,o=String(r??"").replace(/\s+/g,"");if(!o)return 0;if(t.test(o))return Number(o.replace(/[,+,]/g,""));const n=o.match(e);if(!n)return 0;const s=Number(n[1].replace(/[,,]/g,""));if(!Number.isFinite(s))return 0;const a=n[2].toLowerCase();return Math.round(s*(a==="w"||a==="万"?1e4:1000))}function u(r,t,e){if(r==null)return"";if(typeof r!=="string")throw new l(`${e}: malformed comment row ${t}`);return r}export function normalizeCommentImages(r,t){if(r==null)return[];if(!Array.isArray(r))throw new l(`${t}: malformed comment row images`);const e=[];for(const o of r){if(typeof o!=="string")throw new l(`${t}: malformed comment row image URL`);const n=o.trim();let s;try{s=new URL(n)}catch{throw new l(`${t}: malformed comment row image URL`)}if(s.protocol!=="https:"&&s.protocol!=="http:"||s.username||s.password)throw new l(`${t}: malformed comment row image URL`);const a=s.toString();if(!e.includes(a))e.push(a)}return e}export function normalizeCommentRows(r,t="xiaohongshu/comments"){if(r==null)return[];if(!Array.isArray(r))throw new l(`${t}: malformed comments payload`);return r.map((e,o)=>{if(!e||typeof e!=="object"||Array.isArray(e))throw new l(`${t}: malformed comment row at index ${o}`);const n=u(e.text,"text",t);if(!n)throw new l(`${t}: malformed comment row text`);const s=Number(e.likes);if(!Number.isInteger(s)||s<0)throw new l(`${t}: malformed comment row likes`);if(typeof e.is_reply!=="boolean")throw new l(`${t}: malformed comment row is_reply`);return{id:e.id??null,root_id:e.root_id??null,parent_id:e.parent_id??null,liked:e.liked??null,reply_count:e.reply_count??null,replies_complete:e.replies_complete??null,author:u(e.author,"author",t),authorHrefRaw:u(e.authorHrefRaw,"authorHrefRaw",t),text:n,likes:s,time:u(e.time,"time",t),is_reply:e.is_reply,reply_to:u(e.reply_to,"reply_to",t),images:normalizeCommentImages(e.images,t)}})}export function compactCommentRow(r){const t={};for(const[e,o]of Object.entries(r)){if(o===null||o===void 0||o==="")continue;if(Array.isArray(o)&&o.length===0)continue;if(e==="root_id"&&o===r.id)continue;t[e]=o}return t}export function buildCommentsExtractJs(r,t=20,e=null,o=0){const n=parseXhsLikeCountText.toString();return`
|
|
2
2
|
(async () => {
|
|
3
3
|
const wait = (ms) => new Promise(r => setTimeout(r, ms))
|
|
4
4
|
const withReplies = ${r}
|
|
5
5
|
const targetCount = ${Number(t)||20}
|
|
6
|
+
const repliesCap = ${Math.max(0,Math.floor(Number(o)||0))}
|
|
6
7
|
|
|
7
8
|
// Check login state
|
|
8
9
|
const bodyText = document.body?.innerText || ''
|
|
@@ -48,11 +49,11 @@ import{cli as h,Strategy as d}from"@jackwener/opencli/registry";import{AuthRequi
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
const clean = (el) => (el?.textContent || '').replace(/\\s+/g, ' ').trim()
|
|
51
|
-
const parseLikeCountText = ${
|
|
52
|
+
const parseLikeCountText = ${n}
|
|
52
53
|
const parseLikes = (el) => {
|
|
53
54
|
return parseLikeCountText(clean(el))
|
|
54
55
|
}
|
|
55
|
-
const HREF_SELECTOR = ${JSON.stringify(
|
|
56
|
+
const HREF_SELECTOR = ${JSON.stringify(C)}
|
|
56
57
|
const extractAuthorHref = (el) => {
|
|
57
58
|
if (!el) return ''
|
|
58
59
|
const anchor = el.querySelector(HREF_SELECTOR)
|
|
@@ -80,6 +81,7 @@ import{cli as h,Strategy as d}from"@jackwener/opencli/registry";import{AuthRequi
|
|
|
80
81
|
const el = root.querySelector('.show-more')
|
|
81
82
|
if (!el) break
|
|
82
83
|
const before = root.querySelectorAll('.comment-item-sub').length
|
|
84
|
+
if (repliesCap > 0 && before >= repliesCap) break
|
|
83
85
|
el.click()
|
|
84
86
|
let progressed = false
|
|
85
87
|
for (let attempt = 0; attempt < 10; attempt++) {
|
|
@@ -146,7 +148,7 @@ import{cli as h,Strategy as d}from"@jackwener/opencli/registry";import{AuthRequi
|
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
if (${JSON.stringify(e)} !== null && !loginWall && !securityBlock) {
|
|
149
|
-
const state = (${
|
|
151
|
+
const state = (${R.toString()})(${JSON.stringify(e)})
|
|
150
152
|
if (state.error) return { pageUrl: location.href, securityBlock, loginWall, identityError: state.error }
|
|
151
153
|
for (const row of results) {
|
|
152
154
|
const entry = state.rows.find(c => c.id === row.id)
|
|
@@ -157,4 +159,4 @@ import{cli as h,Strategy as d}from"@jackwener/opencli/registry";import{AuthRequi
|
|
|
157
159
|
}
|
|
158
160
|
return { pageUrl: location.href, securityBlock, loginWall, results }
|
|
159
161
|
})()
|
|
160
|
-
`}export const command=
|
|
162
|
+
`}export const command=g({site:"xiaohongshu",name:"comments",access:"read",description:"获取小红书笔记评论(支持楼中楼子回复)",domain:"www.xiaohongshu.com",strategy:w.COOKIE,navigateBefore:!1,args:[{name:"note-id",required:!0,positional:!0,help:"Full Xiaohongshu note URL with xsec_token"},{name:"limit",type:"int",default:20,help:"Number of top-level comments (max 50)"},{name:"with-replies",type:"boolean",default:!1,help:"Include nested replies (楼中楼)"},{name:"replies-limit",type:"int",default:3,help:"With --with-replies: max nested replies per top-level comment (default 3, 0 = all). Capped threads show replies_complete=false; read a whole thread with `replies <url> <root-id>`"}],columns:["rank","id","root_id","parent_id","author","userId","text","likes","liked","time","is_reply","reply_to","reply_count","replies_complete","images"],func:async(r,t)=>{const e=parseCommentLimit(t.limit),o=Boolean(t["with-replies"]),n=parseRepliesLimit(t["replies-limit"]),s=String(t["note-id"]),a=_(s),c=await S(r,{url:k(s,{commandName:"xiaohongshu comments"}),extractJs:buildCommentsExtractJs(o,e,a,n),securityHelp:/^https?:\/\//.test(s)?"The page may be temporarily restricted. Try again later or from a different session.":"Try using a full URL from search results (with xsec_token) instead of a bare note ID."});if(!c||typeof c!=="object")throw new b("xiaohongshu/comments","Unexpected evaluate response");if(c.loginWall)throw new x("www.xiaohongshu.com","Note comments require login");if(c.identityError)throw new l(`xiaohongshu/comments: ${c.identityError}`);const d=normalizeCommentRows(c.results,"xiaohongshu/comments"),y=(i,m)=>compactCommentRow({rank:m+1,id:i.id,root_id:i.root_id,parent_id:i.parent_id,author:i.author,userId:i.authorHrefRaw?parseXhsProfileHref(i.authorHrefRaw):"",text:i.text,likes:i.likes,liked:i.liked,time:i.time,is_reply:i.is_reply,reply_to:i.reply_to,reply_count:i.reply_count,replies_complete:i.replies_complete,images:i.images??[]});if(o){const i=[];let m=0,p=null,f=0;for(const h of d){if(!h.is_reply){m++;if(m>e)break;p={...h};f=0;i.push(p);continue}if(n>0&&f>=n){if(p)p.replies_complete=!1;continue}f++;i.push(h)}return i.map(y)}return d.slice(0,e).map(y)}});
|