Files
RSSHub/lib/v2/pku/eecs.js
DylanXie123 10acacfa46 feat(route): add 北京大学学生就业指导服务中心 (#7904)
* feat(route): add 北京大学学生就业指导服务中心

* fix(route): get content from sript tag

request content in script tag to get full text

* fix(doc): fix parameter description

* fix(route): fix header and selector

* add test request and wait

* docs: fix warning

* refactor: migrate to v2

* fix: eecs

refactor: legacy url.resolve

refactor: use parseDate from utils

* docs: fix recruit path

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
2022-04-24 23:14:17 +08:00

65 lines
2.0 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { eecsMap } = require('./utils');
module.exports = async (ctx) => {
const host = 'https://eecs.pku.edu.cn';
let type = ctx.params && parseInt(ctx.params.type);
if (type === undefined) {
type = 0;
}
const response = await got(host + '/xygk1/ggtz/' + eecsMap.get(type));
const $ = cheerio.load(response.data);
let items = $('.hvr-shutter-out-vertical')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.attr('title'),
link: new URL(item.attr('href'), host).href,
pubDate: parseDate(item.find('em').text()),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detail = await got(item.link);
const content = cheerio.load(detail.data);
content('input').remove();
content('h1').remove();
content('.con_xq').remove();
content('form[name=_newscontent_fromname] img').each((_, i) => {
i = $(i);
if (i.attr('src').startsWith('/')) {
i.attr('src', new URL(i.attr('src'), host).href);
}
});
content('form[name=_newscontent_fromname] ul li a').each((_, a) => {
a = $(a);
if (a.attr('href').startsWith('/')) {
a.attr('href', new URL(a.attr('href'), host).href);
}
});
item.description = content('form[name=_newscontent_fromname]').html();
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: host + '/xygk1/ggtz/' + eecsMap.get(type),
description: '北大信科 公告通知',
item: items,
};
};