fix: xidian 路由 (#1731)

This commit is contained in:
ciaran 2019-03-12 14:21:27 +08:00 committed by DIYgod
parent 776fb0bb08
commit c105b41ab5
1 changed files with 34 additions and 35 deletions

View File

@ -13,48 +13,47 @@ module.exports = async (ctx) => {
const data = response.data;
const $ = cheerio.load(data);
let list = $('td[bgcolor="#ffffff"]')
.first()
.children();
const list = $('#__01 > tbody > tr > td:nth-child(2)');
const titleTables = list.children().filter((index) => index % 2 !== 1);
list = list
.filter((index) => index % 2 !== 0)
.splice(0, 10)
.map((index, item) => {
this.index = index;
item = $(item);
return item
.find('tr')
.find('tr')
.map((index, item) => {
item = $(item);
return {
title: item.find('a').attr('title'),
description: arrC[this.index] + '<br>' + item.find('a').attr('title') + '<br><br>全文内容需使用校园网或VPN获取',
pubDate: new Date(
item
.children()
.last()
.text()
).toUTCString(),
link: `https://jwc.xidian.edu.cn/${item.find('a').attr('href')}`,
};
});
})
.get();
let result = [];
if (category === 'all' && arrE.indexOf(category) === -1) {
for (let i = 0; i < list.length; i++) {
result = result.concat(list[i].get());
}
const parseTable = (index, item) => {
const title = arrC[index];
const aTags = $(item)
.next()
.find('a');
return aTags
.map((_, item) => {
item = $(item);
return {
title: item.attr('title'),
description: title + '<br/>' + item.attr('title') + '<br/>全文内容需使用校园网或VPN获取',
pubDate: new Date(
item
.parent()
.next()
.text()
).toUTCString(),
link: `https://jwc.xidian.edu.cn/${item.attr('href')}`,
};
})
.get();
};
const arrIndex = arrE.indexOf(category);
let result;
if (arrIndex === -1) {
result = titleTables.map(parseTable).get();
result.sort((a, b) => new Date(b.pubDate) - new Date(a.pubDate));
} else {
result = list[arrE.indexOf(category)].get();
result = parseTable(arrIndex, titleTables[arrIndex]);
}
ctx.state.data = {
title: '西电教务处',
link: 'https://jwc.xidian.edu.cn',
description: $('meta[Name="keywords"]').attr('Content'),
description: $('title')
.text()
.concat(arrIndex === -1 ? '' : '-' + arrC[arrIndex]),
item: result,
};
};