diff --git a/rss-generator/Class/X/x_list.cs b/rss-generator/Class/X/x_list.cs index 57459ca..30a9e1e 100644 --- a/rss-generator/Class/X/x_list.cs +++ b/rss-generator/Class/X/x_list.cs @@ -1,13 +1,35 @@ -using System.ServiceModel.Syndication; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.ServiceModel.Syndication; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml.Linq; using rss_generator.Class.Common; namespace rss_generator.Class.X; +/// +/// x.com (Twitter) のリストから RSS フィードを生成・統合するクラスです。 +/// internal class x_list { + #region □定数(Private) + private const string BASE_URL = "https://rsshub.armonica-vps.work/twitter/list"; private const string QUERY1 = "showEmojiForRetweetAndReply=1?filterout_title=%5E%28🔁%7CRT%29"; private const string QUERY2 = "showEmojiForRetweetAndReply=1?filter_title=%5E%28🔁%7CRT%29"; + + #endregion + + #region □メソッド(Public) + + /// + /// 各対象リストの RSS を順次生成します。 + /// + /// HTTP 通信用 HttpClient + /// 非同期タスク public static async Task CreateRSSAsync(HttpClient client) { // メモリ消費を最小化するため、各リストを1つずつ順次処理 @@ -36,12 +58,27 @@ internal class x_list } } + /// + /// 単一リストの通常ツイートとRTツイートの RSS を生成します。 + /// + /// HttpClient + /// リストID + /// 出力ファイル名(プレフィックス) + /// 非同期タスク public static async Task CreateSub1(HttpClient client, string listId, string fileName) { await CreateSub2(client, listId, QUERY1, $"{fileName}"); await CreateSub2(client, listId, QUERY2, $"{fileName}RT"); } + /// + /// 指定されたクエリ条件で RSS を取得・整形して出力します。 + /// + /// HttpClient + /// リストID + /// クエリ文字列 + /// 出力ファイル名 + /// 非同期タスク public static async Task CreateSub2(HttpClient client, string listId, string query, string fileName) { try @@ -52,37 +89,112 @@ internal class x_list // 簡潔なRSSに整形 var simplifiedItems = allItems.OrderByDescending(x => x.PublishDate).Select(originalItem => { - var newItem = new SyndicationItem() - { - // タイトルは TextSyndicationContent でラップする必要があります - Title = new TextSyndicationContent(originalItem.Title?.Text ?? "No Title") - , Id = CmnUtil.CreateGuid(originalItem.Id) - , PublishDate = originalItem.PublishDate - , LastUpdatedTime = originalItem.LastUpdatedTime + // リンクの取得 + var link = originalItem.Links.FirstOrDefault()?.Uri; - // 元のコンテンツをそのまま引き継ぐ - // Content = originalItem.Content, - , Summary = originalItem.Summary + // 著者の取得 (originalItem.Authors または dc:creator から取得) + string? authorName = originalItem.Authors.FirstOrDefault()?.Name; + if (string.IsNullOrWhiteSpace(authorName)) + { + // ElementExtensions から dc:creator を取得 + foreach (var ext in originalItem.ElementExtensions) + { + if (string.Equals(ext.OuterName, "creator", StringComparison.OrdinalIgnoreCase)) + { + try + { + authorName = ext.GetObject()?.Value?.Trim(); + if (!string.IsNullOrEmpty(authorName)) + { + break; + } + } + catch + { + try + { + using var reader = ext.GetReader(); + authorName = reader.ReadElementContentAsString()?.Trim(); + if (!string.IsNullOrEmpty(authorName)) + { + break; + } + } + catch + { + } + } + } + } + } + + // リンクURLからスクリーン名を抽出(例: https://x.com/PeroVRC/status/... -> @PeroVRC) + string? screenName = null; + if (link != null) + { + var match = Regex.Match(link.AbsoluteUri, @"(?:x|twitter)\.com/([^/]+)/status/", RegexOptions.IgnoreCase); + if (match.Success) + { + screenName = match.Groups[1].Value; + } + } + + if (string.IsNullOrWhiteSpace(authorName) && !string.IsNullOrWhiteSpace(screenName)) + { + authorName = screenName; + } + + // 本文(Summary)にも著者情報を上部に付与して各種リーダーで確実に可視化 + var rawSummary = originalItem.Summary?.Text ?? string.Empty; + string authorHeader = ""; + if (!string.IsNullOrWhiteSpace(authorName)) + { + string authorDisplay = !string.IsNullOrWhiteSpace(screenName) && !string.Equals(authorName, screenName, StringComparison.OrdinalIgnoreCase) + ? $"{WebUtility.HtmlEncode(authorName)} (@{screenName})" + : WebUtility.HtmlEncode(authorName); + + string authorUrl = !string.IsNullOrWhiteSpace(screenName) + ? $"https://x.com/{screenName}" + : (link?.AbsoluteUri ?? "#"); + + authorHeader = $"

👤 投稿者: {authorDisplay}

\n"; + } + + var newItem = new SyndicationItem + { + Title = new TextSyndicationContent(originalItem.Title?.Text ?? "No Title"), + Id = CmnUtil.CreateGuid(originalItem.Id), + PublishDate = originalItem.PublishDate, + LastUpdatedTime = originalItem.LastUpdatedTime, + Summary = new TextSyndicationContent(authorHeader + rawSummary, TextSyndicationContentKind.Html) }; // リンクを引き継ぐ - var link = originalItem.Links.FirstOrDefault()?.Uri; if (link != null) { newItem.Links.Add(new SyndicationLink(link)); } - // 【追加】元のRSSが持っていたカテゴリ(タグ)をそのまま引き継ぐ + + // 元のRSSが持っていたカテゴリ(タグ)をそのまま引き継ぐ foreach (var category in originalItem.Categories) { category.Scheme = null; newItem.Categories.Add(category); } - foreach (var author in originalItem.Authors) + + // 著者の設定 (Atom用 Authors および RSS 2.0 Dublin Core用 dc:creator) + if (!string.IsNullOrWhiteSpace(authorName)) { - newItem.Authors.Add(author); + newItem.Authors.Add(new SyndicationPerson { Name = authorName }); + newItem.ElementExtensions.Add(new XElement( + XName.Get("creator", "http://purl.org/dc/elements/1.1/"), + authorName + )); } + return newItem; }); + // 新しい統合フィードを作成 var mergedFeed = new SyndicationFeed( fileName, @@ -99,4 +211,6 @@ internal class x_list LogManager.AddLog($"[エラー] 【x_list】 {fileName}: {ex.Message}"); } } -} + + #endregion +} \ No newline at end of file