Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
OnlyFart committed Dec 29, 2024
1 parent ea7df81 commit 51ed284
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latestmajor</LangVersion>
<PackageId>Core</PackageId>
<Version>2.6.2</Version>
<Version>2.6.3</Version>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>

Expand Down
7 changes: 5 additions & 2 deletions Core/Logic/Getters/Renovels/RemangaGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ namespace Core.Logic.Getters.Renovels;

public class RemangaGetter : RenovelsGetterBase {
public RemangaGetter(BookGetterConfig config) : base(config) { }

protected override Uri SystemUrl => new("https://remanga.org/");

protected override string Segment => "manga";
protected override HtmlDocument GetChapterAsHtml(RenovelsApiResponse<RenovelsChapter> response) {

protected override HtmlDocument GetChapterAsHtml(RenovelsChapter response) {
var sb = new StringBuilder();

foreach (var obj in response.Content.Pages) {
foreach (var obj in response.Pages) {
switch (obj) {
case JsonObject:
sb.Append($"<img src='{obj.Deserialize<RenovelsPage>().Link}'/>");
Expand Down
7 changes: 5 additions & 2 deletions Core/Logic/Getters/Renovels/RenovelsGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ namespace Core.Logic.Getters.Renovels;

public class RenovelsGetter : RenovelsGetterBase {
public RenovelsGetter(BookGetterConfig config) : base(config) { }

protected override Uri SystemUrl => new("https://renovels.org/");

protected override string Segment => "novel";
protected override HtmlDocument GetChapterAsHtml(RenovelsApiResponse<RenovelsChapter> response) {
return response.Content.Content.AsHtmlDoc();

protected override HtmlDocument GetChapterAsHtml(RenovelsChapter response) {
return response.Content.AsHtmlDoc();
}
}
32 changes: 19 additions & 13 deletions Core/Logic/Getters/Renovels/RenovelsGetterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected RenovelsGetterBase(BookGetterConfig config) : base(config) { }

protected abstract string Segment { get; }

protected abstract HtmlDocument GetChapterAsHtml(RenovelsApiResponse<RenovelsChapter> response);
protected abstract HtmlDocument GetChapterAsHtml(RenovelsChapter response);

protected override string GetId(Uri url) => url.GetSegment(2);

Expand All @@ -43,7 +43,7 @@ public override async Task Authorize() {
password = Config.Options.Password
};

var response = await Config.Client.PostAsJsonAsync("https://api.recomics.org/api/users/login/".AsUri(), payload);
var response = await Config.Client.PostAsJsonAsync("https://api.renovels.org/api/users/login/".AsUri(), payload);
var data = await response.Content.ReadFromJsonAsync<RenovelsApiResponse<JsonNode>>();
if (string.IsNullOrWhiteSpace(data.Message)) {
var auth = data.Content.Deserialize<RenovelsAuthResponse>();
Expand All @@ -70,17 +70,21 @@ public override async Task<Book> Get(Uri url) {
}

private Author GetAuthor(RenovelsContent content) {
if (content.Publishers.Length == 0) {
if (content.Branches.Length == 0) {
return new Author("Renovels");
}

if (content.Branches[0].Publishers.Length == 0) {
return new Author("Renovels");
}

var author = content.Publishers[0];
var author = content.Branches[0].Publishers[0];
return new Author(author.Name, SystemUrl.MakeRelativeUri($"/team/{author.Dir}"));
}

private async Task<RenovelsContent> GetContent(string bookId) {
var response = await Config.Client.GetFromJsonAsync<RenovelsApiResponse<RenovelsContent>>(_apiUrl.MakeRelativeUri($"/api/titles/{bookId}/"));
return response.Content;
var response = await Config.Client.GetFromJsonAsync<RenovelsContent>(_apiUrl.MakeRelativeUri($"/api/v2/titles/{bookId}/"));
return response;
}

private async Task<IEnumerable<Chapter>> FillChapters(RenovelsContent content, Uri url) {
Expand All @@ -91,8 +95,10 @@ private async Task<IEnumerable<Chapter>> FillChapters(RenovelsContent content, U

foreach (var ranobeChapter in await GetToc(content)) {
Config.Logger.LogInformation($"Загружаю главу {ranobeChapter.Title.CoverQuotes()}");

var chapter = new Chapter();
var doc = await GetChapter(ranobeChapter);

chapter.Images = await GetImages(doc, url);
chapter.Content = doc.DocumentNode.InnerHtml;
chapter.Title = ranobeChapter.Title;
Expand All @@ -104,25 +110,25 @@ private async Task<IEnumerable<Chapter>> FillChapters(RenovelsContent content, U
}

private async Task<HtmlDocument> GetChapter(RenovelsChapter ranobeChapter) {
var makeRelativeUri = _apiUrl.MakeRelativeUri($"/api/titles/chapters/{ranobeChapter.Id}/");
var response = await Config.Client.GetFromJsonAsync<RenovelsApiResponse<RenovelsChapter>>(makeRelativeUri);
var makeRelativeUri = _apiUrl.MakeRelativeUri($"/api/v2/titles/chapters/{ranobeChapter.Id}/");
var response = await Config.Client.GetFromJsonAsync<RenovelsChapter>(makeRelativeUri);
return GetChapterAsHtml(response);
}

private Task<TempFile> GetCover(RenovelsContent book, Uri bookUri) {
var imagePath = book.Img.GetValueOrDefault("high", null) ?? book.Img.FirstOrDefault().Value;
var imagePath = book.Cover.GetValueOrDefault("high", null) ?? book.Cover.FirstOrDefault().Value;
return !string.IsNullOrWhiteSpace(imagePath) ? SaveImage(bookUri.MakeRelativeUri(imagePath)) : Task.FromResult(default(TempFile));
}

private async Task<IEnumerable<RenovelsChapter>> GetToc(RenovelsContent content) {
var result = new List<RenovelsChapter>();

for (var i = 1;; i++) {
var uri = _apiUrl.MakeRelativeUri($"/api/titles/chapters/?branch_id={content.Branches[0].Id}&ordering=-index&user_data=1&count=40&page={i}");
var response = await Config.Client.GetFromJsonAsync<RenovelsApiResponse<RenovelsChapter[]>>(uri);
result.AddRange(response!.Content);
var uri = _apiUrl.MakeRelativeUri($"/api/v2/titles/chapters/?branch_id={content.Branches[0].Id}&ordering=-index&user_data=1&count=40&page={i}");
var response = await Config.Client.GetFromJsonAsync<RenovelsTocResponse>(uri);
result.AddRange(response.Results);

if (response.Content.Length < 40) {
if (response.Results.Length < 40) {
result.Reverse();
return SliceToc(result.Where(c => !c.IsPaid || c.IsBought == true).ToList(), c => c.Name);
}
Expand Down
3 changes: 3 additions & 0 deletions Core/Types/Renovels/RenovelsBranch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ namespace Core.Types.Renovels;
public class RenovelsBranch {
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("publishers")]
public RenovelsPublisher[] Publishers { get; set; }
}
7 changes: 2 additions & 5 deletions Core/Types/Renovels/RenovelsContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
namespace Core.Types.Renovels;

public class RenovelsContent {
[JsonPropertyName("img")]
public Dictionary<string, string> Img { get; set; }
[JsonPropertyName("cover")]
public Dictionary<string, string> Cover { get; set; }

[JsonPropertyName("main_name")]
public string MainName { get; set; }
Expand All @@ -21,7 +21,4 @@ public class RenovelsContent {

[JsonPropertyName("branches")]
public RenovelsBranch[] Branches { get; set; }

[JsonPropertyName("publishers")]
public RenovelsPublisher[] Publishers { get; set; }
}
8 changes: 8 additions & 0 deletions Core/Types/Renovels/RenovelsTocResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace Core.Types.Renovels;

public class RenovelsTocResponse {
[JsonPropertyName("results")]
public RenovelsChapter[] Results { get; set; }
}

0 comments on commit 51ed284

Please sign in to comment.