refactor: ビューアー機能の全体統一、旧コードのClass/Legacy隔離、および/?url=統一エントリポイントの実装
This commit is contained in:
1 parent
01a6376328
commit
fb7f4aa62b
29 files changed
+3824
-590
No files matched your search
@@ -0,0 +1,104 @@
|
||||
using HtmlAgilityPack;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WebImageViewer.Class.Legacy.Models;
|
||||
|
||||
namespace WebImageViewer.Class.Legacy.Common;
|
||||
|
||||
/// <summary>
|
||||
/// 各種Webサイトスクレイパーの共通基底クラス
|
||||
/// </summary>
|
||||
public abstract class BaseScraper : IScraper
|
||||
{
|
||||
protected readonly HttpHelper HttpHelper;
|
||||
protected readonly ILogger Logger;
|
||||
|
||||
public abstract string SiteName { get; }
|
||||
public abstract string SiteKey { get; }
|
||||
public virtual IReadOnlyList<string> Aliases => Array.Empty<string>();
|
||||
|
||||
|
||||
protected BaseScraper(HttpHelper httpHelper, ILogger logger)
|
||||
{
|
||||
HttpHelper = httpHelper;
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public abstract bool CanHandle(string urlOrPath);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public abstract Task<GalleryItem?> ScrapeAsync(string idOrUrl, string? category = null, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 指定されたURLからHTMLを取得し、HtmlDocumentとしてパースします。
|
||||
/// </summary>
|
||||
/// <param name="url">対象URL</param>
|
||||
/// <param name="cancellationToken">キャンセレーショントークン</param>
|
||||
/// <returns>パースされたHtmlDocument</returns>
|
||||
protected Task<HtmlDocument?> LoadHtmlDocumentAsync(string url, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return LoadHtmlDocumentAsync(url, useFlareSolverr: false, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定されたURLからHTMLを取得し、HtmlDocumentとしてパースします。
|
||||
/// </summary>
|
||||
/// <param name="url">対象URL</param>
|
||||
/// <param name="useFlareSolverr">FlareSolverrを強制使用するかどうか</param>
|
||||
/// <param name="cancellationToken">キャンセレーショントークン</param>
|
||||
/// <returns>パースされたHtmlDocument</returns>
|
||||
protected async Task<HtmlDocument?> LoadHtmlDocumentAsync(string url, bool useFlareSolverr, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
string html;
|
||||
if (useFlareSolverr)
|
||||
{
|
||||
Logger.LogInformation("[{SiteName}] FlareSolverr経由でHTMLを取得中: {Url}", SiteName, url);
|
||||
html = await HttpHelper.GetHtmlViaFlareSolverrAsync(url, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.LogInformation("[{SiteName}] HTMLを取得中: {Url}", SiteName, url);
|
||||
try
|
||||
{
|
||||
html = await HttpHelper.GetHtmlAsync(url, cancellationToken);
|
||||
if (html.Contains("Just a moment...") || html.Contains("cf-chl-opt"))
|
||||
{
|
||||
Logger.LogWarning("[{SiteName}] Cloudflareチャレンジを検出しました。FlareSolverrに切り替えます: {Url}", SiteName, url);
|
||||
html = await HttpHelper.GetHtmlViaFlareSolverrAsync(url, cancellationToken);
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.Forbidden || ex.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable)
|
||||
{
|
||||
Logger.LogWarning("[{SiteName}] HTTPエラー ({StatusCode}) を検出しました。FlareSolverrに切り替えます: {Url}", SiteName, ex.StatusCode, url);
|
||||
html = await HttpHelper.GetHtmlViaFlareSolverrAsync(url, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
var doc = new HtmlDocument();
|
||||
doc.LoadHtml(html);
|
||||
return doc;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, "[{SiteName}] HTMLの取得またはパースに失敗しました: {Url}", SiteName, url);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// HTML文字列のエンティティデコードおよび前後の空白をトリムします。
|
||||
/// </summary>
|
||||
protected static string CleanText(string? input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return HtmlEntity.DeEntitize(input).Trim();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user