1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| import { Route } from '@/types'; import cache from '@/utils/cache'; import got from '@/utils/got'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date';
export const route: Route = { path: '/updates', categories: ['programming'], example: '/elixirmerge/updates', parameters: {}, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false, }, radar: [ { source: ['elixirmerge.com/'], }, ], name: 'Latest Articles', maintainers: [], handler, url: 'elixirmerge.com', description: 'Get the latest articles from Elixir Merge, a daily Elixir development newsletter.' };
async function handler() { const url = 'https://elixirmerge.com/';
const response = await got({ method: 'get', url, });
const $ = load(response.data);
const articles = [];
$('body').find('div, article').each((_, element) => { const $element = $(element); const dateText = $element.text().match(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{1,2},\s\d{4}/)?.[0];
if (dateText) { const fullText = $element.text().trim(); const parts = fullText.split(dateText);
if (parts.length >= 1) { const title = parts[0].trim();
if (title.length > 10 && title !== 'Subscribe') { let description = '';
const prevElement = $element.prev(); if (prevElement.text().length > 20 && !prevElement.text().includes('Subscribe')) { description = prevElement.text().trim(); }
const slug = title .toLowerCase() .replace(/[^\w\s-]/g, '') .replace(/\s+/g, '-');
const link = `https://elixirmerge.com/p/${slug}`;
articles.push({ title, link, pubDate: parseDate(dateText, 'MMM D, YYYY'), description: description || title, }); } } } });
$('a:contains("Continue reading")').each((_, element) => { const $parent = $(element).closest('div');
let title = $parent.find('h1, h2, h3').text().trim(); if (!title) { const $container = $parent.parent(); title = $container.find('h1, h2, h3').first().text().trim(); }
let description = ''; $parent.find('p').each((_, p) => { const text = $(p).text().trim(); if (text.length > 30) { description = text; return false; } });
const dateMatch = $parent.text().match(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{1,2},\s\d{4}/); const dateText = dateMatch ? dateMatch[0] : '';
if (title && dateText) { const slug = title .toLowerCase() .replace(/[^\w\s-]/g, '') .replace(/\s+/g, '-');
const link = `https://elixirmerge.com/p/${slug}`;
articles.push({ title, link, pubDate: parseDate(dateText, 'MMM D, YYYY'), description: description || title, }); } });
$('.grid, .flex, .container, main').find('div').each((_, container) => { const $container = $(container); const dateMatch = $container.text().match(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{1,2},\s\d{4}/);
if (dateMatch && $container.text().length > 50 && $container.text().length < 1000) { const dateText = dateMatch[0]; let title = ''; let description = '';
$container.children().each((_, child) => { const text = $(child).text().trim(); if (!title && text.length > 10 && !text.includes(dateText)) { title = text; } else if (title && text.length > 30 && !text.includes(dateText)) { description = text; } });
if (!title) { const fullText = $container.text().replace(dateText, '').trim(); const lines = fullText.split('\n').map(line => line.trim()).filter(line => line); if (lines.length > 0) { title = lines[0]; if (lines.length > 1) { description = lines.slice(1).join(' '); } } }
if (title && title !== 'Subscribe') { const slug = title .toLowerCase() .replace(/[^\w\s-]/g, '') .replace(/\s+/g, '-');
const link = `https://elixirmerge.com/p/${slug}`;
articles.push({ title, link, pubDate: parseDate(dateText, 'MMM D, YYYY'), description: description || title, }); } } });
const uniqueArticles = []; const titleSet = new Set();
for (const article of articles) { if (!titleSet.has(article.title)) { titleSet.add(article.title); uniqueArticles.push(article); } }
uniqueArticles.sort((a, b) => new Date(b.pubDate).getTime() - new Date(a.pubDate).getTime());
const latestArticles = uniqueArticles.slice(0, 30);
return { title: 'Elixir Merge - Daily Elixir Newsletter', link: url, description: 'The latest updates from Elixir Merge - A daily newsletter with the best Elixir content in just 5 minutes', item: latestArticles, language: 'en', }; }
|