From d51179bd0e1f8610a644df7d139ee8e1498513cf Mon Sep 17 00:00:00 2001 From: SettingDust Date: Sun, 27 Jun 2021 08:03:12 +0800 Subject: [PATCH] feat(route): dlu add fulltext (#7769) --- lib/routes/universities/dlu/jiaowu/news.js | 58 ++++++++++------------ 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/lib/routes/universities/dlu/jiaowu/news.js b/lib/routes/universities/dlu/jiaowu/news.js index 6f206d5a86..2f91ba5b37 100644 --- a/lib/routes/universities/dlu/jiaowu/news.js +++ b/lib/routes/universities/dlu/jiaowu/news.js @@ -1,45 +1,37 @@ const got = require('@/utils/got'); const iconv = require('iconv-lite'); const cheerio = require('cheerio'); -const resolve_url = require('url').resolve; +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); -const base_url = 'http://jwc1.dlu.edu.cn/'; +const baseUrl = 'http://jwc1.dlu.edu.cn/'; -// const map = { -// 2: '/gsgg.htm', -// 3: '/jwtz.htm', -// 4: '/jwwj.htm', -// 5: '/xxwj.htm' -// }; module.exports = async (ctx) => { - // const type = ctx.params.type || '2'; - // const link = `${base_url}${map[type]}`; + let resp = await got({ url: baseUrl, responseType: 'buffer' }); + const index = cheerio.load(iconv.decode(resp.data, 'utf-8')); - const response = await got({ - method: 'get', - url: base_url, - // url:link, - responseType: 'buffer', - headers: { - Referer: base_url, - }, - }); - - const $ = cheerio.load(iconv.decode(response.data, 'utf-8')); - - const regex = /(\d+)\.htm/; + const items = index('td[valign="top"]>table[cellspacing="3"][width="100%"] tr') + .toArray() + .map(async (elem) => { + const link = new URL(index('a', elem).attr('href'), baseUrl).href; + return await ctx.cache.tryGet(link, async () => { + resp = await got({ url: link, responseType: 'buffer' }); + const article = cheerio.load(iconv.decode(resp.data, 'utf-8'), { decodeEntities: false }); + return { + link: link.toString(), + title: index('a', elem).attr('title'), + pubDate: timezone(parseDate(article('td[height="28"]').text(), 'YYYY年MM月DD日 HH:mm'), +8).toUTCString(), + description: article('form[name="_newscontent_fromname"] > div > *:not(table)') + .toArray() + .map((it) => article(it).html()) + .join('\n'), + }; + }); + }); ctx.state.data = { - link: base_url, + link: baseUrl, title: '大连大学教务处', - item: $('td[valign="top"]>table[cellspacing="3"][width="100%"] tr') - .map((_, elem) => ({ - link: resolve_url(base_url, $('a', elem).attr('href')), - title: $('a', elem).attr('title'), - pubDate: new Date(new Date().getFullYear() + '-' + $('[class*="timestyle"]', elem).text().replace('/', '-')).toUTCString(), - })) - .get() - .sort((one, two) => two.link.match(regex)[1] - one.link.match(regex)[1]) - .slice(0, 20), + item: await Promise.all(items), }; };