From d6c17870fcf9567e06d04b686cd9348aa5b1dac4 Mon Sep 17 00:00:00 2001 From: Simon Shi Date: Mon, 11 Jun 2018 18:18:22 +0800 Subject: [PATCH] feat: Add Unix Socket Support && rss: Add shmtu route (#285) * feat: Add Unix Socket Support * rss: Add shmtu route * fix: docs & bugs --- README.md | 4 ++++ config.js | 5 ++++- docs/README.md | 26 +++++++++++++++++++++ docs/install/README.md | 2 ++ index.js | 19 ++++++++++++++-- router.js | 5 +++++ routes/shmtu/events.js | 41 +++++++++++++++++++++++++++++++++ routes/shmtu/jwc.js | 51 ++++++++++++++++++++++++++++++++++++++++++ routes/shmtu/notes.js | 41 +++++++++++++++++++++++++++++++++ 9 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 routes/shmtu/events.js create mode 100644 routes/shmtu/jwc.js create mode 100644 routes/shmtu/notes.js diff --git a/README.md b/README.md index 6c91ea2aa..c6a32951b 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,10 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - Lookup Torrents by IMDB ID(根据 IMDB ID 查找种子) - 什么值得买 - 关键词 +- 上海海事大学 + - 学术讲座 + - 通知公告 + - 教务信息 ## 鸣谢 diff --git a/config.js b/config.js index ca7f77c2f..2bbd13179 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,8 @@ module.exports = { - port: process.env.PORT || 1200, // 监听端口 + connect: { + port: process.env.PORT || 1200, // 监听端口 + socket: process.env.SOCKET || null, // 监听 Unix Socket, null 为禁用 + }, cacheType: process.env.CACHE_TYPE || 'memory', // 缓存类型,支持 'memory' 和 'redis',设为空可以禁止缓存 cacheExpire: process.env.CACHE_EXPIRE || 5 * 60, // 缓存时间,单位为秒 ua: process.env.UA || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36', diff --git a/docs/README.md b/docs/README.md index 59115e15d..e27342809 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1130,3 +1130,29 @@ id, 专辑 id, 可在对应专辑页面的 URL 中找到 路由: `/smzdm/keyword/:keyword` 参数: keyword,你想订阅的关键词 + +## 上海海事大学 + +### 学术讲座 + +举例: [https://rsshub.app/shmtu/events](https://rsshub.app/shmtu/events) + +路由: `/shmtu/events` + +参数: 无 + +### 通知公告 + +举例: [https://rsshub.app/shmtu/notes](https://rsshub.app/shmtu/notes) + +路由: `/shmtu/notes` + +参数: 无 + +### 教务信息 + +举例: [https://rsshub.app/shmtu/jwc/1](https://rsshub.app/shmtu/jwc/1) + +路由: `/shmtu/jwc/:type` + +参数: type,1 为教务新闻,2 为教务公告 diff --git a/docs/install/README.md b/docs/install/README.md index 46eddf97c..53fbd8954 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -329,6 +329,8 @@ gcloud app deploy `PORT`: 监听端口,默认为 `1200` +`SOCKET`: 监听 Unix Socket,默认为 `null` + `CACHE_TYPE`: 缓存类型,可为 `memory` 和 `redis`,设为空可以禁止缓存,默认为 `memory` `CACHE_EXPIRE`: 缓存过期时间,单位为秒,默认 300 diff --git a/index.js b/index.js index a45ce92e3..d5dbd0088 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const Koa = require('koa'); - +const fs = require('fs'); const logger = require('./utils/logger'); const config = require('./config'); @@ -83,4 +83,19 @@ if (config.cacheType === 'memory') { // router app.use(router.routes()).use(router.allowedMethods()); -app.listen(config.port, parseInt(config.listenInaddrAny) ? null : '127.0.0.1'); +// connect +if (config.connect.port) { + app.listen(config.connect.port, parseInt(config.listenInaddrAny) ? null : '127.0.0.1'); + logger.info('Listening Port ' + config.connect.port); +} +if (config.connect.socket) { + if (fs.existsSync(config.connect.socket)) { + fs.unlinkSync(config.connect.socket); + } + app.listen(config.connect.socket, parseInt(config.listenInaddrAny) ? null : '127.0.0.1'); + logger.info('Listening Unix Socket ' + config.connect.socket); + process.on('SIGINT', () => { + fs.unlinkSync(config.connect.socket); + process.exit(); + }); +} diff --git a/router.js b/router.js index 23d0766da..9b279e885 100644 --- a/router.js +++ b/router.js @@ -293,4 +293,9 @@ router.get('/eztv/torrents/:imdb_id', require('./routes/eztv/imdb')); // 什么值得买 router.get('/smzdm/keyword/:keyword', require('./routes/smzdm/keyword')); +// SHMTU +router.get('/shmtu/events', require('./routes/shmtu/events')); +router.get('/shmtu/notes', require('./routes/shmtu/notes')); +router.get('/shmtu/jwc/:type', require('./routes/shmtu/jwc')); + module.exports = router; diff --git a/routes/shmtu/events.js b/routes/shmtu/events.js new file mode 100644 index 000000000..ae23878d4 --- /dev/null +++ b/routes/shmtu/events.js @@ -0,0 +1,41 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const host = 'http://www.shmtu.edu.cn'; + + const response = await axios({ + method: 'get', + url: host + '/events', + headers: { + 'User-Agent': config.ua, + Referer: host, + }, + }); + + const $ = cheerio.load(response.data); + const text = $('tr', 'tbody'); + + ctx.state.data = { + title: '上海海事大学 学术讲座', + link: host + '/events', + description: '上海海事大学 学术讲座', + item: + text && + text + .map((index, item) => { + item = $(item); + return { + title: item.find('.title').text(), + description: '发布部门 - ' + item.find('.department').text(), + pubDate: item + .find('span') + .find('span') + .attr('content'), + link: host + item.find('a').attr('href'), + }; + }) + .get(), + }; +}; diff --git a/routes/shmtu/jwc.js b/routes/shmtu/jwc.js new file mode 100644 index 000000000..1e53793b7 --- /dev/null +++ b/routes/shmtu/jwc.js @@ -0,0 +1,51 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const host = 'http://jwc.shmtu.edu.cn/Information/'; + let type = ctx.params.type; + let info = '教务新闻'; + if (type === '2') { + info = '教务公告'; + } else { + type = '1'; + } + + const response = await axios({ + method: 'get', + url: host + `MoreInfo.aspx?type=${type}`, + headers: { + 'User-Agent': config.ua, + Referer: host, + }, + }); + + const $ = cheerio.load(response.data); + const text = $('.gvRow', '.tdMCViewList'); + + ctx.state.data = { + title: `上海海事大学 ${info}`, + link: host + `MoreInfo.aspx?type=${type}`, + description: `上海海事大学 ${info}`, + item: + text && + text + .map((index, item) => { + item = $(item); + return { + title: item.find('a').attr('title'), + description: + '信息类别 - ' + + $('.gvItemNormal', item) + .slice(1, 2) + .text(), + pubDate: $('.gvItemNormal', item) + .slice(4) + .text(), + link: host + item.find('a').attr('href'), + }; + }) + .get(), + }; +}; diff --git a/routes/shmtu/notes.js b/routes/shmtu/notes.js new file mode 100644 index 000000000..938fb9db6 --- /dev/null +++ b/routes/shmtu/notes.js @@ -0,0 +1,41 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const host = 'http://www.shmtu.edu.cn'; + + const response = await axios({ + method: 'get', + url: host + '/notes', + headers: { + 'User-Agent': config.ua, + Referer: host, + }, + }); + + const $ = cheerio.load(response.data); + const text = $('tr', 'tbody'); + + ctx.state.data = { + title: '上海海事大学 通知公告', + link: host + '/notes', + description: '上海海事大学 通知公告', + item: + text && + text + .map((index, item) => { + item = $(item); + return { + title: item.find('.title').text(), + description: '发布部门 - ' + item.find('.department').text(), + pubDate: item + .find('span') + .find('span') + .attr('content'), + link: host + item.find('a').attr('href'), + }; + }) + .get(), + }; +};