|
| 1 | +import { Route, ViewType } from '@/types'; |
| 2 | +import { asyncPoolAll, fetchArticle } from './utils'; |
| 3 | +import ofetch from '@/utils/ofetch'; |
| 4 | +import { parseDate } from '@/utils/parse-date'; |
| 5 | + |
| 6 | +export const route: Route = { |
| 7 | + path: '/mobile/:path{.+}?', |
| 8 | + categories: ['traditional-media'], |
| 9 | + example: '/apnews/mobile/ap-top-news', |
| 10 | + view: ViewType.Articles, |
| 11 | + parameters: { |
| 12 | + path: { |
| 13 | + description: 'Corresponding path from AP News website', |
| 14 | + default: 'ap-top-news', |
| 15 | + }, |
| 16 | + }, |
| 17 | + features: { |
| 18 | + requireConfig: false, |
| 19 | + requirePuppeteer: false, |
| 20 | + antiCrawler: false, |
| 21 | + supportBT: false, |
| 22 | + supportPodcast: false, |
| 23 | + supportScihub: false, |
| 24 | + }, |
| 25 | + radar: [ |
| 26 | + { |
| 27 | + source: ['apnews.com/'], |
| 28 | + }, |
| 29 | + ], |
| 30 | + name: 'News (from mobile client API)', |
| 31 | + maintainers: ['dzx-dzx'], |
| 32 | + handler, |
| 33 | +}; |
| 34 | + |
| 35 | +async function handler(ctx) { |
| 36 | + const path = ctx.req.param('path') ? `/${ctx.req.param('path')}` : '/hub/ap-top-news'; |
| 37 | + const apiRootUrl = 'https://apnews.com/graphql/delivery/ap/v1'; |
| 38 | + const res = await ofetch(apiRootUrl, { |
| 39 | + query: { |
| 40 | + operationName: 'ContentPageQuery', |
| 41 | + variables: { path }, |
| 42 | + extensions: { persistedQuery: { version: 1, sha256Hash: '3bc305abbf62e9e632403a74cc86dc1cba51156d2313f09b3779efec51fc3acb' } }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + const screen = res.data.Screen; |
| 47 | + |
| 48 | + const list = [...screen.main.filter((e) => e.__typename === 'ColumnContainer').flatMap((_) => _.columns), ...screen.main.filter((e) => e.__typename !== 'ColumnContainer')] |
| 49 | + .filter((e) => e.__typename !== 'GoogleDfPAdModule') |
| 50 | + .flatMap((e) => { |
| 51 | + switch (e.__typename) { |
| 52 | + case 'PageListModule': |
| 53 | + return e.items; |
| 54 | + case 'VideoPlaylistModule': |
| 55 | + return e.playlist; |
| 56 | + default: |
| 57 | + return; |
| 58 | + } |
| 59 | + }) |
| 60 | + .filter(Boolean) |
| 61 | + .map((e) => { |
| 62 | + if (e.__typename === 'PagePromo') { |
| 63 | + return { |
| 64 | + title: e.title, |
| 65 | + link: e.url, |
| 66 | + pubDate: parseDate(e.publishDateStamp), |
| 67 | + category: e.category, |
| 68 | + description: e.description, |
| 69 | + guid: e.id, |
| 70 | + }; |
| 71 | + } else if (e.__typename === 'VideoPlaylistItem') { |
| 72 | + return { |
| 73 | + title: e.title, |
| 74 | + link: e.url, |
| 75 | + description: e.description, |
| 76 | + guid: e.contentId, |
| 77 | + }; |
| 78 | + } else { |
| 79 | + return; |
| 80 | + } |
| 81 | + }) |
| 82 | + .filter(Boolean) |
| 83 | + .sort((a, b) => b.pubDate - a.pubDate) |
| 84 | + .slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20); |
| 85 | + |
| 86 | + const items = ctx.req.query('fulltext') === 'true' ? await asyncPoolAll(10, list, (item) => fetchArticle(item)) : list; |
| 87 | + |
| 88 | + return { |
| 89 | + title: screen.category ?? screen.title, |
| 90 | + item: items, |
| 91 | + link: 'https://apnews.com', |
| 92 | + }; |
| 93 | +} |
0 commit comments