mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-01 14:57:14 +08:00
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:
@@ -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'));
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
'/jwc/:type?': ['shaoye'],
|
||||
};
|
||||
@@ -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';
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = (router) => {
|
||||
router.get('/jwc/:type?', require('./jwc'));
|
||||
};
|
||||
Reference in New Issue
Block a user