feat: add sixthtone news (#2404)

This commit is contained in:
Cloud
2019-06-15 10:28:58 +08:00
committed by DIYgod
parent 59bb15e761
commit c411696488
3 changed files with 66 additions and 0 deletions

View File

@@ -189,6 +189,12 @@ pageClass: routes
</Route> </Route>
## sixthtone
### 最新文章
<Route author="kt286" example="/sixthtone/news" path="/sixthtone/news"/>
## The Verge ## The Verge
### The Verge ### The Verge

View File

@@ -1415,4 +1415,7 @@ router.get('/kkj/news', require('./routes/kkj/news'));
// Outage.Report // Outage.Report
router.get('/outagereport/:name/:count?', require('./routes/outagereport/service')); router.get('/outagereport/:name/:count?', require('./routes/outagereport/service'));
// sixthtone
router.get('/sixthtone/news', require('./routes/sixthtone/news'));
module.exports = router; module.exports = router;

View File

@@ -0,0 +1,57 @@
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://www.sixthtone.com/rss');
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
$('img').each((index, elem) => {
const $elem = $(elem);
$elem.attr('referrerpolicy', 'no-referrer');
});
// 移除广告等内容
$('[class*="advertise-"]').remove();
// 提取内容
return $('.content').html();
};
const items = await Promise.all(
feed.items.map(async (item) => {
const link = 'https:' + item.link.match(/\/\/www.sixthtone.com\/news\/\d+\//g)[0];
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: link,
});
const description = ProcessFeed(response.data);
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: link,
author: item.author,
};
ctx.cache.set(link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: feed.title,
link: feed.link,
description: feed.description,
item: items,
};
};