-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
73 lines (60 loc) · 2.38 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using EpubSharp;
using System.CommandLine;
using Smakolykytl2Epub.Models;
using Smakolykytl2Epub.Utils;
// 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};
var rootCommand = new RootCommand("A simple ranobe loader for Smakolykytl :)");
rootCommand.AddOption(titleIDOption);
rootCommand.AddOption(chapterIDOption);
// Run main program
rootCommand.SetHandler(DownloadTitleAsync, titleIDOption, chapterIDOption);
return rootCommand.InvokeAsync(args).Result;
// Main Program
static async Task DownloadTitleAsync(int titleID, int chapterID){
var client = new HttpClient();
var projectTitle = await Ranobe.GetById(titleID);
if (projectTitle.project != null)
{
// Print
var project = projectTitle.project;
Console.WriteLine(project.title);
Console.WriteLine(project.alternatives);
Console.WriteLine(project.description);
// Basic Info
var writer = new EpubWriter();
writer.AddAuthor(project.author);
writer.SetTitle(project.title);
// Add Cover Image
using (var response = await client.GetAsync(project.image.url))
{
var imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
writer.SetCover(imageBytes, ImageFormat.Png);
}
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, writer));
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("Нічого не знайшли :(");
}
}