From 73e4d079bb540af054db070d8c6fb2611b5f7277 Mon Sep 17 00:00:00 2001 From: SUPER Date: Mon, 18 Jul 2022 22:04:44 +0800 Subject: [PATCH] feat(route): tju news (#10240) * feat(route): tju news * chore: update Co-authored-by: Tony * chore: update date format Co-authored-by: Tony * chore: update Co-authored-by: Tony * doc: update --- docs/en/university.md | 10 +++ docs/university.md | 10 +++ lib/v2/tju/maintainer.js | 1 + lib/v2/tju/news/index.js | 135 +++++++++++++++++++++++++++++++++++++++ lib/v2/tju/radar.js | 32 ++++++++++ lib/v2/tju/router.js | 1 + 6 files changed, 189 insertions(+) create mode 100644 lib/v2/tju/news/index.js diff --git a/docs/en/university.md b/docs/en/university.md index 241789858..e32863be7 100644 --- a/docs/en/university.md +++ b/docs/en/university.md @@ -68,6 +68,16 @@ Note: [Source website](https://gs.bjtu.edu.cn/) only provides articles in Chines +### News + + + +| Focus on TJU | General News | Internal News | Media Report | Pictures of TJU | +| :----------: | :----------: | :-----------: | :----------: | :-------------: | +| focus | general | internal | media | picture | + + + ### The Office of Academic Affairs diff --git a/docs/university.md b/docs/university.md index 0432a85ec..768cf3207 100644 --- a/docs/university.md +++ b/docs/university.md @@ -2403,6 +2403,16 @@ jsjxy.hbut.edu.cn 证书链不全,自建 RSSHub 可设置环境变量 NODE_TLS ## 天津大学 +### 新闻网 + + + +| 聚焦天大 | 综合新闻 | 校内新闻 | 媒体报道 | 图说天大 | +| :---: | :-----: | :------: | :---: | :-----: | +| focus | general | internal | media | picture | + + + ### 智能与计算学部 diff --git a/lib/v2/tju/maintainer.js b/lib/v2/tju/maintainer.js index cff1aa805..f1bb99053 100644 --- a/lib/v2/tju/maintainer.js +++ b/lib/v2/tju/maintainer.js @@ -1,5 +1,6 @@ module.exports = { '/cic/:type?': ['SuperPung'], + '/news/:type?': ['SuperPung'], '/oaa/:type?': ['AmosChenYQ', 'SuperPung'], '/yzb/:type?': ['SuperPung'], }; diff --git a/lib/v2/tju/news/index.js b/lib/v2/tju/news/index.js new file mode 100644 index 000000000..f0b41cdbd --- /dev/null +++ b/lib/v2/tju/news/index.js @@ -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: `

请到此处提交Issue

`, + }, + ], + }; + } 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, + }; + } +}; diff --git a/lib/v2/tju/radar.js b/lib/v2/tju/radar.js index a06d63c63..824d214be 100644 --- a/lib/v2/tju/radar.js +++ b/lib/v2/tju/radar.js @@ -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: '教务处 - 新闻动态', diff --git a/lib/v2/tju/router.js b/lib/v2/tju/router.js index f1dfa3691..bdc1dbeba 100644 --- a/lib/v2/tju/router.js +++ b/lib/v2/tju/router.js @@ -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')); };