Files
rss-generator/rss-generator/Class/RSS/YouTube/YouTubeFeedManager.cs
T
2026-09-03 19:59:23 +09:00

475 lines
19 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;
namespace rss_generator.Class.RSS.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フィード生成を開始します");
try
{
var allLiveItems = new List<YouTubeVideoItem>();
var allVideoItems = new List<YouTubeVideoItem>();
var allShortsItems = new List<YouTubeVideoItem>();
// YouTubeからのレート制限を防ぐため、同時並行数を安全な値(6)に制御
using var semaphore = new System.Threading.SemaphoreSlim(6);
var channelTasks = YouTubeConfig.Channels.Select(async channel =>
{
await semaphore.WaitAsync();
try
{
await ProcessChannelAsync(client, channel, allLiveItems, allVideoItems, allShortsItems);
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] チャンネル処理失敗 ({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}";
ExportMergedFeed(catLive, YouTubeFeedType.Live, liveFile, $"YouTube Live{catTitleSuffix}");
ExportMergedFeed(catVideo, YouTubeFeedType.Video, videoFile, $"YouTube Videos{catTitleSuffix}");
ExportMergedFeed(catShorts, YouTubeFeedType.Shorts, shortsFile, $"YouTube Shorts{catTitleSuffix}");
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] カテゴリ出力失敗 ({category}): {ex.Message}");
}
}
LogManager.AddLog("[情報] YouTubeフィード生成が完了しました");
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] 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($"[警告] 取得アイテム0件 【{channelDisplayName}】 ({channel.IdOrHandle})");
}
// 統合フィード用に蓄積
lock (allLiveItems) { allLiveItems.AddRange(liveItems); }
lock (allVideoItems) { allVideoItems.AddRange(videoItems); }
lock (allShortsItems) { allShortsItems.AddRange(shortsItems); }
}
catch (Exception ex)
{
// チャンネルごとのエラーを出力して処理を続行
LogManager.AddLog($"[エラー] チャンネル処理失敗 【{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($"[警告] タブ取得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($"[エラー] タブ取得失敗 【{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 void ExportMergedFeed(
List<YouTubeVideoItem> items,
YouTubeFeedType feedType,
string fileName,
string feedTitle)
{
string typeLabel = feedType switch
{
YouTubeFeedType.Live => "ライブ配信",
YouTubeFeedType.Video => "動画",
YouTubeFeedType.Shorts => "ショート動画",
_ => "フィード"
};
// 公開日時の新しい順にソートし、重複を除外
var distinctItems = items
.GroupBy(i => i.VideoId)
.Select(g => g.First())
.OrderByDescending(i => i.PublishedDate)
.Take(YouTubeConfig.MaxItemsPerFeed * 3)
.Select(CreateSyndicationItem)
.ToList();
var feed = new SyndicationFeed(
$"{feedTitle} ({typeLabel})",
$"登録チャンネル全件の {typeLabel} 統合RSSフィード",
new Uri("https://www.youtube.com"),
distinctItems
);
CmnUtil.ExportXMLFile(fileName, feed, YouTubeConfig.OutputSubDir);
}
/// <summary>
/// YouTubeVideoItemからSyndicationItemを生成します。
/// </summary>
private static SyndicationItem CreateSyndicationItem(YouTubeVideoItem 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 item = new SyndicationItem
{
Title = new TextSyndicationContent(displayTitle),
Id = CmnUtil.CreateGuid(video.Url),
PublishDate = video.PublishedDate,
LastUpdatedTime = video.PublishedDate
};
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()));
// HTML形式のSummaryを生成
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();
}
}