core: Simple implement

This commit is contained in:
CakesTwix 2023-08-13 13:59:16 +03:00
commit 3d003de237
Signed by: CakesTwix
GPG key ID: 7B11051D5CE19825
9 changed files with 261 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

27
Models/Book.cs Normal file
View file

@ -0,0 +1,27 @@
namespace Smakolykytl2Epub.Models;
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Book
{
public int id { get; set; }
public int rank { get; set; }
public string title { get; set; }
public List<Chapter> chapters { get; set; }
}
public class Chapter
{
public int id { get; set; }
public string title { get; set; }
public string rank { get; set; }
public string content { get; set; }
public DateTime modifiedAt { get; set; }
public List<object> images { get; set; }
public Book book { get; set; }
}
public class Books
{
public List<Book> books { get; set; }
}

7
Models/Chapters.cs Normal file
View file

@ -0,0 +1,7 @@
namespace Smakolykytl2Epub.Models;
public class Chapters
{
public Chapter chapter { get; set; }
}

45
Models/Projects.cs Normal file
View file

@ -0,0 +1,45 @@
namespace Smakolykytl2Epub.Models;
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Genre
{
public int id { get; set; }
public string title { get; set; }
}
public class Image
{
public int id { get; set; }
public string url { get; set; }
public string name { get; set; }
}
public class Project
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string author { get; set; }
public string translator { get; set; }
public DateTime modifiedAt { get; set; }
public string alternatives { get; set; }
public string release { get; set; }
public string nation { get; set; }
public string status { get; set; }
public string status_translate { get; set; }
public Image image { get; set; }
public List<Tag> tags { get; set; }
public List<Genre> genres { get; set; }
}
public class Projects
{
public Project project { get; set; }
}
public class Tag
{
public int id { get; set; }
public string title { get; set; }
}

43
Program.cs Normal file
View file

@ -0,0 +1,43 @@
using System.Text;
using EpubSharp;
using Newtonsoft.Json.Linq;
using Smakolykytl2Epub.Models;
using Smakolykytl2Epub.Utils;
var client = new HttpClient();
var projectTitle = await Ranobe.GetById(int.Parse(args[0]));
if (projectTitle != null)
{
// Print
Project project = projectTitle.project;
Console.WriteLine(project.title);
Console.WriteLine(project.alternatives);
Console.WriteLine(project.description);
// Basic Info
EpubWriter writer = new EpubWriter();
writer.AddAuthor(project.author);
writer.SetTitle(project.title);
// Add Cover Image
using (var response = await client.GetAsync(project.image.url))
{
byte[] imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
writer.SetCover(imageBytes, ImageFormat.Png);
}
var books = (await Ranobe.GetChaptersById(int.Parse(args[0])))?.books[int.Parse(args[1]) - 1];
if (books != null)
foreach (var item in books.chapters)
{
Console.WriteLine(item.title);
var content = ((await Ranobe.GetChapterById(item.id))!).chapter.content;
writer.AddChapter(item.title, HtmlConverter.ConvertJsonToHtml(content));
Thread.Sleep(1000);
}
// Done
writer.Write("new.epub");
}

16
Smakolykytl2Epub.csproj Normal file
View file

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.0.4" />
<PackageReference Include="EpubSharp.dll" Version="1.1.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

16
Smakolykytl2Epub.sln Normal file
View file

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Smakolykytl2Epub", "Smakolykytl2Epub.csproj", "{F48BD0E8-B7F5-44E9-9548-CEDB89284A41}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F48BD0E8-B7F5-44E9-9548-CEDB89284A41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F48BD0E8-B7F5-44E9-9548-CEDB89284A41}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F48BD0E8-B7F5-44E9-9548-CEDB89284A41}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F48BD0E8-B7F5-44E9-9548-CEDB89284A41}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

66
Utils/Json2HTML.cs Normal file
View file

@ -0,0 +1,66 @@
using System.Text;
using System.Text.RegularExpressions;
using AngleSharp.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Smakolykytl2Epub.Utils;
public class Content
{
public string type { get; set; }
public List<Mark> marks { get; set; }
public string text { get; set; }
}
public class Mark
{
public string type { get; set; }
}
public class TextJson
{
public string type { get; set; }
public List<Content> content { get; set; }
}
public class HtmlConverter
{
public static string ConvertJsonToHtml(string json)
{
JToken token = JToken.Parse(json);
return ConvertTokenToHtml(token);
}
private static string ConvertTokenToHtml(JToken token)
{
string html = "";
if (token is JArray)
{
foreach (JToken childToken in token.Children())
{
html += ConvertTokenToHtml(childToken);
}
}
else if (token is JObject)
{
TextJson? text = JsonConvert.DeserializeObject<TextJson>(token.ToString());
foreach (var str in text?.content!)
{
if (str.type == "hardBreak") html += "<br>";
html += str.text;
}
html += "<br>";
}
else
{
html += token.ToString();
}
return html;
}
}

36
Utils/Ranobe.cs Normal file
View file

@ -0,0 +1,36 @@
using Smakolykytl2Epub.Models;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace Smakolykytl2Epub.Utils;
public class Ranobe
{
private static readonly HttpClient Client = new HttpClient();
private const string ApiUrl = "https://api.smakolykytl.site/api/user/";
private const string SiteUrl = "https://smakolykytl.site/";
public static async Task<Projects?> GetById(int id)
{
var response = await Client.GetAsync(ApiUrl + "projects/" + id.ToString());
var content = await response.Content.ReadAsStringAsync();
// Console.WriteLine(content);
return JsonConvert.DeserializeObject<Projects>(content);
}
public static async Task<Books?> GetChaptersById(int id)
{
var response = await Client.GetAsync(ApiUrl + "projects/" + id.ToString() + "/books");
var content = await response.Content.ReadAsStringAsync();
// Console.WriteLine(content);
return JsonConvert.DeserializeObject<Books>(content);
}
public static async Task<Chapters?> GetChapterById(int id)
{
var response = await Client.GetAsync(ApiUrl + "chapters/" + id.ToString());
var content = await response.Content.ReadAsStringAsync();
// Console.WriteLine(content);
return JsonConvert.DeserializeObject<Chapters>(content);
}
}