192 lines
7.6 KiB
C#
192 lines
7.6 KiB
C#
using System.Text.RegularExpressions;
|
||
using HtmlAgilityPack;
|
||
using Microsoft.Extensions.Logging;
|
||
using WebImageViewer.Class.Unified.Common;
|
||
using WebImageViewer.Class.Unified.Models;
|
||
|
||
namespace WebImageViewer.Class.Unified.Scrapers;
|
||
|
||
/// <summary>
|
||
/// ja.hentaipaw.com(エロモフ)固有の統一画像スクレイパー
|
||
/// </summary>
|
||
public class UnifiedHentaiPawScraper : BaseUnifiedScraper
|
||
{
|
||
private const string BaseDomain = "hentaipaw.com";
|
||
private const string BaseUrl = "https://ja.hentaipaw.com";
|
||
|
||
public override string SiteName => "エロモフ (HentaiPaw)";
|
||
public override string SiteKey => "hentaipaw";
|
||
public override IReadOnlyList<string> SupportedDomains => new[] { "hentaipaw.com", "ja.hentaipaw.com" };
|
||
|
||
public UnifiedHentaiPawScraper(UnifiedHttpHelper httpHelper, ILogger<UnifiedHentaiPawScraper> logger)
|
||
: base(httpHelper, logger)
|
||
{
|
||
}
|
||
|
||
public override async Task<UnifiedGalleryItem?> ScrapeAsync(string urlOrPath, CancellationToken ct = default)
|
||
{
|
||
var articleId = ResolveArticleId(urlOrPath);
|
||
if (string.IsNullOrEmpty(articleId))
|
||
{
|
||
Logger.LogWarning("[{SiteName}] 有効な記事IDを抽出できませんでした: {Input}", SiteName, urlOrPath);
|
||
return null;
|
||
}
|
||
|
||
var articleUrl = $"{BaseUrl}/articles/{articleId}";
|
||
var viewerUrl = $"{BaseUrl}/viewer?articleId={articleId}&page=1";
|
||
Logger.LogInformation("[{SiteName}] スクレイピング開始 - ID: {Id}", SiteName, articleId);
|
||
|
||
var viewerDoc = await LoadHtmlDocumentAsync(viewerUrl, useFlareSolverr: true, ct: ct);
|
||
var articleDoc = await LoadHtmlDocumentAsync(articleUrl, useFlareSolverr: true, ct: ct);
|
||
|
||
if (viewerDoc == null && articleDoc == null)
|
||
{
|
||
Logger.LogWarning("[{SiteName}] ビュワーページおよび記事詳細ページの取得に失敗しました: ID={Id}", SiteName, articleId);
|
||
return null;
|
||
}
|
||
|
||
string title = string.Empty;
|
||
if (articleDoc?.DocumentNode != null)
|
||
{
|
||
var h1 = articleDoc.DocumentNode.SelectSingleNode("//h1");
|
||
if (h1 != null) title = CleanText(h1.InnerText);
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(title) && viewerDoc?.DocumentNode != null)
|
||
{
|
||
var titleNode = viewerDoc.DocumentNode.SelectSingleNode("//title");
|
||
title = CleanText(titleNode?.InnerText);
|
||
title = Regex.Replace(title, @"\s*[-|–]\s*(?:無料のエロ漫画|エロモフ|HentaiPaw).*$", "", RegexOptions.IgnoreCase).Trim();
|
||
}
|
||
|
||
if (string.IsNullOrWhiteSpace(title))
|
||
{
|
||
title = $"エロモフ作品 #{articleId}";
|
||
}
|
||
|
||
var galleryItem = new UnifiedGalleryItem
|
||
{
|
||
Id = articleId,
|
||
Title = title,
|
||
Category = "doujinshi",
|
||
SourceSite = SiteName,
|
||
SourceUrl = articleUrl,
|
||
ThumbnailUrl = $"https://cdn.imagedeliveries.com/{articleId}/thumbnails/cover.webp",
|
||
FetchedAt = DateTime.UtcNow
|
||
};
|
||
|
||
if (articleDoc?.DocumentNode != null)
|
||
{
|
||
ExtractMetadata(articleDoc, galleryItem);
|
||
}
|
||
|
||
ExtractImages(viewerDoc, articleDoc, galleryItem, articleId);
|
||
return galleryItem;
|
||
}
|
||
|
||
private static string ResolveArticleId(string idOrUrl)
|
||
{
|
||
var trimmed = idOrUrl.Trim();
|
||
var matchViewer = Regex.Match(trimmed, @"[?&]articleId=(?<id>\d+)", RegexOptions.IgnoreCase);
|
||
if (matchViewer.Success) return matchViewer.Groups["id"].Value;
|
||
|
||
var matchArticles = Regex.Match(trimmed, @"/articles/(?<id>\d+)", RegexOptions.IgnoreCase);
|
||
if (matchArticles.Success) return matchArticles.Groups["id"].Value;
|
||
|
||
var matchNum = Regex.Match(trimmed, @"\d{6,8}");
|
||
return matchNum.Success ? matchNum.Value : "";
|
||
}
|
||
|
||
private static void ExtractMetadata(HtmlDocument articleDoc, UnifiedGalleryItem item)
|
||
{
|
||
var infoRows = articleDoc.DocumentNode.SelectNodes("//div[contains(@class, 'flex') and contains(@class, 'items-center')]");
|
||
if (infoRows == null) return;
|
||
|
||
foreach (var row in infoRows)
|
||
{
|
||
var h3 = row.SelectSingleNode(".//h3");
|
||
var label = CleanText(h3?.InnerText).Replace(":", "").Trim();
|
||
var values = row.SelectNodes(".//a//div | .//a | .//p")?
|
||
.Select(v => CleanText(v.InnerText))
|
||
.Where(v => !string.IsNullOrEmpty(v) && !string.Equals(v, "N/A", StringComparison.OrdinalIgnoreCase))
|
||
.Distinct()
|
||
.ToList() ?? new List<string>();
|
||
|
||
if (values.Count == 0) continue;
|
||
|
||
if (label.Contains("作者") || label.Contains("作家")) item.Author = string.Join(", ", values);
|
||
else if (label.Contains("サークル")) item.Circle = string.Join(", ", values);
|
||
else if (label.Contains("カテゴリー")) item.Category = values[0];
|
||
else if (label.Contains("タグ") || label.Contains("キャラクター")) item.Tags.AddRange(values);
|
||
}
|
||
|
||
item.Tags = item.Tags.Distinct().ToList();
|
||
}
|
||
|
||
private static void ExtractImages(HtmlDocument? viewerDoc, HtmlDocument? articleDoc, UnifiedGalleryItem item, string articleId)
|
||
{
|
||
var collectedImages = new List<UnifiedImageInfo>();
|
||
|
||
if (viewerDoc?.DocumentNode != null)
|
||
{
|
||
var rawHtml = viewerDoc.DocumentNode.OuterHtml;
|
||
var pattern = $@"https://cdn\.imagedeliveries\.com/{Regex.Escape(articleId)}/[a-f0-9]{{32,64}}/(?<page>\d+)\.webp";
|
||
var matches = Regex.Matches(rawHtml, pattern, RegexOptions.IgnoreCase);
|
||
|
||
var pageMap = new Dictionary<int, string>();
|
||
foreach (Match match in matches)
|
||
{
|
||
if (int.TryParse(match.Groups["page"].Value, out var pageNum) && !pageMap.ContainsKey(pageNum))
|
||
{
|
||
pageMap[pageNum] = match.Value;
|
||
}
|
||
}
|
||
|
||
if (pageMap.Count > 0)
|
||
{
|
||
foreach (var kvp in pageMap.OrderBy(k => k.Key))
|
||
{
|
||
int page = kvp.Key;
|
||
string highResUrl = kvp.Value;
|
||
string thumbUrl = $"https://cdn.imagedeliveries.com/{articleId}/thumbnails/{page}.webp";
|
||
|
||
collectedImages.Add(new UnifiedImageInfo
|
||
{
|
||
PageNumber = page,
|
||
ImageUrl = highResUrl,
|
||
ThumbnailUrl = thumbUrl,
|
||
AltText = $"{item.Title} - {page}ページ"
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
if (collectedImages.Count == 0 && articleDoc?.DocumentNode != null)
|
||
{
|
||
var rawHtml = articleDoc.DocumentNode.OuterHtml;
|
||
var thumbPattern = $@"https://cdn\.imagedeliveries\.com/{Regex.Escape(articleId)}/thumbnails/(?<page>\d+)\.webp";
|
||
var matches = Regex.Matches(rawHtml, thumbPattern, RegexOptions.IgnoreCase);
|
||
|
||
var pages = new SortedSet<int>();
|
||
foreach (Match match in matches)
|
||
{
|
||
if (int.TryParse(match.Groups["page"].Value, out var pageNum)) pages.Add(pageNum);
|
||
}
|
||
|
||
foreach (var page in pages)
|
||
{
|
||
string thumbUrl = $"https://cdn.imagedeliveries.com/{articleId}/thumbnails/{page}.webp";
|
||
collectedImages.Add(new UnifiedImageInfo
|
||
{
|
||
PageNumber = page,
|
||
ImageUrl = thumbUrl,
|
||
ThumbnailUrl = thumbUrl,
|
||
AltText = $"{item.Title} - {page}ページ"
|
||
});
|
||
}
|
||
}
|
||
|
||
item.Images = collectedImages;
|
||
}
|
||
}
|