feat: add gwern (#6021)

This commit is contained in:
cerebrater
2020-10-29 16:36:38 +00:00
committed by GitHub
parent 461bcc5147
commit 876c7be15f
3 changed files with 65 additions and 0 deletions
+4
View File
@@ -16,6 +16,10 @@ pageClass: routes
<Route author="hoilc" example="/google/sites/outlierseconomics" path="/google/sites/:id" :paramsDesc="['Site ID, 可在 URL 中找到']" radar="1" rssbud="1"/>
## Gwern Branwen
<Route author="cerebrater" example="/gwern/newest" path="/gwern/:category" :paramsDesc="['網誌主頁的分類訊息']"/>
## Hedwig.pub
<Route author="zwithz" example="/blogs/hedwig/zmd" path="/blogs/hedwig/:type" :paramsDesc="['分类, 见下表']"/>
+3
View File
@@ -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'));
+58
View File
@@ -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('&shy;', '');
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,
};
};