/
blog.ts
85 lines (78 loc) 路 2.61 KB
/
blog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
const rootURL = 'https://www.uber.com';
const apiURL = 'https://blogapi.uber.com';
export const route: Route = {
path: '/blog/:maxPage?',
categories: ['blog'],
example: '/uber/blog',
parameters: { maxPage: 'max number of pages to retrieve, default to 1 page at most' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.uber.com/blog/pittsburgh/engineering'],
target: '/blog',
},
],
name: 'Engineering',
maintainers: ['hulb'],
handler,
url: 'www.uber.com/blog/pittsburgh/engineering',
};
async function handler(ctx) {
let maxPage = Number(ctx.req.param('maxPage'));
if (Number.isNaN(maxPage)) {
maxPage = 1;
}
let pages = await Promise.all(
[...Array(maxPage).keys()].map((pageIdx) =>
got(`${apiURL}/wp-json/blog/v1/data`, {
searchParams: {
page: pageIdx + 1,
parent: 'pittsburgh',
slug: 'engineering',
},
})
)
);
pages = pages.map((page) => page.data);
const result = await Promise.all(
pages.map((page) =>
Promise.all(
page.posts.map((post) =>
cache.tryGet(`${rootURL}${post.link}`, async () => {
let { data: article } = await got(`${apiURL}/wp-json/blog/v1/data`, {
searchParams: {
slug: post.link.replaceAll('/', '').replace('blog', ''),
},
});
article = article.article;
return {
link: article.link,
title: article.title,
description: article.content,
pubDate: parseDate(article.created),
author: article.author,
category: article.categories.map((category) => category.category_name),
};
})
)
)
)
);
return {
title: `Uber Engineering Blog`,
link: rootURL + '/blog/engineering',
description: 'The technology behind Uber Engineering',
item: result.flat(),
};
}