feat: add blizzard news (#6311)

This commit is contained in:
Ethan Shen
2020-12-04 15:50:40 +00:00
committed by GitHub
parent a694450eab
commit 6d162fd3c0
4 changed files with 146 additions and 0 deletions
+3
View File
@@ -3517,6 +3517,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'));
// Blizzard
router.get('/blizzard/news/:language?/:category?', require('./routes/blizzard/news'));
// DeepMind
router.get('/deepmind/blog/:category?', require('./routes/deepmind/blog'));
+52
View File
@@ -0,0 +1,52 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const category = ctx.params.category || '';
const language = ctx.params.language || 'en-us';
const rootUrl = `https://${language === 'zh-cn' ? 'cn.' : ''}news.blizzard.com`;
const currentUrl = `${rootUrl}/${language}/${category}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('.FeaturedArticle-text a, .ArticleListItem article a')
.slice(0, 10)
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.author = content('.ArticleDetail-bylineAuthor').text();
item.description = content('.ArticleDetail-content').html();
item.pubDate = content('.ArticleDetail-subHeadingLeft time').attr('timestamp');
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};