feat: Skebetterの取得処理にFlareSolverrフォールバックを追加およびCmnUtilにGetStringFlareSolverrを実装

This commit is contained in:
armonica committed 2026-09-07 16:41:38 +09:00
1 parent a0158db108
commit 9cb9892cb3
2 files changed
+249 -35

No files matched your search

+112
View File
@@ -704,6 +704,118 @@ public class CmnUtil
}
}
/// <summary>
/// FlareSolverr 経由で対象 URL のレスポンス文字列(HTML または JSON)を取得します。
/// Chromium が JSON を表示した際の &lt;pre&gt;...&lt;/pre&gt; ラップも自動検出してアンラップ・HTMLデコードします。
/// </summary>
/// <param name="client">HttpClient インスタンス</param>
/// <param name="url">取得対象のURL</param>
/// <param name="flareSolverrEndpoint">FlareSolverrのエンドポイントURL(デフォルト: https://udukipc-flaresolverr.armonica-vps.work/v1)</param>
/// <param name="maxTimeout">FlareSolverr側の最大タイムアウトミリ秒(デフォルト: 60000ms)</param>
/// <param name="session">セッションID(セッションを再利用して高速化する場合に指定)</param>
/// <returns>レスポンス文字列(失敗時は null または空文字)</returns>
public static async Task<string?> GetStringFlareSolverr(
HttpClient client,
string url,
string flareSolverrEndpoint = "https://udukipc-flaresolverr.armonica-vps.work/v1",
int maxTimeout = 60000,
string? session = null)
{
await FlareSolverrSemaphore.WaitAsync();
try
{
const int maxRetries = 3;
string? currentSession = session;
for (int attempt = 1; attempt <= maxRetries; attempt++)
{
Console.WriteLine($"Fetching String via FlareSolverr (試行 {attempt}/{maxRetries}): {url}");
var payload = new FlareSolverrRequest
{
Cmd = "request.get",
Url = url,
MaxTimeout = maxTimeout,
Session = currentSession
};
using var request = new HttpRequestMessage(HttpMethod.Post, flareSolverrEndpoint)
{
Content = JsonContent.Create(payload, options: FlareSolverrJsonOptions)
};
try
{
using var response = await client.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
LogManager.AddLog($"[警告] FlareSolverr HTTP失敗 (Status: {(int)response.StatusCode} {response.ReasonPhrase}, URL: {url}, Detail: {responseContent})");
if (currentSession != null)
{
LogManager.AddLog($"[情報] セッション {currentSession} を解除して次回セッションなしで再試行します。");
currentSession = null;
}
if (attempt < maxRetries)
{
await Task.Delay(attempt * 2500);
continue;
}
throw new HttpRequestException($"FlareSolverr サーバーエラー (Status: {(int)response.StatusCode}, Detail: {responseContent})");
}
var flareResponse = JsonSerializer.Deserialize<FlareSolverrResponse>(responseContent, FlareSolverrJsonOptions);
if (flareResponse == null)
{
throw new InvalidOperationException($"FlareSolverr レスポンスのデシリアライズ失敗: {url}");
}
if (!string.Equals(flareResponse.Status, "ok", StringComparison.OrdinalIgnoreCase))
{
string errorMsg = $"FlareSolverr エラー (Status: {flareResponse.Status}, Message: {flareResponse.Message}): {url}";
LogManager.AddLog($"[警告] {errorMsg}");
if (attempt < maxRetries)
{
currentSession = null;
await Task.Delay(attempt * 2500);
continue;
}
throw new InvalidOperationException(errorMsg);
}
string rawContent = flareResponse.Solution?.Response ?? string.Empty;
// Chromium が JSON を表示した際の <pre>...</pre> をアンラップ
var match = Regex.Match(rawContent, @"<pre[^>]*>(.*?)</pre>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (match.Success)
{
return WebUtility.HtmlDecode(match.Groups[1].Value).Trim();
}
return rawContent;
}
catch (Exception ex) when (attempt < maxRetries && ex is not OperationCanceledException)
{
LogManager.AddLog($"[警告] FlareSolverr リクエスト再試行 (試行 {attempt}/{maxRetries}, URL: {url}): {ex.Message}");
currentSession = null;
await Task.Delay(attempt * 2500);
}
}
throw new InvalidOperationException($"FlareSolverr での取得が最大試行回数 ({maxRetries}回) を超えて失敗しました: {url}");
}
finally
{
FlareSolverrSemaphore.Release();
}
}
/// <summary>
/// FlareSolverr 経由で対象 URL の RSS/Atom フィードを取得し、SyndicationItem のコレクションを返します。
/// メモリ割り当ての最小化およびストリーム直接デシリアライズにより、メモリリーク防止と高速化を行っています。