feat: add RF技术社区文章 (#4749)

This commit is contained in:
Ethan Shen
2020-05-18 12:07:03 +08:00
committed by GitHub
parent 41bc394b08
commit 9003f28d8b
3 changed files with 49 additions and 0 deletions

40
lib/routes/rf/article.js Normal file
View File

@@ -0,0 +1,40 @@
const url = require('url');
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const rootUrl = 'https://rf.eefocus.com/article/list-all';
const response = await got({
method: 'get',
url: rootUrl,
});
const $ = cheerio.load(response.data);
const list = $('h3.media-heading')
.slice(0, 10)
.map((_, item) => {
item = $(item);
const a = item.find('a');
return {
title: a.text(),
link: url.resolve('https://rf.eefocus.com/', a.attr('href')),
};
})
.get();
ctx.state.data = {
title: 'RF技术社区 - 文章',
link: rootUrl,
item: await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const res = await got({ method: 'get', url: item.link });
const content = cheerio.load(res.data);
item.pubDate = new Date(content('div.article-subline-desc').eq(0).text().replace('发布时间:', '')).toUTCString();
item.description = content('div.clearfix.article-content').html();
return item;
})
)
),
};
};