From 69b58e83b9c48c6ff2a90ccb8709569418eb1d10 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Tue, 10 Sep 2024 21:56:24 +0800 Subject: [PATCH] feat(route/apnews): Add support for API (#16640) * feat(route/apnews): Add support for API * Add limit * . --- lib/routes/apnews/api.ts | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/routes/apnews/api.ts diff --git a/lib/routes/apnews/api.ts b/lib/routes/apnews/api.ts new file mode 100644 index 00000000000000..fffed46252da09 --- /dev/null +++ b/lib/routes/apnews/api.ts @@ -0,0 +1,57 @@ +import { Route, ViewType } from '@/types'; +import { fetchArticle } from './utils'; +import ofetch from '@/utils/ofetch'; +import timezone from '@/utils/timezone'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/api/:tags?', + categories: ['traditional-media'], + example: '/apnews/api/business', + view: ViewType.Articles, + parameters: { + tags: { + description: 'Getting a list of articles from a public API based on tags. See https://github.com/kovidgoyal/calibre/blob/81666219718b5f57d56b149a7ac017cc2a76b931/recipes/ap.recipe#L43-L46', + default: 'apf-topnews', + }, + }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['apnews.com/'], + }, + ], + name: 'News', + maintainers: ['dzx-dzx'], + handler, +}; + +async function handler(ctx) { + const { tags = 'apf-topnews' } = ctx.req.param(); + const apiRootUrl = 'https://afs-prod.appspot.com/api/v2/feed/tag'; + const url = `${apiRootUrl}?tags=${tags}`; + const res = await ofetch(url); + + const list = res.cards + .map((e) => ({ + title: e.contents[0]?.headline, + link: e.contents[0]?.localLinkUrl, + pubDate: timezone(parseDate(e.publishedDate), 0), + })) + .sort((a, b) => b.pubDate - a.pubDate) + .slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20); + + const items = await Promise.all(list.map((item) => fetchArticle(item))); + + return { + title: `${res.tagObjs[0].name} - AP News`, + item: items, + }; +}