diff --git a/src/Handlers/DatabaseHandler.cs b/src/Handlers/DatabaseHandler.cs index 8a86585..3deca6c 100644 --- a/src/Handlers/DatabaseHandler.cs +++ b/src/Handlers/DatabaseHandler.cs @@ -49,10 +49,11 @@ await collection.Indexes.CreateManyAsync([ return memoryCache.Get(key); } - public string? GetSourceIcon(string sourceId) { - return config.GetValue("Save:SourceIcon") - ? Get(sourceId) - : _sources.First(x => x.Id == sourceId).Icon; + public string GetSourceIcon(string sourceId) { + return (config.GetValue("Save:SourceIcon") + ? Get(sourceId) + : _sources.First(x => x.Id == sourceId).Icon) + ?? "favicon_outline.png"; } public Task AddMangaAsync(Manga manga) { @@ -221,7 +222,7 @@ public async Task> ListSourcesAsync() { var tasks = _sources .Select(async source => { if (memoryCache.TryGetValue(source.Id, out _)) { - return source; + return; } var path = PathMaker @@ -243,10 +244,11 @@ public async Task> ListSourcesAsync() { } memoryCache.Set(source.Id, path.WithIcon(source.Icon)); - return source; }); - return await Task.WhenAll(tasks); + await Task.WhenAll(tasks); + return _sources + .ToArray(); } public async Task DoesSourceExistAsync(string sourceId) { diff --git a/src/Sources/Abstractions/HanmaAbstraction.cs b/src/Sources/Abstractions/HanmaAbstraction.cs index 49ed7a8..624d795 100644 --- a/src/Sources/Abstractions/HanmaAbstraction.cs +++ b/src/Sources/Abstractions/HanmaAbstraction.cs @@ -25,10 +25,10 @@ private HanmaAbstraction(ILogger logger, HttpHandler httpHandler, string name, s _url = url; } - public async Task?> GetMangasAsync() { + public async Task> GetMangasAsync() { using var document = await _httpHandler.ParseAsync($"{_url}/manga-list"); var lastPage = int.Parse(document - .QuerySelector("a.paging_prevnext.next") + .QuerySelector("a.paging_prevnext.next")! .As().Href[^2..]); var urls = await Enumerable @@ -51,7 +51,7 @@ private HanmaAbstraction(ILogger logger, HttpHandler httpHandler, string name, s .GroupBy(x => x.Id) .Select(x => { if (x.Count() == 1) { - return x.FirstOrDefault(); + return x.First(); } var lst = x.ToArray(); @@ -64,16 +64,24 @@ private HanmaAbstraction(ILogger logger, HttpHandler httpHandler, string name, s .ToArray(); } - public async Task GetMangaAsync(string url) { + public async Task GetMangaAsync(string url) { using var document = await _httpHandler.ParseAsync(url); try { var manga = new Manga { Author = GetInfoValue("Author"), - Name = document.QuerySelector("span.series-name > a")?.TextContent, - Summary = document.QuerySelector("div.summary-content").TextContent.Clean(), - Cover = document.QuerySelector("div.img-in-ratio").TextContent, + Name = document + .QuerySelector("span.series-name > a")! + .TextContent, + Summary = document + .QuerySelector("div.summary-content")! + .TextContent + .Clean(), + Cover = document + .QuerySelector("div.img-in-ratio")! + .TextContent, Genre = GetInfoValue("Genre").Split(' '), + SourceId = _name.GetIdFromName(), Metonyms = new[] { GetInfoValue("Other name"), GetInfoValue("Doujinshi") @@ -82,9 +90,15 @@ private HanmaAbstraction(ILogger logger, HttpHandler httpHandler, string name, s .QuerySelectorAll("ul.list-chapters > a") .Select(x => new Chapter { Url = x.As().Href, - Name = x.QuerySelector("div.chapter-name").TextContent, + Name = x + .QuerySelector("div.chapter-name")! + .TextContent, ReleasedOn = DateOnly.ParseExact( - x.QuerySelector("div.chapter-time").TextContent.Split('-')[1].Trim(), + x + .QuerySelector("div.chapter-time")! + .TextContent + .Split('-')[1] + .Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture) }) @@ -95,10 +109,9 @@ private HanmaAbstraction(ILogger logger, HttpHandler httpHandler, string name, s } catch (Exception exception) { _logger.LogError("{}\n{}", url, exception); + throw; } - return default; - string GetInfoValue(string infoName) { var infoElement = document .QuerySelectorAll("span.info-name") @@ -109,8 +122,8 @@ string GetInfoValue(string infoName) { return infoElement .ParentElement - ?.QuerySelector("span.info-value") - ?.TextContent; + ?.QuerySelector("span.info-value")! + .TextContent!; } } @@ -118,12 +131,14 @@ public async Task FetchChapterAsync(Chapter chapter) { using var document = await _httpHandler.ParseAsync(chapter.Url); IElement element; do { - element = document.All.FirstOrDefault(x => x is { LocalName: "div", Id: "chapter-content" }); - } while (element == default && element.Children.Length == 0); + element = document + .All + .First(x => x is { LocalName: "div", Id: "chapter-content" }); + } while (element.Children.Length == 0); chapter.Pages = element .Children - .Select(x => x.Attributes[1].Value) + .Select(x => x.Attributes[1]!.Value) .ToArray(); return chapter; } diff --git a/src/Sources/ArenaScansSource.cs b/src/Sources/ArenaScansSource.cs deleted file mode 100644 index c0c9aa7..0000000 --- a/src/Sources/ArenaScansSource.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Grimoire.Handlers; -using Grimoire.Models; -using Grimoire.Sources.Abstractions; -using Grimoire.Sources.Interfaces; - -namespace Grimoire.Sources; - -public class ArenaScansSource( - ILogger logger, - HttpHandler httpHandler) : IGrimoireSource { - public string Name - => "Arena Scans"; - - public string Url - => "https://team11x11.fun/"; - - public string Icon - => $"{Url}/favicon.ico"; - - public Task> GetMangasAsync() { - return WordPressAbstraction - .Helper(logger, httpHandler, Name, Url) - .GetMangasAsync(); - } - - public Task GetMangaAsync(string url) { - return WordPressAbstraction - .Helper(logger, httpHandler, Name, Url) - .GetMangaAsync(url); - } - - public Task FetchChapterAsync(Chapter chapter) { - return WordPressAbstraction - .Helper(logger, httpHandler, Name, Url) - .FetchChapterAsync(chapter); - } -} \ No newline at end of file