Files
RSSHub/lib/v2/jou/utils/index.js
T
Jaya 322dd10a43 feat(routes): add 南京晓庄学院图书馆通知公告、江苏海洋大学通知公告、江苏海洋大学研招网通知公告 (#12176)
* feat: add jou yztzgg

* feat: add njxzc lib tzgg

* feat: add jou index and yz && njxzc lib

* docs: update university.md

* fix: fix typos in jou and njxzc

* docs: remove the  extra backslash

* fix: fix university.md

* refactor: Extract common getItems function into utils/index.js

* fix: fix typos in njxzc/radar.js

* refactor: filter the tr items with css selector

* fix: fix invalid date

* style: format jou utils

* fix: add timeStyleClass2 in jou yz
2023-03-27 20:11:01 +08:00

61 lines
2.5 KiB
JavaScript

// 导入got库,该库用来请求网页数据
const got = require('@/utils/got');
// 导入cheerio库,该库用来解析网页数据
const cheerio = require('cheerio');
// 导入parseDate函数,该函数用于日期处理
const { parseDate } = require('@/utils/parse-date');
// 导入timezone库,该库用于时区处理
const timezone = require('@/utils/timezone');
async function getItems(ctx, url, host, tableClass, timeStyleClass1, titleStyleClass, timeStyleClass2) {
// 发起Http请求,获取网页数据
const response = await got({ url, https: { rejectUnauthorized: false } });
// 解析网页数据
const $ = cheerio.load(response.data);
// 通知公告的items的标题、url链接、发布日期
const list = $(`table.${tableClass} > tbody > tr[height=20]`)
.toArray()
.map((item) => {
const currentItem = $(item); // 获取当前tr元素的jQuery对象
const item1 = currentItem.find('td:eq(1)'); // 获取当前tr元素下的第二个td
const item2 = currentItem.find('td:eq(2)'); // 获取当前tr元素下的第三个td
const link = new URL(item1.find('a').attr('href'), host).href;
return {
title: item1.find('a').attr('title'),
link,
pubDate: timezone(parseDate(item2.find(`.${timeStyleClass1}`).text(), 'YYYY-MM-DD'), +8),
};
});
const out = await Promise.all(
list.map((item) =>
// 使用缓存
ctx.cache.tryGet(item.link, async () => {
const response = await got({ url: item.link, https: { rejectUnauthorized: false } });
if (response.redirectUrls.length) {
item.link = response.redirectUrls[0];
item.description = '该通知无法直接预览,请点击原文链接↑查看';
} else {
const $ = cheerio.load(response.data);
item.title = $(`.${titleStyleClass}`).text();
item.description = $('.v_news_content')
.html()
.replace(/src="\//g, `src="${new URL('.', host).href}`)
.replace(/href="\//g, `href="${new URL('.', host).href}`)
.trim();
item.pubDate = timezone(parseDate($(`.${timeStyleClass2}`).text().replace('发布时间:', '')), +8);
}
return item;
})
)
);
return out;
}
module.exports = {
getItems,
};