From 0b5d76ed12d47089f48cff86e95cbc5e9bc4191b Mon Sep 17 00:00:00 2001 From: hoilc Date: Fri, 7 Feb 2020 03:27:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E7=BD=91=E6=98=93=E5=85=AC?= =?UTF-8?q?=E5=BC=80=E8=AF=BE=20(#3895)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/radar-rules.js | 16 +++++++- docs/study.md | 10 +++++ lib/router.js | 4 ++ lib/routes/netease/open/latest.js | 68 +++++++++++++++++++++++++++++++ lib/routes/netease/open/vip.js | 65 +++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 lib/routes/netease/open/latest.js create mode 100644 lib/routes/netease/open/vip.js diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 8208ed7a18..938aee0242 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -982,12 +982,26 @@ _name: '网易', ds: [ { - title: '网易大神', + title: '大神', docs: 'https://docs.rsshub.app/game.html#wang-yi-da-shen', source: '/user/:id', target: '/netease/ds/:id', }, ], + open: [ + { + title: '公开课 - 精品课程', + docs: 'https://docs.rsshub.app/study.html#wang-yi-gong-kai-ke', + source: '/', + target: '/open163/vip', + }, + { + title: '公开课 - 最新课程', + docs: 'https://docs.rsshub.app/study.html#wang-yi-gong-kai-ke', + source: '/', + target: '/open163/latest', + }, + ], }, 'suzhou.gov.cn': { _name: '苏州市政府', diff --git a/docs/study.md b/docs/study.md index eea08479a4..761a6126ac 100644 --- a/docs/study.md +++ b/docs/study.md @@ -103,6 +103,16 @@ pageClass: routes +## 网易公开课 + +### 精品课程 + + + +### 最新课程 + + + ## 下厨房 ### 用户作品 diff --git a/lib/router.js b/lib/router.js index 85897595f2..21a424b73d 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2198,4 +2198,8 @@ router.get('/lastfm/top/:country?', require('./routes/lastfm/top')); router.get('/piapro/user/:pid', require('./routes/piapro/user')); router.get('/piapro/public/:type/:tag?/:category?', require('./routes/piapro/public')); +// 网易公开课 +router.get('/open163/vip', require('./routes/netease/open/vip')); +router.get('/open163/latest', require('./routes/netease/open/latest')); + module.exports = router; diff --git a/lib/routes/netease/open/latest.js b/lib/routes/netease/open/latest.js new file mode 100644 index 0000000000..4e52b01d03 --- /dev/null +++ b/lib/routes/netease/open/latest.js @@ -0,0 +1,68 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const list_response = await got.get('https://c.open.163.com/open/getLatestMovies.do?callback='); + + const list = JSON.parse(list_response.data.match(/\((.*?)\)/)[1]) || []; + + const parseContent = (htmlString) => { + const $ = cheerio.load(htmlString); + + const author = $('.m-courseinfo__side ul > li:nth-child(2)'); + + const images = $('img'); + for (let k = 0; k < images.length; k++) { + const testRealURL = $(images[k]) + .attr('src') + .match(/\?url=([^&]+)/); + if (testRealURL) { + $(images[k]).replaceWith(``); + } + } + + const content = $('.m-courseinfo__side'); + $('.i-container__title').remove(); + + return { + author: author.text().trim(), + description: content.html(), + pubDate: new Date(), + }; + }; + + const out = await Promise.all( + list.map(async (item) => { + const link = item.url; + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const rssitem = { + title: item.title, + link: link, + }; + + try { + const response = await got.get(link); + const result = parseContent(response.data); + + rssitem.author = result.author; + rssitem.description = result.description; + rssitem.pubDate = result.pubDate; + } catch (err) { + return Promise.resolve(''); + } + ctx.cache.set(link, JSON.stringify(rssitem)); + return Promise.resolve(rssitem); + }) + ); + + ctx.state.data = { + title: '网易公开课 - 最新课程', + link: 'https://open.163.com/', + item: out.filter((item) => item !== ''), + }; +}; diff --git a/lib/routes/netease/open/vip.js b/lib/routes/netease/open/vip.js new file mode 100644 index 0000000000..738ae4ed1a --- /dev/null +++ b/lib/routes/netease/open/vip.js @@ -0,0 +1,65 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const url = 'https://open.163.com/'; + + const list_response = await got.get(url); + const $ = cheerio.load(list_response.data); + + const list = $('.tabcon > div:nth-child(1) ul.list li').toArray(); + + const parseContent = (htmlString) => { + const $ = cheerio.load(htmlString); + + const author = $('.CourseCommonDetail_info > div:nth-child(4)'); + const content = $('.CourseCommonDetail_contentDesc'); + + return { + author: author.text().trim(), + description: content.html(), + pubDate: new Date(), + }; + }; + + const out = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const title = $('.ltxt') + .text() + .trim(); + const link = $('a.item') + .attr('href') + .split('?')[0]; + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const rssitem = { + title: title, + link: link, + }; + + try { + const response = await got.get(link); + const result = parseContent(response.data); + + rssitem.author = result.author; + rssitem.description = result.description; + rssitem.pubDate = result.pubDate; + } catch (err) { + return Promise.resolve(''); + } + ctx.cache.set(link, JSON.stringify(rssitem)); + return Promise.resolve(rssitem); + }) + ); + + ctx.state.data = { + title: '网易公开课 - 精品课程', + link: url, + item: out.filter((item) => item !== ''), + }; +};