Skip to content

Commit

Permalink
fix(route): 武汉大学新闻网 (#13502)
Browse files Browse the repository at this point in the history
* fix(route): 武汉大学新闻网

* fix typo

* docs: fix docs

---------
  • Loading branch information
nczitzk authored Oct 12, 2023
1 parent 1114f7e commit 4d23701
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 108 deletions.
2 changes: 1 addition & 1 deletion lib/v2/whu/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
'/cs/:type': ['ttyfly'],
'/gs/:type?': ['Delreyaa'],
'/news/:type*': ['SChen1024'],
'/news/:category?': ['SChen1024', 'nczitzk'],
};
128 changes: 48 additions & 80 deletions lib/v2/whu/news.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,70 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const { art } = require('@/utils/render');
const path = require('path');

// 参考 bit/jwc 北京理工大学的的页面写成
const { domain, processMeta, getMeta, processItems } = require('./util');

const baseUrl = 'https://news.whu.edu.cn/';

// 专门定义一个function用于加载文章内容
async function load(link) {
let description = '';
let pubDate = '';

let response;

// 外部链接访问不到不处理, 校内页面访问不到 记录错误
try {
// 异步请求文章
response = await got(link);
} catch (err) {
// 如果网络问题 直接出错
if (err.name && (err.name === 'HTTPError' || err.name === 'RequestError')) {
description = 'Page 404 Please Check!';
}
return { description };
}
// 加载文章内容
const $ = cheerio.load(response.data);
module.exports = async (ctx) => {
const { category = 'wdzx/wdyw' } = ctx.params;
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 10;

pubDate = $('meta[name="PubDate"]').length ? timezone(parseDate($('meta[name="PubDate"]').attr('content')), 8) : undefined;
const rootUrl = `https://news.${domain}`;
const currentUrl = new URL(`${category}.htm`, rootUrl).href;

$('div.v_news_content')
.find('img')
.each((_, e) => {
e = $(e);
if (e.attr('orisrc')) {
e.attr('src', new URL(e.attr('orisrc'), response.url).href);
e.removeAttr('orisrc');
e.removeAttr('vurl');
}
});
const { data: response } = await got(currentUrl);

// 提取文章内容
description = $('div.v_news_content').html();
// 返回解析的结果
return {
description,
pubDate,
author: $('meta[name="ContentSource"]').attr('content'),
};
}
const $ = cheerio.load(response);

module.exports = async (ctx) => {
// 默认 武大要闻 然后获取列表页面
const type = ctx.params.type || 'wdzx/wdyw';
const listPageUrl = baseUrl + type + '.htm';
const response = await got(listPageUrl);
const $ = cheerio.load(response.data);

// 获取当前页面的 list
const list = $('.nyleft ul li')
// The elements where the information is located vary with the category.
// 武大资讯 https://news.whu.edu.cn/wdzx/wdyw.htm => ul.wdzxList li a[title]
// 学术动态 https://news.whu.edu.cn/kydt.htm => ul.xsdtList li a
// 珞珈影像 https://news.whu.edu.cn/stkj/ljyx.htm => div.topPic a[title], ul.nypicList li a[title]
// 武大视频 https://news.whu.edu.cn/stkj/wdsp.htm => div.topVid a[title], ul.nyvidList li a[title]
let items = $('ul.wdzxList li a[title], ul.xsdtList li a, div.topPic a[title], ul.nypicList li a[title], div.topVid a[title], ul.nyvidList li a[title]')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);
const a = item.find('a').first();

const image = item.find('div.img img');

return {
title: a.attr('title') || item.find('.eclips').text(),
link: new URL(a.attr('href'), listPageUrl).href,
description: item.find('.line2').text(),
pubDate: item.find('time').length ? parseDate(item.find('time').text(), 'YYYY.MM.DD') : item.find('.line3').length ? parseDate(item.find('.line3').text()) : undefined,
title: item.prop('title') ?? item.find('h4.eclips').text(),
link: new URL(item.prop('href'), rootUrl).href,
pubDate: parseDate(item.find('time').text(), ['YYYY.MM.DD', 'DDYYYY.MM']),
description: art(path.join(__dirname, 'templates/description.art'), {
description: item.find('div.txt p').html(),
image: image.prop('src')
? {
src: new URL(image.prop('src'), rootUrl).href,
alt: image.prop('alt'),
}
: undefined,
}),
};
});

const result = await Promise.all(
// 遍历每一篇文章
list.map((item) =>
// 合并解析后的结果集作为该篇文章最终的输出结果
ctx.cache.tryGet(item.link, async () => {
// 如果不是 武汉大学的站点, 直接返回简单的标题即可
// 判断 是否外站链接,如果是 则直接返回页面 不做单独的解析
if (!item.link.startsWith(baseUrl)) {
return item;
}

const { description, pubDate, author } = await load(item.link);
items = await processItems(items, ctx.cache.tryGet, rootUrl);

item.description = description;
item.pubDate = pubDate;
item.author = author;
const meta = processMeta(response);
const siteName = getMeta(meta, 'SiteName');
const columnName = getMeta(meta, 'ColumnName');

return item;
})
)
);
const icon = new URL($('link[rel="shortcut icon"]').prop('href'), rootUrl).href;

