Skip to content

Commit d8efe7b

Browse files
authored
feat(apnews): Mobile API (#18819)
* feat(apnews): Mobile API * . * 更新 mobile-api.ts * 更新 mobile-api.ts * .
1 parent b0181ee commit d8efe7b

File tree

3 files changed

+95
-79
lines changed

3 files changed

+95
-79
lines changed

lib/routes/apnews/api.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

lib/routes/apnews/mobile-api.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

lib/routes/apnews/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export function fetchArticle(item) {
3838
$('div.Enhancement').remove();
3939
const section = $("meta[property='article:section']").attr('content');
4040
return {
41+
...item,
4142
title: ldjson.headline,
4243
pubDate: parseDate(ldjson.datePublished),
4344
updated: parseDate(ldjson.dateModified),
4445
description: $('div.RichTextStoryBody').html() || $(':is(.VideoLead, .VideoPage-pageSubHeading)').html(),
4546
category: [...(section ? [section] : []), ...(ldjson.keywords ?? [])],
4647
guid: $("meta[name='brightspot.contentId']").attr('content'),
4748
author: ldjson.author,
48-
...item,
4949
};
5050
} else {
5151
// Live
@@ -56,11 +56,11 @@ export function fetchArticle(item) {
5656
const pubDate = url.hash ? parseDate(Number.parseInt($(url.hash).parent().attr('data-posted-date-timestamp'), 10)) : parseDate(ldjson.coverageStartTime);
5757

5858
return {
59+
...item,
5960
category: ldjson.keywords,
6061
pubDate,
6162
description,
6263
guid: $("meta[name='brightspot.contentId']").attr('content'),
63-
...item,
6464
};
6565
}
6666
});

0 commit comments

Comments
 (0)