fix njupt posts with url redirections (#8961)

* fix njupt posts with url redirections

skip posts that are redirected to error pages due to access control

* fix: migrate to v2

* fix: pubDate depreciated url.resolve

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
This commit is contained in:
Shao ye
2022-03-13 07:24:06 -07:00
committed by GitHub
parent 713415093f
commit ee1eab23d8
6 changed files with 116 additions and 87 deletions
-3
View File
@@ -607,9 +607,6 @@ router.get('/ustc/job/:category?', lazyloadRouteHandler('./routes/universities/u
// UTdallas ISSO
router.get('/utdallas/isso', lazyloadRouteHandler('./routes/universities/utdallas/isso'));
// 南京邮电大学
router.get('/njupt/jwc/:type?', lazyloadRouteHandler('./routes/universities/njupt/jwc'));
// 南昌航空大学
router.get('/nchu/jwc/:type?', lazyloadRouteHandler('./routes/universities/nchu/jwc'));
-84
View File
@@ -1,84 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const host = 'http://jwc.njupt.edu.cn';
const map = {
notice: '/1594',
news: '/1596',
};
module.exports = async (ctx) => {
const type = ctx.params.type || 'notice';
const link = host + map[type] + '/list.htm';
const response = await got({
method: 'get',
url: link,
headers: {
Referer: host,
},
});
const $ = cheerio.load(response.data);
const urlList = $('.content')
.find('a')
.slice(0, 10)
.map((i, e) => $(e).attr('href'))
.get();
const titleList = $('.content')
.find('a')
.slice(0, 10)
.map((i, e) => $(e).attr('title'))
.get();
const dateList = $('.content tr')
.find('div')
.slice(0, 10)
.map((i, e) => $(e).text().replace('发布时间:', ''))
.get();
const out = await Promise.all(
urlList.map(async (itemUrl, index) => {
itemUrl = url.resolve(host, itemUrl);
if (itemUrl.indexOf('.htm') !== -1) {
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got.get(itemUrl);
const $ = cheerio.load(response.data);
const single = {
title: $('.Article_Title').text(),
link: itemUrl,
description: $('.wp_articlecontent')
.html()
.replace(/src="\//g, `src="${url.resolve(host, '.')}`)
.replace(/href="\//g, `href="${url.resolve(host, '.')}`)
.trim(),
pubDate: new Date($('.Article_PublishDate').text().replace('发布时间:', '')).toUTCString(),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
} else {
const single = {
title: titleList[index],
link: itemUrl,
description: '该通知为文件,请点击原文链接↑下载',
pubDate: new Date(dateList[index]).toUTCString(),
};
return Promise.resolve(single);
}
})
);
let info = '通知公告';
if (type === 'news') {
info = '教务快讯';
}
ctx.state.data = {
title: '南京邮电大学 -- ' + info,
link,
item: out,
};
};
+90
View File
@@ -0,0 +1,90 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const host = 'https://jwc.njupt.edu.cn';
const map = {
notice: '/1594',
news: '/1596',
};
module.exports = async (ctx) => {
const type = ctx.params.type ?? 'notice';
const link = host + map[type] + '/list.htm';
const response = await got({
method: 'get',
url: link,
headers: {
Referer: host,
},
});
const $ = cheerio.load(response.data);
const urlList = $('.content')
.find('a')
.slice(0, 10)
.map((i, e) => $(e).attr('href'))
.get();
const titleList = $('.content')
.find('a')
.slice(0, 10)
.map((i, e) => $(e).attr('title'))
.get();
const dateList = $('.content tr')
.find('div')
.slice(0, 10)
.map((i, e) => $(e).text().replace('发布时间:', ''))
.get();
const out = await Promise.all(
urlList.map((itemUrl, index) => {
itemUrl = new URL(itemUrl, host).href;
if (itemUrl.indexOf('.htm') !== -1) {
return ctx.cache.tryGet(itemUrl, async () => {
const response = await got.get(itemUrl);
if (response.redirectUrls.length !== 0) {
const single = {
title: titleList[index],
link: itemUrl,
description: '该通知无法直接预览, 请点击原文链接↑查看',
pubDate: parseDate(dateList[index]),
};
return single;
}
const $ = cheerio.load(response.data);
const single = {
title: $('.Article_Title').text(),
link: itemUrl,
description: $('.wp_articlecontent')
.html()
.replace(/src="\//g, `src="${new URL('.', host).href}`)
.replace(/href="\//g, `href="${new URL('.', host).href}`)
.trim(),
pubDate: parseDate($('.Article_PublishDate').text().replace('发布时间:', '')),
};
return single;
});
} else {
const single = {
title: titleList[index],
link: itemUrl,
description: '该通知为文件,请点击原文链接↑下载',
pubDate: parseDate(dateList[index]),
};
return single;
}
})
);
let info = '通知公告';
if (type === 'news') {
info = '教务快讯';
}
ctx.state.data = {
title: '南京邮电大学 -- ' + info,
link,
item: out,
};
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/jwc/:type?': ['shaoye'],
};
+20
View File
@@ -0,0 +1,20 @@
module.exports = {
'njupt.edu.cn': {
_name: '南京邮电大学',
jwc: [
{
title: '教务处通知与新闻',
docs: 'https://docs.rsshub.app/university.html#nan-jing-you-dian-da-xue',
source: '/*/list.htm',
target: (_params, url) => {
url = new URL(url);
if (url.pathname.indexOf('/1594') !== -1) {
return '/njupt/notice';
} else if (url.pathname.indexOf('/1596') !== -1) {
return '/njupt/news';
}
},
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/jwc/:type?', require('./jwc'));
};