增加 The Guardian Editorial 全文 (#1101)

This commit is contained in:
Henry Wang
2018-11-10 14:15:37 +08:00
committed by DIYgod
parent e0fcc1e1d5
commit 85851b9d5f
5 changed files with 89 additions and 0 deletions
+14
View File
@@ -1595,6 +1595,20 @@ Category 列表:
</route>
### 卫报 The Guardian
<route name="Editorial" author="HenryQW" example="/guardian/editorial" path="/guardian/editorial">
::: tip 提示
由于众所周知的原因,文章内的图片在中国大陆可能无法正常显示。
:::
通过提取文章全文,以提供比官方源更佳的阅读体验。
</route>
### 多维新闻网
<route name="要闻" author="HenryQW" example="/dwnews/yaowen/global" path="/dwnews/yaowen/:region?" :paramsDesc="['要闻地区,默认`全部`,可选地区如下']">
+8
View File
@@ -389,3 +389,11 @@ Provides a better reading experience (full text articles) over the official one.
### United Nations
<routeEn name="Security Council Vetoed a Resolution" author="HenryQW" example="/un/scveto" path="/un/scveto"/>
### The Guardian
<routeEn name="Editorial" author="HenryQW" example="/guardian/editorial" path="/guardian/editorial">
Provides a better reading experience (full text articles) over the official one.
</routeEn>
+3
View File
@@ -773,6 +773,9 @@ router.get('/tanwu/products', require('./routes/tanwu/products'));
// GitChat
router.get('/gitchat/newest', require('./routes/gitchat/newest'));
// The Guardian
router.get('/guardian/editorial', require('./routes/guardian/editorial'));
// 下厨房
router.get('/xiachufang/user/cooked/:id', require('./routes/xiachufang/user/cooked'));
router.get('/xiachufang/user/created/:id', require('./routes/xiachufang/user/created'));
+32
View File
@@ -0,0 +1,32 @@
const axios = require('../../utils/axios');
const utils = require('./utils');
const Parser = require('rss-parser');
const parser = new Parser();
module.exports = async (ctx) => {
const feed = await parser.parseURL('https://www.theguardian.com/tone/editorials/rss');
const items = await Promise.all(
feed.items.splice(0, 10).map(async (item) => {
const response = await axios({
method: 'get',
url: item.link,
});
const description = utils.ProcessFeed(response.data);
const single = {
title: item.title,
description,
pubDate: item.pubDate,
link: item.link,
};
return Promise.resolve(single);
})
);
ctx.state.data = {
title: 'The Guardian - Editorial',
link: 'https://www.theguardian.com/profile/editorial',
description: 'The Guardian - Editorial',
item: items,
};
};
+32
View File
@@ -0,0 +1,32 @@
const cheerio = require('cheerio');
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
const content = $('div.content__article-body');
// remove useless DOMs
content.find('aside, .ad-slot, .after-article, .contributions__epic, .submeta').each((i, e) => {
$(e).remove();
});
if ($('#img-1').length > 0) {
// there is a cover photo
const photo = $('#img-1').find('img');
let cover = `<figure><img alt='${photo[0].attribs.alt}' src='${photo[0].attribs.src}'><br><figcaption>`;
const caption = $('#img-1').find('figcaption');
cover += `${$(caption[0])
.text()
.trim()}</figcaption></figure>`;
$(cover).insertBefore(content[0].firstChild);
}
return content.html();
};
module.exports = {
ProcessFeed,
};