From 7dd591ca82a55df84dc3fb40fce90618311368bc Mon Sep 17 00:00:00 2001 From: Elliott <59078359+wzc-blog@users.noreply.github.com> Date: Tue, 22 Feb 2022 22:48:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E5=AD=A6=E9=99=A2=E8=B7=AF=E7=94=B1=20(#7777)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat:add 信息学院路由 * refactor: migrate to v2 Co-authored-by: TonyRL --- docs/university.md | 10 +++ lib/router.js | 11 ++-- lib/{routes/universities => v2}/bjfu/grs.js | 0 lib/v2/bjfu/it/index.js | 49 ++++++++++++++ lib/v2/bjfu/it/utils.js | 64 +++++++++++++++++++ .../universities => v2}/bjfu/jwc/index.js | 0 .../universities => v2}/bjfu/jwc/utils.js | 19 +++--- lib/{routes/universities => v2}/bjfu/kjc.js | 0 lib/v2/bjfu/maintainer.js | 7 ++ .../universities => v2}/bjfu/news/index.js | 0 .../universities => v2}/bjfu/news/utils.js | 19 +++--- lib/v2/bjfu/radar.js | 45 +++++++++++++ lib/v2/bjfu/router.js | 7 ++ 13 files changed, 208 insertions(+), 23 deletions(-) rename lib/{routes/universities => v2}/bjfu/grs.js (100%) create mode 100644 lib/v2/bjfu/it/index.js create mode 100644 lib/v2/bjfu/it/utils.js rename lib/{routes/universities => v2}/bjfu/jwc/index.js (100%) rename lib/{routes/universities => v2}/bjfu/jwc/utils.js (80%) rename lib/{routes/universities => v2}/bjfu/kjc.js (100%) create mode 100644 lib/v2/bjfu/maintainer.js rename lib/{routes/universities => v2}/bjfu/news/index.js (100%) rename lib/{routes/universities => v2}/bjfu/news/utils.js (80%) create mode 100644 lib/v2/bjfu/radar.js create mode 100644 lib/v2/bjfu/router.js diff --git a/docs/university.md b/docs/university.md index 71832b25c..ad72fd7a3 100644 --- a/docs/university.md +++ b/docs/university.md @@ -258,6 +258,16 @@ pageClass: routes +### 信息学院通知 + + + +| 学院新闻 | 科研动态 | 本科生培养 | 研究生培养 | +| ---- | ---- | ----- | ----- | +| xyxw | kydt | pydt | pydt2 | + + + ## 北京物资学院 ### 通知公告 diff --git a/lib/router.js b/lib/router.js index ef293356e..f2366932e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -536,11 +536,12 @@ router.get('/sayhuahuo', lazyloadRouteHandler('./routes/galgame/sayhuahuo')); // 终点分享 router.get('/zdfx', lazyloadRouteHandler('./routes/galgame/zdfx')); -// 北京林业大学 -router.get('/bjfu/grs', lazyloadRouteHandler('./routes/universities/bjfu/grs')); -router.get('/bjfu/kjc', lazyloadRouteHandler('./routes/universities/bjfu/kjc')); -router.get('/bjfu/jwc/:type', lazyloadRouteHandler('./routes/universities/bjfu/jwc/index')); -router.get('/bjfu/news/:type', lazyloadRouteHandler('./routes/universities/bjfu/news/index')); +// 北京林业大学 migrated to v2 +// router.get('/bjfu/grs', lazyloadRouteHandler('./routes/universities/bjfu/grs')); +// router.get('/bjfu/kjc', lazyloadRouteHandler('./routes/universities/bjfu/kjc')); +// router.get('/bjfu/jwc/:type', lazyloadRouteHandler('./routes/universities/bjfu/jwc/index')); +// router.get('/bjfu/news/:type', lazyloadRouteHandler('./routes/universities/bjfu/news/index')); +// router.get('/bjfu/it/:type', lazyloadRouteHandler('./routes/universities/bjfu/it/index')); // 北京理工大学 router.get('/bit/jwc', lazyloadRouteHandler('./routes/universities/bit/jwc/jwc')); diff --git a/lib/routes/universities/bjfu/grs.js b/lib/v2/bjfu/grs.js similarity index 100% rename from lib/routes/universities/bjfu/grs.js rename to lib/v2/bjfu/grs.js diff --git a/lib/v2/bjfu/it/index.js b/lib/v2/bjfu/it/index.js new file mode 100644 index 000000000..5ce783dea --- /dev/null +++ b/lib/v2/bjfu/it/index.js @@ -0,0 +1,49 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const util = require('./utils'); +const iconv = require('iconv-lite'); // 转码 + +module.exports = async (ctx) => { + const type = ctx.params.type; + let title, path; + switch (type) { + case 'kydt': + title = '科研动态'; + path = 'kyxz/kydt/'; + break; + case 'pydt': + title = '本科生培养'; + path = 'bkspy/pydt/'; + break; + case 'pydt2': + title = '研究生培养'; + path = 'yjspy/pydt2/'; + break; + default: + title = '学院新闻'; + path = 'xyxw/'; + } + const base = 'http://it.bjfu.edu.cn/' + path; + + const response = await got({ + method: 'get', + responseType: 'buffer', // 转码 + url: base, + }); + + const data = iconv.decode(response.data, 'gb2312'); // 转码 + const $ = cheerio.load(data); + + // const list = $('div[item-content]').slice(0, 10).get(); + + const list = $('.item-content').get(); + + const result = await util.ProcessFeed(base, list, ctx.cache); // 感谢@hoilc指导 + + ctx.state.data = { + title: '北林信息 - ' + title, + link: 'http://it.bjfu.edu.cn/' + path, + description: '北京林业大学信息学院 - ' + title, + item: result, + }; +}; diff --git a/lib/v2/bjfu/it/utils.js b/lib/v2/bjfu/it/utils.js new file mode 100644 index 000000000..45938bc0c --- /dev/null +++ b/lib/v2/bjfu/it/utils.js @@ -0,0 +1,64 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); // 转码 +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +// 完整文章页 +async function load(link) { + const response = await got.get(link, { + responseType: 'buffer', + }); + + const data = iconv.decode(response.data, 'gb2312'); // 转码 + + // 加载文章内容 + const $ = cheerio.load(data); + + // 解析日期 + const pubDate = timezone( + parseDate( + $('.template-head-info') + .text() + .match(/\d{4}-\d{2}-\d{2}/) + ), + +8 + ); + + // 提取内容 + const description = $('.template-body').html(); + + // 返回解析的结果 + return { description, pubDate }; +} + +const ProcessFeed = async (base, list, caches) => + // 使用 Promise.all() 进行 async 并发 + await Promise.all( + // 遍历每一篇文章 + list.map(async (item) => { + const $ = cheerio.load(item); + + const $title = $('a'); + // 还原相对链接为绝对链接 + const itemUrl = new URL($title.attr('href'), base).href; // 感谢@hoilc指导 + + // 列表上提取到的信息 + const single = { + title: $title.text(), + link: itemUrl, + author: '北林信息', + guid: itemUrl, + }; + + // 使用tryGet方法从缓存获取内容。 + // 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。 + const other = await caches.tryGet(itemUrl, async () => await load(itemUrl)); + + // 合并解析后的结果集作为该篇文章最终的输出结果 + return { ...single, ...other }; + }) + ); +module.exports = { + ProcessFeed, +}; diff --git a/lib/routes/universities/bjfu/jwc/index.js b/lib/v2/bjfu/jwc/index.js similarity index 100% rename from lib/routes/universities/bjfu/jwc/index.js rename to lib/v2/bjfu/jwc/index.js diff --git a/lib/routes/universities/bjfu/jwc/utils.js b/lib/v2/bjfu/jwc/utils.js similarity index 80% rename from lib/routes/universities/bjfu/jwc/utils.js rename to lib/v2/bjfu/jwc/utils.js index ad4cec7f9..a443fd9c0 100644 --- a/lib/routes/universities/bjfu/jwc/utils.js +++ b/lib/v2/bjfu/jwc/utils.js @@ -1,6 +1,8 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); // 转码 +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); // 完整文章页 async function load(link) { @@ -14,16 +16,15 @@ async function load(link) { const $ = cheerio.load(data); // 解析日期 - const date = new Date( - $('div #con_djl') - .text() - .match(/\d{4}-\d{2}-\d{2}/) + const pubDate = timezone( + parseDate( + $('div #con_djl') + .text() + .match(/\d{4}-\d{2}-\d{2}/) + ), + +8 ); - const timeZone = 8; - const serverOffset = date.getTimezoneOffset() / 60; - const pubDate = new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString(); - // 提取内容 const description = $('#con_c').html(); @@ -54,7 +55,7 @@ const ProcessFeed = (base, list, caches) => const other = await caches.tryGet(itemUrl, () => load(itemUrl)); // 合并解析后的结果集作为该篇文章最终的输出结果 - return Promise.resolve(Object.assign({}, single, other)); + return { ...single, ...other }; }) ); module.exports = { diff --git a/lib/routes/universities/bjfu/kjc.js b/lib/v2/bjfu/kjc.js similarity index 100% rename from lib/routes/universities/bjfu/kjc.js rename to lib/v2/bjfu/kjc.js diff --git a/lib/v2/bjfu/maintainer.js b/lib/v2/bjfu/maintainer.js new file mode 100644 index 000000000..5c42dfb68 --- /dev/null +++ b/lib/v2/bjfu/maintainer.js @@ -0,0 +1,7 @@ +module.exports = { + '/grs': ['markmingjie'], + '/it/:type': ['wzc-blog'], + '/jwc/:type': ['markmingjie'], + '/kjc': ['markmingjie'], + '/news/:type': ['markmingjie'], +}; diff --git a/lib/routes/universities/bjfu/news/index.js b/lib/v2/bjfu/news/index.js similarity index 100% rename from lib/routes/universities/bjfu/news/index.js rename to lib/v2/bjfu/news/index.js diff --git a/lib/routes/universities/bjfu/news/utils.js b/lib/v2/bjfu/news/utils.js similarity index 80% rename from lib/routes/universities/bjfu/news/utils.js rename to lib/v2/bjfu/news/utils.js index 445975a90..1d9851484 100644 --- a/lib/routes/universities/bjfu/news/utils.js +++ b/lib/v2/bjfu/news/utils.js @@ -1,6 +1,8 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); // 转码 +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); // 完整文章页 async function load(link) { @@ -14,16 +16,15 @@ async function load(link) { const $ = cheerio.load(data); // 解析日期 - const date = new Date( - $('.article') - .text() - .match(/\d{4}-\d{2}-\d{2}/) + const pubDate = timezone( + parseDate( + $('.article') + .text() + .match(/\d{4}\/\d{2}\/\d{2}/) + ), + +8 ); - const timeZone = 8; - const serverOffset = date.getTimezoneOffset() / 60; - const pubDate = new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString(); - // 提取内容 const description = $('.article_con').html(); @@ -54,7 +55,7 @@ const ProcessFeed = (base, list, caches) => const other = await caches.tryGet(itemUrl, () => load(itemUrl)); // 合并解析后的结果集作为该篇文章最终的输出结果 - return Promise.resolve(Object.assign({}, single, other)); + return { ...single, ...other }; }) ); module.exports = { diff --git a/lib/v2/bjfu/radar.js b/lib/v2/bjfu/radar.js new file mode 100644 index 000000000..c97d9e590 --- /dev/null +++ b/lib/v2/bjfu/radar.js @@ -0,0 +1,45 @@ +module.exports = { + 'bjfu.edu.cn': { + _name: '北京林业大学', + graduate: [ + { + title: '研究生院培养动态', + docs: 'https://docs.rsshub.app/university.html#bei-jing-lin-ye-da-xue', + source: '/', + target: '/bjfu/grs', + }, + ], + it: [ + { + title: '信息学院通知', + docs: 'https://docs.rsshub.app/university.html#bei-jing-lin-ye-da-xue', + source: '/:type/index.html', + target: '/bjfu/it/:type', + }, + ], + jwc: [ + { + title: '教务处通知公告', + docs: 'https://docs.rsshub.app/university.html#bei-jing-lin-ye-da-xue', + source: '/:type/index.html', + target: '/bjfu/jwc/:type', + }, + ], + kyc: [ + { + title: '科技处通知公告', + docs: 'https://docs.rsshub.app/university.html#bei-jing-lin-ye-da-xue', + source: '/', + target: '/bjfu/kjc', + }, + ], + news: [ + { + title: '绿色新闻网', + docs: 'https://docs.rsshub.app/university.html#bei-jing-lin-ye-da-xue', + source: '/:type/index.html', + target: '/bjfu/news/:type', + }, + ], + }, +}; diff --git a/lib/v2/bjfu/router.js b/lib/v2/bjfu/router.js new file mode 100644 index 000000000..00dde4326 --- /dev/null +++ b/lib/v2/bjfu/router.js @@ -0,0 +1,7 @@ +module.exports = (router) => { + router.get('/grs', require('./grs')); + router.get('/it/:type', require('./it/index')); + router.get('/jwc/:type', require('./jwc/index')); + router.get('/kjc', require('./kjc')); + router.get('/news/:type', require('./news/index')); +};