246 lines
9.7 KiB
C#
246 lines
9.7 KiB
C#
using System.Text.RegularExpressions;
|
||
using HtmlAgilityPack;
|
||
using Microsoft.Extensions.Logging;
|
||
using WebImageViewer.Class.Common;
|
||
using WebImageViewer.Class.Models;
|
||
|
||
namespace WebImageViewer.Class.Scrapers;
|
||
|
||
/// <summary>
|
||
/// momon-ga.com 固有のスクレイパークラス
|
||
/// </summary>
|
||
public partial class MomongaScraper : BaseScraper
|
||
{
|
||
private const string BaseDomain = "momon-ga.com";
|
||
private const string BaseUrl = "https://momon-ga.com";
|
||
|
||
public override string SiteName => "momon-ga";
|
||
public override string SiteKey => "momonga";
|
||
public override IReadOnlyList<string> Aliases => new[] { "momon-ga", "momon-ga.com", "mo", "fanzine", "magazine" };
|
||
|
||
public MomongaScraper(HttpHelper httpHelper, ILogger<MomongaScraper> logger)
|
||
: base(httpHelper, logger)
|
||
{
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public override bool CanHandle(string urlOrPath)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(urlOrPath))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
var trimmed = urlOrPath.Trim();
|
||
|
||
// サイトキーまたはエイリアスと完全一致
|
||
if (string.Equals(trimmed, SiteKey, StringComparison.OrdinalIgnoreCase) ||
|
||
Aliases.Any(a => string.Equals(trimmed, a, StringComparison.OrdinalIgnoreCase)))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// momon-ga のドメインが含まれているか、または典型的なID形式(moから始まる等)かパス形式
|
||
return trimmed.Contains(BaseDomain, StringComparison.OrdinalIgnoreCase) ||
|
||
trimmed.StartsWith("mo", StringComparison.OrdinalIgnoreCase) ||
|
||
trimmed.Contains("fanzine", StringComparison.OrdinalIgnoreCase) ||
|
||
trimmed.Contains("magazine", StringComparison.OrdinalIgnoreCase);
|
||
}
|
||
|
||
|
||
/// <inheritdoc/>
|
||
public override async Task<GalleryItem?> ScrapeAsync(string idOrUrl, string? category = null, CancellationToken cancellationToken = default)
|
||
{
|
||
var (targetUrl, normalizedId, resolvedCategory) = ResolveUrlAndId(idOrUrl, category);
|
||
|
||
Logger.LogInformation("[{SiteName}] スクレイピング開始 - ID: {Id}, Category: {Category}, URL: {Url}",
|
||
SiteName, normalizedId, resolvedCategory, targetUrl);
|
||
|
||
var doc = await LoadHtmlDocumentAsync(targetUrl, cancellationToken);
|
||
if (doc == null || doc.DocumentNode == null)
|
||
{
|
||
Logger.LogWarning("[{SiteName}] ドキュメントの読み込みに失敗しました: {Url}", SiteName, targetUrl);
|
||
return null;
|
||
}
|
||
|
||
// タイトルの抽出(h1タグ、またはpost-data内のh1、またはtitleタグ)
|
||
var titleNode = doc.DocumentNode.SelectSingleNode("//div[@id='post-data']//h1") ??
|
||
doc.DocumentNode.SelectSingleNode("//h1") ??
|
||
doc.DocumentNode.SelectSingleNode("//title");
|
||
var title = CleanText(titleNode?.InnerText) ?? normalizedId;
|
||
|
||
// 余分なサイト名表記を除去(例: "新妻契約 - 同人誌 - エロ漫画 momon:GA(モモンガッ!!)" -> "新妻契約")
|
||
if (title.Contains(" - "))
|
||
{
|
||
title = title.Split(" - ")[0].Trim();
|
||
}
|
||
|
||
var galleryItem = new GalleryItem
|
||
{
|
||
Id = normalizedId,
|
||
Title = string.IsNullOrEmpty(title) ? normalizedId : title,
|
||
Category = resolvedCategory,
|
||
SourceSite = SiteName,
|
||
SourceUrl = targetUrl,
|
||
FetchedAt = DateTime.UtcNow
|
||
};
|
||
|
||
// サークル、作者、タグの抽出
|
||
ExtractTagsAndMetadata(doc, galleryItem);
|
||
|
||
// 画像一覧の抽出(#post-hentai img)
|
||
ExtractImages(doc, galleryItem);
|
||
|
||
// サムネイルの決定(data-thumb属性、または1枚目の画像)
|
||
var historyNode = doc.DocumentNode.SelectSingleNode("//div[contains(@class, 'history')]");
|
||
var dataThumb = historyNode?.GetAttributeValue("data-thumb", null);
|
||
if (!string.IsNullOrWhiteSpace(dataThumb))
|
||
{
|
||
galleryItem.ThumbnailUrl = dataThumb;
|
||
}
|
||
else if (galleryItem.Images.Count > 0)
|
||
{
|
||
galleryItem.ThumbnailUrl = galleryItem.Images[0].ImageUrl;
|
||
}
|
||
|
||
Logger.LogInformation("[{SiteName}] スクレイピング完了: 『{Title}』, 画像数: {Count}枚",
|
||
SiteName, galleryItem.Title, galleryItem.Images.Count);
|
||
|
||
return galleryItem;
|
||
}
|
||
|
||
/// <summary>
|
||
/// IDまたはURLから完全なリクエストURL、正規化されたID、カテゴリを解決します。
|
||
/// </summary>
|
||
private static (string TargetUrl, string NormalizedId, string ResolvedCategory) ResolveUrlAndId(string idOrUrl, string? category)
|
||
{
|
||
var trimmed = idOrUrl.Trim();
|
||
string resolvedCategory = string.IsNullOrWhiteSpace(category) ? "fanzine" : category.Trim().ToLowerInvariant();
|
||
string normalizedId;
|
||
|
||
if (trimmed.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
|
||
trimmed.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
// URL形式の場合
|
||
var uri = new Uri(trimmed);
|
||
var segments = uri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||
if (segments.Length >= 2)
|
||
{
|
||
resolvedCategory = segments[0];
|
||
normalizedId = segments[1];
|
||
}
|
||
else if (segments.Length == 1)
|
||
{
|
||
normalizedId = segments[0];
|
||
}
|
||
else
|
||
{
|
||
normalizedId = trimmed;
|
||
}
|
||
|
||
return ($"{BaseUrl}/{resolvedCategory}/{normalizedId}/", normalizedId, resolvedCategory);
|
||
}
|
||
|
||
// パスまたはIDのみの場合(例: "fanzine/mo3469109" や "mo3469109")
|
||
if (trimmed.Contains('/'))
|
||
{
|
||
var parts = trimmed.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||
if (parts.Length >= 2)
|
||
{
|
||
resolvedCategory = parts[0];
|
||
normalizedId = parts[1];
|
||
}
|
||
else
|
||
{
|
||
normalizedId = parts[0];
|
||
}
|
||
}
|
||
else
|
||
{
|
||
normalizedId = trimmed;
|
||
}
|
||
|
||
var fullUrl = $"{BaseUrl}/{resolvedCategory}/{normalizedId}/";
|
||
return (fullUrl, normalizedId, resolvedCategory);
|
||
}
|
||
|
||
/// <summary>
|
||
/// タグテーブルからサークル、作者、内容タグを抽出します。
|
||
/// </summary>
|
||
private static void ExtractTagsAndMetadata(HtmlDocument doc, GalleryItem item)
|
||
{
|
||
var tagTables = doc.DocumentNode.SelectNodes("//div[@id='post-tag']//div[contains(@class, 'post-tag-table')]");
|
||
if (tagTables == null) return;
|
||
|
||
foreach (var table in tagTables)
|
||
{
|
||
var titleNode = table.SelectSingleNode(".//div[contains(@class, 'post-tag-title')]");
|
||
var titleText = CleanText(titleNode?.InnerText);
|
||
|
||
var linkNodes = table.SelectNodes(".//div[contains(@class, 'post-tags')]//a");
|
||
if (linkNodes == null) continue;
|
||
|
||
var tagValues = linkNodes.Select(n => CleanText(n.InnerText)).Where(s => !string.IsNullOrEmpty(s)).ToList();
|
||
|
||
if (titleText.Contains("サークル", StringComparison.OrdinalIgnoreCase) || titleText.Contains("グループ", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
item.Circle = string.Join(", ", tagValues);
|
||
}
|
||
else if (titleText.Contains("作者", StringComparison.OrdinalIgnoreCase) || titleText.Contains("漫画家", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
item.Author = string.Join(", ", tagValues);
|
||
}
|
||
else if (titleText.Contains("内容", StringComparison.OrdinalIgnoreCase) || titleText.Contains("タグ", StringComparison.OrdinalIgnoreCase) || titleText.Contains("キャラクター", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
item.Tags.AddRange(tagValues);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 本文領域から画像一覧を抽出します。
|
||
/// </summary>
|
||
private static void ExtractImages(HtmlDocument doc, GalleryItem item)
|
||
{
|
||
var imgNodes = doc.DocumentNode.SelectNodes("//div[@id='post-hentai']//img") ??
|
||
doc.DocumentNode.SelectNodes("//div[contains(@class, 'entry-content')]//img") ??
|
||
doc.DocumentNode.SelectNodes("//article//img");
|
||
|
||
if (imgNodes == null) return;
|
||
|
||
int pageNum = 1;
|
||
foreach (var img in imgNodes)
|
||
{
|
||
// src または data-src, data-lazy-src 属性から画像URLを取得
|
||
var src = img.GetAttributeValue("src", null) ??
|
||
img.GetAttributeValue("data-src", null) ??
|
||
img.GetAttributeValue("data-lazy-src", null);
|
||
|
||
if (string.IsNullOrWhiteSpace(src)) continue;
|
||
|
||
// 広告画像やアイコン等を除外
|
||
if (src.Contains("themes", StringComparison.OrdinalIgnoreCase) ||
|
||
src.Contains("plugins", StringComparison.OrdinalIgnoreCase) ||
|
||
src.Contains("banner", StringComparison.OrdinalIgnoreCase) ||
|
||
src.Contains("ad_", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
var alt = img.GetAttributeValue("alt", null);
|
||
int? width = int.TryParse(img.GetAttributeValue("width", null), out var w) ? w : null;
|
||
int? height = int.TryParse(img.GetAttributeValue("height", null), out var h) ? h : null;
|
||
|
||
item.Images.Add(new ImageInfo
|
||
{
|
||
PageNumber = pageNum++,
|
||
ImageUrl = src,
|
||
ThumbnailUrl = src,
|
||
AltText = CleanText(alt),
|
||
Width = width,
|
||
Height = height
|
||
});
|
||
}
|
||
}
|
||
}
|