feat: add engadget cn (#3041)

* feat: add engadget cn

* remove log
This commit is contained in:
JamesWDGu 2019-09-10 19:14:25 +08:00 committed by DIYgod
parent 0b0b211bf9
commit 8b2eb2cee6
3 changed files with 53 additions and 0 deletions

View File

@ -57,6 +57,12 @@ pageClass: routes
</Route>
## Engadget 瘾科技
### 中文全文
<Route author="JamesWDGu" example="/engadget-cn" path="/engadget-cn"/>
## iDownloadBlog
### blog

View File

@ -1707,4 +1707,7 @@ router.get('/andyt/:view?', require('./routes/andyt/index'));
// 品途商业评论
router.get('/pintu360/:type?', require('./routes/pintu360/index'));
// engadget中国版
router.get('/engadget-cn', require('./routes/engadget-cn/home'));
module.exports = router;

View File

@ -0,0 +1,44 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const parser = require('@/utils/rss-parser');
module.exports = async (ctx) => {
const feed = await parser.parseURL('https://cn.engadget.com/rss.xml');
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
return $.html('figure img') + '<br>' + $('.article-text p').html();
};
const items = await Promise.all(
feed.items.map(async (item) => {
const cache = await ctx.cache.get(item.link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: item.link,
});
const description = ProcessFeed(response.data);
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: item.link,
author: item.author,
};
ctx.cache.set(item.link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: 'Engadget 中文版 全文',
link: 'https://cn.engadget.com/',
description: feed.description,
item: items,
};
};