Skip to content

Commit b6d656f

Browse files
committed
RSS生成限制频率
1 parent fc25c92 commit b6d656f

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

lib/notion/getPostBlocks.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import BLOG from '@/blog.config'
2-
import { NotionAPI } from 'notion-client'
32
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
3+
import { NotionAPI } from 'notion-client'
44
import { deepClone, delay } from '../utils'
55

66
/**
@@ -56,10 +56,18 @@ export async function getSingleBlock(id, from) {
5656
*/
5757
export async function getPageWithRetry(id, from, retryAttempts = 3) {
5858
if (retryAttempts && retryAttempts > 0) {
59-
console.log('[API-->>请求]', `from:${from}`, `id:${id}`, retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : '')
59+
console.log(
60+
'[API-->>请求]',
61+
`from:${from}`,
62+
`id:${id}`,
63+
retryAttempts < 3 ? `剩余重试次数:${retryAttempts}` : ''
64+
)
6065
try {
6166
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
62-
const api = new NotionAPI({ authToken, userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone })
67+
const api = new NotionAPI({
68+
authToken,
69+
userTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
70+
})
6371
const start = new Date().getTime()
6472
const pageData = await api.getPage(id)
6573
const end = new Date().getTime()
@@ -125,9 +133,13 @@ function filterPostBlocks(id, blockMap, slice) {
125133
}
126134

127135
// 如果是文件,或嵌入式PDF,需要重新加密签名
128-
if ((b?.value?.type === 'file' || b?.value?.type === 'pdf' || b?.value?.type === 'video' || b?.value?.type === 'audio') &&
129-
b?.value?.properties?.source?.[0][0] &&
130-
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
136+
if (
137+
(b?.value?.type === 'file' ||
138+
b?.value?.type === 'pdf' ||
139+
b?.value?.type === 'video' ||
140+
b?.value?.type === 'audio') &&
141+
b?.value?.properties?.source?.[0][0] &&
142+
b?.value?.properties?.source?.[0][0].indexOf('amazonaws.com') > 0
131143
) {
132144
const oldUrl = b?.value?.properties?.source?.[0][0]
133145
const newUrl = `https://notion.so/signed/${encodeURIComponent(oldUrl)}?table=block&id=${b?.value?.id}`

lib/rss.js

+30
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const createFeedContent = async post => {
2525
}
2626
}
2727

28+
/**
29+
* 生成RSS数据
30+
* @param {*} props
31+
*/
2832
export async function generateRss(props) {
2933
const { NOTION_CONFIG, siteInfo, latestPosts } = props
3034
const TITLE = siteInfo?.title
@@ -34,6 +38,13 @@ export async function generateRss(props) {
3438
const LANG = NOTION_CONFIG?.LANG || BLOG.LANG
3539
const SUB_PATH = NOTION_CONFIG?.SUB_PATH || BLOG.SUB_PATH
3640
const CONTACT_EMAIL = NOTION_CONFIG?.CONTACT_EMAIL || BLOG.CONTACT_EMAIL
41+
42+
// 检查 feed 文件是否在30分钟内更新过
43+
if (isFeedRecentlyUpdated('./public/rss/feed.xml', 60)) {
44+
return
45+
}
46+
47+
console.log('[RSS订阅] 生成/rss/feed.xml')
3748
const year = new Date().getFullYear()
3849
const feed = new Feed({
3950
title: TITLE,
@@ -69,3 +80,22 @@ export async function generateRss(props) {
6980
// RSS被高频词访问将大量消耗服务端资源,故作为静态文件
7081
}
7182
}
83+
84+
/**
85+
* 检查上次更新,如果60分钟内更新过就不操作。
86+
* @param {*} filePath
87+
* @param {*} intervalMinutes
88+
* @returns
89+
*/
90+
function isFeedRecentlyUpdated(filePath, intervalMinutes = 60) {
91+
try {
92+
const stats = fs.statSync(filePath)
93+
const now = new Date()
94+
const lastModified = new Date(stats.mtime)
95+
const timeDifference = (now - lastModified) / (1000 * 60) // 转换为分钟
96+
return timeDifference < intervalMinutes
97+
} catch (error) {
98+
// 如果文件不存在,我们需要创建它
99+
return false
100+
}
101+
}

pages/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ export async function getStaticProps(req) {
6161
// 生成robotTxt
6262
generateRobotsTxt(props)
6363
// 生成Feed订阅
64-
if (JSON.parse(BLOG.ENABLE_RSS)) {
65-
generateRss(props)
66-
}
64+
generateRss(props)
6765

6866
// 生成全文索引 - 仅在 yarn build 时执行 && process.env.npm_lifecycle_event === 'build'
6967

0 commit comments

Comments
 (0)