fix(x): RSSの著者(dc:creator)取得と本文リンク表示に対応
This commit is contained in:
1 parent
c788acaeca
commit
f2b2edc247
1 file changed
+130
-16
+130
-16
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// x.com (Twitter) のリストから RSS フィードを生成・統合するクラスです。
|
||||
/// </summary>
|
||||
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)
|
||||
|
||||
/// <summary>
|
||||
/// 各対象リストの RSS を順次生成します。
|
||||
/// </summary>
|
||||
/// <param name="client">HTTP 通信用 HttpClient</param>
|
||||
/// <returns>非同期タスク</returns>
|
||||
public static async Task CreateRSSAsync(HttpClient client)
|
||||
{
|
||||
// メモリ消費を最小化するため、各リストを1つずつ順次処理
|
||||
@@ -36,12 +58,27 @@ internal class x_list
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 単一リストの通常ツイートとRTツイートの RSS を生成します。
|
||||
/// </summary>
|
||||
/// <param name="client">HttpClient</param>
|
||||
/// <param name="listId">リストID</param>
|
||||
/// <param name="fileName">出力ファイル名(プレフィックス)</param>
|
||||
/// <returns>非同期タスク</returns>
|
||||
public static async Task CreateSub1(HttpClient client, string listId, string fileName)
|
||||
{
|
||||
await CreateSub2(client, listId, QUERY1, $"{fileName}");
|
||||
await CreateSub2(client, listId, QUERY2, $"{fileName}RT");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定されたクエリ条件で RSS を取得・整形して出力します。
|
||||
/// </summary>
|
||||
/// <param name="client">HttpClient</param>
|
||||
/// <param name="listId">リストID</param>
|
||||
/// <param name="query">クエリ文字列</param>
|
||||
/// <param name="fileName">出力ファイル名</param>
|
||||
/// <returns>非同期タスク</returns>
|
||||
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<XElement>()?.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 = $"<p style=\"font-size: 0.95em; color: #475569; margin-bottom: 8px;\"><strong>👤 投稿者:</strong> <a href=\"{authorUrl}\" target=\"_blank\" rel=\"noopener noreferrer\">{authorDisplay}</a></p>\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
|
||||
}
|
||||
Reference in new issue
Block a user