Files
rss-generator/rss-generator/Class/YouTube/YouTubeFeedManager.cs
T

559 lines
23 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using rss_generator.Class.Common;
namespace rss_generator.Class.YouTube;
/// <summary>
/// YouTubeのライブ配信・通常動画・ショート動画を分離してRSS(XML)を生成・出力するマネージャークラス
/// </summary>
public static class YouTubeFeedManager
{
/// <summary>
/// YouTubeの各チャンネルからフィードを生成してXMLファイルに出力します。
/// </summary>
/// <param name="client">HttpClient インスタンス</param>
public static async Task CreateRSSAsync(HttpClient client)
{
LogManager.AddLog("[情報] 【YouTube】 YouTubeフィード生成を開始します");
try
{
// 外部ファイル(C:\feeds\src\youtube.json)から最新のチャンネル設定を読み込み
await YouTubeConfig.LoadAsync();
var allLiveItems = new List<YouTubeVideoItem>();
var allVideoItems = new List<YouTubeVideoItem>();
var allShortsItems = new List<YouTubeVideoItem>();
// メモリ消費とレート制限を抑えるため、同時並行数を2に制御
using var semaphore = new System.Threading.SemaphoreSlim(2);
var channelTasks = YouTubeConfig.Channels.Select(async channel =>
{
await semaphore.WaitAsync();
try
{
await ProcessChannelAsync(client, channel, allLiveItems, allVideoItems, allShortsItems);
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] 【YouTube】 チャンネル処理失敗 ({channel.Name ?? channel.IdOrHandle}): {ex.Message}");
}
finally
{
semaphore.Release();
}
});
await Task.WhenAll(channelTasks);
// 登録されたカテゴリ一覧を取得してカテゴリ別にフィードを出力
var categories = YouTubeConfig.Channels
.Select(c => string.IsNullOrWhiteSpace(c.Category) ? "その他" : c.Category.Trim())
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
foreach (var category in categories)
{
try
{
var catLive = allLiveItems.Where(i => string.Equals(i.Category, category, StringComparison.OrdinalIgnoreCase)).ToList();
var catVideo = allVideoItems.Where(i => string.Equals(i.Category, category, StringComparison.OrdinalIgnoreCase)).ToList();
var catShorts = allShortsItems.Where(i => string.Equals(i.Category, category, StringComparison.OrdinalIgnoreCase)).ToList();
string slug = GetCategoryFileSlug(category);
string liveFile = $"{slug}_live.xml";
string videoFile = $"{slug}_video.xml";
string shortsFile = $"{slug}_shorts.xml";
string catTitleSuffix = $" - {category}";
await ExportMergedFeedAsync(client, catLive, YouTubeFeedType.Live, liveFile, $"YouTube Live{catTitleSuffix}");
await ExportMergedFeedAsync(client, catVideo, YouTubeFeedType.Video, videoFile, $"YouTube Videos{catTitleSuffix}");
await ExportMergedFeedAsync(client, catShorts, YouTubeFeedType.Shorts, shortsFile, $"YouTube Shorts{catTitleSuffix}");
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] 【YouTube】 カテゴリ出力失敗 ({category}): {ex.Message}");
}
}
LogManager.AddLog("[情報] 【YouTube】 YouTubeフィード生成が完了しました");
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] 【YouTube】 YouTubeFeedManager.CreateRSSAsync: {ex.Message}");
CmnUtil.PrintCallStackToEvent(ex);
}
}
/// <summary>
/// 1つのチャンネルについて各タブ(Live, Video, Shorts)を取得・処理します。
/// </summary>
private static async Task ProcessChannelAsync(
HttpClient client,
YouTubeChannelTarget channel,
List<YouTubeVideoItem> allLiveItems,
List<YouTubeVideoItem> allVideoItems,
List<YouTubeVideoItem> allShortsItems)
{
string channelDisplayName = channel.Name ?? channel.IdOrHandle;
try
{
// 3つのタブを並行取得
var liveTask = FetchTabItemsAsync(client, channel, YouTubeFeedType.Live);
var videoTask = FetchTabItemsAsync(client, channel, YouTubeFeedType.Video);
var shortsTask = FetchTabItemsAsync(client, channel, YouTubeFeedType.Shorts);
// チャンネルIDが判明している場合、公式Atomフィードから正確な公開日マップを並行取得
string channelId = ExtractChannelId(channel);
Task<Dictionary<string, DateTimeOffset>> officialDatesTask = !string.IsNullOrEmpty(channelId)
? FetchOfficialPublishedDatesAsync(client, channelId)
: Task.FromResult(new Dictionary<string, DateTimeOffset>(StringComparer.OrdinalIgnoreCase));
await Task.WhenAll(liveTask, videoTask, shortsTask, officialDatesTask);
var liveItems = await liveTask;
var videoItems = await videoTask;
var shortsItems = await shortsTask;
var officialDates = await officialDatesTask;
// 公式フィードの日時(秒単位の正確な公開日)で補完・上書き
if (officialDates.Count > 0)
{
ApplyOfficialDates(liveItems, officialDates);
ApplyOfficialDates(videoItems, officialDates);
ApplyOfficialDates(shortsItems, officialDates);
}
// ショート動画で日付が取れなかったものの並び順推定(取得日時に固定されるのを防止)
AdjustShortsDates(shortsItems);
int totalCount = liveItems.Count + videoItems.Count + shortsItems.Count;
if (totalCount == 0)
{
LogManager.AddLog($"[警告] 【YouTube】 取得アイテム0件 【{channelDisplayName}】 ({channel.IdOrHandle})");
}
// 統合フィード用に蓄積
lock (allLiveItems) { allLiveItems.AddRange(liveItems); }
lock (allVideoItems) { allVideoItems.AddRange(videoItems); }
lock (allShortsItems) { allShortsItems.AddRange(shortsItems); }
}
catch (Exception ex)
{
// チャンネルごとのエラーを出力して処理を続行
LogManager.AddLog($"[エラー] 【YouTube】 チャンネル処理失敗 【{channelDisplayName}】 ({channel.IdOrHandle}): {ex.Message}");
}
}
/// <summary>
/// 指定されたタブ種別のHTMLを取得してパースします。
/// </summary>
private static async Task<List<YouTubeVideoItem>> FetchTabItemsAsync(
HttpClient client,
YouTubeChannelTarget channel,
YouTubeFeedType feedType)
{
string channelDisplayName = channel.Name ?? channel.IdOrHandle;
string url = channel.GetTabUrl(feedType);
try
{
// HttpRequestMessageを作成してリクエスト送信
using var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("Accept-Language", "ja-JP,ja;q=0.9");
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
LogManager.AddLog($"[警告] 【YouTube】 タブ取得HTTPエラー 【{channelDisplayName}】({feedType}) [{(int)response.StatusCode} {response.StatusCode}]: {url}");
return new List<YouTubeVideoItem>();
}
string html = await response.Content.ReadAsStringAsync();
var items = YouTubeParser.ParseTabContent(html, feedType, channel);
// 最大件数の制御
return items.Take(YouTubeConfig.MaxItemsPerFeed).ToList();
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] 【YouTube】 タブ取得失敗 【{channelDisplayName}】({feedType}): {ex.Message}");
return new List<YouTubeVideoItem>();
}
}
/// <summary>
/// チャンネルID(UC...)を抽出します。
/// </summary>
private static string ExtractChannelId(YouTubeChannelTarget channel)
{
string target = channel.IdOrHandle ?? "";
if (target.StartsWith("UC", StringComparison.OrdinalIgnoreCase) && target.Length >= 24)
{
return target.Substring(0, 24);
}
var match = Regex.Match(target, @"UC[a-zA-Z0-9_-]{22}");
if (match.Success)
{
return match.Value;
}
return "";
}
/// <summary>
/// YouTube公式Atomフィード(通常動画・ショート用)から videoId -> publishedDate の辞書を取得します。
/// </summary>
private static async Task<Dictionary<string, DateTimeOffset>> FetchOfficialPublishedDatesAsync(
HttpClient client,
string channelId)
{
var result = new Dictionary<string, DateTimeOffset>(StringComparer.OrdinalIgnoreCase);
// 1. チャンネル直近フィード
string channelFeedUrl = $"https://www.youtube.com/feeds/videos.xml?channel_id={channelId}";
// 2. ショート用プレイリストフィード (UUSH + channelId[2..])
string shortsFeedUrl = channelId.Length >= 24
? $"https://www.youtube.com/feeds/videos.xml?playlist_id=UUSH{channelId.Substring(2)}"
: "";
var task1 = FetchAtomDatesAsync(client, channelFeedUrl);
var task2 = !string.IsNullOrEmpty(shortsFeedUrl)
? FetchAtomDatesAsync(client, shortsFeedUrl)
: Task.FromResult(new Dictionary<string, DateTimeOffset>());
await Task.WhenAll(task1, task2);
foreach (var kvp in await task1)
{
result[kvp.Key] = kvp.Value;
}
foreach (var kvp in await task2)
{
result[kvp.Key] = kvp.Value;
}
return result;
}
/// <summary>
/// 指定されたAtomフィードURLから videoId -> publishedDate を抽出します。
/// </summary>
private static async Task<Dictionary<string, DateTimeOffset>> FetchAtomDatesAsync(HttpClient client, string url)
{
var dict = new Dictionary<string, DateTimeOffset>(StringComparer.OrdinalIgnoreCase);
try
{
using var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode) return dict;
string xml = await response.Content.ReadAsStringAsync();
var doc = XDocument.Parse(xml);
XNamespace atom = "http://www.w3.org/2005/Atom";
XNamespace yt = "http://www.youtube.com/xml/schemas/2015";
foreach (var entry in doc.Descendants(atom + "entry"))
{
string videoId = entry.Element(yt + "videoId")?.Value ?? "";
string pubStr = entry.Element(atom + "published")?.Value ?? "";
if (!string.IsNullOrWhiteSpace(videoId) &&
DateTimeOffset.TryParse(pubStr, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out var pubDate))
{
dict[videoId] = pubDate;
}
}
}
catch
{
// Atomフィード取得失敗時は無視してHTML側の日時を使用
}
return dict;
}
/// <summary>
/// 公式フィードの日時をアイテムに反映します。
/// </summary>
private static void ApplyOfficialDates(
List<YouTubeVideoItem> items,
Dictionary<string, DateTimeOffset> officialDates)
{
foreach (var item in items)
{
if (officialDates.TryGetValue(item.VideoId, out var pubDate))
{
item.PublishedDate = pubDate;
}
}
}
/// <summary>
/// ショート動画で公開日が未設定(現在日時のまま)のアイテムに対し、並び順に基づく過去日時の減算を適用します。
/// </summary>
private static void AdjustShortsDates(List<YouTubeVideoItem> shorts)
{
if (shorts == null || shorts.Count == 0) return;
// 公式日付が得られている最新アイテムの日時、または現在日時を基準にする
var now = DateTimeOffset.Now;
DateTimeOffset baseDate = shorts.FirstOrDefault(s => s.PublishedDate < now.AddMinutes(-5))?.PublishedDate ?? now.AddHours(-1);
for (int i = 0; i < shorts.Count; i++)
{
// 現在時刻(直近5分以内)のままのものは、順序に応じて過去日時に推定
if (shorts[i].PublishedDate >= now.AddMinutes(-5))
{
shorts[i].PublishedDate = baseDate.AddDays(-(i + 1));
}
else
{
baseDate = shorts[i].PublishedDate;
}
}
}
/// <summary>
/// 全チャンネル統合フィードを出力します。
/// </summary>
private static async Task ExportMergedFeedAsync(
HttpClient client,
List<YouTubeVideoItem> items,
YouTubeFeedType feedType,
string fileName,
string feedTitle)
{
string typeLabel = feedType switch
{
YouTubeFeedType.Live => "ライブ配信",
YouTubeFeedType.Video => "動画",
YouTubeFeedType.Shorts => "ショート動画",
_ => "フィード"
};
// 既存フィードを読み込み、過去の公開日時・本文HTML・タイトルを引き継げるようにする
var existingDict = LoadExistingFeedItems(fileName, YouTubeConfig.OutputSubDir);
// 重複を除外
var distinctRawItems = items
.GroupBy(i => i.VideoId)
.Select(g => g.First())
.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)
.ToList();
var feed = new SyndicationFeed(
$"{feedTitle} ({typeLabel})",
$"登録チャンネル全件の {typeLabel} 統合RSSフィード",
new Uri("https://www.youtube.com"),
finalItems
);
// 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,
SyndicationItem? existingItem = null,
YouTubeFeedType feedType = YouTubeFeedType.Video)
{
// フィードタイトルの先頭にチャンネル・ユーザー名を付与(例: 【白上フブキ】 動画タイトル)
string channelName = !string.IsNullOrWhiteSpace(video.ChannelTitle) ? video.ChannelTitle.Trim() : "";
string displayTitle = video.Title;
if (!string.IsNullOrWhiteSpace(channelName))
{
string prefix = $"【{channelName}】";
if (!displayTitle.StartsWith(prefix))
{
displayTitle = $"{prefix} {displayTitle}";
}
}
// 既存アイテムが存在する場合、ライブ配信以外は前回の公開日時を完全に維持
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 = pubDate,
LastUpdatedTime = lastUpdate
};
item.Links.Add(new SyndicationLink(new Uri(video.Url)));
if (!string.IsNullOrWhiteSpace(video.ChannelTitle))
{
item.Authors.Add(new SyndicationPerson { Name = video.ChannelTitle });
}
item.Categories.Add(new SyndicationCategory(video.FeedType.ToString()));
// 既存エントリがあり、かつライブ配信でない場合は、前回の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))
{
body.Append($@"<div><a href=""{video.Url}"" target=""_blank"" rel=""noopener noreferrer"" referrerpolicy=""no-referrer""><img src=""{video.ThumbnailUrl}"" referrerpolicy=""no-referrer"" alt=""{WebUtility.HtmlEncode(video.Title)}"" style=""max-width: 100%; height: auto; margin-bottom: 8px; border-radius: 6px;"" /></a></div>");
}
body.Append($@"<div style=""margin-bottom: 4px;""><strong>タイトル:</strong> <a href=""{video.Url}"" target=""_blank"" rel=""noopener noreferrer"" referrerpolicy=""no-referrer"">{WebUtility.HtmlEncode(video.Title)}</a></div>");
body.Append($@"<div style=""margin-bottom: 4px;""><strong>チャンネル:</strong> {WebUtility.HtmlEncode(video.ChannelTitle)}</div>");
if (!string.IsNullOrWhiteSpace(video.PublishedText))
{
body.Append($@"<div style=""margin-bottom: 4px;""><strong>ステータス/日時:</strong> {WebUtility.HtmlEncode(video.PublishedText)}</div>");
}
if (!string.IsNullOrWhiteSpace(video.ViewCountText))
{
body.Append($@"<div style=""margin-bottom: 4px;""><strong>視聴情報:</strong> {WebUtility.HtmlEncode(video.ViewCountText)}</div>");
}
item.Summary = new TextSyndicationContent(body.ToString(), TextSyndicationContentKind.Html);
return item;
}
/// <summary>
/// カテゴリ名からファイル名用の英数字スラッグを取得します。
/// (未登録のカスタムカテゴリはURLセーフな文字列としてサニタイズされます)
/// </summary>
public static string GetCategoryFileSlug(string category)
{
string norm = (category ?? "").Trim();
if (string.IsNullOrWhiteSpace(norm) || norm == "その他" || norm.Equals("default", StringComparison.OrdinalIgnoreCase) || norm.Equals("other", StringComparison.OrdinalIgnoreCase))
{
return "other"; // その他用のフィード(other_live.xml, other_video.xml, other_shorts.xml)
}
// 一般的な日本語カテゴリ名の英字マッピング
var map = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "野球", "baseball" },
{ "baseball", "baseball" },
{ "ゲーム", "game" },
{ "game", "game" },
{ "音楽", "music" },
{ "music", "music" },
{ "ニュース", "news" },
{ "news", "news" },
{ "アニメ", "anime" },
{ "anime", "anime" },
{ "vtuber", "vtuber" },
{ "料理", "cooking" },
{ "cooking", "cooking" },
{ "スポーツ", "sports" },
{ "sports", "sports" },
{ "apex", "apex" },
{ "公式", "official" },
{ "公式チャンネル", "official" },
{ "official", "official" },
{ "ホロライブ", "hololive" },
{ "hololive", "hololive" },
{ "にじさんじ", "nijisanji" },
{ "nijisanji", "nijisanji" },
{ "その他", "other" },
{ "other", "other" }
};
if (map.TryGetValue(norm, out var slug))
{
return slug;
}
// 未定義のカテゴリはサニタイズして小文字で使用
string safe = norm;
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
safe = safe.Replace(c, '_');
}
return safe.ToLowerInvariant();
}
}