-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,72 @@ | ||
using EpubSharp; | ||
using System.CommandLine; | ||
using Smakolykytl2Epub.Models; | ||
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]); | ||
int chapsterId = int.Parse(args[1]) - 1; | ||
var rootCommand = new RootCommand("A simple ranobe loader for Smakolykytl :)"); | ||
rootCommand.AddOption(titleIDOption); | ||
rootCommand.AddOption(chapterIDOption); | ||
|
||
var projectTitle = await Ranobe.GetById(titleId); | ||
if (projectTitle != null) | ||
{ | ||
var project = projectTitle.project; | ||
Console.WriteLine(project.title); | ||
Console.WriteLine(project.alternatives); | ||
Console.WriteLine(project.description); | ||
// Run main program | ||
rootCommand.SetHandler(DownloadTitleAsync, titleIDOption, chapterIDOption); | ||
|
||
// Basic Info | ||
var writer = new EpubWriter(); | ||
writer.AddAuthor(project.author); | ||
writer.SetTitle(project.title); | ||
return rootCommand.InvokeAsync(args).Result; | ||
|
||
// Add Cover Image | ||
using (var response = await client.GetAsync(project.image.url)) | ||
// Main Program | ||
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); | ||
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); | ||
Book? book = books.books[chapsterId]; | ||
// Basic Info | ||
var writer = new EpubWriter(); | ||
writer.AddAuthor(project.author); | ||
writer.SetTitle(project.title); | ||
|
||
if (book != null) | ||
{ | ||
foreach (var item in book.chapters) | ||
// Add Cover Image | ||
using (var response = await client.GetAsync(project.image.url)) | ||
{ | ||
Console.WriteLine("Завантаження розділу: {0}", item.title); | ||
var content = (await Ranobe.GetChapterById(item.id))!.chapter.content; | ||
writer.AddChapter(item.title, HtmlConverter.ConvertJsonToHtml(content)); | ||
Thread.Sleep(1000); | ||
var imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); | ||
writer.SetCover(imageBytes, ImageFormat.Png); | ||
} | ||
|
||
// Done | ||
var fileName = string.Format("{0} - {1}.epub", project.title, book.title); | ||
writer.Write(fileName); | ||
Console.WriteLine("Файл збережено як: {0}", fileName); | ||
Books? books = await Ranobe.GetChaptersById(titleID); | ||
Book? book = books.books[chapterID]; | ||
|
||
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("Нічого не знайшли :("); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters