From e9a3c1b8b4d15ce12b65bffec9f040cca770e2cf Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Sat, 10 Nov 2018 06:10:09 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BA=BD=E7=BA=A6=E6=97=B6?= =?UTF-8?q?=E6=8A=A5=E4=B8=AD=E5=9B=BD=E7=BD=91=E5=85=A8=E6=96=87=E8=BE=93?= =?UTF-8?q?=E5=87=BA=20(#1100)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #1076 --- docs/README.md | 8 ++++++-- router.js | 2 +- routes/nytimes/index.js | 34 ++++++++++++++++++++++++++++++++++ routes/nytimes/morning_post.js | 25 ------------------------- routes/nytimes/utils.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 routes/nytimes/index.js delete mode 100644 routes/nytimes/morning_post.js create mode 100644 routes/nytimes/utils.js diff --git a/docs/README.md b/docs/README.md index c8dacfb2bc..8b8a57c5db 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1512,13 +1512,17 @@ Category 列表: ### 纽约时报 + + ::: tip 提示 -纽约时报 RSS: https://cn.nytimes.com/rss/ +由于众所周知的原因,文章内的图片在中国大陆可能无法正常显示。 ::: - +通过提取文章全文,以提供比官方源更佳的阅读体验。 + + ### 新京报 diff --git a/router.js b/router.js index 27d2d4fe0f..c46fe6f385 100644 --- a/router.js +++ b/router.js @@ -322,7 +322,7 @@ router.get('/yande.re/post/popular_recent', require('./routes/yande.re/post_popu router.get('/yande.re/post/popular_recent/:period', require('./routes/yande.re/post_popular_recent')); // 纽约时报 -router.get('/nytimes/morning_post', require('./routes/nytimes/morning_post')); +router.get('/nytimes', require('./routes/nytimes/index')); // 3dm router.get('/3dm/:name/:type', require('./routes/3dm/game')); diff --git a/routes/nytimes/index.js b/routes/nytimes/index.js new file mode 100644 index 0000000000..4205732c3a --- /dev/null +++ b/routes/nytimes/index.js @@ -0,0 +1,34 @@ +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://cn.nytimes.com/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, + author: item['dc:creator'], + }; + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: '纽约时报中文网 国际纵览', + link: 'https://cn.nytimes.com', + description: '纽约时报中文网 国际纵览', + item: items, + }; +}; diff --git a/routes/nytimes/morning_post.js b/routes/nytimes/morning_post.js deleted file mode 100644 index 8d2822889a..0000000000 --- a/routes/nytimes/morning_post.js +++ /dev/null @@ -1,25 +0,0 @@ -const Parser = require('rss-parser'); -const parser = new Parser(); - -module.exports = async (ctx) => { - const feed = await parser.parseURL('https://cn.nytimes.com/rss/'); - const items = []; - feed.items.forEach((item) => { - if (item.title.startsWith('早报:')) { - items.push({ - title: item.title, - description: item.content, - pubDate: item.pubDate, - guid: item.guid, - link: item.link, - }); - } - }); - - ctx.state.data = { - title: '纽约时报 新闻早报', - link: encodeURI('https://cn.nytimes.com/search?query=%E6%97%A9%E6%8A%A5&lang=&dt=json&from=0&size=10'), - description: '纽约时报 新闻早报', - item: items, - }; -}; diff --git a/routes/nytimes/utils.js b/routes/nytimes/utils.js new file mode 100644 index 0000000000..6ffe6bcaa8 --- /dev/null +++ b/routes/nytimes/utils.js @@ -0,0 +1,29 @@ +const cheerio = require('cheerio'); + +const ProcessFeed = (data) => { + const $ = cheerio.load(data); + const content = $('section.article-body'); + + // remove useless DOMs + content.find('div.big_ad, div.article-body-aside').each((i, e) => { + $(e).remove(); + }); + + if ($('figure.article-span-photo').length > 0) { + // there is a cover photo + const cover = $('figure.article-span-photo'); + $(cover).insertBefore(content[0].firstChild); + } + + if ($('footer.author-info').length > 0) { + // credit to original author and translators + const footer = $('footer.author-info'); + $(footer).insertAfter(content[0].lastChild); + } + + return content.html(); +}; + +module.exports = { + ProcessFeed, +};