2024-12-14 21:53:29 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using HtmlAgilityPack;
|
|
|
|
|
using PuppeteerSharp;
|
|
|
|
|
|
|
|
|
|
namespace API.MangaDownloadClients;
|
|
|
|
|
|
|
|
|
|
internal class ChromiumDownloadClient : DownloadClient
|
|
|
|
|
{
|
|
|
|
|
private static IBrowser? _browser;
|
|
|
|
|
private readonly HttpDownloadClient _httpDownloadClient;
|
|
|
|
|
|
|
|
|
|
private static async Task<IBrowser> StartBrowser()
|
|
|
|
|
{
|
|
|
|
|
return await Puppeteer.LaunchAsync(new LaunchOptions
|
|
|
|
|
{
|
|
|
|
|
Headless = true,
|
|
|
|
|
Args = new [] {
|
|
|
|
|
"--disable-gpu",
|
|
|
|
|
"--disable-dev-shm-usage",
|
|
|
|
|
"--disable-setuid-sandbox",
|
|
|
|
|
"--no-sandbox"},
|
2025-01-15 22:56:25 +01:00
|
|
|
|
Timeout = 30000
|
2024-12-14 21:53:29 +01:00
|
|
|
|
}, new LoggerFactory([new LogProvider()])); //TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class LogProvider : ILoggerProvider
|
|
|
|
|
{
|
|
|
|
|
//TODO
|
|
|
|
|
public void Dispose() { }
|
|
|
|
|
|
|
|
|
|
public ILogger CreateLogger(string categoryName) => new Logger();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class Logger : ILogger
|
|
|
|
|
{
|
|
|
|
|
public Logger() : base() { }
|
|
|
|
|
|
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
|
|
|
|
{
|
|
|
|
|
if (logLevel <= LogLevel.Information)
|
|
|
|
|
return;
|
|
|
|
|
//TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsEnabled(LogLevel logLevel) => true;
|
|
|
|
|
|
|
|
|
|
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ChromiumDownloadClient()
|
|
|
|
|
{
|
|
|
|
|
_httpDownloadClient = new();
|
|
|
|
|
if(_browser is null)
|
|
|
|
|
_browser = StartBrowser().Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly Regex _imageUrlRex = new(@"https?:\/\/.*\.(?:p?jpe?g|gif|a?png|bmp|avif|webp)(\?.*)?");
|
|
|
|
|
internal override RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null)
|
|
|
|
|
{
|
|
|
|
|
return _imageUrlRex.IsMatch(url)
|
|
|
|
|
? _httpDownloadClient.MakeRequestInternal(url, referrer)
|
|
|
|
|
: MakeRequestBrowser(url, referrer, clickButton);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RequestResult MakeRequestBrowser(string url, string? referrer = null, string? clickButton = null)
|
|
|
|
|
{
|
2025-01-15 22:24:55 +01:00
|
|
|
|
if (_browser is null)
|
|
|
|
|
return new RequestResult(HttpStatusCode.InternalServerError, null, Stream.Null);
|
2024-12-14 21:53:29 +01:00
|
|
|
|
IPage page = _browser.NewPageAsync().Result;
|
|
|
|
|
page.DefaultTimeout = 10000;
|
|
|
|
|
IResponse response;
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-01-15 23:14:15 +01:00
|
|
|
|
response = page.GoToAsync(url, WaitUntilNavigation.Networkidle0).Result;
|
2025-01-15 22:56:25 +01:00
|
|
|
|
//Log($"Page loaded. {url}");
|
2024-12-14 21:53:29 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2025-01-15 22:56:25 +01:00
|
|
|
|
//Log($"Could not load Page {url}\n{e.Message}");
|
2024-12-14 21:53:29 +01:00
|
|
|
|
page.CloseAsync();
|
|
|
|
|
return new RequestResult(HttpStatusCode.InternalServerError, null, Stream.Null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream stream = Stream.Null;
|
|
|
|
|
HtmlDocument? document = null;
|
|
|
|
|
|
|
|
|
|
if (response.Headers.TryGetValue("Content-Type", out string? content))
|
|
|
|
|
{
|
|
|
|
|
if (content.Contains("text/html"))
|
|
|
|
|
{
|
|
|
|
|
if (clickButton is not null && page.QuerySelectorAsync(clickButton).Result is not null)
|
|
|
|
|
page.ClickAsync(clickButton).Wait();
|
|
|
|
|
string htmlString = page.GetContentAsync().Result;
|
|
|
|
|
stream = new MemoryStream(Encoding.Default.GetBytes(htmlString));
|
|
|
|
|
document = new ();
|
|
|
|
|
document.LoadHtml(htmlString);
|
|
|
|
|
}else if (content.Contains("image"))
|
|
|
|
|
{
|
|
|
|
|
stream = new MemoryStream(response.BufferAsync().Result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
page.CloseAsync();
|
|
|
|
|
return new RequestResult(HttpStatusCode.InternalServerError, null, Stream.Null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page.CloseAsync();
|
|
|
|
|
return new RequestResult(response.Status, document, stream, false, "");
|
|
|
|
|
}
|
|
|
|
|
}
|