mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-29 15:10:06 +00:00
RSS生成限制频率
This commit is contained in:
30
lib/rss.js
30
lib/rss.js
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user