|
| 1 | +import { load } from 'cheerio'; |
| 2 | +import pMap from 'p-map'; |
| 3 | + |
| 4 | +import type { Route } from '@/types'; |
| 5 | +import cache from '@/utils/cache'; |
| 6 | +import ofetch from '@/utils/ofetch'; |
| 7 | +import { parseDate } from '@/utils/parse-date'; |
| 8 | + |
| 9 | +export const route: Route = { |
| 10 | + path: '/red', |
| 11 | + categories: ['programming'], |
| 12 | + example: '/anthropic/red', |
| 13 | + radar: [ |
| 14 | + { |
| 15 | + source: ['red.anthropic.com'], |
| 16 | + }, |
| 17 | + ], |
| 18 | + name: 'Frontier Red Team', |
| 19 | + maintainers: ['shoeper'], |
| 20 | + handler, |
| 21 | + url: 'red.anthropic.com', |
| 22 | +}; |
| 23 | + |
| 24 | +async function handler() { |
| 25 | + const baseUrl = 'https://red.anthropic.com'; |
| 26 | + const link = `${baseUrl}/red`; |
| 27 | + const response = await ofetch(link); |
| 28 | + const $ = load(response); |
| 29 | + |
| 30 | + const list = $('a[class^="note"]') |
| 31 | + .toArray() |
| 32 | + .map((element) => { |
| 33 | + const $e = $(element); |
| 34 | + return { |
| 35 | + title: $e.find('h2, h3').text().trim(), |
| 36 | + link: `${baseUrl}/${$e.attr('href')}`, |
| 37 | + }; |
| 38 | + }); |
| 39 | + |
| 40 | + const items = await pMap( |
| 41 | + list, |
| 42 | + (item) => |
| 43 | + cache.tryGet(item.link, async () => { |
| 44 | + const response = await ofetch(item.link); |
| 45 | + const $ = load(response); |
| 46 | + |
| 47 | + item.pubDate = parseDate($('d-article p').first().text().trim()); |
| 48 | + $('h3:contains("Subscribe")').remove(); |
| 49 | + $('d-article p').first().remove(); |
| 50 | + const content = $('d-article'); |
| 51 | + content.find('img').each((_, e) => { |
| 52 | + const $e = $(e); |
| 53 | + $e.removeAttr('style srcset'); |
| 54 | + const src = $e.attr('src'); |
| 55 | + const params = new URLSearchParams(src); |
| 56 | + const newSrc = params.get('/_next/image?url'); |
| 57 | + if (newSrc) { |
| 58 | + $e.attr('src', newSrc); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + item.description = content.html(); |
| 63 | + |
| 64 | + return item; |
| 65 | + }), |
| 66 | + { concurrency: 5 } |
| 67 | + ); |
| 68 | + |
| 69 | + return { |
| 70 | + title: 'Anthropic Frontier Red Team', |
| 71 | + link, |
| 72 | + image: `${baseUrl}/anthropic-serve/favicon.ico`, |
| 73 | + item: items, |
| 74 | + }; |
| 75 | +} |
0 commit comments