|
| 1 | +import { load } from 'cheerio'; |
| 2 | + |
| 3 | +import type { Route } from '@/types'; |
| 4 | +import cache from '@/utils/cache'; |
| 5 | +import ofetch from '@/utils/ofetch'; |
| 6 | +import { parseDate } from '@/utils/parse-date'; |
| 7 | + |
| 8 | +export const route: Route = { |
| 9 | + path: '/updates/:category?', |
| 10 | + categories: ['new-media'], |
| 11 | + example: '/maldita/updates/desinfo', |
| 12 | + parameters: { category: 'Category to fetch' }, |
| 13 | + features: { |
| 14 | + requireConfig: false, |
| 15 | + requirePuppeteer: false, |
| 16 | + antiCrawler: false, |
| 17 | + supportBT: false, |
| 18 | + supportPodcast: false, |
| 19 | + supportScihub: false, |
| 20 | + }, |
| 21 | + radar: [ |
| 22 | + { |
| 23 | + source: ['maldita.es/:category/'], |
| 24 | + target: '/updates/:category', |
| 25 | + }, |
| 26 | + ], |
| 27 | + name: 'Latest Updates', |
| 28 | + maintainers: ['canonnizq'], |
| 29 | + description: 'Categories: all | desinfo | prebunking | investigaciones | control-del-poder | policy', |
| 30 | + |
| 31 | + handler: async (ctx) => { |
| 32 | + const { category = 'all' } = ctx.req.param(); |
| 33 | + const url = `https://maldita.es/${category === 'all' ? '' : `${category}/`}`; |
| 34 | + |
| 35 | + const response = await ofetch(url); |
| 36 | + const $ = load(response); |
| 37 | + |
| 38 | + const name = $('h1').text() || 'All updates'; |
| 39 | + |
| 40 | + const links = $(category === 'all' ? 'a.ac-link' : '#content a[href*="maldita.es"]:has(div)') |
| 41 | + .toArray() |
| 42 | + .map((item) => $(item).attr('href')!); |
| 43 | + |
| 44 | + const items = await Promise.all( |
| 45 | + links.map((link) => |
| 46 | + cache.tryGet(link, async () => { |
| 47 | + const response = await ofetch(link); |
| 48 | + const $ = load(response); |
| 49 | + |
| 50 | + return { |
| 51 | + title: $('h1').text(), |
| 52 | + link, |
| 53 | + category: $('#article-metadata .flex a') |
| 54 | + .toArray() |
| 55 | + .map((x) => $(x).text()), |
| 56 | + pubDate: parseDate($('time').attr('datetime')!), |
| 57 | + itunes_item_image: $('#featuredImage img').attr('src'), |
| 58 | + content: { |
| 59 | + html: $('#keys').html()! + $('#article-content').html()!, |
| 60 | + }, |
| 61 | + }; |
| 62 | + }) |
| 63 | + ) |
| 64 | + ); |
| 65 | + |
| 66 | + return { |
| 67 | + title: `Maldita.es - ${name}`, |
| 68 | + link: url, |
| 69 | + item: items, |
| 70 | + }; |
| 71 | + }, |
| 72 | +}; |
0 commit comments