feat: add deepl blog (#6293)

This commit is contained in:
Ethan Shen 2020-12-01 19:54:36 +08:00 committed by GitHub
parent 6c5198f728
commit 227e4289a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

View File

@ -118,6 +118,12 @@ pageClass: routes
<Route author="kt286" example="/cnbeta" path="/cnbeta"/>
## DeepL
### Blog
<Route author="nczitzk" example="/deepl/blog" path="/deepl/blog/:lang?" :paramsDesc="['语言,可选 `en` 指 英语 和 `zh` 指 汉语,默认为 en']"/>
## Deutsche Welle 德国之声
<Route author="nczitzk" example="/dw/zh" path="/dw/:lang?/:caty?" :paramsDesc="['语言,可在对应语言版本页的 URL 中找到,默认为德语', '分类,见下表,默认为全部']">

View File

@ -3516,6 +3516,9 @@ router.get('/uisdc/news', require('./routes/uisdc/news'));
router.get('/uisdc/zt/:title?', require('./routes/uisdc/zt'));
router.get('/uisdc/topic/:title?/:sort?', require('./routes/uisdc/topic'));
// DeepL
router.get('/deepl/blog/:lang?', require('./routes/deepl/blog'));
// OpenAI
router.get('/openai/blog/:tag?', require('./routes/openai/blog'));

34
lib/routes/deepl/blog.js Normal file
View File

@ -0,0 +1,34 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const lang = ctx.params.lang || 'en';
const rootUrl = 'https://www.deepl.com';
const currentUrl = `${rootUrl}/zh/blog`;
const blogUrl = `${rootUrl}/gatsby/locales/${lang}/blog.json`;
const response = await got({
method: 'get',
url: blogUrl,
});
const items = [];
const data = response.data;
for (const key in data) {
const matches = key.match(/post_content_(\d{8})/);
if (matches) {
const date = matches[1];
items.push({
description: data[key],
link: `${currentUrl}/${date}`,
title: data['post_title_' + date],
pubDate: new Date(`${date.substr(0, 4)}-${date.substr(4, 2)}-${date.substr(6, 2)}`).toUTCString(),
});
}
}
ctx.state.data = {
title: response.data.title,
link: currentUrl,
item: items,
};
};