Skip to content

Commit

Permalink
finished omega scans
Browse files Browse the repository at this point in the history
  • Loading branch information
Yucked committed Nov 15, 2023
1 parent b7b9409 commit c090cc0
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions src/Sources/OmegaScansSource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections.Concurrent;
using System.Text.Json;
using AngleSharp.Html.Dom;
using Grimoire.Handlers;
using Grimoire.Helpers;
using Grimoire.Models;
using Grimoire.Sources.Interfaces;

Expand All @@ -17,9 +20,9 @@ public string Url
public string Icon
=> "https://omegascans.org/icon.png";

private readonly Dictionary<string, (string Name, string Cover, string Genre)> _apiCache = new();
private readonly ConcurrentDictionary<string, (string Name, string Cover, string Genre)> _apiCache = new();

public async Task<IReadOnlyList<Manga>> GetMangasAsync() {
public async Task<IReadOnlyList<Manga>?> GetMangasAsync() {
var stream =
await httpHandler.GetStreamAsync($"{Url}/query?visibility=Public&series_type=All&perPage=100");
using var document = await JsonDocument.ParseAsync(stream!);
Expand All @@ -33,7 +36,7 @@ public async Task<IReadOnlyList<Manga>> GetMangasAsync() {
.AsParallel()
.Select(x => {
var slug = x.GetProperty("series_slug").GetString()!;
_apiCache.Add(slug,
_apiCache.TryAdd(slug,
(x.GetProperty("title").GetString()!,
x.GetProperty("thumbnail").GetString()!,
x.GetProperty("series_type").GetString()!));
Expand All @@ -43,21 +46,60 @@ public async Task<IReadOnlyList<Manga>> GetMangasAsync() {

var mangas = await Task.WhenAll(tasks);
_apiCache.Clear();
return mangas;
return mangas!;
}

public async Task<Manga> GetMangaAsync(string url) {
public async Task<Manga?> GetMangaAsync(string url) {
var document = await httpHandler.ParseAsync(url);
if (document == null) {
return default;
}

var cached = _apiCache[url.Split('/')[^1]];
var manga = new Manga {
Name = cached.Name,
Cover = cached.Cover,
Genre = [cached.Genre],
Author = document
.QuerySelectorAll("div.flex > p")
.FirstOrDefault(x => x.TextContent.Contains("Author:"))
?.Children[^1]
.TextContent!,
Summary = document
.QuerySelector("div.bg-gray-800 > p")
.QuerySelector("div.bg-gray-800 > p")!
.TextContent,
Metonyms = document
.QuerySelector("div.col-span-12 > p.text-center")!
.TextContent
.Split('|'),
Chapters = document
.QuerySelectorAll("ul.grid > a.text-gray-50")
.Select(x => new Chapter {
Name = x.QuerySelector("div.flex > span.m-0")!.TextContent,
ReleasedOn = x.QuerySelector("div.flex > span.block")!.TextContent
.ToDate(),
Url = x.As<IHtmlAnchorElement>().Href
})
.ToArray(),
LastFetch = DateTimeOffset.Now,
Url = url,
SourceId = nameof(OmegaScansSource).GetIdFromName()
};

return manga;
}

public async Task<Chapter> FetchChapterAsync(Chapter chapter) {
throw new NotImplementedException();
using var document = await httpHandler.ParseAsync(chapter.Url);
if (document == null) {
return chapter;
}

chapter.Pages = document
.QuerySelectorAll("p.flex > img")
.Select(x => x.As<IHtmlAnchorElement>().Href)
.ToArray();

return chapter;
}
}

0 comments on commit c090cc0

Please sign in to comment.