diff --git a/docs/README.md b/docs/README.md index 47ec896b3f..afe66e418c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1595,6 +1595,20 @@ Category 列表: +### 卫报 The Guardian + + + +::: tip 提示 + +由于众所周知的原因,文章内的图片在中国大陆可能无法正常显示。 + +::: + +通过提取文章全文,以提供比官方源更佳的阅读体验。 + + + ### 多维新闻网 diff --git a/docs/en/README.md b/docs/en/README.md index f42acdca3b..ac4ec6f5c8 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -389,3 +389,11 @@ Provides a better reading experience (full text articles) over the official one. ### United Nations + +### The Guardian + + + +Provides a better reading experience (full text articles) over the official one. + + diff --git a/router.js b/router.js index c46fe6f385..fef1f7bf49 100644 --- a/router.js +++ b/router.js @@ -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')); diff --git a/routes/guardian/editorial.js b/routes/guardian/editorial.js new file mode 100644 index 0000000000..7f5db2947f --- /dev/null +++ b/routes/guardian/editorial.js @@ -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, + }; +}; diff --git a/routes/guardian/utils.js b/routes/guardian/utils.js new file mode 100644 index 0000000000..324ffe4d11 --- /dev/null +++ b/routes/guardian/utils.js @@ -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 = `
${photo[0].attribs.alt}
`; + + const caption = $('#img-1').find('figcaption'); + + cover += `${$(caption[0]) + .text() + .trim()}
`; + + $(cover).insertBefore(content[0].firstChild); + } + + return content.html(); +}; + +module.exports = { + ProcessFeed, +};