commit 3d003de237ff42d1ce8ec01f462157618d48cb87 Author: CakesTwix Date: Sun Aug 13 13:59:16 2023 +0300 core: Simple implement diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/Models/Book.cs b/Models/Book.cs new file mode 100644 index 0000000..8bc9e88 --- /dev/null +++ b/Models/Book.cs @@ -0,0 +1,27 @@ +namespace Smakolykytl2Epub.Models; + +// Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); +public class Book +{ + public int id { get; set; } + public int rank { get; set; } + public string title { get; set; } + public List 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 images { get; set; } + public Book book { get; set; } +} + +public class Books +{ + public List books { get; set; } +} + diff --git a/Models/Chapters.cs b/Models/Chapters.cs new file mode 100644 index 0000000..2943611 --- /dev/null +++ b/Models/Chapters.cs @@ -0,0 +1,7 @@ +namespace Smakolykytl2Epub.Models; + +public class Chapters +{ + public Chapter chapter { get; set; } +} + diff --git a/Models/Projects.cs b/Models/Projects.cs new file mode 100644 index 0000000..c8f9ebc --- /dev/null +++ b/Models/Projects.cs @@ -0,0 +1,45 @@ +namespace Smakolykytl2Epub.Models; + +// Root myDeserializedClass = JsonConvert.DeserializeObject(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 tags { get; set; } + public List genres { get; set; } +} + +public class Projects +{ + public Project project { get; set; } +} + +public class Tag +{ + public int id { get; set; } + public string title { get; set; } +} + diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..ae04afd --- /dev/null +++ b/Program.cs @@ -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"); +} \ No newline at end of file diff --git a/Smakolykytl2Epub.csproj b/Smakolykytl2Epub.csproj new file mode 100644 index 0000000..0430e72 --- /dev/null +++ b/Smakolykytl2Epub.csproj @@ -0,0 +1,16 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + + diff --git a/Smakolykytl2Epub.sln b/Smakolykytl2Epub.sln new file mode 100644 index 0000000..5fc1800 --- /dev/null +++ b/Smakolykytl2Epub.sln @@ -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 diff --git a/Utils/Json2HTML.cs b/Utils/Json2HTML.cs new file mode 100644 index 0000000..37c10fb --- /dev/null +++ b/Utils/Json2HTML.cs @@ -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 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 { 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(token.ToString()); + foreach (var str in text?.content!) + { + if (str.type == "hardBreak") html += "
"; + html += str.text; + + } + + html += "
"; + } + else + { + html += token.ToString(); + } + + return html; + } +} \ No newline at end of file diff --git a/Utils/Ranobe.cs b/Utils/Ranobe.cs new file mode 100644 index 0000000..16d9d73 --- /dev/null +++ b/Utils/Ranobe.cs @@ -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 GetById(int id) + { + var response = await Client.GetAsync(ApiUrl + "projects/" + id.ToString()); + var content = await response.Content.ReadAsStringAsync(); + // Console.WriteLine(content); + return JsonConvert.DeserializeObject(content); + } + + public static async Task 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(content); + } + + public static async Task GetChapterById(int id) + { + var response = await Client.GetAsync(ApiUrl + "chapters/" + id.ToString()); + var content = await response.Content.ReadAsStringAsync(); + // Console.WriteLine(content); + return JsonConvert.DeserializeObject(content); + } +} \ No newline at end of file