core: Implement System.CommandLine

This commit is contained in:
CakesTwix 2024-08-02 10:29:51 +03:00
parent 91d1c74d78
commit de398badab
Signed by: CakesTwix
GPG key ID: 7B11051D5CE19825
2 changed files with 61 additions and 37 deletions

View file

@ -1,49 +1,72 @@
using EpubSharp; using EpubSharp;
using System.CommandLine;
using Smakolykytl2Epub.Models; using Smakolykytl2Epub.Models;
using Smakolykytl2Epub.Utils; using Smakolykytl2Epub.Utils;
var client = new HttpClient(); // CLI
var titleIDOption = new Option<int>(
aliases: ["--title", "-t"],
description: "Title ID, can be taken from the link"
) {IsRequired = true};
var chapterIDOption = new Option<int>(
aliases: ["--chapter", "-c"],
description: "Chapter ID, can be taken from the link"
) {IsRequired = true};
int titleId = int.Parse(args[0]); var rootCommand = new RootCommand("A simple ranobe loader for Smakolykytl :)");
int chapsterId = int.Parse(args[1]) - 1; rootCommand.AddOption(titleIDOption);
rootCommand.AddOption(chapterIDOption);
var projectTitle = await Ranobe.GetById(titleId); // Run main program
if (projectTitle != null) rootCommand.SetHandler(DownloadTitleAsync, titleIDOption, chapterIDOption);
{
// Print
var project = projectTitle.project;
Console.WriteLine(project.title);
Console.WriteLine(project.alternatives);
Console.WriteLine(project.description);
// Basic Info return rootCommand.InvokeAsync(args).Result;
var writer = new EpubWriter();
writer.AddAuthor(project.author);
writer.SetTitle(project.title);
// Add Cover Image // Main Program
using (var response = await client.GetAsync(project.image.url)) static async Task DownloadTitleAsync(int titleID, int chapterID){
var client = new HttpClient();
var projectTitle = await Ranobe.GetById(titleID);
if (projectTitle.project != null)
{ {
var imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); // Print
writer.SetCover(imageBytes, ImageFormat.Png); var project = projectTitle.project;
}
Console.WriteLine(project.title);
Console.WriteLine(project.alternatives);
Console.WriteLine(project.description);
Books? books = await Ranobe.GetChaptersById(titleId); // Basic Info
Book? book = books.books[chapsterId]; var writer = new EpubWriter();
writer.AddAuthor(project.author);
writer.SetTitle(project.title);
if (book != null) // Add Cover Image
{ using (var response = await client.GetAsync(project.image.url))
foreach (var item in book.chapters)
{ {
Console.WriteLine("Завантаження розділу: {0}", item.title); var imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
var content = (await Ranobe.GetChapterById(item.id))!.chapter.content; writer.SetCover(imageBytes, ImageFormat.Png);
writer.AddChapter(item.title, HtmlConverter.ConvertJsonToHtml(content));
Thread.Sleep(1000);
} }
// Done Books? books = await Ranobe.GetChaptersById(titleID);
var fileName = string.Format("{0} - {1}.epub", project.title, book.title); Book? book = books.books[chapterID];
writer.Write(fileName);
Console.WriteLine("Файл збережено як: {0}", fileName); if (book != null)
{
foreach (var item in book.chapters)
{
Console.WriteLine("Завантаження розділу: {0}", item.title);
var content = (await Ranobe.GetChapterById(item.id))!.chapter.content;
writer.AddChapter(item.title, HtmlConverter.ConvertJsonToHtml(content));
Thread.Sleep(1000);
}
// Done
var fileName = string.Format("{0} - {1}.epub", project.title, book.title);
writer.Write(fileName);
Console.WriteLine("Файл збережено як: {0}", fileName);
}
} else {
Console.WriteLine("Нічого не знайшли :(");
} }
} }

View file

@ -8,9 +8,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AngleSharp" Version="1.1.2"/> <PackageReference Include="AngleSharp" Version="1.1.2" />
<PackageReference Include="EpubSharp.dll" Version="1.1.5"/> <PackageReference Include="EpubSharp.dll" Version="1.1.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>