diff --git a/docs/other.md b/docs/other.md index 67b3e072d..503782030 100644 --- a/docs/other.md +++ b/docs/other.md @@ -235,6 +235,18 @@ type 为 all 时,category 参数不支持 cost 和 free +## 东莞教研网 + +### 信息公开 + + + +| 动态 | 公示 | 通知 | +| ---- | ------------ | ------ | +| news | announcement | notice | + + + ## 福利资源 - met.red ### 福利资源 - met.red diff --git a/lib/router.js b/lib/router.js index 289ad79bd..6835eba9d 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2795,4 +2795,7 @@ router.get('/law/jh', require('./routes/law/jh')); // Mobilism router.get('/mobilism/release', require('./routes/mobilism/release')); +// 东莞教研网 +router.get('/dgjyw/:type', require('./routes/dgjyw/index')); + module.exports = router; diff --git a/lib/routes/dgjyw/index.js b/lib/routes/dgjyw/index.js new file mode 100644 index 000000000..e110af154 --- /dev/null +++ b/lib/routes/dgjyw/index.js @@ -0,0 +1,67 @@ +const url = require('url'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const rootUrl = 'http://zxls.dgjyw.com/'; + +const config = { + news: { + link: '/node/57620', + title: '动态', + }, + announcement: { + link: '/node/57725', + title: '公示', + }, + notice: { + link: '/node/57621', + title: '通知', + }, +}; + +module.exports = async (ctx) => { + const cfg = config[ctx.params.type]; + if (!cfg) { + throw Error('Bad category. See docs'); + } + + const currentUrl = url.resolve(rootUrl, cfg.link); + const response = await got({ + method: 'get', + url: currentUrl, + }); + const $ = cheerio.load(response.data); + const list = $('ul.list-ul') + .eq(2) + .find('li') + .slice(0, 20) + .map((_, item) => { + item = $(item); + const a = item.find('a[title]'); + return { + title: a.text(), + link: a.attr('href'), + pubDate: new Date(new Date().getFullYear().toString().substr(0, 2) + item.find('span').text()).toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got({ method: 'get', url: item.link }); + const content = cheerio.load(res.data); + + item.description = content('div.text').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: '东莞教研网 - ' + cfg.title, + link: rootUrl, + item: items, + }; +};