From ab70c993dcfcf946207ff9e035fb489479a240c8 Mon Sep 17 00:00:00 2001 From: oppilate <64395752+oppilate@users.noreply.github.com> Date: Fri, 18 Dec 2020 16:58:17 +0000 Subject: [PATCH] add: phoronix (#6474) --- docs/en/traditional-media.md | 6 ++ docs/traditional-media.md | 6 ++ lib/router.js | 3 + lib/routes/phoronix/index.js | 113 +++++++++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 lib/routes/phoronix/index.js diff --git a/docs/en/traditional-media.md b/docs/en/traditional-media.md index 74a7489a6d..debefbbf14 100644 --- a/docs/en/traditional-media.md +++ b/docs/en/traditional-media.md @@ -128,6 +128,12 @@ Generates full-text feeds that the official feed doesn't provide. +## Phoronix + +### News & Reviews + + + ## Reuters ### Channel diff --git a/docs/traditional-media.md b/docs/traditional-media.md index ca60e08454..05ec13ee7a 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -162,6 +162,12 @@ pageClass: routes +## Phoronix + +### 新闻与评测 + + + ## Solidot ### 最新消息 diff --git a/lib/router.js b/lib/router.js index 76e74ef7fb..cbad3a6780 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3526,6 +3526,9 @@ router.get('/uisdc/topic/:title?/:sort?', require('./routes/uisdc/topic')); // 中国劳工观察 router.get('/chinalaborwatch/reports/:lang?/:industry?', require('./routes/chinalaborwatch/reports')); +// Phoronix +router.get('/phoronix/:page/:queryOrItem?', require('./routes/phoronix/index')); + // 美国中央情报局 router.get('/cia/foia-annual-report', require('./routes/us/cia/foia-annual-report')); diff --git a/lib/routes/phoronix/index.js b/lib/routes/phoronix/index.js new file mode 100644 index 0000000000..30d4c11fa4 --- /dev/null +++ b/lib/routes/phoronix/index.js @@ -0,0 +1,113 @@ +const parser = require('@/utils/rss-parser'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const rssUrl = new URL('/rss.php', 'https://www.phoronix.com'); + rssUrl.searchParams.set('page', ctx.params.page); + if (ctx.params.queryOrItem) { + if (ctx.params.page === 'category') { + rssUrl.searchParams.set('item', ctx.params.queryOrItem); + } else { + rssUrl.searchParams.set('q', ctx.params.queryOrItem); + } + } + + const feed = await parser.parseURL(rssUrl.toString()); + + const items = await Promise.all( + feed.items.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const response = await got(item.link); + const html = response.body; + const $ = cheerio.load(html); + const content = $('.content'); + + // Maybe it's paginated + const links = []; + $('.pagination > a').each((_i, pager) => { + const link = pager.attribs.href; + links.push(link); + }); + + const pages = await Promise.all( + links.map( + async (link) => + await ctx.cache.tryGet(link, async () => { + const response = await got(item.link); + const html = response.body; + const $$ = cheerio.load(html); + const page = $$('.content'); + return page.html(); + }) + ) + ); + content.append(pages); + + // Summary + const summary = $('.content > p:nth-child(1)'); + + // Author + const author = []; + const authorSelector = $('.author > a'); + // thel last 2 are the category and comments + authorSelector.slice(0, authorSelector.length - 2).each((_i, e) => author.push(e.attribs.content)); + + // Remove unwanted DOMs + const unwanted_element_selectors = [ + '[class*="-advert"]', + '.social-share', + '.article-body-after', + 'scmp-chinaR2-early-text', + '.newsletter-widget-wrapper', + '[id*="-tracker"]', + '[class^="advert-"]', + 'amp-list', + '.more-on-this', + ]; + unwanted_element_selectors.forEach((selector) => { + content.find(selector).each((_i, e) => { + $(e).remove(); + }); + }); + + return { + title: item.title, + id: item.guid, + pubDate: item.pubDate, + author: author.join(), + link: item.link, + summary: summary.html(), + description: content.html(), + icon: 'https://www.phoronix.com/android-chrome-192x192.png', + logo: 'https://www.phoronix.com/phxcms7-css/phoronix.png', + }; + }) + ) + ); + + ctx.state.data = { + title: feed.title, + link: feed.link, + description: feed.description, + item: items, + language: feed.language, + icon: 'https://www.phoronix.com/android-chrome-192x192.png', + logo: 'https://www.phoronix.com/phxcms7-css/phoronix.png', + // Copied from thier web page metadata + category: [ + 'Linux Hardware Reviews', + 'Linux hardware benchmarks', + 'Linux Hardware', + 'Linux benchmarking', + 'Desktop Linux', + 'GNU/Linux benchmarks', + 'Open Source AMD', + 'Linux How To', + 'X.Org drivers', + 'Ubuntu hardware', + 'Phoronix Test Suite', + ], + }; +};