|
| 1 | +import { Route } from '@/types'; |
| 2 | +import cache from '@/utils/cache'; |
| 3 | +import parser from '@/utils/rss-parser'; |
| 4 | +import ofetch from '@/utils/ofetch'; |
| 5 | +import { load } from 'cheerio'; |
| 6 | +const host = 'https://www.newyorker.com'; |
| 7 | +export const route: Route = { |
| 8 | + path: '/:category/:subCategory?', |
| 9 | + categories: ['traditional-media'], |
| 10 | + example: '/newyorker/everything', |
| 11 | + parameters: { category: 'rss category. can be found at `https://www.newyorker.com/about/feeds`' }, |
| 12 | + features: { |
| 13 | + requireConfig: false, |
| 14 | + requirePuppeteer: false, |
| 15 | + antiCrawler: false, |
| 16 | + supportBT: false, |
| 17 | + supportPodcast: false, |
| 18 | + supportScihub: false, |
| 19 | + }, |
| 20 | + radar: [ |
| 21 | + { |
| 22 | + source: ['newyorker.com/feed/:category/:subCategory?'], |
| 23 | + }, |
| 24 | + ], |
| 25 | + name: 'The New Yorker', |
| 26 | + maintainers: ['EthanWng97'], |
| 27 | + handler, |
| 28 | +}; |
| 29 | + |
| 30 | +async function handler(ctx) { |
| 31 | + const { category, subCategory } = ctx.req.param(); |
| 32 | + const rssUrl = subCategory ? `${host}/feed/${category}/${subCategory}` : `${host}/feed/${category}`; |
| 33 | + const feed = await parser.parseURL(rssUrl); |
| 34 | + const items = await Promise.all( |
| 35 | + feed.items.map((item) => |
| 36 | + cache.tryGet(item.link, async () => { |
| 37 | + const data = await ofetch(item.link); |
| 38 | + const $ = load(data); |
| 39 | + const description = $('#main-content'); |
| 40 | + description.find('h1').remove(); |
| 41 | + description.find('.article-body__footer').remove(); |
| 42 | + description.find('.social-icons').remove(); |
| 43 | + description.find('div[class^="ActionBarWrapperContent-"]').remove(); |
| 44 | + return { |
| 45 | + title: item.title, |
| 46 | + pubDate: item.pubDate, |
| 47 | + link: item.link, |
| 48 | + category: item.categories, |
| 49 | + description: description.html(), |
| 50 | + }; |
| 51 | + }) |
| 52 | + ) |
| 53 | + ); |
| 54 | + |
| 55 | + return { |
| 56 | + title: `The New Yorker - ${feed.title}`, |
| 57 | + link: host, |
| 58 | + description: 'Reporting, Profiles, breaking news, cultural coverage, podcasts, videos, and cartoons from The New Yorker.', |
| 59 | + item: items, |
| 60 | + }; |
| 61 | +} |
0 commit comments