Skip to content

Commit

Permalink
fix(route/newrank): wechat route error
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu authored and artefaritaKuniklo committed Dec 13, 2024
1 parent 0c91ff9 commit 381bb9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
7 changes: 6 additions & 1 deletion lib/routes/newrank/wechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const route: Route = {
supportScihub: false,
},
name: '微信公众号',
maintainers: ['lessmoe'],
maintainers: ['lessmoe', 'pseudoyu'],
handler,
};

Expand Down Expand Up @@ -70,16 +70,21 @@ async function handler(ctx) {
xyz: utils.decrypt_wechat_detail_xyz(uid, nonce),
},
});

const name = response.data.value.user.name;
const realTimeArticles = utils.flatten(response.data.value.realTimeArticles);
const articles = utils.flatten(response.data.value.articles);
const newArticles = [...realTimeArticles, ...articles];

const items = newArticles.map((item) => ({
id: item.id,
title: item.title,
description: '',
link: item.url,
pubDate: item.publicTime,
}));

// TODO: link is empty
await Promise.all(items.map((item) => finishArticleItem(item)));

return {
Expand Down
28 changes: 15 additions & 13 deletions lib/utils/wechat-mp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,19 +623,21 @@ const fetchArticle = (url: string, bypassHostCheck: boolean = false) => {
* @return {Promise<object>} - The incoming `item` object, with the article and its metadata filled in.
*/
const finishArticleItem = async (item, setMpNameAsAuthor = false, skipLink = false) => {
const fetchedItem = await fetchArticle(item.link);
for (const key in fetchedItem) {
switch (key) {
case 'author':
item.author = setMpNameAsAuthor
? fetchedItem.mpName || item.author // the Official Account itself. if your route return articles from different accounts, you may want to use this
: fetchedItem.author || item.author; // the real author of the article. if your route return articles from a certain account, use this
break;
case 'link':
item.link = skipLink ? item.link : fetchedItem.link || item.link;
break;
default:
item[key] = item[key] || fetchedItem[key];
if (item.link) {
const fetchedItem = await fetchArticle(item.link);
for (const key in fetchedItem) {
switch (key) {
case 'author':
item.author = setMpNameAsAuthor
? fetchedItem.mpName || item.author // the Official Account itself. if your route return articles from different accounts, you may want to use this
: fetchedItem.author || item.author; // the real author of the article. if your route return articles from a certain account, use this
break;
case 'link':
item.link = skipLink ? item.link : fetchedItem.link || item.link;
break;
default:
item[key] = item[key] || fetchedItem[key];
}
}
}
return item;
Expand Down

0 comments on commit 381bb9f

Please sign in to comment.