From 876c7be15f67a6bca6020adcbfbbc75fdea3165a Mon Sep 17 00:00:00 2001 From: cerebrater <49020242+cerebrater@users.noreply.github.com> Date: Fri, 30 Oct 2020 00:36:38 +0800 Subject: [PATCH] feat: add gwern (#6021) --- docs/blog.md | 4 +++ lib/router.js | 3 ++ lib/routes/gwern/category.js | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 lib/routes/gwern/category.js diff --git a/docs/blog.md b/docs/blog.md index 831f8b83d4..ca5ba72b73 100644 --- a/docs/blog.md +++ b/docs/blog.md @@ -16,6 +16,10 @@ pageClass: routes +## Gwern Branwen + + + ## Hedwig.pub diff --git a/lib/router.js b/lib/router.js index b7aa6d7156..b890fd1786 100644 --- a/lib/router.js +++ b/lib/router.js @@ -468,6 +468,9 @@ router.get('/xueqiu/hots', require('./routes/xueqiu/hots')); // Greasy Fork router.get('/greasyfork/:language/:domain?', require('./routes/greasyfork/scripts')); +// Gwern Bran­wen +router.get('/gwern/:category', require('./routes/gwern/category')); + // LinkedKeeper router.get('/linkedkeeper/:type/:id?', require('./routes/linkedkeeper/index')); diff --git a/lib/routes/gwern/category.js b/lib/routes/gwern/category.js new file mode 100644 index 0000000000..afb86063a3 --- /dev/null +++ b/lib/routes/gwern/category.js @@ -0,0 +1,58 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const category = ctx.params.category; + const name = category.charAt(0).toUpperCase() + category.slice(1); + const baseURL = `https://www.gwern.net`; + const url = baseURL + `/index`; + const res = await got.get(url); + const $ = cheerio.load(res.data); + const list = $(`section#${category} ul > li`).get(); + + const out = await Promise.all( + list.map(async (item) => { + const grasp = cheerio.load(item); + const link = baseURL + grasp('a').attr('href'); + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const res = await got.get(link); + const $ = cheerio.load(res.data); + + const title = $('header > h1').text(); + const time = $('span#page-creation').text(); + // const modified = $('span#page-modified').text(); + + $('a').each((index, item) => { + item = $(item); + item.removeAttr('data-popup-title'); + item.removeAttr('data-popup-title-html'); + item.removeAttr('data-popup-author'); + item.removeAttr('data-popup-abstract'); + item.removeAttr('data-popup-doi'); + item.removeAttr('data-popup-date'); + }); + $; + + const contents = $('div#markdownBody').html().replace('­', ''); + + const single = { + title: title, + author: 'Gwern Bran­wen', + description: contents, + link: link, + guid: link, + pubDate: new Date(time).toUTCString(), + }; + ctx.cache.set(link, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + ctx.state.data = { + title: `Gwern - ${name}`, + link: url, + item: out, + }; +};