From 8ab858c0494b089decafbebd8377ea3380ace62d Mon Sep 17 00:00:00 2001 From: Dwemer <70470685+dwemerx@users.noreply.github.com> Date: Mon, 19 Oct 2020 13:55:38 +0300 Subject: [PATCH] feat: add mit ocw route (#5939) Co-authored-by: dwemerx Co-authored-by: Henry Wang --- docs/en/university.md | 4 +++ docs/university.md | 4 +++ lib/router.js | 1 + lib/routes/universities/mit/ocw-top.js | 39 ++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 lib/routes/universities/mit/ocw-top.js diff --git a/docs/en/university.md b/docs/en/university.md index c11abfd615..d46ee4ca38 100644 --- a/docs/en/university.md +++ b/docs/en/university.md @@ -6,6 +6,10 @@ pageClass: routes ## MIT +### MIT OCW Most popular courses of the month + + + ### MIT graduateadmissions's all blogs diff --git a/docs/university.md b/docs/university.md index 13aa9abc45..9241564146 100644 --- a/docs/university.md +++ b/docs/university.md @@ -6,6 +6,10 @@ pageClass: routes ## MIT +### MIT OCW 每月热门课程 + + + ### MIT graduateadmissions's all blogs diff --git a/lib/router.js b/lib/router.js index 84b94c7201..6297b5e2e1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1608,6 +1608,7 @@ router.get('/metred/fuli', require('./routes/metred/fuli')); // MIT router.get('/mit/graduateadmissions/:type/:name', require('./routes/universities/mit/graduateadmissions')); +router.get('/mit/ocw-top', require('./routes/universities/mit/ocw-top')); // 毕马威 router.get('/kpmg/insights', require('./routes/kpmg/insights')); diff --git a/lib/routes/universities/mit/ocw-top.js b/lib/routes/universities/mit/ocw-top.js new file mode 100644 index 0000000000..5d7f562647 --- /dev/null +++ b/lib/routes/universities/mit/ocw-top.js @@ -0,0 +1,39 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const link = 'https://ocw.mit.edu/courses/most-visited-courses/'; + + const getHTML = async () => { + const response = await got({ + method: 'get', + url: link, + }); + + return response.data; + }; + + const html = await ctx.cache.tryGet(link, getHTML); + const courses = []; + + const $ = cheerio.load(html); + $('.courseList tbody') + .children() + .each(function () { + const course = { + title: $('td', this).eq(1).text(), + link: `https://ocw.mit.edu${$('td a', this).eq(1).attr('href')}`, + description: $('td', this).eq(2).text(), + }; + + courses.push(course); + }); + + ctx.state.data = { + title: 'MIT OCW top courses of the month', + link, + description: 'Most visited courses based on the site traffic of the previous month', + language: 'en-us', + item: courses, + }; +};