From ef2f74d08e094736a989daf2e089da541be8eba5 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 27 Nov 2021 16:54:24 +0800 Subject: [PATCH] feat(route): add Distill (#8491) --- docs/en/programming.md | 6 ++++ docs/programming.md | 6 ++++ lib/v2/distill/index.js | 64 ++++++++++++++++++++++++++++++++++++ lib/v2/distill/maintainer.js | 3 ++ lib/v2/distill/radar.js | 13 ++++++++ lib/v2/distill/router.js | 3 ++ 6 files changed, 95 insertions(+) create mode 100644 lib/v2/distill/index.js create mode 100644 lib/v2/distill/maintainer.js create mode 100644 lib/v2/distill/radar.js create mode 100644 lib/v2/distill/router.js diff --git a/docs/en/programming.md b/docs/en/programming.md index 560c2e447d..71ea49f124 100644 --- a/docs/en/programming.md +++ b/docs/en/programming.md @@ -16,6 +16,12 @@ pageClass: routes +## Distill + +### Latest + + + ## GitHub ::: tip diff --git a/docs/programming.md b/docs/programming.md index d6b1d4101a..6d151db2c0 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -58,6 +58,12 @@ pageClass: routes +## Distill + +### Latest + + + ## Dockone ### 周报 diff --git a/lib/v2/distill/index.js b/lib/v2/distill/index.js new file mode 100644 index 0000000000..3e4f90f88b --- /dev/null +++ b/lib/v2/distill/index.js @@ -0,0 +1,64 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const rootUrl = 'https://distill.pub'; + + const response = await got({ + method: 'get', + url: rootUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('.post-preview') + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.find('.title').text(), + link: `${rootUrl}/${item.children('a').attr('href')}`, + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + content('d-contents').remove(); + + content('img').each(function () { + content(this).attr('src', `${item.link}/${content(this).attr('src')}`); + }); + + item.doi = content('meta[name="citation_doi"]').attr('content'); + item.pubDate = parseDate(content('meta[property="article:published"]').attr('content')); + item.description = content('d-article') + .children() + .toArray() + .map((c) => content(c).html()) + .join(''); + item.author = content('meta[property="article:author"]') + .toArray() + .map((author) => content(author).attr('content')) + .join(', '); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: rootUrl, + item: items, + }; +}; diff --git a/lib/v2/distill/maintainer.js b/lib/v2/distill/maintainer.js new file mode 100644 index 0000000000..ca7cf1eb13 --- /dev/null +++ b/lib/v2/distill/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/': ['nczitzk'], +}; diff --git a/lib/v2/distill/radar.js b/lib/v2/distill/radar.js new file mode 100644 index 0000000000..a2772baa15 --- /dev/null +++ b/lib/v2/distill/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'distill.pub': { + _name: 'Distill', + '.': [ + { + title: 'Latest', + docs: 'https://docs.rsshub.app/programming.html#distill', + source: ['/'], + target: '/distill', + }, + ], + }, +}; diff --git a/lib/v2/distill/router.js b/lib/v2/distill/router.js new file mode 100644 index 0000000000..20c52b09d7 --- /dev/null +++ b/lib/v2/distill/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/', require('./index')); +};