From f869d9db0185f89861b3d8e714853718a37f83cd Mon Sep 17 00:00:00 2001 From: Chenyang Shi Date: Tue, 30 Apr 2019 00:23:32 +0800 Subject: [PATCH] add MIT Grad Blog (#2017) --- docs/university.md | 8 +++ lib/router.js | 3 + .../universities/mit/graduateadmissions.js | 65 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib/routes/universities/mit/graduateadmissions.js diff --git a/docs/university.md b/docs/university.md index 25c5a2259..ce9d5f641 100644 --- a/docs/university.md +++ b/docs/university.md @@ -708,3 +708,11 @@ https://rsshub.app/**nuist**/`bulletin` 或 https://rsshub.app/**nuist**/`bullet ## 广东海洋大学 + +## MIT + + + + + + diff --git a/lib/router.js b/lib/router.js index 811774e28..54cb1f577 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1290,6 +1290,9 @@ router.get('/mlhang', require('./routes/mlhang/latest')); // PlayStation Store router.get('/ps/list/:gridName', require('./routes/ps/list')); +// MIT +router.get('/mit/graduateadmissions/:type/:name', require('./routes/universities/mit/graduateadmissions')); + // 毕马威 router.get('/kpmg/insights', require('./routes/kpmg/insights')); diff --git a/lib/routes/universities/mit/graduateadmissions.js b/lib/routes/universities/mit/graduateadmissions.js new file mode 100644 index 000000000..f87bb51c0 --- /dev/null +++ b/lib/routes/universities/mit/graduateadmissions.js @@ -0,0 +1,65 @@ +const axios = require('../../../utils/axios'); +const cheerio = require('cheerio'); + +const host = 'https://gradadmissions.mit.edu'; + +module.exports = async (ctx) => { + const type = ctx.params.type; + const name = ctx.params.name; + + let link = host + ''; + switch (type) { + case 'index': + link = host + '/blog'; + break; + case 'category': + link = host + `/blog/browse/category/${name}`; + break; + case 'department': + link = host + `/blog/browse/department/${name}`; + break; + } + const response = await axios.get(link); + const $ = cheerio.load(response.data); + + const title = $('#page-title').text(); + const list = $('div.views-row'); + const out = await Promise.all( + list + .slice(0, 10) + .map(async (_, elem) => { + const $elem = $(elem); + + const title = $elem.find('h3.item-title a').text() + '-' + $elem.find('h4.item-subtitle').text(); + const itemUrl = host + $elem.find('h3.item-title a').attr('href'); + const author = $elem.find('div.item-info a').text(); + + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await axios.get(itemUrl); + const $$ = cheerio.load(response.data); + const description = $$('div.field-name-body div.field-items') + .html() + .replace(/src="/g, `src="${host}`); + + const single = { + title: title, + link: itemUrl, + author: author, + description: description, + }; + ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + .get() + ); + + ctx.state.data = { + title: `${title}-MIT`, + link: link, + item: out, + }; +};