fix: shmtu route rewrite (#1907)

This commit is contained in:
Simon Shi
2019-04-10 12:00:24 +08:00
committed by DIYgod
parent f87fcdd004
commit e117b2b33b
6 changed files with 173 additions and 140 deletions
+2 -4
View File
@@ -2,11 +2,9 @@
## 上海海事大学
<Route name="学术讲座" author="simonsmh" example="/shmtu/events" path="/universities/shmtu/events"/>
<Route name="官网信息" author="simonsmh" example="/shmtu/www/events" path="/shmtu/www/:type" :paramsDesc="['events 为学术讲座, notes 为通知公告']"/>
<Route name="通知公告" author="simonsmh" example="/shmtu/notes" path="/universities/shmtu/notes"/>
<Route name="教务信息" author="simonsmh" example="/shmtu/jwc/1" path="/universities/shmtu/jwc/:type" :paramsDesc="['1 为教务新闻, 2 为教务公告']"/>
<Route name="教务信息" author="simonsmh" example="/shmtu/jwc/jiaowugonggao" path="/shmtu/jwc/:type" :paramsDesc="['jiaowuxinwen 为教务新闻, jiaowugonggao 为教务公告']"/>
## 西南科技大学
+1 -2
View File
@@ -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'));
// 西南科技大学
-53
View File
@@ -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'),
};
}),
};
};
+93 -28
View File
@@ -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,
};
};
-53
View File
@@ -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'),
};
}),
};
};
+77
View File
@@ -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,
};
};