From 7d858bc03de80b963bcbeba38ba999322b41c318 Mon Sep 17 00:00:00 2001 From: Derek Date: Sun, 28 Nov 2021 07:31:07 +0800 Subject: [PATCH] feat(route): change csc to v2 (#8596) --- assets/radar-rules.js | 9 ---- lib/router.js | 3 -- lib/v2/csc/maintainer.js | 3 ++ lib/v2/csc/notice.js | 99 ++++++++++++++++++++++++++++++++++++++++ lib/v2/csc/radar.js | 31 +++++++++++++ lib/v2/csc/router.js | 3 ++ 6 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 lib/v2/csc/maintainer.js create mode 100644 lib/v2/csc/notice.js create mode 100644 lib/v2/csc/radar.js create mode 100644 lib/v2/csc/router.js diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 2e15a0f6c..8c0257371 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -653,15 +653,6 @@ ], }, 'apnews.com': { _name: 'AP News', '.': [{ title: '话题', docs: 'https://docs.rsshub.app/traditional-media.html#ap-news', source: '/:topic', target: '/apnews/topics/:topic' }] }, - 'csc.edu.cn': { - _name: '国家留学网', - www: [ - { title: '遴选通知', docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', source: '/*', target: '/csc/notice/lxtz' }, - { title: '综合项目专栏', docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', source: '/*', target: '/csc/notice/xmzl' }, - { title: '常见问题解答', docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', source: '/*', target: '/csc/notice/wtjd' }, - { title: '录取公告', docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', source: '/*', target: '/csc/notice/lqgg' }, - ], - }, 'biquge5200.com': { www: [{ title: 'biquge5200.com', docs: 'https://docs.rsshub.app/reading.html#bi-qu-ge-biquge5200-com', source: '/:id', target: '/novel/biquge/:id' }] }, 'biquge.info': { www: [{ title: 'biquge.info', docs: 'https://docs.rsshub.app/reading.html#bi-qu-ge-biquge-info', source: '/:id', target: '/novel/biqugeinfo/:id' }] }, 'matters.news': { diff --git a/lib/router.js b/lib/router.js index 7205ac212..6a7fa9a62 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2683,9 +2683,6 @@ router.get('/krankenkassen', lazyloadRouteHandler('./routes/krankenkassen')); // 桂林航天工业学院 router.get('/guat/news/:type?', lazyloadRouteHandler('./routes/guat/news')); -// 国家留学网 -router.get('/csc/notice/:type?', lazyloadRouteHandler('./routes/csc/notice')); - // LearnKu router.get('/learnku/:community/:category?', lazyloadRouteHandler('./routes/learnku/topic')); diff --git a/lib/v2/csc/maintainer.js b/lib/v2/csc/maintainer.js new file mode 100644 index 000000000..0c01dcfcd --- /dev/null +++ b/lib/v2/csc/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/notice/:type?': ['Derekmini'], +}; diff --git a/lib/v2/csc/notice.js b/lib/v2/csc/notice.js new file mode 100644 index 000000000..881fcbcf6 --- /dev/null +++ b/lib/v2/csc/notice.js @@ -0,0 +1,99 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const url = require('url'); +const iconv = require('iconv-lite'); + +const baseUrl = 'https://www.csc.edu.cn'; + +const typeMap = { + lxtz: { + name: '遴选通知', + url: '/chuguo/list/24', + }, + xmzl: { + name: '综合项目专栏', + url: '/chuguo/list/26', + }, + wtjd: { + name: '常见问题解答', + url: '/chuguo/list/27', + }, + lqgg: { + name: '录取公告', + url: '/chuguo/list/28', + }, + xwzx: { + name: '新闻资讯', + url: '/news', + }, + xwgg: { + name: '新闻公告', + url: '/news/gonggao', + }, +}; + +module.exports = async (ctx) => { + const type = ctx.params.type || 'jjyw'; + const link = baseUrl + typeMap[type].url; + const response = await got({ + method: 'get', + url: link, + headers: { + Referer: baseUrl, + }, + responseType: 'buffer', + }); + const responseHtml = iconv.decode(response.data, 'gbk'); + const $ = cheerio.load(responseHtml); + + const urlList = $('.list-a li') + .slice(0, 10) + .map((i, e) => $('a', e).attr('href')) + .get(); + + const titleList = $('.list-a li') + .slice(0, 10) + .map((i, e) => $('a', e).attr('title')) + .get(); + + const dateList = $('.list-a li') + .slice(0, 10) + .map((i, e) => $('span', e).text()) + .get(); + + const out = await Promise.all( + urlList.map(async (itemUrl, index) => { + itemUrl = url.resolve(baseUrl, itemUrl); + + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const response = await got({ + method: 'get', + url: itemUrl, + responseType: 'buffer', + }); + const responseHtmlItem = iconv.decode(response.data, 'gbk'); + const $ = cheerio.load(responseHtmlItem); + const single = { + title: titleList[index], + link: itemUrl, + description: $('.contents') + .html() + .replace(/src="\//g, `src="${url.resolve(baseUrl, '.')}`) + .replace(/href="\//g, `href="${url.resolve(baseUrl, '.')}`) + .trim(), + pubDate: dateList[index], + }; + ctx.cache.set(itemUrl, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: '国家留学网-' + typeMap[type].name, + link, + item: out, + }; +}; diff --git a/lib/v2/csc/radar.js b/lib/v2/csc/radar.js new file mode 100644 index 000000000..43b34033c --- /dev/null +++ b/lib/v2/csc/radar.js @@ -0,0 +1,31 @@ +module.exports = { + 'csc.edu.cn': { + _name: '国家留学网', + www: [ + { + title: '遴选通知', + docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', + source: '/*', + target: '/csc/notice/lxtz', + }, + { + title: '综合项目专栏', + docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', + source: '/*', + target: '/csc/notice/xmzl', + }, + { + title: '常见问题解答', + docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', + source: '/*', + target: '/csc/notice/wtjd', + }, + { + title: '录取公告', + docs: 'https://docs.rsshub.app/other.html#guo-jia-liu-xue-wang', + source: '/*', + target: '/csc/notice/lqgg', + }, + ], + }, +}; diff --git a/lib/v2/csc/router.js b/lib/v2/csc/router.js new file mode 100644 index 000000000..f493eb3cf --- /dev/null +++ b/lib/v2/csc/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/notice/:type?', require('./notice')); +};