Files
rss-generator/rss-generator/Class/RSS/Danbooru/rank.cs
T

316 lines
12 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 AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
namespace rss_generator.Class.RSS.Danbooru;
internal class rank
{
/// <summary>
/// Danbooruのランクフィードを取得し、各投稿ページから詳細情報を取得して整形したRSSを出力します。
/// </summary>
/// <param name="client">HttpClient インスタンス</param>
public static async Task CreateRSSAsync(HttpClient client, string? sessionId = null)
{
var pageMaster = "https://danbooru.donmai.us/posts.atom?tags=order%3Arank";
try
{
// 外部からセッションが渡されなかった場合は自前で作成
bool ownSession = sessionId == null;
if (ownSession)
{
sessionId = await CmnUtil.CreateFlareSolverrSession(client);
}
try
{
// FlareSolverr 経由で Atom フィードアイテムを取得
var items = await CmnUtil.GetFeedItemsFlareSolverr(client, pageMaster, session: sessionId);
var simplifiedItems = new List<SyndicationItem>();
foreach (var item in items)
{
var simplified = await Simplified(client, item, sessionId);
if (simplified != null)
{
simplifiedItems.Add(simplified);
}
}
// 新しい統合フィードを作成
var mergedFeed = new SyndicationFeed(
"Danbooru",
"Danbooru Rank Posts",
new Uri("https://danbooru.donmai.us/posts?tags=order%3Arank"),
simplifiedItems
);
CmnUtil.ExportXMLFile("rank.xml", mergedFeed, "danbooru");
}
finally
{
if (ownSession && sessionId != null)
{
await CmnUtil.DestroyFlareSolverrSession(client, sessionId);
}
}
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] danbooru:{ex.Message}");
CmnUtil.PrintCallStackToEvent(ex);
}
}
/// <summary>
/// 元のフィードアイテムとDanbooru投稿詳細ページからコンテンツ(画像・タグ・作者・元ソース等)を取得・整形します。
/// </summary>
/// <param name="client">HttpClient インスタンス</param>
/// <param name="originalItem">元のフィードアイテム</param>
/// <param name="sessionId">FlareSolverr セッションID</param>
/// <returns>整形後のSyndicationItem</returns>
public static async Task<SyndicationItem> Simplified(HttpClient client, SyndicationItem originalItem, string? sessionId = null)
{
var link = originalItem.Links.FirstOrDefault(l => l.RelationshipType == "alternate" || string.IsNullOrEmpty(l.RelationshipType))?.Uri
?? originalItem.Links.FirstOrDefault()?.Uri;
IDocument? docContents = null;
if (link != null)
{
try
{
docContents = await CmnUtil.GetDocumentFlareSolverr(client, link.ToString(), session: sessionId);
}
catch (Exception ex)
{
LogManager.AddLog($"[警告] Danbooru 投稿ページ取得失敗 ({link}): {ex.Message}");
}
}
// 1. タイトルの決定(個別ページのog:titleまたはAtomのTitle)
var title = originalItem.Title?.Text ?? "Danbooru Post";
if (docContents != null)
{
var ogTitle = docContents.QuerySelector("meta[property='og:title']")?.GetAttribute("content");
if (!string.IsNullOrWhiteSpace(ogTitle))
{
title = ogTitle;
if (title.EndsWith(" | Danbooru", StringComparison.OrdinalIgnoreCase))
{
title = title[..^11].Trim();
}
}
}
var newItem = new SyndicationItem
{
Title = new TextSyndicationContent(title),
Id = CmnUtil.CreateGuid(originalItem.Id ?? link?.ToString() ?? Guid.NewGuid().ToString()),
PublishDate = originalItem.PublishDate,
LastUpdatedTime = originalItem.LastUpdatedTime
};
if (link != null)
{
newItem.Links.Add(new SyndicationLink(link));
}
// 2. タグ分類(Artist / Copyright / Character / General / Meta)の取得
List<string> GetTagsBySelector(string selector)
{
var tagList = new List<string>();
if (docContents == null) return tagList;
var elements = docContents.QuerySelectorAll(selector);
foreach (var el in elements)
{
var tagName = el.GetAttribute("data-tag-name");
if (string.IsNullOrWhiteSpace(tagName))
{
tagName = el.QuerySelector(".search-tag")?.TextContent?.Trim();
}
if (!string.IsNullOrWhiteSpace(tagName) && !tagList.Contains(tagName))
{
tagList.Add(tagName);
}
}
return tagList;
}
var artistTags = GetTagsBySelector("#tag-list .artist-tag-list li");
var copyrightTags = GetTagsBySelector("#tag-list .copyright-tag-list li");
var characterTags = GetTagsBySelector("#tag-list .character-tag-list li");
var generalTags = GetTagsBySelector("#tag-list .general-tag-list li");
var metaTags = GetTagsBySelector("#tag-list .meta-tag-list li");
// 3. カテゴリの登録(重複防止)
var addedCategories = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
void AddCategories(IEnumerable<string> tags)
{
foreach (var tag in tags)
{
if (!string.IsNullOrWhiteSpace(tag) && addedCategories.Add(tag))
{
newItem.Categories.Add(new SyndicationCategory(tag));
}
}
}
AddCategories(artistTags);
AddCategories(copyrightTags);
AddCategories(characterTags);
AddCategories(generalTags);
AddCategories(metaTags);
// 元のアイテムのカテゴリも引き継ぐ
foreach (var category in originalItem.Categories)
{
if (!string.IsNullOrWhiteSpace(category.Name) && addedCategories.Add(category.Name))
{
category.Scheme = null;
newItem.Categories.Add(category);
}
}
// 4. 作者(Authors)の設定
if (artistTags.Count > 0)
{
foreach (var artist in artistTags)
{
newItem.Authors.Add(new SyndicationPerson { Name = artist });
}
}
else
{
foreach (var author in originalItem.Authors)
{
newItem.Authors.Add(author);
}
}
// 5. 画像URLの取得(最高画質 / オリジナル画像)
string? imageUrl = null;
if (docContents != null)
{
// og:image はオリジナル画像URLであることが多い
imageUrl = docContents.QuerySelector("meta[property='og:image']")?.GetAttribute("content")
?? docContents.QuerySelector("#image-container img")?.GetAttribute("src")
?? docContents.QuerySelector("#image")?.GetAttribute("src")
?? docContents.QuerySelector("meta[name='twitter:image']")?.GetAttribute("content");
}
// HTMLのフォールバック画像抽出
if (string.IsNullOrEmpty(imageUrl) && originalItem.Content is TextSyndicationContent textContent)
{
var contentParser = new HtmlParser();
var contentDoc = contentParser.ParseDocument(textContent.Text);
imageUrl = contentDoc.QuerySelector("img")?.GetAttribute("src");
}
// 6. 元ソースURLの取得
string? sourceUrl = null;
if (docContents != null)
{
var sourceElem = docContents.QuerySelector("#post-info-source a") ?? docContents.QuerySelector("#post-information a[href^='http']");
sourceUrl = sourceElem?.GetAttribute("href");
}
if (string.IsNullOrEmpty(sourceUrl))
{
var relatedLink = originalItem.Links.FirstOrDefault(l => l.RelationshipType == "related")?.Uri;
if (relatedLink != null)
{
sourceUrl = relatedLink.ToString();
}
}
// 7. 作者コメント(artist-commentary)の抽出
var commentaryHtml = new StringBuilder();
if (docContents != null)
{
var commentaryContainer = docContents.QuerySelector("#artist-commentary");
if (commentaryContainer != null)
{
var sections = commentaryContainer.QuerySelectorAll("section");
foreach (var sec in sections)
{
var isTranslated = sec.Id?.Contains("translated", StringComparison.OrdinalIgnoreCase) == true;
var label = isTranslated ? "翻訳コメント" : "作者コメント";
var h3Title = sec.QuerySelector("h3")?.TextContent?.Trim();
var proseElem = sec.QuerySelector(".prose") ?? sec.QuerySelector("div");
var contentHtml = proseElem?.InnerHtml?.Trim();
// 空文字やプレースホルダー("????")の除外判定
bool hasTitle = !string.IsNullOrWhiteSpace(h3Title);
bool hasContent = !string.IsNullOrWhiteSpace(contentHtml) && contentHtml != "<p>????</p>" && contentHtml != "????";
if (hasTitle || hasContent)
{
commentaryHtml.Append($@"<div style=""margin: 8px 0; padding: 8px 12px; background: #f8f9fa; border-left: 4px solid #4a90e2; border-radius: 4px;"">");
commentaryHtml.Append($@"<div style=""font-weight: bold; margin-bottom: 4px;"">【{label}】{(hasTitle ? $" {WebUtility.HtmlEncode(h3Title)}" : "")}</div>");
if (hasContent)
{
commentaryHtml.Append($@"<div>{contentHtml}</div>");
}
commentaryHtml.Append("</div>");
}
}
}
}
// 8. HTML本文(Summary)の生成
var body = new StringBuilder();
// 画像の配置
if (!string.IsNullOrEmpty(imageUrl))
{
body.Append($@"<div><a href=""{link}""><img src=""{imageUrl}"" alt=""{WebUtility.HtmlEncode(title)}"" style=""max-width: 100%; height: auto;"" /></a></div>");
}
// 画像のすぐ下に作者コメントを追加
if (commentaryHtml.Length > 0)
{
body.Append(commentaryHtml);
}
body.Append($@"<div><strong>タイトル:</strong> {WebUtility.HtmlEncode(title)}</div>");
if (copyrightTags.Count > 0)
{
body.Append($@"<div><strong>作品名:</strong> {WebUtility.HtmlEncode(string.Join(", ", copyrightTags))}</div>");
}
if (characterTags.Count > 0)
{
body.Append($@"<div><strong>キャラクター:</strong> {WebUtility.HtmlEncode(string.Join(", ", characterTags))}</div>");
}
if (artistTags.Count > 0)
{
body.Append($@"<div><strong>作者:</strong> {WebUtility.HtmlEncode(string.Join(", ", artistTags))}</div>");
}
if (!string.IsNullOrEmpty(sourceUrl))
{
body.Append($@"<div><strong>元ソース:</strong> <a href=""{sourceUrl}"" target=""_blank"">{WebUtility.HtmlEncode(sourceUrl)}</a></div>");
}
if (generalTags.Count > 0)
{
body.Append($@"<div><strong>タグ:</strong> {WebUtility.HtmlEncode(string.Join(", ", generalTags))}</div>");
}
newItem.Summary = new TextSyndicationContent(body.ToString(), TextSyndicationContentKind.Html);
return newItem;
}
}