From c411696488a3c64988d46ffcd46427e959d40930 Mon Sep 17 00:00:00 2001 From: Cloud Date: Sat, 15 Jun 2019 10:28:58 +0800 Subject: [PATCH] feat: add sixthtone news (#2404) --- docs/other.md | 6 ++++ lib/router.js | 3 ++ lib/routes/sixthtone/news.js | 57 ++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 lib/routes/sixthtone/news.js diff --git a/docs/other.md b/docs/other.md index 956fd14e83..d262aa5ab5 100644 --- a/docs/other.md +++ b/docs/other.md @@ -189,6 +189,12 @@ pageClass: routes +## sixthtone + +### 最新文章 + + + ## The Verge ### The Verge diff --git a/lib/router.js b/lib/router.js index faef4928d6..e5c820975b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1415,4 +1415,7 @@ router.get('/kkj/news', require('./routes/kkj/news')); // Outage.Report router.get('/outagereport/:name/:count?', require('./routes/outagereport/service')); +// sixthtone +router.get('/sixthtone/news', require('./routes/sixthtone/news')); + module.exports = router; diff --git a/lib/routes/sixthtone/news.js b/lib/routes/sixthtone/news.js new file mode 100644 index 0000000000..a355126ad3 --- /dev/null +++ b/lib/routes/sixthtone/news.js @@ -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, + }; +};