From e91f2ffdb41ea81cf0d26757a82b1c90367372f5 Mon Sep 17 00:00:00 2001 From: uduki Date: Thu, 11 Jun 2026 09:28:19 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AB=E3=83=86=E3=82=B4=E3=83=AA=E3=82=92?= =?UTF-8?q?=E3=81=8D=E3=82=8C=E3=81=84=E3=81=AB=E5=87=BA=E5=8A=9B=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RSSAPI/Program.cs | 134 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/RSSAPI/Program.cs b/RSSAPI/Program.cs index deb916c..e49a5e9 100644 --- a/RSSAPI/Program.cs +++ b/RSSAPI/Program.cs @@ -386,6 +386,67 @@ app.MapGet("/momonga/fanzine", async (HttpContext context, IHttpClientFactory ht // 元のRSSが重い画像や長文を含んでいても、ここでシンプルな説明文に上書きして削ぎ落とす // var simpleContent = SyndicationContent.CreatePlaintextContent("記事の詳細はリンク先で確認してください。"); + List getAddCategory() + { + List result = new List(); + + string orgText = originalItem.Summary.Text; + var parser = new HtmlParser(); + var document = parser.ParseDocument(orgText); + + var firstP = document.QuerySelector("p"); + if (firstP != null) + { + var targetText = firstP.TextContent; + int tagIndex = targetText.IndexOf("【タグ】"); + if (tagIndex >= 0) + { + // 【タグ】が見つかった場合、そこから前の文字列だけを残す + targetText = targetText.Substring(0, tagIndex); + } + // ---------------------------------------------------- + // 正規表現で各項目の値を抽出する + // ---------------------------------------------------- + // パターン解説: + // 【[^】]+】 : 「【」で始まり、「】以外の文字」が続き、「】」で終わる文字列 (例: 【パロディ】) + // ([^【]*) : その後、「【」以外の文字が続く限り取得する (Group1としてキャプチャ) + string pattern = @"【[^】]+】([^【]*)"; + var matches = Regex.Matches(targetText, pattern); + + foreach (Match match in matches) + { + // match.Groups[1].Value には "パロ1, パロ 2 " などの値が入ります + // (項目の中身が空の場合は空文字になります) + string valuesStr = match.Groups[1].Value; + + if (string.IsNullOrWhiteSpace(valuesStr)) + { + continue; // 中身が空の場合はスキップ + } + + // ---------------------------------------------------- + // 3. カンマで分割し、前後の空白を除去してカテゴリに追加 + // ---------------------------------------------------- + // ※もし全角カンマ「、」も考慮したい場合は new[] { ',', '、' } と記述します + string[] splitValues = valuesStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (var val in splitValues) + { + // 前後の半角・全角スペースを取り除く + string categoryName = val.Trim(); + + // 空でなければカテゴリとして追加 + if (!string.IsNullOrEmpty(categoryName)) + { + // domain属性なし(前回回答の手法)でカテゴリを追加 + result.Add(new SyndicationCategory(categoryName)); + } + } + } + } + return result; + } + var newItem = new SyndicationItem() { // タイトルは TextSyndicationContent でラップする必要があります @@ -405,9 +466,15 @@ app.MapGet("/momonga/fanzine", async (HttpContext context, IHttpClientFactory ht { newItem.Links.Add(new SyndicationLink(link)); } + // カテゴリに本文内の作者などの情報を追加する + foreach (var category in getAddCategory()) + { + newItem.Categories.Add(category); + } // 【追加】元のRSSが持っていたカテゴリ(タグ)をそのまま引き継ぐ foreach (var category in originalItem.Categories) { + category.Scheme = null; newItem.Categories.Add(category); } foreach (var author in originalItem.Authors) @@ -505,6 +572,67 @@ app.MapGet("/momonga/magazine", async (HttpContext context, IHttpClientFactory h // 元のRSSが重い画像や長文を含んでいても、ここでシンプルな説明文に上書きして削ぎ落とす // var simpleContent = SyndicationContent.CreatePlaintextContent("記事の詳細はリンク先で確認してください。"); + List getAddCategory() + { + List result = new List(); + + string orgText = originalItem.Summary.Text; + var parser = new HtmlParser(); + var document = parser.ParseDocument(orgText); + + var firstP = document.QuerySelector("p"); + if (firstP != null) + { + var targetText = firstP.TextContent; + int tagIndex = targetText.IndexOf("【タグ】"); + if (tagIndex >= 0) + { + // 【タグ】が見つかった場合、そこから前の文字列だけを残す + targetText = targetText.Substring(0, tagIndex); + } + // ---------------------------------------------------- + // 正規表現で各項目の値を抽出する + // ---------------------------------------------------- + // パターン解説: + // 【[^】]+】 : 「【」で始まり、「】以外の文字」が続き、「】」で終わる文字列 (例: 【パロディ】) + // ([^【]*) : その後、「【」以外の文字が続く限り取得する (Group1としてキャプチャ) + string pattern = @"【[^】]+】([^【]*)"; + var matches = Regex.Matches(targetText, pattern); + + foreach (Match match in matches) + { + // match.Groups[1].Value には "パロ1, パロ 2 " などの値が入ります + // (項目の中身が空の場合は空文字になります) + string valuesStr = match.Groups[1].Value; + + if (string.IsNullOrWhiteSpace(valuesStr)) + { + continue; // 中身が空の場合はスキップ + } + + // ---------------------------------------------------- + // 3. カンマで分割し、前後の空白を除去してカテゴリに追加 + // ---------------------------------------------------- + // ※もし全角カンマ「、」も考慮したい場合は new[] { ',', '、' } と記述します + string[] splitValues = valuesStr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + + foreach (var val in splitValues) + { + // 前後の半角・全角スペースを取り除く + string categoryName = val.Trim(); + + // 空でなければカテゴリとして追加 + if (!string.IsNullOrEmpty(categoryName)) + { + // domain属性なし(前回回答の手法)でカテゴリを追加 + result.Add(new SyndicationCategory(categoryName)); + } + } + } + } + return result; + } + var newItem = new SyndicationItem() { // タイトルは TextSyndicationContent でラップする必要があります @@ -524,9 +652,15 @@ app.MapGet("/momonga/magazine", async (HttpContext context, IHttpClientFactory h { newItem.Links.Add(new SyndicationLink(link)); } + // カテゴリに本文内の作者などの情報を追加する + foreach (var category in getAddCategory()) + { + newItem.Categories.Add(category); + } // 【追加】元のRSSが持っていたカテゴリ(タグ)をそのまま引き継ぐ foreach (var category in originalItem.Categories) { + category.Scheme = null; newItem.Categories.Add(category); } foreach (var author in originalItem.Authors)