From e117b2b33bb5e4d3d7106ed960195ae13501b2f0 Mon Sep 17 00:00:00 2001 From: Simon Shi Date: Wed, 10 Apr 2019 12:00:24 +0800 Subject: [PATCH] fix: shmtu route rewrite (#1907) --- docs/university.md | 6 +- lib/router.js | 3 +- lib/routes/universities/shmtu/events.js | 53 ----------- lib/routes/universities/shmtu/jwc.js | 121 ++++++++++++++++++------ lib/routes/universities/shmtu/notes.js | 53 ----------- lib/routes/universities/shmtu/www.js | 77 +++++++++++++++ 6 files changed, 173 insertions(+), 140 deletions(-) delete mode 100644 lib/routes/universities/shmtu/events.js delete mode 100644 lib/routes/universities/shmtu/notes.js create mode 100644 lib/routes/universities/shmtu/www.js diff --git a/docs/university.md b/docs/university.md index 3442e4f21..db7c37ffa 100644 --- a/docs/university.md +++ b/docs/university.md @@ -2,11 +2,9 @@ ## 上海海事大学 - + - - - + ## 西南科技大学 diff --git a/lib/router.js b/lib/router.js index 6bb91f549..fc988e12b 100755 --- a/lib/router.js +++ b/lib/router.js @@ -615,8 +615,7 @@ router.get('/ju/jwc/:type?', require('./routes/universities/ju/jwc')); router.get('/pku/eecs/:type?', require('./routes/universities/pku/eecs')); // 上海海事大学 -router.get('/shmtu/events', require('./routes/universities/shmtu/events')); -router.get('/shmtu/notes', require('./routes/universities/shmtu/notes')); +router.get('/shmtu/www/:type', require('./routes/universities/shmtu/www')); router.get('/shmtu/jwc/:type', require('./routes/universities/shmtu/jwc')); // 西南科技大学 diff --git a/lib/routes/universities/shmtu/events.js b/lib/routes/universities/shmtu/events.js deleted file mode 100644 index 3235ebbc0..000000000 --- a/lib/routes/universities/shmtu/events.js +++ /dev/null @@ -1,53 +0,0 @@ -const axios = require('../../../utils/axios'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const host = 'http://www.shmtu.edu.cn'; - - const response = await axios({ - method: 'get', - url: host + '/events', - headers: { - Referer: host, - }, - }); - - const $ = cheerio.load(response.data); - const text = $('tr', 'tbody') - .slice(0, 10) - .get(); - - ctx.state.data = { - title: '上海海事大学 学术讲座', - link: host + '/events', - description: '上海海事大学 学术讲座', - item: - text && - text.map((item) => { - item = $(item); - return { - title: item - .find('.title') - .text() - .trim(), - description: item - .find('.title') - .text() - .trim(), - author: - '发布部门 - ' + - item - .find('.department') - .text() - .trim(), - pubDate: new Date( - item - .find('span') - .find('span') - .attr('content') - ).toUTCString(), - link: host + item.find('a').attr('href'), - }; - }), - }; -}; diff --git a/lib/routes/universities/shmtu/jwc.js b/lib/routes/universities/shmtu/jwc.js index 114a00973..f4eca0599 100644 --- a/lib/routes/universities/shmtu/jwc.js +++ b/lib/routes/universities/shmtu/jwc.js @@ -1,49 +1,114 @@ const axios = require('../../../utils/axios'); const cheerio = require('cheerio'); +const url = require('url'); +const host = 'http://jwc.shmtu.edu.cn'; + +async function load(link) { + const response = await axios.get(link); + const $ = cheerio.load(response.data); + + $('img').each((index, elem) => { + const $elem = $(elem); + const src = $elem.attr('src'); + if (src && src !== '') { + $elem.attr('src', url.resolve(host, src)); + } + $elem.removeAttr('style'); + $elem.removeAttr('alt'); + }); + + $('div').each((index, elem) => { + const $elem = $(elem); + $elem.replaceWith($elem.html().trim()); + }); + + $('p').each((index, elem) => { + const $elem = $(elem); + $elem.replaceWith($elem.html().trim()); + }); + $('b').each((index, elem) => { + const $elem = $(elem); + $elem.replaceWith($elem.html().trim()); + }); + $('u').each((index, elem) => { + const $elem = $(elem); + $elem.replaceWith($elem.html().trim()); + }); + $('strong').each((index, elem) => { + const $elem = $(elem); + $elem.replaceWith($elem.html().trim()); + }); + while ($('span').text() !== '') { + $('span').each((index, elem) => { + const $elem = $(elem); + $elem.replaceWith($elem.html().trim()); + }); + } + + const description = $('.field-item').html(); + + return { description }; +} + +const ProcessFeed = async (list, caches) => + await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + + const itemUrl = url.resolve( + host, + $('.views-field-nothing', item) + .find('a') + .slice(1) + .attr('href') + ); + const category = $('.views-field-field-xxlb', item) + .text() + .trim(); + const author = $('.views-field-field-xxly', item) + .text() + .trim(); + + const single = { + title: $('.views-field-nothing', item) + .text() + .trim(), + link: itemUrl, + guid: itemUrl, + pubDate: new Date($('.views-field-created', item).text()).toUTCString(), + category: category, + author: author === '' ? category : author, + }; + + const other = await caches.tryGet(itemUrl, async () => await load(itemUrl)); + + return Promise.resolve(Object.assign({}, single, other)); + }) + ); module.exports = async (ctx) => { - const host = 'http://jwc.shmtu.edu.cn/Information/'; const type = ctx.params.type; - const info = type === '2' ? '教务公告' : '教务新闻'; + const info = type === 'jiaowugonggao' ? '教务公告' : '教务新闻'; const response = await axios({ method: 'get', - url: host + `MoreInfo.aspx?type=${type}`, + url: host + `/${type}`, headers: { Referer: host, }, }); const $ = cheerio.load(response.data); - const text = $('.gvRow', '.tdMCViewList') + const list = $('tr', 'tbody') .slice(0, 10) .get(); + const result = await ProcessFeed(list, ctx.cache); + ctx.state.data = { title: `上海海事大学 ${info}`, - link: host + `MoreInfo.aspx?type=${type}`, - description: `上海海事大学 ${info}`, - item: - text && - text.map((item) => { - item = $(item); - return { - title: item.find('a').attr('title'), - description: item.find('a').attr('title'), - author: - '信息类别 - ' + - $('.gvItemNormal', item) - .slice(1, 2) - .text() - .trim(), - pubDate: new Date( - $('.gvItemNormal', item) - .slice(4) - .text() - .trim() - ).toUTCString(), - link: (host + item.find('a').attr('href')).replace('/Information/..', ''), - }; - }), + link: host + `/${type}`, + description: '上海海事大学 教务信息', + item: result, }; }; diff --git a/lib/routes/universities/shmtu/notes.js b/lib/routes/universities/shmtu/notes.js deleted file mode 100644 index 80dd04155..000000000 --- a/lib/routes/universities/shmtu/notes.js +++ /dev/null @@ -1,53 +0,0 @@ -const axios = require('../../../utils/axios'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const host = 'http://www.shmtu.edu.cn'; - - const response = await axios({ - method: 'get', - url: host + '/notes', - headers: { - Referer: host, - }, - }); - - const $ = cheerio.load(response.data); - const text = $('tr', 'tbody') - .slice(0, 10) - .get(); - - ctx.state.data = { - title: '上海海事大学 通知公告', - link: host + '/notes', - description: '上海海事大学 通知公告', - item: - text && - text.map((item) => { - item = $(item); - return { - title: item - .find('.title') - .text() - .trim(), - description: item - .find('.title') - .text() - .trim(), - author: - '发布部门 - ' + - item - .find('.department') - .text() - .trim(), - pubDate: new Date( - item - .find('span') - .find('span') - .attr('content') - ).toUTCString(), - link: host + item.find('a').attr('href'), - }; - }), - }; -}; diff --git a/lib/routes/universities/shmtu/www.js b/lib/routes/universities/shmtu/www.js new file mode 100644 index 000000000..18c24564c --- /dev/null +++ b/lib/routes/universities/shmtu/www.js @@ -0,0 +1,77 @@ +const axios = require('../../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); +const host = 'http://www.shmtu.edu.cn'; + +async function load(link) { + const response = await axios.get(link); + const $ = cheerio.load(response.data); + + const description = $('meta[name="description"]').attr('content'); + + return { description }; +} + +const ProcessFeed = async (list, caches) => + await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + + const itemUrl = url.resolve( + host, + $('.title', item) + .find('a') + .attr('href') + ); + const category = $('.department', item) + .text() + .trim(); + + const single = { + title: $('.title', item) + .text() + .trim(), + link: itemUrl, + guid: itemUrl, + pubDate: new Date( + $('.date', item) + .find('span') + .find('span') + .attr('content') + ).toUTCString(), + category: category, + author: category, + }; + + const other = await caches.tryGet(itemUrl, async () => await load(itemUrl)); + + return Promise.resolve(Object.assign({}, single, other)); + }) + ); + +module.exports = async (ctx) => { + const type = ctx.params.type; + const info = type === 'notes' ? '通知公告' : '学术讲座'; + + const response = await axios({ + method: 'get', + url: host + `/${type}`, + headers: { + Referer: host, + }, + }); + + const $ = cheerio.load(response.data); + const list = $('tr', 'tbody') + .slice(0, 10) + .get(); + + const result = await ProcessFeed(list, ctx.cache); + + ctx.state.data = { + title: `上海海事大学 ${info}`, + link: host + `/${type}`, + description: '上海海事大学 官网信息', + item: result, + }; +};