feat(route): dlu add fulltext (#7769)

This commit is contained in:
SettingDust
2021-06-26 17:03:12 -07:00
committed by GitHub
parent 61d9e2dd4e
commit d51179bd0e
+25 -33
View File
@@ -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),
};
};