feat(route/thinkingmachines): add news route for Thinking Machines Lab (#21609)
* route(thinkingmachines): add news route for Thinking Machines Lab Add route for Thinking Machines Lab (thinkingmachines.ai) news page. Founded by Mira Murati (ex-OpenAI CTO), the lab publishes news about their AI research and products. Closes #0 * fix: use import type and sort imports for oxlint * fix: correct import sort order per simple-import-sort * fix: strip title/author/pubDate heading from description, remove unnecessary try/catch Address review feedback: - Remove .post-heading block from description (title, author, pubDate have dedicated fields) - Remove try/catch wrapper (requests do not throw errors) * fix: remove article fallback and strip paginator from description
This commit is contained in:
parent
d8eaee5889
commit
2933f894ac
|
|
@ -0,0 +1,6 @@
|
|||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'Thinking Machines Lab',
|
||||
url: 'thinkingmachines.ai',
|
||||
};
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
import { load } from 'cheerio';
|
||||
|
||||
import type { Route } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/news',
|
||||
name: 'News',
|
||||
url: 'thinkingmachines.ai/news',
|
||||
maintainers: ['w3nhao'],
|
||||
example: '/thinkingmachines/news',
|
||||
categories: ['programming'],
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['thinkingmachines.ai/news', 'thinkingmachines.ai/news/'],
|
||||
target: '/news',
|
||||
},
|
||||
],
|
||||
handler,
|
||||
};
|
||||
|
||||
async function handler() {
|
||||
const baseUrl = 'https://thinkingmachines.ai';
|
||||
const listUrl = `${baseUrl}/news/`;
|
||||
|
||||
const response = await ofetch(listUrl);
|
||||
const $ = load(response);
|
||||
|
||||
const items = $('main li a')
|
||||
.toArray()
|
||||
.map((el) => {
|
||||
const $el = $(el);
|
||||
const title = $el.find('.post-title').text().trim();
|
||||
const dateStr = $el.find('time.desktop-time').text().trim();
|
||||
const href = $el.attr('href') || '';
|
||||
const link = href.startsWith('http') ? href : `${baseUrl}${href}`;
|
||||
|
||||
return { title, dateStr, link };
|
||||
})
|
||||
.filter((item) => item.title && item.link);
|
||||
|
||||
const fullItems = await Promise.all(
|
||||
items.map((item) =>
|
||||
cache.tryGet(item.link, async () => {
|
||||
const articleResponse = await ofetch(item.link);
|
||||
const $article = load(articleResponse);
|
||||
|
||||
// Remove non-content elements
|
||||
$article('nav, footer, header, script, style').remove();
|
||||
// Remove heading (title, author, pubDate) and paginator
|
||||
$article('.post-heading, #post-prev-link, #post-next-link').remove();
|
||||
|
||||
const description = $article('main').html()?.trim() || '';
|
||||
|
||||
return {
|
||||
title: item.title,
|
||||
link: item.link,
|
||||
pubDate: parseDate(item.dateStr, 'MMM D, YYYY'),
|
||||
description,
|
||||
};
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
title: 'Thinking Machines Lab - News',
|
||||
link: listUrl,
|
||||
item: fullItems,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in New Issue