/
user.ts
64 lines (58 loc) · 2.25 KB
/
user.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Route } from '@/types';
import cache from '@/utils/cache';
import { getUser } from './util';
import InvalidParameterError from '@/errors/types/invalid-parameter';
export const route: Route = {
path: '/user/:user_id/:category',
name: 'Unknown',
maintainers: [],
handler,
};
async function handler(ctx) {
const userId = ctx.req.param('user_id');
const category = ctx.req.param('category');
const url = `https://www.xiaohongshu.com/user/profile/${userId}`;
const {
userPageData: { basicInfo, interactions, tags },
notes,
collect,
} = await getUser(url, cache);
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 new InvalidParameterError('该用户已设置收藏内容不可见');
}
if (collect.code !== 0) {
throw new Error(JSON.stringify(collect));
}
if (!collect.data.notes.length) {
throw new InvalidParameterError('该用户已设置收藏内容不可见');
}
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,
}));
};
return {
title,
description,
image,
link: url,
item: category === 'notes' ? renderNote(notes) : renderCollect(collect),
};
}