From 1c8812b074239f918a19c1c19fcb4bae6e6dedde Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Tue, 5 Jul 2022 21:05:33 +0800 Subject: [PATCH] fix(route): Darwin Awards (#10136) * fix(route): Darwin Awards * fix typo --- docs/en/other.md | 4 +-- docs/other.md | 4 +-- lib/router.js | 2 +- lib/routes/darwinawards/articles.js | 42 ---------------------- lib/v2/darwinawards/index.js | 56 +++++++++++++++++++++++++++++ lib/v2/darwinawards/maintainer.js | 4 +++ lib/v2/darwinawards/radar.js | 13 +++++++ lib/v2/darwinawards/router.js | 4 +++ 8 files changed, 82 insertions(+), 47 deletions(-) delete mode 100644 lib/routes/darwinawards/articles.js create mode 100644 lib/v2/darwinawards/index.js create mode 100644 lib/v2/darwinawards/maintainer.js create mode 100644 lib/v2/darwinawards/radar.js create mode 100644 lib/v2/darwinawards/router.js diff --git a/docs/en/other.md b/docs/en/other.md index c4f210e94..17f2d717c 100644 --- a/docs/en/other.md +++ b/docs/en/other.md @@ -59,9 +59,9 @@ Official Website: ## Darwin Awards -### Articles +### Award Winners - + ## dcinside diff --git a/docs/other.md b/docs/other.md index 113078e99..04ec75ef0 100644 --- a/docs/other.md +++ b/docs/other.md @@ -99,9 +99,9 @@ pageClass: routes ## Darwin Awards -### 文章 +### Award Winners - + ## dcinside diff --git a/lib/router.js b/lib/router.js index 2439a78ae..2e3f82047 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3034,7 +3034,7 @@ router.get('/jlu/oa', lazyloadRouteHandler('./routes/universities/jlu/oa')); router.get('/hfut/tzgg', lazyloadRouteHandler('./routes/universities/hfut/tzgg')); // Darwin Awards -router.get('/darwinawards/all', lazyloadRouteHandler('./routes/darwinawards/articles')); +// router.get('/darwinawards/all', lazyloadRouteHandler('./routes/darwinawards/articles')); // 四川职业技术学院 router.get('/scvtc/xygg', lazyloadRouteHandler('./routes/universities/scvtc/xygg')); diff --git a/lib/routes/darwinawards/articles.js b/lib/routes/darwinawards/articles.js deleted file mode 100644 index 9bb40ba97..000000000 --- a/lib/routes/darwinawards/articles.js +++ /dev/null @@ -1,42 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const response = await got({ - method: 'get', - url: 'https://darwinawards.com/', - }); - - const data = response.data; - - const $ = cheerio.load(data); - const list = []; - $('div.column.content') - .find('p') - .each((index, item) => { - if (!$(item).find('a').text().startsWith('20') && $(item).find('a').text() !== '') { - list.push(item); - } - }); - - let index = 0; - - ctx.state.data = { - title: 'Darwin Awards', - link: 'https://darwinawards.com/', - item: list.map((item) => { - const title = $(item).find('a').text(); - - const time = new Date(); - time.setMinutes(time.getMinutes() - index); - index++; - - return { - title, - description: $(item).text().replace(title, ''), - link: $(item).find('a').attr('href'), - pubDate: time, - }; - }), - }; -}; diff --git a/lib/v2/darwinawards/index.js b/lib/v2/darwinawards/index.js new file mode 100644 index 000000000..a9a25430c --- /dev/null +++ b/lib/v2/darwinawards/index.js @@ -0,0 +1,56 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = 'https://darwinawards.com'; + const currentUrl = `${rootUrl}/darwin/`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + $('.cameo').remove(); + + $('.topvote_title_desc, .topvote_title_minimal, .topvote_minimal').each(function () { + $(this).find('a').first().remove(); + }); + + let items = $('#article_index a') + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.text(), + link: item.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('h2, nav, footer, table, form').remove(); + + item.description = content('article').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/darwinawards/maintainer.js b/lib/v2/darwinawards/maintainer.js new file mode 100644 index 000000000..ed0c24907 --- /dev/null +++ b/lib/v2/darwinawards/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/all': ['zoenglinghou', 'nczitzk'], + '/': ['nczitzk'], +}; diff --git a/lib/v2/darwinawards/radar.js b/lib/v2/darwinawards/radar.js new file mode 100644 index 000000000..a23122e2a --- /dev/null +++ b/lib/v2/darwinawards/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'darwinawards.com': { + _name: 'Darwin Awards', + '.': [ + { + title: 'Award Winners', + docs: 'https://docs.rsshub.app/other.html#darwin-awards-award-winners', + source: ['/darwin', '/'], + target: '/darwinawards', + }, + ], + }, +}; diff --git a/lib/v2/darwinawards/router.js b/lib/v2/darwinawards/router.js new file mode 100644 index 000000000..f0a50a124 --- /dev/null +++ b/lib/v2/darwinawards/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get('/all', require('./index')); + router.get('/', require('./index')); +};