From cc8c8697f1297fe286db5ef9d9ca752d3fe56e3f Mon Sep 17 00:00:00 2001 From: Yuri Kiriyama <51554406+legr4ndk@users.noreply.github.com> Date: Mon, 6 Feb 2023 19:05:26 +0800 Subject: [PATCH] fix: update pku.ss hosts due to the host change on November 2022 (#11784) * update hosts due to the host change on November 2022 * refactor: combine duplicated function --------- --- lib/v2/pku/ss/admission.js | 46 ++++---------------------------------- lib/v2/pku/ss/common.js | 37 ++++++++++++++++++++++++++++++ lib/v2/pku/ss/notice.js | 46 ++++---------------------------------- lib/v2/pku/ss/pg_admin.js | 46 ++++---------------------------------- 4 files changed, 49 insertions(+), 126 deletions(-) create mode 100644 lib/v2/pku/ss/common.js diff --git a/lib/v2/pku/ss/admission.js b/lib/v2/pku/ss/admission.js index 585b8553a..b7cd0572c 100644 --- a/lib/v2/pku/ss/admission.js +++ b/lib/v2/pku/ss/admission.js @@ -1,48 +1,10 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); +const { baseUrl, getSingleRecord, getArticle } = require('./common'); -const link = 'http://www.ss.pku.edu.cn'; -const host = 'http://www.ss.pku.edu.cn/index.php/admission/admnotice'; - -const getSingleRecord = async () => { - const res = await got(host); - - const $ = cheerio.load(res.data); - const list = $('#info-list-ul').find('li'); - - return ( - list && - list - .map((index, item) => { - item = $(item); - const date = item.find('.time').text(); - return { - title: item.find('a').attr('title'), - pubDate: parseDate(date), - link: link + item.find('a').attr('href'), - }; - }) - .get() - ); -}; +const host = `${baseUrl}/admission/admnotice/`; module.exports = async (ctx) => { - const items = await getSingleRecord(); - const out = await Promise.all( - items.map((item) => - ctx.cache.tryGet(item.link, async () => { - const response = await got(item.link); - const $ = cheerio.load(response.data); - return { - title: item.title, - link: item.link, - description: $('.article-content').html(), - pubDate: item.pubDate, - }; - }) - ) - ); + const items = await getSingleRecord(host); + const out = await Promise.all(items.map((item) => getArticle(item, ctx.cache.tryGet))); ctx.state.data = { title: '北大软微-招生通知', diff --git a/lib/v2/pku/ss/common.js b/lib/v2/pku/ss/common.js new file mode 100644 index 000000000..d518c7965 --- /dev/null +++ b/lib/v2/pku/ss/common.js @@ -0,0 +1,37 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +const baseUrl = 'https://www.ss.pku.edu.cn'; + +const getSingleRecord = async (url) => { + const res = await got(url); + + const $ = cheerio.load(res.data); + const list = $('#info-list-ul').find('li'); + + return list.toArray().map((item) => { + item = $(item); + const date = item.find('.time').text(); + return { + title: item.find('a').attr('title'), + pubDate: parseDate(date), + link: baseUrl + item.find('a').attr('href'), + }; + }); +}; + +const getArticle = (item, tryGet) => + tryGet(item.link, async () => { + const response = await got(item.link); + const $ = cheerio.load(response.data); + + item.description = $('.article-content').html(); + return item; + }); + +module.exports = { + baseUrl, + getSingleRecord, + getArticle, +}; diff --git a/lib/v2/pku/ss/notice.js b/lib/v2/pku/ss/notice.js index 778a1810f..71dfeb058 100644 --- a/lib/v2/pku/ss/notice.js +++ b/lib/v2/pku/ss/notice.js @@ -1,48 +1,10 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); +const { baseUrl, getSingleRecord, getArticle } = require('./common'); -const link = 'http://www.ss.pku.edu.cn'; -const host = 'http://www.ss.pku.edu.cn/index.php/newscenter/notice'; - -const getSingleRecord = async () => { - const res = await got(host); - - const $ = cheerio.load(res.data); - const list = $('#info-list-ul').find('li'); - - return ( - list && - list - .map((index, item) => { - item = $(item); - const date = item.find('.time').text(); - return { - title: item.find('a').attr('title'), - pubDate: parseDate(date), - link: link + item.find('a').attr('href'), - }; - }) - .get() - ); -}; +const host = `${baseUrl}/newscenter/notice/`; module.exports = async (ctx) => { - const items = await getSingleRecord(); - const out = await Promise.all( - items.map((item) => - ctx.cache.tryGet(item.link, async () => { - const response = await got(item.link); - const $ = cheerio.load(response.data); - return { - title: item.title, - link: item.link, - description: $('.article-content').html(), - pubDate: item.pubDate, - }; - }) - ) - ); + const items = await getSingleRecord(host); + const out = await Promise.all(items.map((item) => getArticle(item, ctx.cache.tryGet))); ctx.state.data = { title: '北大软微-通知公告', diff --git a/lib/v2/pku/ss/pg_admin.js b/lib/v2/pku/ss/pg_admin.js index 734fc2829..2c7fdd801 100644 --- a/lib/v2/pku/ss/pg_admin.js +++ b/lib/v2/pku/ss/pg_admin.js @@ -1,48 +1,10 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); +const { baseUrl, getSingleRecord, getArticle } = require('./common'); -const link = 'http://www.ss.pku.edu.cn'; -const host = 'http://www.ss.pku.edu.cn/index.php/admission/admbrochure/admission01'; - -const getSingleRecord = async () => { - const res = await got(host); - - const $ = cheerio.load(res.data); - const list = $('#info-list-ul').find('li'); - - return ( - list && - list - .map((index, item) => { - item = $(item); - const date = item.find('.time').text(); - return { - title: item.find('a').attr('title'), - pubDate: parseDate(date), - link: link + item.find('a').attr('href'), - }; - }) - .get() - ); -}; +const host = `${baseUrl}/admission/admbrochure/`; module.exports = async (ctx) => { - const items = await getSingleRecord(); - const out = await Promise.all( - items.map((item) => - ctx.cache.tryGet(item.link, async () => { - const response = await got(item.link); - const $ = cheerio.load(response.data); - return { - title: item.title, - link: item.link, - description: $('.article-content').html(), - pubDate: item.pubDate, - }; - }) - ) - ); + const items = await getSingleRecord(host); + const out = await Promise.all(items.map((item) => getArticle(item, ctx.cache.tryGet))); ctx.state.data = { title: '北大软微-硕士统考招生',