fix: nuaa 研究生院 (#6312)

This commit is contained in:
junfengP 2020-12-07 02:01:19 +08:00 committed by GitHub
parent e3f68d7967
commit e2c4eb7c41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 22 deletions

View File

@ -2,7 +2,7 @@ const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const host = 'http://www.graduate.nuaa.edu.cn/';
const getCookie = require('../utils/pypasswaf');
const formatPubDate = require('@/utils/date.js');
const map = {
latest: '2146/list.htm',
@ -12,12 +12,12 @@ const map = {
xxfw: '2147/list.htm',
};
async function load(link, ctx, gotConfig) {
async function load(link, ctx) {
const cache = await ctx.cache.get(link);
if (cache) {
return cache;
}
const response = await got.get(host + link, gotConfig);
const response = await got.get(host + link);
const $ = cheerio.load(response.data);
const images = $('img');
for (let k = 0; k < images.length; k++) {
@ -29,35 +29,25 @@ async function load(link, ctx, gotConfig) {
}
module.exports = async (ctx) => {
const cookie = await getCookie();
const gotConfig = {
headers: {
cookie,
},
};
const type = ctx.params.type || 'latest';
const response = await got(
{
method: 'get',
url: host + map[type],
},
gotConfig
);
const response = await got({
method: 'get',
url: host + map[type],
});
const $ = cheerio.load(response.data);
const list = $('#news_list > tbody > tr > td > table > tbody > tr:nth-child(1)').slice(0, 10).get();
const list = $('#news_list ul.news_ul > li').slice(0, 10).get();
const process = await Promise.all(
list.map(async (item) => {
const a = $(item).find('td:nth-child(2) > a:nth-child(2)');
const t = $(item).find('td:nth-child(3)');
const a = $(item).find('a');
const t = $(item).find('span');
const itemUrl = a.attr('href');
const single = {
title: a.text(),
link: url.resolve(host, itemUrl),
guid: url.resolve(host, itemUrl),
pubDate: t.text().slice(0, 10),
pubDate: formatPubDate(t.text()),
};
const other = await load(itemUrl, ctx, gotConfig);
const other = await load(itemUrl, ctx);
return Promise.resolve(Object.assign({}, single, other));
})
);