feat(route): tju news (#10240)

* feat(route): tju news

* chore: update

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* chore: update date format

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* chore: update

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* doc: update
This commit is contained in:
SUPER 2022-07-18 22:04:44 +08:00 committed by GitHub
parent 4be123c139
commit 73e4d079bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 189 additions and 0 deletions

View File

@ -68,6 +68,16 @@ Note: [Source website](https://gs.bjtu.edu.cn/) only provides articles in Chines
</RouteEn>
### News
<RouteEn author="SuperPung" example="/tju/news/focus" path="/tju/news/:type?" :paramsDesc="['default `focus`']">
| Focus on TJU | General News | Internal News | Media Report | Pictures of TJU |
| :----------: | :----------: | :-----------: | :----------: | :-------------: |
| focus | general | internal | media | picture |
</RouteEn>
### The Office of Academic Affairs
<RouteEn author="AmosChenYQ SuperPung" example="/tju/oaa/news" path="/tju/oaa/:type?" :paramsDesc="['default `news`']">

View File

@ -2403,6 +2403,16 @@ jsjxy.hbut.edu.cn 证书链不全,自建 RSSHub 可设置环境变量 NODE_TLS
## 天津大学
### 新闻网
<Route author="SuperPung" example="/tju/news/focus" path="/tju/news/:type?" :paramsDesc="['默认为 `focus`']">
| 聚焦天大 | 综合新闻 | 校内新闻 | 媒体报道 | 图说天大 |
| :---: | :-----: | :------: | :---: | :-----: |
| focus | general | internal | media | picture |
</Route>
### 智能与计算学部
<Route author="SuperPung" example="/tju/cic/news" path="/tju/cic/:type?" :paramsDesc="['默认为 `news`']">

View File

@ -1,5 +1,6 @@
module.exports = {
'/cic/:type?': ['SuperPung'],
'/news/:type?': ['SuperPung'],
'/oaa/:type?': ['AmosChenYQ', 'SuperPung'],
'/yzb/:type?': ['SuperPung'],
};

135
lib/v2/tju/news/index.js Normal file
View File

@ -0,0 +1,135 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const news_base_url = 'http://news.tju.edu.cn/';
const repo_url = 'https://github.com/DIYgod/RSSHub/issues';
const pageType = (href) => {
if (href === undefined) {
return 'unknown';
} else if (!href.startsWith('http')) {
return 'in-site';
}
const url = new URL(href);
if (url.hostname === 'news.tju.edu.cn') {
return 'tju-news';
} else {
return 'unknown';
}
};
module.exports = async (ctx) => {
const type = ctx.params && ctx.params.type;
let path, subtitle;
switch (type) {
case 'focus':
subtitle = '聚焦天大';
path = 'jjtd.htm';
break;
case 'general':
subtitle = '综合新闻';
path = 'zhxw.htm';
break;
case 'internal':
subtitle = '校内新闻';
path = 'xnxw1/qb.htm';
break;
case 'media':
subtitle = '媒体报道';
path = 'mtbd.htm';
break;
case 'picture':
subtitle = '图说天大';
path = 'tstd.htm';
break;
default:
subtitle = '聚焦天大';
path = 'jjtd.htm';
}
let response = null;
try {
response = await got(news_base_url + path, {
headers: {
Referer: news_base_url,
},
});
} catch (e) {
// ignore error handler
// console.log(e);
}
if (response === null) {
ctx.state.data = {
title: '天津大学新闻网 - ' + subtitle,
link: news_base_url + path,
description: '链接失效' + news_base_url + path,
item: [
{
title: '提示信息',
link: repo_url,
description: `<h2>请到<a href=${repo_url}>此处</a>提交Issue</h2>`,
},
],
};
} else {
const $ = cheerio.load(response.data);
let list;
if (type === 'picture') {
list = $('.picList > li').get();
} else {
list = $('.indexList > li').get();
}
list = list.map((item) => {
const href = $('h4 > a', item).attr('href');
const type = pageType(href);
return {
title: $('h4 > a', item).text(),
link: type === 'in-site' ? news_base_url + href : href,
type,
};
});
const items = await Promise.all(
list.map((item) => {
switch (item.type) {
case 'tju-news':
case 'in-site':
return ctx.cache.tryGet(item.link, async () => {
let detailResponse = null;
try {
delete item.type;
detailResponse = await got(item.link);
const content = cheerio.load(detailResponse.data);
item.pubDate = timezone(
parseDate(
content('.contentTime')
.text()
.match(/\d{4}-\d{2}-\d{2}/)[0],
'YYYY-MM-DD'
),
+8
);
item.description = content('.v_news_content').html();
} catch (e) {
// ignore error handler
}
return item;
});
default:
return item;
}
})
);
ctx.state.data = {
title: '天津大学新闻网 - ' + subtitle,
link: news_base_url + path,
item: items,
};
}
};

View File

@ -21,6 +21,38 @@ module.exports = {
target: '/tju/cic/forum',
},
],
news: [
{
title: '新闻网 - 聚焦天大',
docs: 'https://docs.rsshub.app/university.html#tian-jin-da-xue-xin-wen-wang',
source: ['/jjtd.htm', '/'],
target: '/tju/news/focus',
},
{
title: '新闻网 - 综合新闻',
docs: 'https://docs.rsshub.app/university.html#tian-jin-da-xue-xin-wen-wang',
source: ['/zhxw.htm', '/'],
target: '/tju/news/general',
},
{
title: '新闻网 - 校内新闻',
docs: 'https://docs.rsshub.app/university.html#tian-jin-da-xue-xin-wen-wang',
source: ['/xnxw1/qb.htm', '/'],
target: '/tju/news/internal',
},
{
title: '新闻网 - 媒体报道',
docs: 'https://docs.rsshub.app/university.html#tian-jin-da-xue-xin-wen-wang',
source: ['/mtbd.htm', '/'],
target: '/tju/news/media',
},
{
title: '新闻网 - 图说天大',
docs: 'https://docs.rsshub.app/university.html#tian-jin-da-xue-xin-wen-wang',
source: ['/tstd.htm', '/'],
target: '/tju/news/picture',
},
],
oaa: [
{
title: '教务处 - 新闻动态',

View File

@ -1,5 +1,6 @@
module.exports = function (router) {
router.get('/cic/:type?', require('./cic'));
router.get('/news/:type?', require('./news'));
router.get('/oaa/:type?', require('./oaa'));
router.get('/yzb/:type?', require('./yzb'));
};