Files
RSSHub/lib/v2/zju/list.js
尹傲雄 1e11388c90 fix(route): update zju/grs (#9551)
* update zju/grs in university

* refactor: migrate to v2

* fix: add radar

* fix: not working routes

* fix: add migration note

* docs: remove N/A types from physics table

* fix(radar): migrate old rules
2022-04-18 02:04:53 +08:00

67 lines
1.9 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const host = 'https://www.zju.edu.cn/';
module.exports = async (ctx) => {
const type = ctx.params.type ?? 'xs';
const link = host + type + `/list.htm`;
const response = await got({
method: 'get',
url: link,
headers: {
Referer: host,
},
});
const $ = cheerio.load(response.data);
function sortUrl(e) {
if (e.search('redirect') !== -1) {
return link;
} else {
return e;
}
}
const list = $('#wp_news_w7 ul.news li')
.map(function () {
const info = {
title: $(this).find('a').attr('title'),
link: sortUrl($(this).find('a').attr('href')),
date: $(this)
.text()
.match(/\d{4}-\d{2}-\d{2}/)[0],
};
return info;
})
.get();
const out = await Promise.all(
list.map((info) => {
const title = info.title;
const date = info.date;
const itemUrl = new URL(info.link, host).href;
return ctx.cache.tryGet(itemUrl, async () => {
const response = await got({
method: 'get',
url: itemUrl,
headers: {
Referer: link,
},
});
const $ = cheerio.load(response.data);
const description = $('.right_content').html();
return {
title,
link: itemUrl,
description,
pubDate: parseDate(date),
};
});
})
);
ctx.state.data = {
title: `浙江大学` + $('ul.submenu .selected').text(),
link,
item: out,
};
};