// 从"抄"到的接口 JSON 里提取笔记卡片、笔记详情、评论。 // 不绑定具体接口路径,按数据形状识别,接口改版时不容易全废。 import { walk } from './cdp.mjs'; const num = (v) => { if (v == null) return null; if (typeof v === 'number') return v; const s = String(v).trim(); if (!s) return null; if (/w|W|万/.test(s)) return Math.round(parseFloat(s) * 10000); if (/k|K|千/.test(s)) return Math.round(parseFloat(s) * 1000); const n = parseInt(s.replace(/[^\d]/g, ''), 10); return Number.isFinite(n) ? n : null; }; function pickCard(nc, o) { const user = nc.user || o.user || {}; return { note_id: o.id || o.note_id || nc.note_id || null, xsec_token: o.xsec_token || nc.xsec_token || null, title: nc.display_title || nc.title || '', desc: nc.desc || '', note_type: nc.type || null, author_id: user.user_id || user.userId || null, author_name: user.nickname || user.nick_name || user.name || null, author_avatar: user.avatar || null, liked_count: num(nc.interact_info?.liked_count), collected_count: num(nc.interact_info?.collected_count), comment_count: num(nc.interact_info?.comment_count), shared_count: num(nc.interact_info?.shared_count), cover: nc.cover?.url_default || nc.cover?.url || nc.image_list?.[0]?.url_default || null, publish_time: nc.time ?? nc.last_update_time ?? null, ip_location: nc.ip_location || null, }; } /** 搜索结果 / 详情里所有带 note_card 的对象 */ export function harvestCards(bodies) { const map = new Map(); for (const b of bodies) { walk(b.json, (o) => { const nc = o.note_card; if (!nc || typeof nc !== 'object') return; const id = o.id || o.note_id || nc.note_id; if (!id) return; const card = pickCard(nc, o); const prev = map.get(id); // 保留信息更全的那条(有 xsec_token、有标题的优先) const score = (c) => (c.xsec_token ? 4 : 0) + (c.title ? 2 : 0) + (c.author_name ? 1 : 0); if (!prev || score(card) >= score(prev)) map.set(id, { ...prev, ...card }); }); } return [...map.values()]; } /** 详情接口里的正文 + 标签 + 图片 */ export function harvestDetails(bodies) { const map = new Map(); for (const b of bodies) { walk(b.json, (o) => { const nc = o.note_card; if (!nc || typeof nc !== 'object') return; const id = o.id || o.note_id || nc.note_id; if (!id) return; const base = pickCard(nc, o); const detail = { ...base, images: (nc.image_list || []).map((i) => i.url_default || i.url).filter(Boolean), tags: (nc.tag_list || []).map((t) => t.name).filter(Boolean), video: nc.video?.media?.stream?.h264?.[0]?.master_url || null, }; const prev = map.get(id) || {}; map.set(id, { ...prev, ...Object.fromEntries(Object.entries(detail).filter(([, v]) => v != null && v !== '')) }); }); } return [...map.values()]; } /** 评论区:有可能在多页响应里,逐条累计 */ export function harvestComments(bodies) { const map = new Map(); const take = (c, noteId, parentId = null) => { if (!c || typeof c !== 'object') return; const id = c.id || c.comment_id; const content = c.content; if (!id || typeof content !== 'string') return; if (map.has(id)) return; map.set(id, { note_id: noteId || c.note_id || null, comment_id: id, parent_id: parentId, user_id: c.user_info?.user_id || c.user_info?.userId || null, user_name: c.user_info?.nickname || c.user_info?.nick_name || null, content, like_count: num(c.like_count), sub_comment_count: num(c.sub_comment_count), create_time: c.create_time ?? null, ip_location: c.ip_location || null, is_author: !!c.show_tags?.includes?.('is_author') || false, }); if (Array.isArray(c.sub_comments)) for (const s of c.sub_comments) take(s, noteId, id); }; for (const b of bodies) { walk(b.json, (o) => { if (Array.isArray(o.comments)) for (const c of o.comments) take(c, o.note_id || o.id); }); } return [...map.values()]; }