-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { renderRss2 } from '../../utils/util'; | ||
|
||
let getUser = async (url) => { | ||
let res = await fetch(url); | ||
let scripts = []; | ||
let rewriter = new HTMLRewriter() | ||
.on('script', { | ||
element(element) { | ||
scripts.push(''); | ||
}, | ||
text(text) { | ||
scripts[scripts.length - 1] += text.text; | ||
}, | ||
}) | ||
.transform(res); | ||
await rewriter.text(); | ||
let script = scripts.find((script) => script.startsWith('window.__INITIAL_STATE__=')); | ||
script = script.slice('window.__INITIAL_STATE__='.length); | ||
// replace undefined to null | ||
script = script.replace(/undefined/g, 'null'); | ||
let state = JSON.parse(script); | ||
return state.user; | ||
}; | ||
|
||
let deal = async (ctx) => { | ||
// const uid = ctx.params.user_id; | ||
// const category = ctx.params.category; | ||
const { uid } = ctx.req.param(); | ||
const category = 'notes'; | ||
const url = `https://www.xiaohongshu.com/user/profile/${uid}`; | ||
|
||
const { | ||
userPageData: { basicInfo, interactions, tags }, | ||
notes, | ||
collect, | ||
} = await getUser(url); | ||
|
||
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 renderNote = (notes) => | ||
notes.flatMap((n) => | ||
n.map(({ noteCard }) => ({ | ||
title: noteCard.displayTitle, | ||
link: `${url}/${noteCard.noteId}`, | ||
description: `<img src ="${noteCard.cover.infoList.pop().url}"><br>${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: `<img src ="${item.cover.info_list.pop().url}"><br>${item.display_title}`, | ||
author: item.user.nickname, | ||
upvotes: item.interact_info.likedCount, | ||
})); | ||
}; | ||
|
||
ctx.header('Content-Type', 'application/xml'); | ||
return ctx.text( | ||
renderRss2({ | ||
title, | ||
description, | ||
image, | ||
link: url, | ||
items: category === 'notes' ? renderNote(notes) : renderCollect(collect), | ||
}) | ||
); | ||
}; | ||
|
||
let setup = (route) => { | ||
route.get('/xiaohongshu/user/:uid', deal); | ||
}; | ||
|
||
export default { setup }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters