fix(youtube): 既存フィードキャッシュ引き継ぎによる再生回数変動に伴うFreshRSS未読化防止対応
This commit is contained in:
1 parent
70b5bfa088
commit
4302fee3fe
1 file changed
+89
-11
@@ -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<SyndicationItem>();
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 既存のXMLファイルから SyndicationItem を読み込み、GUIDおよびURLをキーとする辞書を返します。
|
||||
/// </summary>
|
||||
private static Dictionary<string, SyndicationItem> LoadExistingFeedItems(string fileName, string subDir)
|
||||
{
|
||||
var dict = new Dictionary<string, SyndicationItem>(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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// YouTubeVideoItemからSyndicationItemを生成します。
|
||||
/// 既存アイテムが存在する場合は、前回のPublishDateおよびSummaryHtmlを最優先で維持し、
|
||||
/// 再生回数の増加や「○時間前」の更新によるFreshRSSの未読化を防ぎます。
|
||||
/// </summary>
|
||||
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))
|
||||
|
||||
Reference in new issue
Block a user