ctx.state.data = {
title: $('title').first().text(),
link: listPageUrl,
item: result,
item: items,
title: `${siteName} - ${columnName}`,
link: currentUrl,
description: getMeta(meta, 'description'),
language: $('html').prop('lang'),
image: new URL($('div.logo img').prop('src'), rootUrl).href,
icon,
logo: icon,
subtitle: columnName,
author: siteName,
allowEmpty: true,
};
};
128 changes: 124 additions & 4 deletions lib/v2/whu/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,130 @@ module.exports = {
],
news: [
{
title: '新闻网',
docs: 'https://docs.rsshub.app/routes/university#wu-han-da-xue',
source: ['/*path'],
target: (params) => `/whu/news/${params.path.replace('.htm', '')}`,
title: '新闻网 - 武大资讯',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/wdzx/wdyw', '/wdzx'],
target: '/whu/news/wdzx/wdyw',
},
{
title: '新闻网 - 武大要闻',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/wdzx/wdyw', '/wdzx'],
target: '/whu/news/wdzx/wdyw',
},
{
title: '新闻网 - 综合新闻',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/wdzx/zhxw', '/wdzx'],
target: '/whu/news/wdzx/zhxw',
},
{
title: '新闻网 - 合作交流',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/wdzx/hzjl', '/wdzx'],
target: '/whu/news/wdzx/hzjl',
},
{
title: '新闻网 - 一线传真',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/wdzx/yxcz', '/wdzx'],
target: '/whu/news/wdzx/yxcz',
},
{
title: '新闻网 - 学术动态',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/kydt'],
target: '/whu/news/kydt',
},
{
title: '新闻网 - 校园文化',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/xywh/bfxy', '/xywh'],
target: '/whu/news/xywh/bfxy',
},
{
title: '新闻网 - 缤纷校园',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/xywh/bfxy', '/xywh'],
target: '/whu/news/xywh/bfxy',
},
{
title: '新闻网 - 校友之声',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/xywh/xyzs', '/xywh'],
target: '/whu/news/xywh/xyzs',
},
{
title: '新闻网 - 珞珈副刊',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/xywh/ljfk', '/xywh'],
target: '/whu/news/xywh/ljfk',
},
{
title: '新闻网 - 校史钩沉',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/xywh/xsgc', '/xywh'],
target: '/whu/news/xywh/xsgc',
},
{
title: '新闻网 - 主题教育',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/ztbd/ztjy', '/ztbd'],
target: '/whu/news/ztbd/ztjy',
},
{
title: '新闻网 - 顶天立地建学科',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/ztbd/dtldjxk', '/ztbd'],
target: '/whu/news/ztbd/dtldjxk',
},
{
title: '新闻网 - 学习在线',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/ztbd/xxzx', '/ztbd'],
target: '/whu/news/ztbd/xxzx',
},
{
title: '新闻网 - 记者调查',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/ztbd/jzdc', '/ztbd'],
target: '/whu/news/ztbd/jzdc',
},
{
title: '新闻网 - 校庆征文 我与武大',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/ztbd/xqzw_wywd', '/ztbd'],
target: '/whu/news/ztbd/xqzw_wywd',
},
{
title: '新闻网 - 媒体武大',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/mtwd'],
target: '/whu/news/mtwd',
},
{
title: '新闻网 - 珞珈人物',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/ljrw'],
target: '/whu/news/ljrw',
},
{
title: '新闻网 - 视听空间',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/stkj/ljyx', '/stkj'],
target: '/whu/media/stkj/ljyx',
},
{
title: '新闻网 - 珞珈影像',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/stkj/ljyx', '/stkj'],
target: '/whu/media/stkj/ljyx',
},
{
title: '新闻网 - 武大视频',
docs: 'https://docs.rsshub.app/routes/wu-han-da-xue#wu-han-da-xue-xin-wen-wang',
source: ['/stkj/wdsp', '/stkj'],
target: '/whu/media/stkj/wdsp',
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion lib/v2/whu/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function (router) {
router.get('/cs/:type', require('./cs'));
router.get('/gs/:type?', require('./gs/index.js'));
router.get('/news/:type*', require('./news'));
router.get('/news/:category*', require('./news'));
};
39 changes: 39 additions & 0 deletions lib/v2/whu/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{ if description }}
{{@ description }}
{{ /if }}

{{ if image }}
<figure>
<img src="{{ image.src }}"
{{ if image.alt }}
alt={{ image.alt }}
{{ /if }}
{{ if image.width }}
width={{ image.width }}
{{ /if }}
>
</figure>
{{ /if }}

{{ if video }}
<video controls
{{ if video.width }}
width={{ video.width }}
{{ /if }}
{{ if video.height }}
height={{ video.height }}
{{ /if }}>
<source src="{{ video.src }}" type="video/{{ video.src.split('.').pop() }}">
</video>
{{ /if }}

{{ if attachments && attachments.length > 0 }}
<b>附件</b>
<ul>
{{ each attachments attachment }}
<li>
<a href="{{ attachment.link }}">{{ attachment.title }}</a>
</li>
{{ /each }}
</ul>
{{ /if }}
Loading

0 comments on commit 4d23701

Please sign in to comment.