Skip to content

Commit

Permalink
feat(route): output full text for msn news (#17991)
Browse files Browse the repository at this point in the history
* feat(route): output full text for msn news

* feat(route): cache msn news article response

* remove msn news article cache config

* fix msn news article full content fetching
  • Loading branch information
KTachibanaM authored Dec 28, 2024
1 parent 75971d5 commit 6233163
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/routes/msn/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import cache from '@/utils/cache';
import { load } from 'cheerio';

const apiKey = '0QfOX3Vn51YCzitbLaRkTTBadtWpgTN8NZLW0C1SEM';
Expand Down Expand Up @@ -44,13 +45,34 @@ export const route: Route = {
const requestMuid = parsedSettings.fd_muid;

const jsonData = await ofetch(`https://assets.msn.com/service/news/feed/pages/providerfullpage?market=${market}&query=newest&CommunityProfileId=${truncatedId}&apikey=${apiKey}&user=m-${requestMuid}`);
const items = jsonData.sections[0].cards.map((card) => ({
title: card.title,
link: card.url,
description: card.abstract,
pubDate: parseDate(card.publishedDateTime),
category: [card.category],
}));
const items = await Promise.all(
jsonData.sections[0].cards.map(async (card) => {
let articleContentHtml = '';

const articleUrl = card.url;
const parsedArticleUrl = URL.parse(articleUrl);
let articleId = parsedArticleUrl?.pathname.split('/').pop();
if (articleId?.startsWith('ar-')) {
articleId = articleId.substring(3);
const fetchedArticleContentHtml = (await cache.tryGet(articleId, async () => {
const articleData = await ofetch(`https://assets.msn.com/content/view/v2/Detail/${market}/${articleId}`);
return articleData.body;
})) as string; // cache article content for 3 months
articleContentHtml = fetchedArticleContentHtml;
}

return {
title: card.title,
link: articleUrl,
description: card.abstract,
content: {
html: articleContentHtml,
},
pubDate: parseDate(card.publishedDateTime),
category: [card.category],
};
})
);

const channelLink = `https://www.msn.com/${market}/channel/source/${name}/${id}`;
return {
Expand Down

0 comments on commit 6233163

Please sign in to comment.