feat(route/xiaohongshu): add fallback get notes logics
This commit is contained in:
parent
790ea65fa7
commit
e286c77901
|
|
@ -56,67 +56,76 @@ async function handler(ctx) {
|
|||
const cookie = config.xiaohongshu.cookie;
|
||||
|
||||
if (cookie && category === 'notes') {
|
||||
const urlNotePrefix = 'https://www.xiaohongshu.com/explore';
|
||||
const user = await getUserWithCookie(url, cookie);
|
||||
const notes = await renderNotesFulltext(user.notes, urlNotePrefix);
|
||||
return {
|
||||
title: `${user.userPageData.basicInfo.nickname} - 笔记 • 小红书 / RED`,
|
||||
description: user.userPageData.basicInfo.desc,
|
||||
image: user.userPageData.basicInfo.imageb || user.userPageData.basicInfo.images,
|
||||
link: url,
|
||||
item: notes,
|
||||
};
|
||||
try {
|
||||
const urlNotePrefix = 'https://www.xiaohongshu.com/explore';
|
||||
const user = await getUserWithCookie(url, cookie);
|
||||
const notes = await renderNotesFulltext(user.notes, urlNotePrefix);
|
||||
return {
|
||||
title: `${user.userPageData.basicInfo.nickname} - 笔记 • 小红书 / RED`,
|
||||
description: user.userPageData.basicInfo.desc,
|
||||
image: user.userPageData.basicInfo.imageb || user.userPageData.basicInfo.images,
|
||||
link: url,
|
||||
item: notes,
|
||||
};
|
||||
} catch {
|
||||
// Fallback to normal logic if cookie method fails
|
||||
return await getUserFeedWithoutCookie(url, category);
|
||||
}
|
||||
} else {
|
||||
const {
|
||||
userPageData: { basicInfo, interactions, tags },
|
||||
notes,
|
||||
collect,
|
||||
} = await getUser(url, cache);
|
||||
|
||||
const title = `${basicInfo.nickname} - 小红书${category === 'notes' ? '笔记' : '收藏'}`;
|
||||
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(({ id, noteCard }) => ({
|
||||
title: noteCard.displayTitle,
|
||||
link: `${url}/${noteCard.noteId || id}`,
|
||||
guid: noteCard.noteId || id || noteCard.displayTitle,
|
||||
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),
|
||||
};
|
||||
return await getUserFeedWithoutCookie(url, category);
|
||||
}
|
||||
}
|
||||
|
||||
async function getUserFeedWithoutCookie(url: string, category: string) {
|
||||
const {
|
||||
userPageData: { basicInfo, interactions, tags },
|
||||
notes,
|
||||
collect,
|
||||
} = await getUser(url, cache);
|
||||
|
||||
const title = `${basicInfo.nickname} - 小红书${category === 'notes' ? '笔记' : '收藏'}`;
|
||||
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(({ id, noteCard }) => ({
|
||||
title: noteCard.displayTitle,
|
||||
link: `${url}/${noteCard.noteId || id}`,
|
||||
guid: noteCard.noteId || id || noteCard.displayTitle,
|
||||
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),
|
||||
};
|
||||
}
|
||||
|
||||
async function renderNotesFulltext(notes, urlPrex) {
|
||||
const data: Array<{
|
||||
title: string;
|
||||
|
|
|
|||
Loading…
Reference in New Issue