Skip to content

Commit

Permalink
RSS生成限制频率
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed May 15, 2024
1 parent fc25c92 commit b6d656f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
24 changes: 18 additions & 6 deletions lib/notion/getPostBlocks.js
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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}`
Expand Down
30 changes: 30 additions & 0 deletions lib/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
}
4 changes: 1 addition & 3 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit b6d656f

Please sign in to comment.