* feat: Enhance link resolution and content selection - Update utils/index.ts to check for non-empty redirect URLs before assignment. - Modify yjs.ts and ai.ts to select content using a higher-level ID '#vsb_content' for more comprehensive data extraction. - Ensure all relative URLs within content are transformed into absolute URLs to improve link accessibility and reliability in RSS feeds. * feat(njxzc/utils/index.ts): Enhance link resolution and handle specific access restrictions - Replace direct string manipulations with conditional checks for 'response.redirectUrls'. - Implement checks for '.wp_error_msg' to manage content access based on IP restrictions. - Remove 'replaceAll' function usage to improve handling of relative URLs in 'contentSelector.content'. - Ensure robust handling of content that can't be directly previewed due to restrictions or missing permissions. * feat(jou/utils/index): Enhance URL handling and integrate PDF viewing logic - Replace usage of 'replaceAll' with conditional checks for 'response.redirectUrls' to ensure robust URL handling. - Add logic to detect embedded PDF scripts in web content, setting descriptions appropriately when PDF content is detected. - This update aims to improve the system's ability to handle different types of content securely and effectively, particularly enhancing how PDF files are managed within the application. * feat: Replace got with ofetch in route handlers - Updated /lib/routes/gxmzu, /lib/routes/jou, and /lib/routes/njxzc to use ofetch instead of got for HTTP requests. - Adjusted redirection handling logic to align with ofetch's API and behavior. - Ensured all route handlers now properly handle redirects by checking response status codes instead of relying on redirectUrls array from got. This change enhances the reliability and maintainability of HTTP request handling across different university library routes by utilizing ofetch, which is more consistent with our current architecture and requirements.
70 lines
2.6 KiB
TypeScript
70 lines
2.6 KiB
TypeScript
import cache from '@/utils/cache';
|
|
import ofetch from '@/utils/ofetch'; // 使用ofetch库
|
|
import { load } from 'cheerio';
|
|
import { parseDate } from '@/utils/parse-date';
|
|
import timezone from '@/utils/timezone';
|
|
|
|
async function getItems(ctx, url, host, tableClass, timeStyleClass1, titleStyleClass, timeStyleClass2) {
|
|
const response = await ofetch(url).catch(() => null);
|
|
if (!response) {
|
|
return [];
|
|
}
|
|
const $ = load(response);
|
|
|
|
const list = $(`table.${tableClass} > tbody > tr[height=20]`)
|
|
.toArray()
|
|
.map((item) => {
|
|
const currentItem = $(item);
|
|
const item1 = currentItem.find('td:eq(1)');
|
|
const item2 = currentItem.find('td:eq(2)');
|
|
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) =>
|
|
cache.tryGet(item.link, async () => {
|
|
const response = await ofetch(item.link).catch(() => null);
|
|
if (!response || (response.status >= 300 && response.status < 400)) {
|
|
// 响应为空或状态码表明发生了重定向
|
|
return {
|
|
...item,
|
|
description: '该通知无法直接预览,请点击原文链接↑查看',
|
|
};
|
|
}
|
|
const $ = load(response);
|
|
|
|
item.title = $(`.${titleStyleClass}`).text();
|
|
const hasEmbeddedPDFScript = $('script:contains("showVsbpdfIframe")').length > 0;
|
|
|
|
if (hasEmbeddedPDFScript) {
|
|
item.description = '该通知无法直接预览,请点击原文链接↑查看';
|
|
} else {
|
|
const contentHtml = $('.v_news_content').html();
|
|
const $content = load(contentHtml);
|
|
$content('a').each(function () {
|
|
const a = $(this);
|
|
const href = a.attr('href');
|
|
if (href && !href.startsWith('http')) {
|
|
a.attr('href', new URL(href, host).href);
|
|
}
|
|
});
|
|
item.description = $content.html();
|
|
}
|
|
item.pubDate = timezone(parseDate($(`.${timeStyleClass2}`).text().replace('发布时间:', '')), +8);
|
|
|
|
return item;
|
|
})
|
|
)
|
|
);
|
|
|
|
return out;
|
|
}
|
|
|
|
export { getItems };
|