From b6d656f05b608580b7366b1fc1578d0411ffa662 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Wed, 15 May 2024 15:52:04 +0800 Subject: [PATCH] =?UTF-8?q?RSS=E7=94=9F=E6=88=90=E9=99=90=E5=88=B6?= =?UTF-8?q?=E9=A2=91=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/notion/getPostBlocks.js | 24 ++++++++++++++++++------ lib/rss.js | 30 ++++++++++++++++++++++++++++++ pages/index.js | 4 +--- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/lib/notion/getPostBlocks.js b/lib/notion/getPostBlocks.js index c9652370fc2..2c53a1faa1d 100644 --- a/lib/notion/getPostBlocks.js +++ b/lib/notion/getPostBlocks.js @@ -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}` diff --git a/lib/rss.js b/lib/rss.js index fd183b6d58c..534597c761b 100644 --- a/lib/rss.js +++ b/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 + } +} diff --git a/pages/index.js b/pages/index.js index f5bade7f016..b925bc0e93b 100644 --- a/pages/index.js +++ b/pages/index.js @@ -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'