RSS生成限制频率

This commit is contained in:
tangly1024.com
2024-05-15 15:52:04 +08:00
parent fc25c9220e
commit b6d656f05b
3 changed files with 49 additions and 9 deletions

View File

@@ -25,6 +25,10 @@ const createFeedContent = async post => {
}
}
/**
* 生成RSS数据
* @param {*} props
*/
export async function generateRss(props) {
const { NOTION_CONFIG, siteInfo, latestPosts } = props
const TITLE = siteInfo?.title
@@ -34,6 +38,13 @@ export async function generateRss(props) {
const LANG = NOTION_CONFIG?.LANG || BLOG.LANG
const SUB_PATH = NOTION_CONFIG?.SUB_PATH || BLOG.SUB_PATH
const CONTACT_EMAIL = NOTION_CONFIG?.CONTACT_EMAIL || BLOG.CONTACT_EMAIL
// 检查 feed 文件是否在30分钟内更新过
if (isFeedRecentlyUpdated('./public/rss/feed.xml', 60)) {
return
}
console.log('[RSS订阅] 生成/rss/feed.xml')
const year = new Date().getFullYear()
const feed = new Feed({
title: TITLE,
@@ -69,3 +80,22 @@ export async function generateRss(props) {
// RSS被高频词访问将大量消耗服务端资源故作为静态文件
}
}
/**
* 检查上次更新如果60分钟内更新过就不操作。
* @param {*} filePath
* @param {*} intervalMinutes
* @returns
*/
function isFeedRecentlyUpdated(filePath, intervalMinutes = 60) {
try {
const stats = fs.statSync(filePath)
const now = new Date()
const lastModified = new Date(stats.mtime)
const timeDifference = (now - lastModified) / (1000 * 60) // 转换为分钟
return timeDifference < intervalMinutes
} catch (error) {
// 如果文件不存在,我们需要创建它
return false
}
}