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

@@ -1,6 +1,6 @@
import BLOG from '@/blog.config'
import { NotionAPI } from 'notion-client'
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
import { NotionAPI } from 'notion-client'
import { deepClone, delay } from '../utils'
/**
@@ -56,10 +56,18 @@ export async function getSingleBlock(id, from) {
*/
export async function getPageWithRetry(id, from, retryAttempts = 3) {
if (retryAttempts && retryAttempts > 0) {
console.log('[API-->>请求]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '')
console.log(
'[API-->>请求]',
`from:${from}`,
`id:${id}`,
retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : ''
)
try {
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
const api = new NotionAPI({ authToken, userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone })
const api = new NotionAPI({
authToken,
userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
})
const start = new Date().getTime()
const pageData = await api.getPage(id)
const end = new Date().getTime()
@@ -125,9 +133,13 @@ function filterPostBlocks(id, blockMap, slice) {
}
// 如果是文件或嵌入式PDF需要重新加密签名
if ((b?.value?.type === 'file' || b?.value?.type === 'pdf' || b?.value?.type === 'video' || b?.value?.type === 'audio') &&
b?.value?.properties?.source?.[0][0] &&
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
if (
(b?.value?.type === 'file' ||
b?.value?.type === 'pdf' ||
b?.value?.type === 'video' ||
b?.value?.type === 'audio') &&
b?.value?.properties?.source?.[0][0] &&
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
) {
const oldUrl = b?.value?.properties?.source?.[0][0]
const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}`

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
}
}

View File

@@ -61,9 +61,7 @@ export async function getStaticProps(req) {
// 生成robotTxt
generateRobotsTxt(props)
// 生成Feed订阅
if (JSON.parse(BLOG.ENABLE_RSS)) {
generateRss(props)
}
generateRss(props)
// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'