From 7e0dc17ef86ebbafb18422f317c82d2ba802378c Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Thu, 22 Aug 2024 23:36:29 +0800 Subject: [PATCH] feat(route/ftm): Add route (#16503) * feat(route/ftm): Add route * Update index.ts --- lib/routes/ftm/index.ts | 60 +++++++++++++++++++++++++++++++++++++ lib/routes/ftm/namespace.ts | 6 ++++ 2 files changed, 66 insertions(+) create mode 100644 lib/routes/ftm/index.ts create mode 100644 lib/routes/ftm/namespace.ts diff --git a/lib/routes/ftm/index.ts b/lib/routes/ftm/index.ts new file mode 100644 index 000000000..c84033789 --- /dev/null +++ b/lib/routes/ftm/index.ts @@ -0,0 +1,60 @@ +import { Route } from '@/types'; + +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; + +export const route: Route = { + path: '/', + categories: ['new-media'], + example: '/ftm', + parameters: {}, + name: '文章', + maintainers: ['dzx-dzx'], + radar: [ + { + source: ['www.ftm.eu'], + }, + ], + handler, +}; + +async function handler(ctx) { + const rootUrl = 'https://www.ftm.eu'; + const currentUrl = `${rootUrl}/articles`; + const response = await ofetch(currentUrl); + + const $ = load(response); + + const list = $('.article-card') + .toArray() + .map((e) => ({ link: $(e).attr('href'), title: $(e).find('h2').text() })) + .slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : Infinity); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const content = load(await ofetch(item.link)); + const ldjson = JSON.parse(content('[type="application/ld+json"]:not([data-schema])').text()); + + item.pubDate = parseDate(ldjson.datePublished); + item.updated = parseDate(ldjson.dateModified); + + item.author = content("[name='author']") + .toArray() + .map((e) => ({ name: $(e).attr('content') })); + item.category = content('.collection .tab').text().trim() || null; + + item.description = content('.body').html(); + + return item; + }) + ) + ); + return { + title: $('title').text(), + link: currentUrl, + item: items, + }; +} diff --git a/lib/routes/ftm/namespace.ts b/lib/routes/ftm/namespace.ts new file mode 100644 index 000000000..df6951f56 --- /dev/null +++ b/lib/routes/ftm/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Follow The Money', + url: 'www.ftm.eu', +};