feat: SkebetterおよびHentaiPawのRSS生成処理を追加

- Skebetter (漫画・イラスト急上昇) の直接取得と元投稿日時 (Snowflake ID) の反映
- 複数ページつながっている連作漫画を同一記事内で連結表示する機能を追加
- HentaiPaw (エロモフ) の最新同人誌・漫画のRSS生成処理を追加
- CmnUtil.ExportXMLFileAsync に filter24Hours パラメータを追加
This commit is contained in:
armonica committed 2026-09-07 15:34:49 +09:00
1 parent 2f3ac958bf
commit a0158db108
5 files changed
+1090 -3

No files matched your search

+3 -2
View File
@@ -391,10 +391,11 @@ public class CmnUtil
/// <param name="client">画像ダウンロード用 HttpClient</param>
/// <param name="addDir">サブディレクトリ</param>
/// <param name="embedBase64">画像を Base64 変換するかどうか(Pixiv 以外は false)</param>
public static async Task ExportXMLFileAsync(string fileName, SyndicationFeed feed, HttpClient? client = null, string addDir = "", bool embedBase64 = false)
/// <param name="filter24Hours">24時間以上前の古い記事を除外するかどうか(デフォルト: true)</param>
public static async Task ExportXMLFileAsync(string fileName, SyndicationFeed feed, HttpClient? client = null, string addDir = "", bool embedBase64 = false, bool filter24Hours = true)
{
// 24時間以上前の古い記事を除外
if (feed.Items != null)
if (filter24Hours && feed.Items != null)
{
feed.Items = FilterItemsWithinHours(feed.Items, 24);
}
@@ -4,6 +4,7 @@ using System.Net.Http;
using System.Threading.Tasks;
using rss_generator.Class.AuraHentai;
using rss_generator.Class.Danbooru;
using rss_generator.Class.HentaiPaw;
using rss_generator.Class.HitomiLa;
using rss_generator.Class.MissAV;
using rss_generator.Class.MissAV123;
@@ -110,6 +111,16 @@ internal static class FlareSolverrFeedManager
{
LogManager.AddLog($"[エラー] [FlareSolverrFeedManager] spankbang 実行失敗: {ex.Message}");
}
// hentaipaw (エロモフ)
try
{
await hentaipaw.CreateRSSAsync(client, sessionId);
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] [FlareSolverrFeedManager] hentaipaw 実行失敗: {ex.Message}");
}
}
catch (Exception ex)
{
+393
View File
@@ -0,0 +1,393 @@
using AngleSharp.Dom;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using rss_generator.Class.Common;
namespace rss_generator.Class.HentaiPaw;
/// <summary>
/// ja.hentaipaw.com(エロモフ)の最新同人誌・漫画一覧を取得し、
/// 公式ビュワーリンクやメタ情報を含む RSS フィード (hentaipaw.xml) を生成します。
/// </summary>
internal static class hentaipaw
{
private const string BaseUrl = "https://ja.hentaipaw.com";
private const string TargetUrl = "https://ja.hentaipaw.com/";
/// <summary>
/// 既存フィード記事の比較・キャッシュ用データ保持クラス
/// </summary>
private class ExistingFeedEntry
{
public string Title { get; set; } = string.Empty;
public string SummaryHtml { get; set; } = string.Empty;
public DateTimeOffset PublishDate { get; set; }
public List<string> Categories { get; set; } = new();
public List<string> Authors { get; set; } = new();
}
/// <summary>
/// ja.hentaipaw.com の最新同人誌・漫画 RSS を生成します。
/// </summary>
/// <param name="client">HttpClient インスタンス</param>
/// <param name="sessionId">FlareSolverr セッションID(省略時は自動生成・自動破棄)</param>
public static async Task CreateRSSAsync(HttpClient client, string? sessionId = null)
{
var feed = new SyndicationFeed(
"エロモフ - 最新エロ漫画・エロ同人",
"ja.hentaipaw.com 最新エロ漫画・エロ同人誌",
new Uri(TargetUrl)
);
var items = new List<SyndicationItem>();
try
{
// 既存の hentaipaw.xml から各記事(タイトル・本文・日時・カテゴリ)を読み込んでキャッシュ化
var existingEntries = LoadExistingEntries("hentaipaw.xml");
// 外部からセッションが渡されなかった場合は自前で作成
bool ownSession = sessionId == null;
if (ownSession)
{
sessionId = await CmnUtil.CreateFlareSolverrSession(client);
}
try
{
var document = await CmnUtil.GetDocumentFlareSolverr(client, TargetUrl, session: sessionId);
if (document == null)
{
LogManager.AddLog("[警告] [hentaipaw] トップページの取得に失敗しました。");
return;
}
// 新着セクションのカード(Grid 2)から各作品カードリンクを取得
var articleCards = document.QuerySelectorAll("a[href^='/articles/']")
.Where(a => !string.Equals(a.GetAttribute("href"), "/articles/rank", StringComparison.OrdinalIgnoreCase))
.ToList();
var linkList = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
int itemIndex = 0;
foreach (var card in articleCards)
{
var relHref = card.GetAttribute("href")?.Trim() ?? string.Empty;
if (string.IsNullOrEmpty(relHref))
{
continue;
}
var fullUrl = relHref.StartsWith("http", StringComparison.OrdinalIgnoreCase)
? relHref
: $"{BaseUrl.TrimEnd('/')}/{relHref.TrimStart('/')}";
// 重複URLのスキップ
if (!linkList.Add(fullUrl))
{
continue;
}
// タイトルを取得
var titleDiv = card.QuerySelector("div[title]");
var title = titleDiv?.GetAttribute("title")?.Trim()
?? card.QuerySelector("img")?.GetAttribute("alt")?.Split(',').FirstOrDefault()?.Trim()
?? card.TextContent?.Trim()
?? "タイトルなし";
// サムネイル画像URLを取得
var img = card.QuerySelector("img");
var imgSrc = img?.GetAttribute("src")?.Trim() ?? img?.GetAttribute("data-src")?.Trim() ?? string.Empty;
// 記事IDの抽出
var matchId = Regex.Match(relHref, @"/articles/(\d+)");
string articleId = matchId.Success ? matchId.Groups[1].Value : relHref.TrimEnd('/').Split('/').Last();
string guid = CmnUtil.CreateGuid(fullUrl);
var itemUri = new Uri(fullUrl);
// 既存キャッシュ判定(URL または GUID で検索)
ExistingFeedEntry? prevEntry = null;
if (existingEntries.TryGetValue(fullUrl, out var byUrl))
{
prevEntry = byUrl;
}
else if (existingEntries.TryGetValue(guid, out var byGuid))
{
prevEntry = byGuid;
}
DateTimeOffset itemPubDate;
string summaryHtml;
var categories = new List<string>();
var authors = new List<string>();
if (prevEntry != null && !string.IsNullOrEmpty(prevEntry.SummaryHtml))
{
// 既に取得済みの記事はキャッシュを活用(FlareSolverrへのアクセスを削減)
itemPubDate = prevEntry.PublishDate;
summaryHtml = prevEntry.SummaryHtml;
categories.AddRange(prevEntry.Categories);
authors.AddRange(prevEntry.Authors);
}
else
{
// 新規記事の場合、個別ページから詳細メタ情報を取得
itemPubDate = new DateTimeOffset(DateTime.Now.AddSeconds(-itemIndex));
string? author = null;
string? circle = null;
string? parody = null;
var characters = new List<string>();
var tags = new List<string>();
string? language = null;
string? category = null;
try
{
var detailDoc = await CmnUtil.GetDocumentFlareSolverr(client, fullUrl, session: sessionId);
if (detailDoc != null)
{
// タイトルの更新
var h1Title = detailDoc.QuerySelector("h1")?.TextContent?.Trim();
if (!string.IsNullOrEmpty(h1Title))
{
title = h1Title;
}
// カバー画像の更新
var detailImg = detailDoc.QuerySelector("img[src*='imagedeliveries.com']") ?? detailDoc.QuerySelector("div#article-details img");
var detailImgSrc = detailImg?.GetAttribute("src")?.Trim();
if (!string.IsNullOrEmpty(detailImgSrc))
{
imgSrc = detailImgSrc;
}
// メタ情報タグ群 (#article-tag-information)
var rows = detailDoc.QuerySelectorAll("#article-tag-information .flex.flex-wrap");
foreach (var row in rows)
{
var label = row.QuerySelector("h3")?.TextContent?.Trim()?.Replace(":", "")?.Replace("<!-- -->", "")?.Trim() ?? string.Empty;
var values = row.QuerySelectorAll("a div, a, p")
.Select(e => e.TextContent.Trim())
.Where(v => !string.IsNullOrEmpty(v) && !string.Equals(v, "N/A", StringComparison.OrdinalIgnoreCase))
.Distinct()
.ToList();
if (values.Count == 0)
{
continue;
}
if (label.Contains("作者") || label.Contains("作家"))
{
author = values[0];
authors.AddRange(values);
categories.AddRange(values);
}
else if (label.Contains("サークル"))
{
circle = values[0];
categories.AddRange(values);
}
else if (label.Contains("原作"))
{
parody = values[0];
categories.AddRange(values);
}
else if (label.Contains("キャラクター"))
{
characters.AddRange(values);
categories.AddRange(values);
}
else if (label.Contains("タグ"))
{
tags.AddRange(values);
categories.AddRange(values);
}
else if (label.Contains("言語"))
{
language = values[0];
categories.AddRange(values);
}
else if (label.Contains("カテゴリー"))
{
category = values[0];
categories.AddRange(values);
}
}
}
}
catch (Exception ex)
{
LogManager.AddLog($"[警告] hentaipaw 詳細ページ取得失敗 ({fullUrl}): {ex.Message}");
}
// 本文 HTML (Summary) の生成
var body = new StringBuilder();
string viewerUrl = $"https://ja.hentaipaw.com/viewer?articleId={Uri.EscapeDataString(articleId)}&page=1";
// 1. 公式ビュワーリンク & 外部リンク
body.Append($@"<div style=""margin-bottom: 12px; padding: 10px 14px; background: rgba(59, 130, 246, 0.08); border-radius: 8px; border-left: 4px solid #3b82f6;"">");
body.Append($@"<p style=""margin: 0 0 6px 0;""><a href=""{viewerUrl}"" target=""_blank"" rel=""noopener noreferrer"" style=""font-weight: bold; font-size: 1.1em; color: #2563eb; text-decoration: none;"">📖 公式ビュワーで読む</a></p>");
body.Append($@"<p style=""margin: 0; font-size: 0.9em; color: #64748b;""><a href=""{itemUri}"" target=""_blank"" rel=""noopener noreferrer"" referrerpolicy=""no-referrer"">エロモフ公式ページで開く</a></p>");
body.Append($@"</div>");
// 2. タイトルリンク
body.Append($@"<div style=""margin-bottom: 8px;""><a href=""{itemUri}"" target=""_blank"" rel=""noopener noreferrer"" referrerpolicy=""no-referrer"" style=""font-size: 1.1em; font-weight: bold; text-decoration: none;"">{WebUtility.HtmlEncode(title)}</a></div>");
// 3. カバー画像
if (!string.IsNullOrEmpty(imgSrc))
{
body.Append($@"<div style=""margin-bottom: 10px;"">");
body.Append($@"<a href=""{viewerUrl}"" target=""_blank"" rel=""noopener noreferrer"">");
body.Append($@"<img src=""{imgSrc}"" referrerpolicy=""no-referrer"" alt=""{WebUtility.HtmlEncode(title)}"" style=""max-width: 100%; height: auto; border-radius: 6px;"" />");
body.Append($@"</a>");
body.Append($@"</div>");
}
// 4. メタ情報一覧
body.Append(@"<ul style=""font-size: 0.9em; color: #555; list-style-type: none; padding-left: 0; margin: 6px 0;"">");
if (!string.IsNullOrEmpty(author))
{
body.Append($@"<li>✍ <strong>作者:</strong> {WebUtility.HtmlEncode(author)}</li>");
}
if (!string.IsNullOrEmpty(circle))
{
body.Append($@"<li>🏢 <strong>サークル:</strong> {WebUtility.HtmlEncode(circle)}</li>");
}
if (!string.IsNullOrEmpty(parody))
{
body.Append($@"<li>📚 <strong>原作:</strong> {WebUtility.HtmlEncode(parody)}</li>");
}
if (characters.Count > 0)
{
body.Append($@"<li>👤 <strong>キャラクター:</strong> {WebUtility.HtmlEncode(string.Join(", ", characters))}</li>");
}
if (!string.IsNullOrEmpty(category))
{
body.Append($@"<li>📂 <strong>カテゴリー:</strong> {WebUtility.HtmlEncode(category)}</li>");
}
if (tags.Count > 0)
{
body.Append($@"<li>🏷 <strong>タグ:</strong> {WebUtility.HtmlEncode(string.Join(", ", tags))}</li>");
}
if (!string.IsNullOrEmpty(language))
{
body.Append($@"<li>🌐 <strong>言語:</strong> {WebUtility.HtmlEncode(language)}</li>");
}
body.Append(@"</ul>");
// 下部リンク
body.Append($@"<p style=""margin-top: 10px;""><a href=""{viewerUrl}"" target=""_blank"" rel=""noopener noreferrer"" style=""font-weight: bold; color: #2563eb;"">📖 公式ビュワーで読む</a> | <a href=""{itemUri}"" target=""_blank"" rel=""noopener noreferrer"" referrerpolicy=""no-referrer"">エロモフ公式で作品を見る</a></p>");
summaryHtml = body.ToString();
}
itemIndex++;
var item = new SyndicationItem(
title,
string.Empty,
itemUri,
guid,
itemPubDate
)
{
PublishDate = itemPubDate,
LastUpdatedTime = itemPubDate,
Summary = new TextSyndicationContent(summaryHtml, TextSyndicationContentKind.Html)
};
foreach (var c in categories.Distinct())
{
item.Categories.Add(new SyndicationCategory(c));
}
foreach (var a in authors.Distinct())
{
item.Authors.Add(new SyndicationPerson { Name = a });
}
items.Add(item);
}
feed.Items = items;
await CmnUtil.ExportXMLFileAsync("hentaipaw.xml", feed, client);
}
finally
{
if (ownSession && sessionId != null)
{
await CmnUtil.DestroyFlareSolverrSession(client, sessionId);
}
}
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] hentaipaw: {ex.Message}");
CmnUtil.PrintCallStackToEvent(ex);
}
}
/// <summary>
/// 既存の hentaipaw.xml から各記事のエントリ情報を読み込み、キャッシュディクショナリを構築します。
/// </summary>
private static Dictionary<string, ExistingFeedEntry> LoadExistingEntries(string fileName)
{
var existingEntries = new Dictionary<string, ExistingFeedEntry>(StringComparer.OrdinalIgnoreCase);
try
{
string xmlPath = CmnUtil.GetExportXmlPath(fileName);
if (!File.Exists(xmlPath))
{
return existingEntries;
}
using var reader = XmlReader.Create(xmlPath);
var existingFeed = SyndicationFeed.Load(reader);
if (existingFeed?.Items != null)
{
foreach (var item in existingFeed.Items)
{
var linkUrl = item.Links.FirstOrDefault()?.Uri?.AbsoluteUri;
var guid = item.Id;
var date = item.PublishDate != DateTimeOffset.MinValue
? item.PublishDate
: item.LastUpdatedTime;
var entry = new ExistingFeedEntry
{
Title = item.Title?.Text?.Trim() ?? string.Empty,
SummaryHtml = item.Summary?.Text?.Trim() ?? string.Empty,
PublishDate = date,
Categories = item.Categories.Select(c => c.Name).ToList(),
Authors = item.Authors.Select(a => a.Name).ToList()
};
if (!string.IsNullOrWhiteSpace(linkUrl) && !existingEntries.ContainsKey(linkUrl))
{
existingEntries[linkUrl] = entry;
}
if (!string.IsNullOrWhiteSpace(guid) && !existingEntries.ContainsKey(guid))
{
existingEntries[guid] = entry;
}
}
}
}
catch (Exception ex)
{
LogManager.AddLog($"[情報] hentaipaw 既存エントリの読み込みスキップ: {ex.Message}");
}
return existingEntries;
}
}
+679
View File
@@ -0,0 +1,679 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using rss_generator.Class.Common;
namespace rss_generator.Class.Skebetter;
/// <summary>
/// Skebetter (https://skebetter.com/) の急上昇(漫画・総合)およびイラスト急上昇 (https://skebetter.com/illust) を取得し、
/// Twitter Snowflake ID から復元した元の投稿日時を含む RSS フィード (skebetter.xml, skebetter_illust.xml) を生成します。
/// 複数ページつながっている連作漫画・シリーズは同一記事内に全ページ・全ツイートをつなげて出力します。
/// </summary>
internal static class skebetter
{
private const string BaseUrl = "https://skebetter.com";
private const string MangaApiUrl = "https://skebetter.com/api/hotv3?type=hot";
private const string MangaPageUrl = "https://skebetter.com/";
private const string IllustApiUrl = "https://skebetter.com/api/illust/hot?type=hot";
private const string IllustPageUrl = "https://skebetter.com/illust";
private const string SeriesDetailApiUrl = "https://skebetter.com/api/mangaseriesdetailv2?id=";
private const string UserTweetDetailApiUrl = "https://skebetter.com/api/usertweetdetailv2";
/// <summary>
/// Skebetter の全フィード(漫画急上昇・イラスト急上昇)を順次生成します。
/// </summary>
public static async Task CreateRSSAsync(HttpClient client)
{
// 1. 急上昇(漫画・総合)の生成
try
{
await CreateMangaHotRssAsync(client);
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] [skebetter] 漫画急上昇 RSS 生成失敗: {ex.Message}");
CmnUtil.PrintCallStackToEvent(ex);
}
// 2. イラスト急上昇の生成
try
{
await CreateIllustHotRssAsync(client);
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] [skebetter] イラスト急上昇 RSS 生成失敗: {ex.Message}");
CmnUtil.PrintCallStackToEvent(ex);
}
}
/// <summary>
/// 急上昇(漫画・総合)の RSS フィード (skebetter.xml) を生成します。
/// 複数ページつながっている漫画は同一記事内で全ページをつなげて出力します。
/// </summary>
public static async Task CreateMangaHotRssAsync(HttpClient client)
{
LogManager.AddLog("[情報] [skebetter] 漫画急上昇の取得を開始します");
var posts = await FetchPostsAsync(client, MangaApiUrl, MangaPageUrl, isIllust: false);
if (posts == null || posts.Count == 0)
{
LogManager.AddLog("[警告] [skebetter] 漫画急上昇の投稿が取得できませんでした");
return;
}
var feed = new SyndicationFeed(
"Skebetter - 急上昇",
"Skebetter 急上昇 (漫画・総合)",
new Uri(MangaPageUrl)
);
var items = await ConvertToSyndicationItemsAsync(client, posts, isIllust: false);
feed.Items = items;
// 急上昇フィードのため、過去の人気作品・連載が除外されないよう filter24Hours: false で出力
await CmnUtil.ExportXMLFileAsync("skebetter.xml", feed, client, embedBase64: false, filter24Hours: false);
}
/// <summary>
/// イラスト急上昇の RSS フィード (skebetter_illust.xml) を生成します。
/// </summary>
public static async Task CreateIllustHotRssAsync(HttpClient client)
{
LogManager.AddLog("[情報] [skebetter] イラスト急上昇の取得を開始します");
var posts = await FetchPostsAsync(client, IllustApiUrl, IllustPageUrl, isIllust: true);
if (posts == null || posts.Count == 0)
{
LogManager.AddLog("[警告] [skebetter] イラスト急上昇の投稿が取得できませんでした");
return;
}
var feed = new SyndicationFeed(
"Skebetter Illust - 急上昇",
"Skebetter 急上昇 (イラスト)",
new Uri(IllustPageUrl)
);
var items = await ConvertToSyndicationItemsAsync(client, posts, isIllust: true);
feed.Items = items;
// 急上昇フィードのため、過去の人気作品が除外されないよう filter24Hours: false で出力
await CmnUtil.ExportXMLFileAsync("skebetter_illust.xml", feed, client, embedBase64: false, filter24Hours: false);
}
/// <summary>
/// API から投稿リストを取得します。失敗した場合は Web ページの __NEXT_DATA__ からフォールバック取得します。
/// </summary>
private static async Task<List<SkebetterPost>> FetchPostsAsync(HttpClient client, string apiUrl, string pageUrl, bool isIllust)
{
// 1. API からの取得を試行
try
{
using var request = new HttpRequestMessage(HttpMethod.Get, apiUrl);
request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36");
request.Headers.Add("Accept", "application/json, text/plain, */*");
request.Headers.Referrer = new Uri(pageUrl);
using var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
var apiResult = JsonSerializer.Deserialize<SkebetterApiResponse>(jsonString, JsonOptions);
if (apiResult != null)
{
var list = CombinePosts(apiResult);
if (list.Count > 0)
{
return list;
}
}
}
else
{
LogManager.AddLog($"[警告] [skebetter] API 取得失敗 (Status: {response.StatusCode}): {apiUrl}。Web ページからの抽出を試みます。");
}
}
catch (Exception ex)
{
LogManager.AddLog($"[警告] [skebetter] API リクエスト例外: {ex.Message}。Web ページからの抽出を試みます。");
}
// 2. フォールバック: Web ページの __NEXT_DATA__ からの抽出
try
{
using var pageRequest = new HttpRequestMessage(HttpMethod.Get, pageUrl);
pageRequest.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36");
pageRequest.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
using var pageResponse = await client.SendAsync(pageRequest, HttpCompletionOption.ResponseContentRead);
if (pageResponse.IsSuccessStatusCode)
{
var html = await pageResponse.Content.ReadAsStringAsync();
var match = Regex.Match(html, @"<script id=""__NEXT_DATA__"" type=""application/json"">(.*?)</script>", RegexOptions.Singleline);
if (match.Success)
{
var nextDataJson = match.Groups[1].Value;
var nextData = JsonSerializer.Deserialize<NextDataWrapper>(nextDataJson, JsonOptions);
var postsObj = nextData?.Props?.PageProps?.Posts;
if (postsObj != null)
{
var list = CombinePosts(postsObj);
if (list.Count > 0)
{
LogManager.AddLog($"[情報] [skebetter] Web ページの __NEXT_DATA__ から {list.Count} 件の投稿を抽出しました");
return list;
}
}
}
}
}
catch (Exception ex)
{
LogManager.AddLog($"[エラー] [skebetter] Web ページからの抽出例外: {ex.Message}");
}
return new List<SkebetterPost>();
}
/// <summary>
/// API レスポンス内の tweet と series を統合し、重複を排除したリストを作成します。
/// </summary>
private static List<SkebetterPost> CombinePosts(SkebetterApiResponse result)
{
var combined = new List<SkebetterPost>();
var seenIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// 通常ツイート
if (result.Tweet != null)
{
foreach (var t in result.Tweet)
{
string key = t.SeriesId != null && t.SeriesId > 0
? $"series_{t.SeriesId}"
: (!string.IsNullOrEmpty(t.Id) ? t.Id : t.TweetUri ?? string.Empty);
if (!string.IsNullOrEmpty(key) && seenIds.Add(key))
{
combined.Add(t);
}
else if (string.IsNullOrEmpty(key))
{
combined.Add(t);
}
}
}
// シリーズ連載(漫画のみ存在)
if (result.Series != null)
{
foreach (var s in result.Series)
{
string key = s.SeriesId != null && s.SeriesId > 0
? $"series_{s.SeriesId}"
: (s.TweetUri ?? s.Id ?? string.Empty);
if (!string.IsNullOrEmpty(key) && seenIds.Add(key))
{
combined.Add(s);
}
else if (string.IsNullOrEmpty(key))
{
combined.Add(s);
}
}
}
return combined;
}
/// <summary>
/// シリーズ作品の全ツイート・全ページ情報を API から取得します。
/// </summary>
private static async Task<List<SkebetterPost>?> FetchSeriesTweetsAsync(HttpClient client, long seriesId)
{
try
{
string url = $"{SeriesDetailApiUrl}{seriesId}";
using var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36");
request.Headers.Add("Accept", "application/json, text/plain, */*");
request.Headers.Referrer = new Uri($"{BaseUrl}/series/{seriesId}");
using var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<MangaSeriesDetailResponse>(jsonString, JsonOptions);
var tweets = result?.Tweets?.Tweets;
if (tweets != null && tweets.Count > 0)
{
return tweets;
}
}
}
catch (Exception ex)
{
LogManager.AddLog($"[警告] [skebetter] シリーズ詳細取得失敗 (series_id: {seriesId}): {ex.Message}");
}
return null;
}
/// <summary>
/// 通常作品のリプライツリー情報を API から取得します(複数ツイートがある場合)。
/// </summary>
private static async Task<List<SkebetterPost>?> FetchUserTweetDetailAsync(HttpClient client, string? userId, string? tweetId)
{
if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(tweetId)) return null;
try
{
string url = $"{UserTweetDetailApiUrl}?user_id={userId}&tweet_uri={tweetId}&ssr=1";
using var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36");
request.Headers.Add("Accept", "application/json, text/plain, */*");
request.Headers.Referrer = new Uri($"{BaseUrl}/author/{userId}/manga/{tweetId}");
using var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);
if (response.IsSuccessStatusCode)
{
var jsonString = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<UserTweetDetailResponse>(jsonString, JsonOptions);
if (result?.Tweet != null && result.Tweet.Count > 1)
{
return result.Tweet;
}
}
}
catch (Exception ex)
{
LogManager.AddLog($"[警告] [skebetter] 個別詳細取得失敗 (tweet: {tweetId}): {ex.Message}");
}
return null;
}
/// <summary>
/// タイトルや本文から、作品が複数ページ(ツリー投稿・連作)である可能性を判定します。
/// </summary>
private static bool IsMultiPageHint(SkebetterPost post)
{
if (post.SeriesId != null && post.SeriesId > 0) return true;
var text = post.Text ?? string.Empty;
return Regex.IsMatch(text, @"\(\s*\d+\s*/\s*\d+\s*\)|【\s*\d+\s*/\s*\d+\s*】|\b\d+\s*/\s*\d+\b|第\s*\d+\s*話");
}
/// <summary>
/// Skebetter 投稿一覧を SyndicationItem のリストに変換します。
/// 複数ページつながっている漫画は全ページをつなげて1つの記事にします。
/// </summary>
private static async Task<List<SyndicationItem>> ConvertToSyndicationItemsAsync(HttpClient client, List<SkebetterPost> posts, bool isIllust)
{
var items = new List<SyndicationItem>();
int index = 0;
foreach (var post in posts)
{
index++;
// 1. リンクと GUID の決定
string linkUrl;
bool isSeries = post.SeriesId != null && post.SeriesId > 0;
if (isSeries)
{
linkUrl = $"{BaseUrl}/series/{post.SeriesId}";
}
else if (isIllust)
{
linkUrl = $"{BaseUrl}/author/{post.UserId}/illust/{post.Id}";
}
else
{
linkUrl = $"{BaseUrl}/author/{post.UserId}/manga/{post.Id}";
}
// 2. 複数ページ漫画の取得(シリーズ作品またはページヒントのある漫画)
List<SkebetterPost>? connectedTweets = null;
if (!isIllust)
{
if (isSeries && post.SeriesId.HasValue)
{
connectedTweets = await FetchSeriesTweetsAsync(client, post.SeriesId.Value);
}
else if (IsMultiPageHint(post))
{
connectedTweets = await FetchUserTweetDetailAsync(client, post.UserId, post.Id);
}
}
// 3. 元の投稿日時の算出(Twitter Snowflake ID)
// 複数ツイートある場合は最初のツイートの日時を優先
var firstPost = connectedTweets?.FirstOrDefault() ?? post;
var postDate = GetPostDateTime(firstPost) ?? GetPostDateTime(post) ?? DateTimeOffset.Now.AddSeconds(-index);
// 4. タイトルの決定
var rawText = (post.Text ?? string.Empty).Trim();
var singleLineText = Regex.Replace(rawText, @"\s+", " ").Trim();
string title;
if (!string.IsNullOrEmpty(singleLineText))
{
title = singleLineText;
}
else if (!string.IsNullOrEmpty(post.UserName))
{
title = $"{post.UserName} の投稿";
}
else
{
title = "タイトルなし";
}
// 5. 本文 (Summary HTML) の構築
var body = new StringBuilder();
// いいね・リツイート数
body.Append("<p>");
if (!string.IsNullOrEmpty(post.Fav))
{
body.Append($"❤{WebUtility.HtmlEncode(post.Fav)} ");
}
if (!string.IsNullOrEmpty(post.Retweet))
{
body.Append($"🔁{WebUtility.HtmlEncode(post.Retweet)}");
}
body.Append("</p>\n");
// 複数ページつながっている場合の一括表示
if (connectedTweets != null && connectedTweets.Count > 1)
{
int totalImages = connectedTweets.Sum(t => (t.MediaUrls?.Count ?? 0) > 0 ? t.MediaUrls!.Count : (!string.IsNullOrEmpty(t.MediaUrl) ? 1 : 0));
body.Append($"<p style=\"font-size: 0.9em; color: #64748b; font-weight: bold; margin-bottom: 12px;\">📖 全 {connectedTweets.Count} 投稿 / 全 {totalImages} ページ(連作漫画)</p>\n");
for (int i = 0; i < connectedTweets.Count; i++)
{
var part = connectedTweets[i];
var partText = (part.Text ?? string.Empty).Trim();
if (i > 0)
{
body.Append("<hr style=\"border: none; border-top: 1px dashed #cbd5e1; margin: 16px 0;\">\n");
}
body.Append("<div style=\"margin-bottom: 14px;\">\n");
if (!string.IsNullOrEmpty(partText))
{
body.Append("<p style=\"white-space: pre-wrap; margin: 0 0 8px 0; color: #1e293b;\">");
body.Append(WebUtility.HtmlEncode(partText).Replace("\n", "<br>\n"));
body.Append("</p>\n");
}
var partMedias = GetMediaList(part);
foreach (var media in partMedias)
{
if (!string.IsNullOrEmpty(media.MediaUri))
{
body.Append($"<p style=\"margin: 0 0 8px 0;\"><img src=\"{media.MediaUri}\" referrerpolicy=\"no-referrer\" style=\"max-width: 100%; height: auto; border-radius: 4px;\"></p>\n");
}
}
body.Append("</div>\n");
}
}
else
{
// 単一ツイートの場合
if (!string.IsNullOrEmpty(rawText))
{
body.Append("<p style=\"white-space: pre-wrap; margin-bottom: 12px;\">");
body.Append(WebUtility.HtmlEncode(rawText).Replace("\n", "<br>\n"));
body.Append("</p>\n");
}
var mediaList = GetMediaList(post);
foreach (var media in mediaList)
{
if (!string.IsNullOrEmpty(media.MediaUri))
{
body.Append($"<p style=\"margin: 0 0 8px 0;\"><img src=\"{media.MediaUri}\" referrerpolicy=\"no-referrer\" style=\"max-width: 100%; height: auto; border-radius: 4px;\"></p>\n");
}
}
}
// リンクと作者情報
body.Append($"<p style=\"margin-top: 16px; font-size: 0.9em; color: #64748b;\">");
body.Append($"<a href=\"{linkUrl}\" target=\"_blank\" rel=\"noopener noreferrer\" style=\"font-weight: bold;\">Skebetter で見る</a>");
var authorScreenName = post.ScreenName ?? connectedTweets?.FirstOrDefault()?.ScreenName;
if (!string.IsNullOrEmpty(authorScreenName))
{
body.Append($" | <a href=\"https://x.com/{authorScreenName}\" target=\"_blank\" rel=\"noopener noreferrer\">@{authorScreenName}</a>");
}
body.Append("</p>");
// 6. SyndicationItem の生成
var itemUri = new Uri(linkUrl);
var item = new SyndicationItem(
title,
string.Empty,
itemUri,
linkUrl,
postDate
)
{
PublishDate = postDate,
LastUpdatedTime = postDate,
Summary = new TextSyndicationContent(body.ToString(), TextSyndicationContentKind.Html)
};
// 著者の設定 (Atom用 a10:author および RSS2.0/Dublin Core用 dc:creator)
var authorName = post.UserName ?? connectedTweets?.FirstOrDefault()?.UserName;
if (!string.IsNullOrEmpty(authorName))
{
item.Authors.Add(new SyndicationPerson { Name = authorName });
item.ElementExtensions.Add(new System.Xml.Linq.XElement(
System.Xml.Linq.XName.Get("creator", "http://purl.org/dc/elements/1.1/"),
authorName
));
}
// カテゴリ(シリーズ・イラスト等の種別タグ)
if (isSeries || (connectedTweets != null && connectedTweets.Count > 1))
{
item.Categories.Add(new SyndicationCategory("シリーズ連載"));
}
item.Categories.Add(new SyndicationCategory(isIllust ? "イラスト" : "漫画"));
items.Add(item);
}
return items;
}
/// <summary>
/// 投稿から有効な画像メディア一覧を抽出します。
/// </summary>
private static List<SkebetterMedia> GetMediaList(SkebetterPost post)
{
var list = post.MediaUrls != null ? new List<SkebetterMedia>(post.MediaUrls) : new List<SkebetterMedia>();
if (list.Count == 0 && !string.IsNullOrEmpty(post.MediaUrl))
{
var singleUri = post.MediaUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)
? post.MediaUrl
: $"https://pbs.twimg.com/media/{post.MediaUrl}";
list.Add(new SkebetterMedia { MediaUri = singleUri });
}
return list;
}
/// <summary>
/// Twitter Snowflake ID から正確な投稿日時 (JST) を算出します。
/// </summary>
public static DateTimeOffset? GetPostDateTime(SkebetterPost post)
{
// 1. series の場合は tweet_uri、通常は id から Tweet ID を取得
string? idStr = !string.IsNullOrEmpty(post.TweetUri) ? post.TweetUri : post.Id;
if (!string.IsNullOrEmpty(idStr) && long.TryParse(idStr, out long tweetId))
{
// Twitter Snowflake エポック: 1288834974657 ms (2010-11-04 01:42:54.657 UTC)
// (tweetId >> 22) + 1288834974657L
long timestampMs = (tweetId >> 22) + 1288834974657L;
if (timestampMs > 1288834974657L)
{
return DateTimeOffset.FromUnixTimeMilliseconds(timestampMs).ToOffset(TimeSpan.FromHours(9));
}
}
// 2. datePublished フィールド ("2026-04-13 20:46:30" 等) がある場合
if (!string.IsNullOrEmpty(post.DatePublished) && DateTimeOffset.TryParse(post.DatePublished, out var dtParsed))
{
return dtParsed.ToOffset(TimeSpan.FromHours(9));
}
// 3. フォールバック: "〇時間前" などの文字列から概算
if (!string.IsNullOrEmpty(post.Date))
{
var matchHour = Regex.Match(post.Date, @"(\d+)\s*時間前");
if (matchHour.Success && int.TryParse(matchHour.Groups[1].Value, out int hours))
{
return DateTimeOffset.Now.AddHours(-hours);
}
var matchMin = Regex.Match(post.Date, @"(\d+)\s*分前");
if (matchMin.Success && int.TryParse(matchMin.Groups[1].Value, out int mins))
{
return DateTimeOffset.Now.AddMinutes(-mins);
}
var matchDay = Regex.Match(post.Date, @"(\d+)\s*日前");
if (matchDay.Success && int.TryParse(matchDay.Groups[1].Value, out int days))
{
return DateTimeOffset.Now.AddDays(-days);
}
}
return null;
}
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
#region JSON モデルクラス群
internal class NextDataWrapper
{
[JsonPropertyName("props")]
public NextDataProps? Props { get; set; }
}
internal class NextDataProps
{
[JsonPropertyName("pageProps")]
public NextDataPageProps? PageProps { get; set; }
}
internal class NextDataPageProps
{
[JsonPropertyName("posts")]
public SkebetterApiResponse? Posts { get; set; }
}
internal class SkebetterApiResponse
{
[JsonPropertyName("tweet")]
public List<SkebetterPost>? Tweet { get; set; }
[JsonPropertyName("series")]
public List<SkebetterPost>? Series { get; set; }
}
internal class MangaSeriesDetailResponse
{
[JsonPropertyName("tweets")]
public MangaSeriesDetailTweets? Tweets { get; set; }
}
internal class MangaSeriesDetailTweets
{
[JsonPropertyName("id")]
public string? Id { get; set; }
[JsonPropertyName("title")]
public string? Title { get; set; }
[JsonPropertyName("tweet_uri")]
public string? TweetUri { get; set; }
[JsonPropertyName("user_id")]
public string? UserId { get; set; }
[JsonPropertyName("tweets")]
public List<SkebetterPost>? Tweets { get; set; }
}
internal class UserTweetDetailResponse
{
[JsonPropertyName("tweet")]
public List<SkebetterPost>? Tweet { get; set; }
}
internal class SkebetterPost
{
[JsonPropertyName("id")]
public string? Id { get; set; }
[JsonPropertyName("screen_name")]
public string? ScreenName { get; set; }
[JsonPropertyName("text")]
public string? Text { get; set; }
[JsonPropertyName("date")]
public string? Date { get; set; }
[JsonPropertyName("datePublished")]
public string? DatePublished { get; set; }
[JsonPropertyName("user_name")]
public string? UserName { get; set; }
[JsonPropertyName("fav")]
public string? Fav { get; set; }
[JsonPropertyName("retweet")]
public string? Retweet { get; set; }
[JsonPropertyName("user_id")]
public string? UserId { get; set; }
[JsonPropertyName("media_url")]
public string? MediaUrl { get; set; }
[JsonPropertyName("media_urls")]
public List<SkebetterMedia>? MediaUrls { get; set; }
[JsonPropertyName("series_id")]
public long? SeriesId { get; set; }
[JsonPropertyName("tweet_uri")]
public string? TweetUri { get; set; }
}
internal class SkebetterMedia
{
[JsonPropertyName("media_uri")]
public string? MediaUri { get; set; }
[JsonPropertyName("type")]
public string? Type { get; set; }
}
#endregion
}