From d31b3cffcb0b544c2dc2af5cd6c8c1eb3e059a89 Mon Sep 17 00:00:00 2001 From: Cesaryuan <35998162+cesaryuan@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:11:22 +0800 Subject: [PATCH] feat(route): Add PAIR - AI Exploreables (#16786) * Add PAIR - AI Exploreables * fix: add radar * fix: maintainers and cache scope * fix: missing categoies --- lib/routes/withgoogle/explorables.ts | 54 ++++++++++++++++++++++++++++ lib/routes/withgoogle/namespace.ts | 6 ++++ 2 files changed, 60 insertions(+) create mode 100644 lib/routes/withgoogle/explorables.ts create mode 100644 lib/routes/withgoogle/namespace.ts diff --git a/lib/routes/withgoogle/explorables.ts b/lib/routes/withgoogle/explorables.ts new file mode 100644 index 00000000000000..868ace82b2ab0d --- /dev/null +++ b/lib/routes/withgoogle/explorables.ts @@ -0,0 +1,54 @@ +import type { Route, DataItem } from '@/types'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; + +export const route: Route = { + name: 'PAIR - AI Exploreables', + url: 'pair.withgoogle.com/explorables', + path: '/explorables', + maintainers: ['cesaryuan'], + example: '/withgoogle/explorables', + categories: ['blog'], + radar: [ + { + source: ['pair.withgoogle.com/explorables'], + target: '', + }, + ], + handler: async () => { + const baseUrl = 'https://pair.withgoogle.com'; + const response = await ofetch(baseUrl + '/explorables', { + method: 'GET', + }); + const $ = load(response); + const items = await Promise.all( + $('div.explorable-card') + .map(async (_, el) => { + const title = $(el).find('h3').text(); + const image = $(el).find('img').attr('src'); + const link = baseUrl + $(el).find('a').attr('href'); + return (await cache.tryGet(link, async () => { + const response = await ofetch(link); + const $item = load(response); + let description = $item('body').html(); + if (!description || description.trim() === '') { + description = $('p').text(); + } + return { + title, + link, + description, + image, + }; + })) as DataItem; + }) + .toArray() + ); + return { + title: 'PAIR - AI Exploreables', + link: 'https://pair.withgoogle.com/explorables', + item: items, + }; + }, +}; diff --git a/lib/routes/withgoogle/namespace.ts b/lib/routes/withgoogle/namespace.ts new file mode 100644 index 00000000000000..1e53c171ba0605 --- /dev/null +++ b/lib/routes/withgoogle/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'People + AI Research (PAIR)', + url: 'pair.withgoogle.com', +};