Skip to content

Commit

Permalink
Merge pull request #294 from C9Glax/cuttingedge
Browse files Browse the repository at this point in the history
Merge cuttingedge into master
  • Loading branch information
C9Glax authored Dec 12, 2024
2 parents 9cc4f8c + 747df0b commit 113c0ab
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CLI/CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console.Cli" Version="0.47.1-preview.0.11" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion Tranga/Chapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public int CompareTo(object? obj)
_ => chapterNumberFloat.CompareTo(otherChapterNumberFloat)
};
}
else throw new FormatException($"Value could not be parsed");
else throw new FormatException($"Value could not be parsed.\n" +
$"\tVolumeNumber: '{volumeNumber}' ChapterNumber: '{chapterNumber}'\n" +
$"\tOther-VolumeNumber: '{otherChapter.volumeNumber}' Other-ChapterNumber: '{otherChapter.chapterNumber}'\n" +
$"\t{this}\n" +
$"\t{otherChapter}");
}

/// <summary>
Expand Down
35 changes: 31 additions & 4 deletions Tranga/MangaConnectors/ChromiumDownloadClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using Microsoft.Extensions.Logging;
using PuppeteerSharp;

namespace Tranga.MangaConnectors;

internal class ChromiumDownloadClient : DownloadClient
{
private static readonly IBrowser Browser = StartBrowser().Result;
private static IBrowser? _browser;
private const int StartTimeoutMs = 10000;
private readonly HttpDownloadClient _httpDownloadClient;

private static async Task<IBrowser> StartBrowser()
private static async Task<IBrowser> StartBrowser(Logging.Logger? logger = null)
{
logger?.WriteLine("Starting ChromiumDownloadClient Puppeteer");
return await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true,
Expand All @@ -23,12 +25,37 @@ private static async Task<IBrowser> StartBrowser()
"--disable-setuid-sandbox",
"--no-sandbox"},
Timeout = StartTimeoutMs
});
}, new LoggerFactory([new LogProvider(logger)]));
}

private class LogProvider : GlobalBase, ILoggerProvider
{
public LogProvider(Logging.Logger? logger) : base(logger) { }

public void Dispose() { }

public ILogger CreateLogger(string categoryName) => new Logger(logger);
}

private class Logger : GlobalBase, ILogger
{
public Logger(Logging.Logger? logger) : base(logger) { }

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
logger?.WriteLine("Puppeteer", formatter.Invoke(state, exception));
}

public bool IsEnabled(LogLevel logLevel) => true;

public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
}

public ChromiumDownloadClient(GlobalBase clone) : base(clone)
{
_httpDownloadClient = new(this);
if(_browser is null)
_browser = StartBrowser(this.logger).Result;
}

private readonly Regex _imageUrlRex = new(@"https?:\/\/.*\.(?:p?jpe?g|gif|a?png|bmp|avif|webp)(\?.*)?");
Expand All @@ -41,7 +68,7 @@ internal override RequestResult MakeRequestInternal(string url, string? referrer

private RequestResult MakeRequestBrowser(string url, string? referrer = null, string? clickButton = null)
{
IPage page = Browser.NewPageAsync().Result;
IPage page = _browser.NewPageAsync().Result;
page.DefaultTimeout = 10000;
IResponse response;
try
Expand Down
2 changes: 1 addition & 1 deletion Tranga/MangaConnectors/Mangaworld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private List<Chapter> ParseChaptersFromHtml(Manga manga, HtmlDocument document)
"//div[contains(concat(' ',normalize-space(@class),' '),'chapters-wrapper')]");

Regex volumeRex = new(@"[Vv]olume ([0-9]+).*");
Regex chapterRex = new(@"[Cc]apitolo ([0-9]+).*");
Regex chapterRex = new(@"[Cc]apitolo ([0-9]+(?:\.[0-9]+)?).*");
Regex idRex = new(@".*\/read\/([a-z0-9]+)(?:[?\/].*)?");
if (chaptersWrapper.Descendants("div").Any(descendant => descendant.HasClass("volume-element")))
{
Expand Down
4 changes: 2 additions & 2 deletions Tranga/Tranga.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

<ItemGroup>
<PackageReference Include="GlaxArguments" Version="1.1.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.46" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.71" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PuppeteerSharp" Version="10.0.0" />
<PackageReference Include="PuppeteerSharp" Version="20.0.5" />
<PackageReference Include="Soenneker.Utils.String.NeedlemanWunsch" Version="2.1.301" />
</ItemGroup>

Expand Down

0 comments on commit 113c0ab

Please sign in to comment.