From e4bd492aafa7df6bcfb07ba6f6d6e4a49fd4412b Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 27 Sep 2023 16:29:28 +0000 Subject: [PATCH] fix(route): xiaohongshu (#13425) --- lib/v2/xiaohongshu/user.js | 42 ++++++++++++++++++++++++++++++-------- lib/v2/xiaohongshu/util.js | 38 +++++++++++++++------------------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/lib/v2/xiaohongshu/user.js b/lib/v2/xiaohongshu/user.js index c8a6ef1d8..54603f81f 100644 --- a/lib/v2/xiaohongshu/user.js +++ b/lib/v2/xiaohongshu/user.js @@ -5,26 +5,50 @@ module.exports = async (ctx) => { const category = ctx.params.category; const url = `https://www.xiaohongshu.com/user/profile/${userId}`; - const content = await getUser(url, ctx.cache); + const { + userPageData: { basicInfo, interactions, tags }, + notes, + collect, + } = await getUser(url, ctx.cache); - const { otherInfo, user_posted, collect } = content; - const title = `${otherInfo.data.basic_info.nickname} - ${category === 'notes' ? '笔记' : '收藏'} • 小红书 / RED`; - const description = otherInfo.data.basic_info.desc; - const image = otherInfo.data.basic_info.imageb || otherInfo.data.basic_info.images; + const title = `${basicInfo.nickname} - ${category === 'notes' ? '笔记' : '收藏'} • 小红书 / RED`; + const description = `${basicInfo.desc} ${tags.map((t) => t.name).join(' ')} ${interactions.map((i) => `${i.count} ${i.name}`).join(' ')}`; + const image = basicInfo.imageb || basicInfo.images; - const items = (category, user_posted, collect) => - (category === 'notes' ? user_posted : collect).data.notes.map((item) => ({ + const renderNote = (notes) => + notes.flatMap((n) => + n.map(({ noteCard }) => ({ + title: noteCard.displayTitle, + link: `${url}/${noteCard.noteId}`, + description: `
${noteCard.displayTitle}`, + author: noteCard.user.nickname, + upvotes: noteCard.interactInfo.likedCount, + })) + ); + const renderCollect = (collect) => { + if (!collect) { + throw Error('该用户已设置收藏内容不可见'); + } + if (collect.code !== 0) { + throw Error(JSON.stringify(collect)); + } + if (!collect.data.notes.length) { + throw ctx.throw(403, '该用户已设置收藏内容不可见'); + } + return collect.data.notes.map((item) => ({ title: item.display_title, link: `${url}/${item.note_id}`, - description: `
${item.display_title}`, + description: `
${item.display_title}`, author: item.user.nickname, + upvotes: item.interact_info.likedCount, })); + }; ctx.state.data = { title, description, image, link: url, - item: items(category, user_posted, collect), + item: category === 'notes' ? renderNote(notes) : renderCollect(collect), }; }; diff --git a/lib/v2/xiaohongshu/util.js b/lib/v2/xiaohongshu/util.js index c4d9ef7c1..2ac4bdf76 100644 --- a/lib/v2/xiaohongshu/util.js +++ b/lib/v2/xiaohongshu/util.js @@ -10,33 +10,29 @@ const getUser = (url, cache) => try { const page = await browser.newPage(); await page.setRequestInterception(true); - let otherInfo = ''; - let user_posted = ''; let collect = ''; page.on('request', (request) => { request.resourceType() === 'document' || request.resourceType() === 'script' || request.resourceType() === 'xhr' || request.resourceType() === 'other' ? request.continue() : request.abort(); }); - page.on('response', async (response) => { - const request = response.request(); - if (request.url().includes('/api/sns/web/v1/user/otherinfo')) { - otherInfo = await response.json(); - } - if (request.url().includes('/api/sns/web/v1/user_posted')) { - user_posted = await response.json(); - } - if (request.url().includes('/api/sns/web/v2/note/collect/page')) { - collect = await response.json(); - } - }); logger.debug(`Requesting ${url}`); - await page.goto(url); - await page.waitForSelector('.feeds-container:nth-child(1) .note-item'); + await page.goto(url, { + waitUntil: 'domcontentloaded', + }); + await page.waitForSelector('div.reds-tab-item:nth-child(2)'); await page.click('div.reds-tab-item:nth-child(2)'); - const privateCollection = await page.evaluate(() => document.querySelector('div.reds-tab-item:nth-child(2) .reds-icon')); - if (!privateCollection) { - await page.waitForSelector('.feeds-container:nth-child(2) .note-item'); - } - return { otherInfo, user_posted, collect }; + const response = await page.waitForResponse((res) => { + const req = res.request(); + return req.url().includes('/api/sns/web/v2/note/collect/page') && req.method() === 'GET' && req.resourceType() === 'xhr'; + }); + collect = await response.json(); + + const initialState = await page.evaluate(() => window.__INITIAL_STATE__); + + let { userPageData, notes } = initialState.user; + userPageData = userPageData._rawValue || userPageData; + notes = notes._rawValue || notes; + + return { userPageData, notes, collect }; } finally { browser.close(); }