diff --git a/services/podcast.ts b/services/podcast.ts
index 01eca89..80a87d4 100644
--- a/services/podcast.ts
+++ b/services/podcast.ts
@@ -43,20 +43,72 @@ export async function updatePodcastRSS() {
`;
}
- const rssXml = `
-
-
- ${channelTitle}
- ${channelLink}
- ${channelDescription}]]>
- ${lastBuildDate}
- ${itemsXml}
-
-
- `;
-
const outputPath = join(__dirname, "../public/podcast.xml");
- // Ensure directory exists
- await fs.mkdir(dirname(outputPath), { recursive: true });
- await fs.writeFile(outputPath, rssXml.trim());
+
+ // 既存のRSSファイルの読み込み
+ let existingXml = "";
+ try {
+ existingXml = await fs.readFile(outputPath, "utf-8");
+ } catch (err) {
+ // ファイルが存在しない場合は新規作成
+ console.log("既存のpodcast.xmlが見つかりません。新規作成します。");
+ }
+
+ if (existingXml) {
+ // 既存のitem部分を抽出
+ const existingItemsMatch = existingXml.match(/([\s\S]*?)<\/channel>/);
+ if (existingItemsMatch) {
+ const existingItems = existingItemsMatch[1];
+ const newItemStartIndex = existingItems.lastIndexOf("- ");
+
+ // 新しいitemを追加
+ const updatedItems = existingItems + itemsXml;
+
+ // lastBuildDateを更新
+ const updatedXml = existingXml.replace(
+ /.*?<\/lastBuildDate>/,
+ `${lastBuildDate}`
+ );
+
+ // items部分を置き換え
+ const finalXml = updatedXml.replace(
+ /[\s\S]*?<\/channel>/,
+ `${updatedItems}`
+ );
+
+ // ファイルに書き込み
+ await fs.writeFile(outputPath, finalXml.trim());
+ } else {
+ // 不正なフォーマットの場合は新規作成
+ const rssXml = `
+
+
+ ${channelTitle}
+ ${channelLink}
+ ${channelDescription}]]>
+ ${lastBuildDate}
+ ${itemsXml}
+
+
+ `;
+ await fs.writeFile(outputPath, rssXml.trim());
+ }
+ } else {
+ // 新規作成
+ const rssXml = `
+
+
+ ${channelTitle}
+ ${channelLink}
+ ${channelDescription}]]>
+ ${lastBuildDate}
+ ${itemsXml}
+
+
+ `;
+
+ // Ensure directory exists
+ await fs.mkdir(dirname(outputPath), { recursive: true });
+ await fs.writeFile(outputPath, rssXml.trim());
+ }
}