forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route/apnews): Add support for API (DIYgod#16640)
* feat(route/apnews): Add support for API * Add limit * .
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} |