2
0
Tranga/Tranga-CLI/Tranga_Cli.cs

336 lines
13 KiB
C#
Raw Permalink Normal View History

using System.Globalization;
using Tranga;
2023-05-18 15:49:08 +02:00
using Tranga.Connectors;
2023-05-18 18:55:11 +02:00
namespace Tranga_CLI;
public static class Tranga_Cli
2023-05-18 15:49:08 +02:00
{
public static void Main(string[] args)
{
2023-05-19 20:33:53 +02:00
string folderPath = Directory.GetCurrentDirectory();
string settingsPath = Path.Join(Directory.GetCurrentDirectory(), "lastPath.setting");
if (File.Exists(settingsPath))
folderPath = File.ReadAllText(settingsPath);
Console.WriteLine($"Output folder path [{folderPath}]:");
string? tmpPath = Console.ReadLine();
while(tmpPath is null)
tmpPath = Console.ReadLine();
if(tmpPath.Length > 0)
folderPath = tmpPath;
File.WriteAllText(settingsPath, folderPath);
2023-05-19 17:39:39 +02:00
Console.Write("Mode (D: Interactive only, T: TaskManager):");
2023-05-19 16:27:56 +02:00
ConsoleKeyInfo mode = Console.ReadKey();
while (mode.Key != ConsoleKey.D && mode.Key != ConsoleKey.T)
mode = Console.ReadKey();
Console.WriteLine();
if(mode.Key == ConsoleKey.D)
DownloadNow(folderPath);
else if (mode.Key == ConsoleKey.T)
TaskMode(folderPath);
}
private static void TaskMode(string folderPath)
{
TaskManager taskManager = new TaskManager(folderPath);
ConsoleKey selection = ConsoleKey.NoName;
int menu = 0;
while (selection != ConsoleKey.Escape && selection != ConsoleKey.Q)
{
switch (menu)
{
case 1:
2023-05-19 19:23:37 +02:00
PrintTasks(taskManager.GetAllTasks());
2023-05-19 16:27:56 +02:00
Console.WriteLine("Press any key.");
Console.ReadKey();
menu = 0;
break;
case 2:
Connector connector = SelectConnector(folderPath, taskManager.GetAvailableConnectors().Values.ToArray());
TrangaTask.Task task = SelectTask();
Publication? publication = null;
if(task != TrangaTask.Task.UpdatePublications)
publication = SelectPublication(connector);
TimeSpan reoccurrence = SelectReoccurrence();
2023-05-20 00:37:10 +02:00
TrangaTask newTask = taskManager.AddTask(task, connector.name, publication, reoccurrence, "en");
Console.WriteLine(newTask);
2023-05-19 16:27:56 +02:00
Console.WriteLine("Press any key.");
Console.ReadKey();
menu = 0;
break;
case 3:
RemoveTask(taskManager);
Console.WriteLine("Press any key.");
Console.ReadKey();
menu = 0;
break;
case 4:
2023-05-19 23:01:04 +02:00
ExecuteTaskNow(taskManager);
Console.WriteLine("Press any key.");
Console.ReadKey();
menu = 0;
break;
case 5:
Console.WriteLine("Search-Query (Name):");
string? query = Console.ReadLine();
while (query is null || query.Length < 1)
query = Console.ReadLine();
PrintTasks(taskManager.GetAllTasks().Where(qTask =>
((Publication)qTask.publication!).sortName.ToLower()
.Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray());
Console.WriteLine("Press any key.");
Console.ReadKey();
menu = 0;
break;
case 6:
PrintTasks(taskManager.GetAllTasks().Where(eTask => eTask.isBeingExecuted).ToArray());
Console.WriteLine("Press any key.");
Console.ReadKey();
menu = 0;
break;
2023-05-19 16:27:56 +02:00
default:
2023-05-19 19:23:37 +02:00
selection = Menu(taskManager, folderPath);
2023-05-19 16:27:56 +02:00
switch (selection)
{
case ConsoleKey.L:
menu = 1;
break;
case ConsoleKey.C:
menu = 2;
break;
case ConsoleKey.D:
menu = 3;
break;
case ConsoleKey.E:
menu = 4;
break;
2023-05-19 23:00:04 +02:00
case ConsoleKey.U:
menu = 0;
break;
case ConsoleKey.S:
menu = 5;
break;
case ConsoleKey.R:
menu = 6;
break;
2023-05-19 16:27:56 +02:00
default:
menu = 0;
break;
}
break;
}
}
if (taskManager.GetAllTasks().Any(task => task.isBeingExecuted))
{
Console.WriteLine("Force quit (Even with running tasks?) y/N");
selection = Console.ReadKey().Key;
taskManager.Shutdown(selection == ConsoleKey.Y);
}else
2023-05-20 01:06:12 +02:00
// ReSharper disable once RedundantArgumentDefaultValue Better readability
taskManager.Shutdown(false);
2023-05-19 16:27:56 +02:00
}
2023-05-19 19:23:37 +02:00
private static ConsoleKey Menu(TaskManager taskManager, string folderPath)
2023-05-19 16:27:56 +02:00
{
2023-05-19 19:23:37 +02:00
int taskCount = taskManager.GetAllTasks().Length;
int taskRunningCount = taskManager.GetAllTasks().Count(task => task.isBeingExecuted);
2023-05-19 16:27:56 +02:00
Console.Clear();
2023-05-19 19:23:37 +02:00
Console.WriteLine($"Download Folder: {folderPath} Tasks (Running/Total): {taskRunningCount}/{taskCount}");
2023-05-19 23:00:04 +02:00
Console.WriteLine("U: Update this Screen");
2023-05-19 16:27:56 +02:00
Console.WriteLine("L: List tasks");
Console.WriteLine("C: Create Task");
Console.WriteLine("D: Delete Task");
Console.WriteLine("E: Execute Task now");
Console.WriteLine("S: Search Task");
Console.WriteLine("R: Running Tasks");
2023-05-19 20:34:09 +02:00
Console.WriteLine("Q: Exit");
2023-05-19 16:27:56 +02:00
ConsoleKey selection = Console.ReadKey().Key;
Console.WriteLine();
return selection;
}
2023-05-19 19:23:37 +02:00
private static void PrintTasks(TrangaTask[] tasks)
2023-05-19 16:27:56 +02:00
{
2023-05-19 19:23:37 +02:00
int taskCount = tasks.Length;
int taskRunningCount = tasks.Count(task => task.isBeingExecuted);
2023-05-19 16:27:56 +02:00
Console.Clear();
int tIndex = 0;
2023-05-19 19:23:37 +02:00
Console.WriteLine($"Tasks (Running/Total): {taskRunningCount}/{taskCount}");
2023-05-19 16:27:56 +02:00
foreach(TrangaTask trangaTask in tasks)
2023-05-20 01:06:12 +02:00
Console.WriteLine($"{tIndex++:000}: {trangaTask}");
2023-05-19 16:27:56 +02:00
}
2023-05-19 23:01:04 +02:00
private static void ExecuteTaskNow(TaskManager taskManager)
{
TrangaTask[] tasks = taskManager.GetAllTasks();
if (tasks.Length < 1)
{
Console.Clear();
Console.WriteLine("There are no available Tasks.");
return;
}
PrintTasks(tasks);
2023-05-19 23:00:04 +02:00
Console.WriteLine($"Select Task (0-{tasks.Length - 1}):");
string? selectedTask = Console.ReadLine();
while(selectedTask is null || selectedTask.Length < 1)
selectedTask = Console.ReadLine();
int selectedTaskIndex = Convert.ToInt32(selectedTask);
taskManager.ExecuteTaskNow(tasks[selectedTaskIndex]);
}
2023-05-19 16:27:56 +02:00
private static void RemoveTask(TaskManager taskManager)
{
TrangaTask[] tasks = taskManager.GetAllTasks();
if (tasks.Length < 1)
{
Console.Clear();
Console.WriteLine("There are no available Tasks.");
return;
}
2023-05-19 19:23:37 +02:00
PrintTasks(tasks);
2023-05-19 23:00:04 +02:00
Console.WriteLine($"Select Task (0-{tasks.Length - 1}):");
2023-05-19 16:27:56 +02:00
string? selectedTask = Console.ReadLine();
while(selectedTask is null || selectedTask.Length < 1)
selectedTask = Console.ReadLine();
int selectedTaskIndex = Convert.ToInt32(selectedTask);
taskManager.RemoveTask(tasks[selectedTaskIndex].task, tasks[selectedTaskIndex].connectorName, tasks[selectedTaskIndex].publication);
}
private static TrangaTask.Task SelectTask()
{
Console.Clear();
string[] taskNames = Enum.GetNames<TrangaTask.Task>();
int tIndex = 0;
Console.WriteLine("Available Tasks:");
foreach (string taskName in taskNames)
Console.WriteLine($"{tIndex++}: {taskName}");
2023-05-19 23:00:04 +02:00
Console.WriteLine($"Select Task (0-{taskNames.Length - 1}):");
2023-05-19 16:27:56 +02:00
string? selectedTask = Console.ReadLine();
while(selectedTask is null || selectedTask.Length < 1)
selectedTask = Console.ReadLine();
int selectedTaskIndex = Convert.ToInt32(selectedTask);
string selectedTaskName = taskNames[selectedTaskIndex];
return Enum.Parse<TrangaTask.Task>(selectedTaskName);
}
private static TimeSpan SelectReoccurrence()
2023-05-19 16:27:56 +02:00
{
Console.WriteLine("Select reoccurrence Timer (Format hh:mm:ss):");
return TimeSpan.Parse(Console.ReadLine()!, new CultureInfo("en-US"));
}
private static void DownloadNow(string folderPath)
{
Connector connector = SelectConnector(folderPath);
2023-05-19 16:27:56 +02:00
Publication publication = SelectPublication(connector);
2023-05-19 16:27:56 +02:00
Chapter[] downloadChapters = SelectChapters(connector, publication);
2023-05-18 20:26:47 +02:00
if (downloadChapters.Length > 0)
{
2023-05-19 16:27:56 +02:00
connector.DownloadCover(publication);
connector.SaveSeriesInfo(publication);
2023-05-18 20:26:47 +02:00
}
foreach (Chapter chapter in downloadChapters)
{
2023-05-19 16:27:56 +02:00
Console.WriteLine($"Downloading {publication.sortName} V{chapter.volumeNumber}C{chapter.chapterNumber}");
connector.DownloadChapter(publication, chapter);
}
}
2023-05-19 16:27:56 +02:00
private static Connector SelectConnector(string folderPath, Connector[]? availableConnectors = null)
{
2023-05-19 16:27:56 +02:00
Console.Clear();
Connector[] connectors = availableConnectors ?? new Connector[] { new MangaDex(folderPath) };
int cIndex = 0;
Console.WriteLine("Connectors:");
foreach (Connector connector in connectors)
Console.WriteLine($"{cIndex++}: {connector.name}");
Console.WriteLine($"Select Connector (0-{connectors.Length - 1}):");
string? selectedConnector = Console.ReadLine();
while(selectedConnector is null || selectedConnector.Length < 1)
selectedConnector = Console.ReadLine();
int selectedConnectorIndex = Convert.ToInt32(selectedConnector);
2023-05-19 16:27:56 +02:00
return connectors[selectedConnectorIndex];
}
2023-05-19 16:27:56 +02:00
private static Publication SelectPublication(Connector connector)
{
2023-05-19 16:27:56 +02:00
Console.Clear();
Console.WriteLine($"Connector: {connector.name}");
Console.WriteLine("Publication search query (leave empty for all):");
string? query = Console.ReadLine();
Publication[] publications = connector.GetPublications(query ?? "");
2023-05-18 17:42:47 +02:00
int pIndex = 0;
2023-05-19 16:27:56 +02:00
Console.WriteLine("Publications:");
2023-05-18 17:42:47 +02:00
foreach(Publication publication in publications)
Console.WriteLine($"{pIndex++}: {publication.sortName}");
Console.WriteLine($"Select publication to Download (0-{publications.Length - 1}):");
string? selected = Console.ReadLine();
while(selected is null || selected.Length < 1)
selected = Console.ReadLine();
return publications[Convert.ToInt32(selected)];
}
2023-05-18 17:42:47 +02:00
2023-05-19 16:27:56 +02:00
private static Chapter[] SelectChapters(Connector connector, Publication publication)
{
2023-05-19 16:27:56 +02:00
Console.Clear();
Console.WriteLine($"Connector: {connector.name} Publication: {publication.sortName}");
Chapter[] chapters = connector.GetChapters(publication, "en");
2023-05-18 17:42:47 +02:00
int cIndex = 0;
2023-05-19 16:27:56 +02:00
Console.WriteLine("Chapters:");
2023-05-18 17:42:47 +02:00
foreach (Chapter ch in chapters)
{
string name = cIndex.ToString();
if (ch.name is not null && ch.name.Length > 0)
name = ch.name;
else if (ch.chapterNumber is not null && ch.chapterNumber.Length > 0)
name = ch.chapterNumber;
2023-05-18 17:42:47 +02:00
Console.WriteLine($"{cIndex++}: {name}");
}
Console.WriteLine($"Select Chapters to download (0-{chapters.Length - 1}) [range x-y or 'a' for all]: ");
string? selected = Console.ReadLine();
2023-05-18 17:42:47 +02:00
while(selected is null || selected.Length < 1)
selected = Console.ReadLine();
int start = 0;
int end;
2023-05-18 17:42:47 +02:00
if (selected == "a")
end = chapters.Length - 1;
else if (selected.Contains('-'))
{
string[] split = selected.Split('-');
start = Convert.ToInt32(split[0]);
end = Convert.ToInt32(split[1]);
}
else
{
start = Convert.ToInt32(selected);
end = Convert.ToInt32(selected);
}
return chapters.Skip(start).Take((end + 1)-start).ToArray();
2023-05-18 15:49:08 +02:00
}
}