Skip to content

Commit

Permalink
feat: add msn news (#17968)
Browse files Browse the repository at this point in the history
* feat: add msn news

* add favicon

* address mr comments for msn news

* add parameter description
  • Loading branch information
KTachibanaM authored Dec 26, 2024
1 parent b56b3a3 commit d13f043
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
63 changes: 63 additions & 0 deletions lib/routes/msn/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { load } from 'cheerio';

const apiKey = '0QfOX3Vn51YCzitbLaRkTTBadtWpgTN8NZLW0C1SEM';

export const route: Route = {
path: '/:market/:name/:id',
parameters: {
market: 'Market code. Find it in MSN url, e.g. zh-tw',
name: 'Name of the channel. Find it in MSN url, e.g. Bloomberg',
id: 'ID of the channel (always starts with sr-vid). Find it in MSN url, e.g. sr-vid-08gw7ky4u229xjsjvnf4n6n7v67gxm0pjmv9fr4y2x9jjmwcri4s',
},
categories: ['traditional-media'],
example: '/zh-tw/Bloomberg/sr-vid-08gw7ky4u229xjsjvnf4n6n7v67gxm0pjmv9fr4y2x9jjmwcri4s',
description: `MSN News`,
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
},
radar: [
{
source: ['www.msn.com/:market/channel/source/:name/:id'],
target: '/:market/:name/:id',
},
],
name: 'News',
maintainers: ['KTachibanaM'],
handler: async (ctx) => {
const { market, name, id } = ctx.req.param();
let truncatedId = id;
if (truncatedId.startsWith('sr-')) {
truncatedId = truncatedId.substring(3);
}

const pageData = await ofetch(`https://www.msn.com/${market}/channel/source/${name}/${id}`);
const $ = load(pageData);
const headElement = $('head');
const dataClientSettings = headElement.attr('data-client-settings') ?? '{}';
const parsedSettings = JSON.parse(dataClientSettings);
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 channelLink = `https://www.msn.com/${market}/channel/source/${name}/${id}`;
return {
title: name,
image: 'https://www.msn.com/favicon.ico',
link: channelLink,
item: items,
};
},
};
10 changes: 10 additions & 0 deletions lib/routes/msn/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'MSN',
url: 'msn.com',

zh: {
name: 'MSN',
},
};

0 comments on commit d13f043

Please sign in to comment.