From 21af75f4106c718e9ae38ae0926beb176a15205e Mon Sep 17 00:00:00 2001 From: Glax Date: Tue, 20 Aug 2024 20:47:13 +0200 Subject: [PATCH] Faster download for images-urls. #224 --- Tranga/MangaConnectors/ChromiumDownloadClient.cs | 15 +++++++++++++-- Tranga/MangaConnectors/DownloadClient.cs | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Tranga/MangaConnectors/ChromiumDownloadClient.cs b/Tranga/MangaConnectors/ChromiumDownloadClient.cs index 8ddf850..c63e85a 100644 --- a/Tranga/MangaConnectors/ChromiumDownloadClient.cs +++ b/Tranga/MangaConnectors/ChromiumDownloadClient.cs @@ -1,5 +1,6 @@ using System.Net; using System.Text; +using System.Text.RegularExpressions; using HtmlAgilityPack; using PuppeteerSharp; using PuppeteerSharp.Input; @@ -11,10 +12,11 @@ internal class ChromiumDownloadClient : DownloadClient private IBrowser browser { get; set; } private const string ChromiumVersion = "1154303"; private const int StartTimeoutMs = 30000; + private HttpDownloadClient _httpDownloadClient; private async Task DownloadBrowser() { - BrowserFetcher browserFetcher = new BrowserFetcher(); + BrowserFetcher browserFetcher = new (); foreach(string rev in browserFetcher.LocalRevisions().Where(rev => rev != ChromiumVersion)) browserFetcher.Remove(rev); if (!browserFetcher.LocalRevisions().Contains(ChromiumVersion)) @@ -58,9 +60,18 @@ internal class ChromiumDownloadClient : DownloadClient public ChromiumDownloadClient(GlobalBase clone) : base(clone) { this.browser = DownloadBrowser().Result; + _httpDownloadClient = new(this); } - protected override RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null) + 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) { IPage page = this.browser.NewPageAsync().Result; page.DefaultTimeout = 10000; diff --git a/Tranga/MangaConnectors/DownloadClient.cs b/Tranga/MangaConnectors/DownloadClient.cs index 0e9b343..f6f876d 100644 --- a/Tranga/MangaConnectors/DownloadClient.cs +++ b/Tranga/MangaConnectors/DownloadClient.cs @@ -40,6 +40,6 @@ internal abstract class DownloadClient : GlobalBase return result; } - protected abstract RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null); + internal abstract RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null); public abstract void Close(); } \ No newline at end of file