Skip to content

Commit

Permalink
添加小红书
Browse files Browse the repository at this point in the history
  • Loading branch information
yllhwa committed Oct 13, 2023
1 parent 331dc88 commit 1da7eca
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RSSWorker 是一个轻量级的 RSS 订阅工具,可以部署在 Cloudflare Wo
- bilibili 动态 (/bilibili/user/dynamic/:uid)
- telegram 频道 (/telegram/channel/:username)
- weibo 用户 (/weibo/user/:uid)
- 小红书用户 (/xiaohongshu/user/:uid)

## 部署

Expand Down
87 changes: 87 additions & 0 deletions src/lib/xiaohongshu/user.js
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 };
3 changes: 2 additions & 1 deletion src/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Hono } from 'hono';
import bilibili_user_dynamic from './lib/bilibili/user/dynamic';
import telegram_channel from './lib/telegram/channel';
import weibo_user from './lib/weibo/user';
import xiaohongshu_user from './lib/xiaohongshu/user';

const route = new Hono();

let plugins = [bilibili_user_dynamic, telegram_channel, weibo_user];
let plugins = [bilibili_user_dynamic, telegram_channel, weibo_user, xiaohongshu_user];

for (let plugin of plugins) {
plugin.setup(route);
Expand Down

0 comments on commit 1da7eca

Please sign in to comment.