diff --git a/docs/social-media.md b/docs/social-media.md index a0c91623e..864003685 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -1507,9 +1507,9 @@ rule -### 用户专辑 +### 用户收藏 - + ### 专辑 diff --git a/lib/v2/xiaohongshu/board.js b/lib/v2/xiaohongshu/board.js index d4c2bfbd4..b25526fee 100644 --- a/lib/v2/xiaohongshu/board.js +++ b/lib/v2/xiaohongshu/board.js @@ -1,14 +1,15 @@ const { parseDate } = require('@/utils/parse-date'); const timezone = require('@/utils/timezone'); -const { getContent } = require('./util'); +const { getBoard } = require('./util'); module.exports = async (ctx) => { const url = `https://www.xiaohongshu.com/board/${ctx.params.board_id}`; - const main = await getContent(url, ctx.cache); + const main = await getBoard(url, ctx.cache); const albumInfo = main.albumInfo; const title = albumInfo.name; const description = albumInfo.desc; + const image = albumInfo.user.images.split('?imageView2')[0]; const list = main.notesDetail; const resultItem = list.map((item) => ({ @@ -22,6 +23,7 @@ module.exports = async (ctx) => { ctx.state.data = { title, link: url, + image, item: resultItem, description, }; diff --git a/lib/v2/xiaohongshu/maintainer.js b/lib/v2/xiaohongshu/maintainer.js index dd357d1bc..a06db40ce 100644 --- a/lib/v2/xiaohongshu/maintainer.js +++ b/lib/v2/xiaohongshu/maintainer.js @@ -1,4 +1,5 @@ module.exports = { '/board/:board_id': ['lotosbin'], - '/user/:user_id/:category': ['lotosbin'], + '/user/:user_id/collect': ['lotosbin'], + '/user/:user_id/notes': ['lotosbin'], }; diff --git a/lib/v2/xiaohongshu/radar.js b/lib/v2/xiaohongshu/radar.js index cb92cb060..ba84a102b 100644 --- a/lib/v2/xiaohongshu/radar.js +++ b/lib/v2/xiaohongshu/radar.js @@ -9,10 +9,10 @@ module.exports = { target: '/xiaohongshu/user/:user_id/notes', }, { - title: '用户专辑', + title: '用户收藏', docs: 'https://docs.rsshub.app/social-media.html#xiao-hong-shu', source: '/user/profile/:user_id', - target: '/xiaohongshu/user/:user_id/album', + target: '/xiaohongshu/user/:user_id/collect', }, { title: '专辑', diff --git a/lib/v2/xiaohongshu/user.js b/lib/v2/xiaohongshu/user.js index 32d211f34..c8a6ef1d8 100644 --- a/lib/v2/xiaohongshu/user.js +++ b/lib/v2/xiaohongshu/user.js @@ -1,51 +1,30 @@ -const { parseDate } = require('@/utils/parse-date'); -const timezone = require('@/utils/timezone'); -const { getContent } = require('./util'); +const { getUser } = require('./util'); module.exports = async (ctx) => { const userId = ctx.params.user_id; const category = ctx.params.category; const url = `https://www.xiaohongshu.com/user/profile/${userId}`; - let main; - try { - main = await getContent(url, ctx.cache); - } catch (e) { - throw Error('滑块验证'); - } - const userDetail = main.userDetail; - const description = `${userDetail.nickname} • 小红书 / RED`; - if (category === 'notes') { - const list = main.notesDetail; - const resultItem = list.map((item) => ({ - title: item.title, - link: `https://www.xiaohongshu.com/discovery/item/${item.id}`, - description: `
${item.title}`, + const content = 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 items = (category, user_posted, collect) => + (category === 'notes' ? user_posted : collect).data.notes.map((item) => ({ + title: item.display_title, + link: `${url}/${item.note_id}`, + description: `
${item.display_title}`, author: item.user.nickname, - pubDate: timezone(parseDate(item.time), 8), })); - const title = `小红书-${userDetail.nickname}-笔记`; - ctx.state.data = { - title, - link: url, - item: resultItem, - description, - }; - } else if (category === 'album') { - const list = main.albumDetail; - const resultItem = list.map((item) => ({ - title: item.title, - link: `https://www.xiaohongshu.com/board/${item.id}`, - description: item.images.map((it) => ``).join(`
`), - })); - - const title = `小红书-${userDetail.nickname}-专辑`; - ctx.state.data = { - title, - link: url, - item: resultItem, - description, - }; - } + ctx.state.data = { + title, + description, + image, + link: url, + item: items(category, user_posted, collect), + }; }; diff --git a/lib/v2/xiaohongshu/util.js b/lib/v2/xiaohongshu/util.js index d74168541..779307023 100644 --- a/lib/v2/xiaohongshu/util.js +++ b/lib/v2/xiaohongshu/util.js @@ -1,6 +1,47 @@ const config = require('@/config').value; +const logger = require('@/utils/logger'); -const getContent = (url, cache) => +const getUser = (url, cache) => + cache.tryGet( + url, + async () => { + const browser = await require('@/utils/puppeteer')(); + 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.click('div.reds-tab-item:nth-child(2)'); + await page.waitForSelector('.feeds-container:nth-child(2) .note-item'); + return { otherInfo, user_posted, collect }; + } finally { + browser.close(); + } + }, + config.cache.routeExpire, + false + ); + +const getBoard = (url, cache) => cache.tryGet( url, async () => { @@ -11,12 +52,7 @@ const getContent = (url, cache) => page.on('request', (request) => { request.resourceType() === 'document' || request.resourceType() === 'script' || request.resourceType() === 'xhr' ? request.continue() : request.abort(); }); - page.on('response', (response) => { - if (response.url().includes('captcha.fengkongcloud.cn')) { - page.close(); - throw Error('滑块验证'); - } - }); + logger.debug(`Requesting ${url}`); await page.goto(url); await page.waitForSelector('.pc-container'); const initialSsrState = await page.evaluate(() => window.__INITIAL_SSR_STATE__); @@ -30,5 +66,6 @@ const getContent = (url, cache) => ); module.exports = { - getContent, + getUser, + getBoard, };