feat(route): 温州大学新闻路由更新 (#8443)

Co-authored-by: Tony <TonyRL@users.noreply.github.com>
This commit is contained in:
yesRabbit
2021-11-27 08:47:43 +00:00
committed by GitHub
co-authored by Tony
parent d251822d51
commit 1c9131ee46
7 changed files with 108 additions and 29 deletions
+7 -1
View File
@@ -1929,7 +1929,13 @@ type 列表:
### 新闻
<Route author="Chandler-Lu" example="/wzu/news" path="/wzu/news" />
<Route author="Chandler-Lu" example="/wzu/news/0" path="/wzu/news/:type" :paramsDesc="['分类, 见下表 默认为`0`']" />
| 温大新闻 | 媒体温大 | 学术温大 | 通知公告 | 招标信息 | 学术公告 |
| :------: | :------: | :------: | :------: | :------: | :------: |
| 0 | 1 | 2 | 3 | 4 | 5 |
</Route>
## 温州商学院
-3
View File
@@ -4165,9 +4165,6 @@ router.get('/harvard/health/blog', lazyloadRouteHandler('./routes/universities/h
// yuzu emulator
router.get('/yuzu-emu/entry', lazyloadRouteHandler('./routes/yuzu-emu/entry'));
// 温州大学
router.get('/wzu/news', lazyloadRouteHandler('./routes/universities/wzu/news'));
// Resources - The Partnership on AI
router.get('/partnershiponai/resources', lazyloadRouteHandler('./routes/partnershiponai/resources'));
-25
View File
@@ -1,25 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const link = 'http://www.wzu.edu.cn/index/wdxw.htm';
const response = await got.get(link);
const $ = cheerio.load(response.data);
const list = $('#News-sidebar-b-nav').find('li');
ctx.state.data = {
title: '温大新闻',
link,
description: '温州大学',
item:
list &&
list
.map((index, item) => {
item = $(item);
const a1 = item.find('li a');
return { title: a1.attr('title'), description: a1.attr('title'), pubDate: parseDate(item.find('li samp').text(), 'YYYY-MM-DD'), link: a1.attr('href') };
})
.get(),
};
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/news/:type?': ['Chandler-Lu'],
};
+84
View File
@@ -0,0 +1,84 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { URL } = require('url');
const { parseDate } = require('@/utils/parse-date');
/* 新闻列表
温大新闻 http://www.wzu.edu.cn/index/wdxw.htm
媒体温大 http://www.wzu.edu.cn/index/mtwd.htm
学术温大 http://www.wzu.edu.cn/index/xswd.htm
通知公告 http://www.wzu.edu.cn/index/tzgg.htm
招标信息 http://www.wzu.edu.cn/index/zbxx.htm
学术公告 http://www.wzu.edu.cn/index/xsgg.htm
*/
const baseUrl = 'http://www.wzu.edu.cn/index/';
const newsType = {
wdxw: '温大新闻',
mtwd: '媒体温大',
xswd: '学术温大',
tzgg: '通知公告',
zbxx: '招标信息',
xsgg: '学术公告',
};
/**
* @description: 抓取文章内容
* @param {*} link
* @return {*} description
*/
async function load(link) {
let videoUrl = '';
// 请求文章页面
const newsResp = await got.get(link);
// 加载文章内容
const $ = cheerio.load(newsResp.data, { decodeEntities: false });
// 图片相对链接处理
$('img').attr('src', (n, v) => new URL(v, baseUrl).href);
// 视频相对链接处理,替换原有播放方法 showVsbVideo
$('.vsbcontent_video').each(function () {
const u1 = $(this).find('script').attr('vurl');
videoUrl = new URL(u1, baseUrl).href;
return $(this)
.html('<video width="100%" src="' + videoUrl + '"></video>')
.html();
});
// 返回解析的结果
return $('div[id^=vsb_content]').html();
}
module.exports = async (ctx) => {
// 获取路由 Tag
const routeTag = parseInt(ctx.params.type) || 0;
// 设定新闻标题及 Url
const newsArr = Object.entries(newsType);
const [k1, newsTitle] = newsArr[routeTag];
const newsLink = new URL(k1 + '.htm', baseUrl).href;
const response = await got.get(newsLink);
const $ = cheerio.load(response.data);
const list = $('#News-sidebar-b-nav').find('li');
ctx.state.data = {
title: newsTitle,
link: newsLink,
description: '温州大学' + ' - ' + newsTitle,
item:
list &&
list
.map(async (item) => {
const $ = cheerio.load(list[item]);
const $a1 = $('li>a');
const $originUrl = $a1.attr('href');
const $itemUrl = new URL($originUrl, baseUrl).href;
return {
title: $a1.attr('title'),
description: await ctx.cache.tryGet($itemUrl, () => load($itemUrl)),
pubDate: parseDate($('li>samp').text(), 'YYYY-MM-DD'),
link: $itemUrl,
};
})
.get(),
};
};
+11
View File
@@ -0,0 +1,11 @@
module.exports = {
'wzu.edu.cn': {
_name: '温州大学',
'.': [
{
title: '温州大学 - 主站新闻',
docs: 'https://docs.rsshub.app/university.html#wen-zhou-da-xue',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news/:type?', require('./news'));
};