From 4302fee3fefb5c3fb81f2138dec19118af824286 Mon Sep 17 00:00:00 2001 From: uduki Date: Fri, 6 Nov 2026 22:44:25 +0900 Subject: [PATCH] =?UTF-8?q?fix(youtube):=20=E6=97=A2=E5=AD=98=E3=83=95?= =?UTF-8?q?=E3=82=A3=E3=83=BC=E3=83=89=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7?= =?UTF-8?q?=E3=83=A5=E5=BC=95=E3=81=8D=E7=B6=99=E3=81=8E=E3=81=AB=E3=82=88?= =?UTF-8?q?=E3=82=8B=E5=86=8D=E7=94=9F=E5=9B=9E=E6=95=B0=E5=A4=89=E5=8B=95?= =?UTF-8?q?=E3=81=AB=E4=BC=B4=E3=81=86FreshRSS=E6=9C=AA=E8=AA=AD=E5=8C=96?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Class/YouTube/YouTubeFeedManager.cs | 100 ++++++++++++++++-- 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/rss-generator/Class/YouTube/YouTubeFeedManager.cs b/rss-generator/Class/YouTube/YouTubeFeedManager.cs index 186c8c7..900a138 100644 --- a/rss-generator/Class/YouTube/YouTubeFeedManager.cs +++ b/rss-generator/Class/YouTube/YouTubeFeedManager.cs @@ -342,30 +342,91 @@ public static class YouTubeFeedManager _ => "フィード" }; - // 公開日時の新しい順にソートし、重複を除外 - var distinctItems = items + // 既存フィードを読み込み、過去の公開日時・本文HTML・タイトルを引き継げるようにする + var existingDict = LoadExistingFeedItems(fileName, YouTubeConfig.OutputSubDir); + + // 重複を除外 + var distinctRawItems = items .GroupBy(i => i.VideoId) .Select(g => g.First()) - .OrderByDescending(i => i.PublishedDate) + .ToList(); + + var syndicationItems = new List(); + foreach (var video in distinctRawItems) + { + string guid = CmnUtil.CreateGuid(video.Url); + existingDict.TryGetValue(guid, out var existingItem); + if (existingItem == null && !string.IsNullOrWhiteSpace(video.Url)) + { + existingDict.TryGetValue(video.Url, out existingItem); + } + + var synItem = CreateSyndicationItem(video, existingItem, feedType); + syndicationItems.Add(synItem); + } + + // 公開日時の新しい順にソートし、最大件数に制限 + var finalItems = syndicationItems + .OrderByDescending(i => i.PublishDate) .Take(YouTubeConfig.MaxItemsPerFeed * 3) - .Select(CreateSyndicationItem) .ToList(); var feed = new SyndicationFeed( $"{feedTitle} ({typeLabel})", $"登録チャンネル全件の {typeLabel} 統合RSSフィード", new Uri("https://www.youtube.com"), - distinctItems + finalItems ); - // YouTube は画像リンクを Base64 変換せず、通常の画像リンクとして出力する - await CmnUtil.ExportXMLFileAsync(fileName, feed, client, YouTubeConfig.OutputSubDir, embedBase64: false); + // YouTube は画像リンクを Base64 変換せず、通常の画像リンクとして出力する(filter24Hours: false で全件維持) + await CmnUtil.ExportXMLFileAsync(fileName, feed, client, YouTubeConfig.OutputSubDir, embedBase64: false, filter24Hours: false); + } + + /// + /// 既存のXMLファイルから SyndicationItem を読み込み、GUIDおよびURLをキーとする辞書を返します。 + /// + private static Dictionary LoadExistingFeedItems(string fileName, string subDir) + { + var dict = new Dictionary(StringComparer.OrdinalIgnoreCase); + var filePath = CmnUtil.GetExportXmlPath(fileName, subDir); + if (!System.IO.File.Exists(filePath)) return dict; + + try + { + using var reader = System.Xml.XmlReader.Create(filePath); + var feed = SyndicationFeed.Load(reader); + if (feed?.Items != null) + { + foreach (var item in feed.Items) + { + if (!string.IsNullOrWhiteSpace(item.Id) && !dict.ContainsKey(item.Id)) + { + dict[item.Id] = item; + } + var link = item.Links.FirstOrDefault()?.Uri?.ToString(); + if (!string.IsNullOrWhiteSpace(link) && !dict.ContainsKey(link)) + { + dict[link] = item; + } + } + } + } + catch (Exception ex) + { + LogManager.AddLog($"[警告] 【YouTube】 既存フィード読み込み失敗 ({fileName}): {ex.Message}"); + } + return dict; } /// /// YouTubeVideoItemからSyndicationItemを生成します。 + /// 既存アイテムが存在する場合は、前回のPublishDateおよびSummaryHtmlを最優先で維持し、 + /// 再生回数の増加や「○時間前」の更新によるFreshRSSの未読化を防ぎます。 /// - private static SyndicationItem CreateSyndicationItem(YouTubeVideoItem video) + private static SyndicationItem CreateSyndicationItem( + YouTubeVideoItem video, + SyndicationItem? existingItem = null, + YouTubeFeedType feedType = YouTubeFeedType.Video) { // フィードタイトルの先頭にチャンネル・ユーザー名を付与(例: 【白上フブキ】 動画タイトル) string channelName = !string.IsNullOrWhiteSpace(video.ChannelTitle) ? video.ChannelTitle.Trim() : ""; @@ -379,12 +440,21 @@ public static class YouTubeFeedManager } } + // 既存アイテムが存在する場合、ライブ配信以外は前回の公開日時を完全に維持 + var pubDate = (existingItem != null && existingItem.PublishDate != DateTimeOffset.MinValue && feedType != YouTubeFeedType.Live) + ? existingItem.PublishDate + : video.PublishedDate; + + var lastUpdate = (existingItem != null && existingItem.LastUpdatedTime != DateTimeOffset.MinValue && feedType != YouTubeFeedType.Live) + ? existingItem.LastUpdatedTime + : pubDate; + var item = new SyndicationItem { Title = new TextSyndicationContent(displayTitle), Id = CmnUtil.CreateGuid(video.Url), - PublishDate = video.PublishedDate, - LastUpdatedTime = video.PublishedDate + PublishDate = pubDate, + LastUpdatedTime = lastUpdate }; item.Links.Add(new SyndicationLink(new Uri(video.Url))); @@ -396,7 +466,15 @@ public static class YouTubeFeedManager item.Categories.Add(new SyndicationCategory(video.FeedType.ToString())); - // HTML形式のSummaryを生成 + // 既存エントリがあり、かつライブ配信でない場合は、前回のSummary(本文HTML)をそのまま引き継ぐ + // (再生回数や相対日時の変化による本文ハッシュ変動を100%防止) + if (existingItem?.Summary != null && !string.IsNullOrWhiteSpace(existingItem.Summary.Text) && feedType != YouTubeFeedType.Live) + { + item.Summary = new TextSyndicationContent(existingItem.Summary.Text, TextSyndicationContentKind.Html); + return item; + } + + // 新規アイテムまたはライブ配信の場合の本文HTML生成 var body = new StringBuilder(); if (!string.IsNullOrWhiteSpace(video.ThumbnailUrl))