Tranga-Website/Tranga/Tranga.cs

580 lines
24 KiB
C#
Raw Normal View History

using System.Globalization;
2023-07-31 00:31:19 +02:00
using System.Runtime.InteropServices;
2023-05-20 21:48:08 +02:00
using Logging;
2023-07-31 00:31:19 +02:00
using Tranga.API;
2023-07-30 17:01:54 +02:00
using Tranga.Connectors;
using Tranga.NotificationManagers;
using Tranga.TrangaTasks;
2023-05-18 15:49:08 +02:00
2023-07-31 00:31:19 +02:00
namespace Tranga;
2023-05-18 18:55:11 +02:00
2023-07-31 00:31:19 +02:00
public static class Tranga
2023-05-18 15:49:08 +02:00
{
public static void Main(string[] args)
{
2023-07-31 00:52:27 +02:00
bool isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API");
string downloadFolderPath = isLinux ? "/Manga" : Path.Join(applicationFolderPath, "Manga");
string logsFolderPath = isLinux ? "/var/log/Tranga" : Path.Join(applicationFolderPath, "log");
string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt");
string settingsFilePath = Path.Join(applicationFolderPath, "settings.json");
2023-05-20 21:48:08 +02:00
2023-07-31 00:31:19 +02:00
Directory.CreateDirectory(logsFolderPath);
2023-07-31 00:52:27 +02:00
Logger logger = isLinux
? new Logger(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath)
: new Logger(new[] { Logger.LoggerType.FileLogger }, Console.Out, Console.Out.Encoding, logFilePath);
2023-07-31 00:31:19 +02:00
logger.WriteLine("Tranga",value: "\n"+
"-------------------------------------------\n"+
" Starting Tranga-API\n"+
"-------------------------------------------");
2023-07-31 01:26:38 +02:00
logger.WriteLine("Tranga", "Migrating...");
2023-07-31 01:42:15 +02:00
Migrator.Migrate(settingsFilePath, logger);
2023-07-31 00:31:19 +02:00
2023-07-31 00:58:22 +02:00
TrangaSettings settings;
if (File.Exists(settingsFilePath))
{
logger.WriteLine("Tranga", $"Loading settings {settingsFilePath}");
settings = TrangaSettings.LoadSettings(settingsFilePath);
}
else
{
2023-07-31 00:58:22 +02:00
settings = new TrangaSettings(downloadFolderPath, applicationFolderPath);
settings.version = Migrator.CurrentVersion;
}
2023-07-31 00:31:19 +02:00
Directory.CreateDirectory(settings.workingDirectory);
Directory.CreateDirectory(settings.downloadLocation);
Directory.CreateDirectory(settings.coverImageCache);
2023-07-31 00:52:27 +02:00
logger.WriteLine("Tranga", $"Is Linux: {isLinux}");
2023-07-31 00:31:19 +02:00
logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}");
logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}");
logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}");
logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}");
logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}");
logger.WriteLine("Tranga", "Loading Taskmanager.");
TaskManager taskManager = new (settings, logger);
2023-07-31 00:31:19 +02:00
Server _ = new (6531, taskManager);
foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers)
nm.SendNotification("Tranga-API", "Started Tranga-API");
2023-05-19 16:27:56 +02:00
2023-07-31 00:52:27 +02:00
if(!isLinux)
2023-07-31 00:41:25 +02:00
TaskMode(taskManager, logger);
2023-05-19 16:27:56 +02:00
}
2023-07-31 00:31:19 +02:00
private static void TaskMode(TaskManager taskManager, Logger logger)
2023-05-19 16:27:56 +02:00
{
ConsoleKey selection = ConsoleKey.EraseEndOfFile;
2023-05-31 21:18:41 +02:00
PrintMenu(taskManager, taskManager.settings.downloadLocation);
while (selection != ConsoleKey.Q)
2023-05-19 16:27:56 +02:00
{
int taskCount = taskManager.GetAllTasks().Length;
int taskRunningCount = taskManager.GetAllTasks().Count(task => task.state == TrangaTask.ExecutionState.Running);
int taskEnqueuedCount =
taskManager.GetAllTasks().Count(task => task.state == TrangaTask.ExecutionState.Enqueued);
Console.SetCursorPosition(0,1);
Console.WriteLine($"Tasks (Running/Queue/Total)): {taskRunningCount}/{taskEnqueuedCount}/{taskCount}");
if (Console.KeyAvailable)
2023-05-19 16:27:56 +02:00
{
2023-05-21 15:34:59 +02:00
selection = Console.ReadKey().Key;
switch (selection)
{
case ConsoleKey.L:
while (!Console.KeyAvailable)
{
PrintTasks(taskManager.GetAllTasks(), logger);
Console.WriteLine("Press any key.");
Thread.Sleep(500);
}
Console.ReadKey();
break;
case ConsoleKey.C:
2023-07-31 00:31:19 +02:00
CreateTask(taskManager);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
case ConsoleKey.D:
2023-07-31 00:31:19 +02:00
DeleteTask(taskManager);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
case ConsoleKey.E:
2023-07-31 00:31:19 +02:00
ExecuteTaskNow(taskManager);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
case ConsoleKey.S:
2023-07-31 00:31:19 +02:00
SearchTasks(taskManager);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
case ConsoleKey.R:
while (!Console.KeyAvailable)
{
PrintTasks(
taskManager.GetAllTasks().Where(eTask => eTask.state == TrangaTask.ExecutionState.Running)
.ToArray(), logger);
Console.WriteLine("Press any key.");
Thread.Sleep(500);
}
Console.ReadKey();
break;
case ConsoleKey.K:
while (!Console.KeyAvailable)
{
PrintTasks(
taskManager.GetAllTasks()
.Where(qTask => qTask.state is TrangaTask.ExecutionState.Enqueued)
.ToArray(), logger);
Console.WriteLine("Press any key.");
Thread.Sleep(500);
}
Console.ReadKey();
break;
case ConsoleKey.F:
TailLog(logger);
Console.ReadKey();
break;
case ConsoleKey.G:
RemoveTaskFromQueue(taskManager, logger);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
case ConsoleKey.B:
AddTaskToQueue(taskManager, logger);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
case ConsoleKey.M:
AddMangaTaskToQueue(taskManager, logger);
Console.WriteLine("Press any key.");
Console.ReadKey();
break;
}
2023-05-31 21:18:41 +02:00
PrintMenu(taskManager, taskManager.settings.downloadLocation);
2023-05-19 16:27:56 +02:00
}
Thread.Sleep(200);
2023-05-19 16:27:56 +02:00
}
logger.WriteLine("Tranga_CLI", "Exiting.");
Console.Clear();
Console.WriteLine("Exiting.");
if (taskManager.GetAllTasks().Any(task => task.state == TrangaTask.ExecutionState.Running))
{
Console.WriteLine("Force quit (Even with running tasks?) y/N");
selection = Console.ReadKey().Key;
while(selection != ConsoleKey.Y && selection != ConsoleKey.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-07-31 00:31:19 +02:00
2023-05-31 21:18:41 +02:00
private static void PrintMenu(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.state == TrangaTask.ExecutionState.Running);
int taskEnqueuedCount =
taskManager.GetAllTasks().Count(task => task.state == TrangaTask.ExecutionState.Enqueued);
2023-05-19 16:27:56 +02:00
Console.Clear();
2023-05-20 16:35:08 +02:00
Console.WriteLine($"Download Folder: {folderPath}");
2023-05-20 22:43:39 +02:00
Console.WriteLine($"Tasks (Running/Queue/Total)): {taskRunningCount}/{taskEnqueuedCount}/{taskCount}");
2023-05-20 16:35:08 +02:00
Console.WriteLine();
Console.WriteLine($"{"C: Create Task",-30}{"L: List tasks",-30}{"B: Enqueue Task", -30}");
2023-05-21 03:08:36 +02:00
Console.WriteLine($"{"D: Delete Task",-30}{"S: Search Tasks", -30}{"K: List Task Queue", -30}");
Console.WriteLine($"{"E: Execute Task now",-30}{"R: List Running Tasks", -30}{"G: Remove Task from Queue", -30}");
Console.WriteLine($"{"M: New Download Manga Task",-30}{"", -30}{"", -30}");
Console.WriteLine($"{"",-30}{"F: Show Log",-30}{"Q: Exit",-30}");
2023-05-19 16:27:56 +02:00
}
2023-07-31 00:31:19 +02:00
private static void PrintTasks(TrangaTask[] tasks, Logger? logger)
2023-05-19 16:27:56 +02:00
{
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "Printing Tasks");
2023-05-19 19:23:37 +02:00
int taskCount = tasks.Length;
int taskRunningCount = tasks.Count(task => task.state == TrangaTask.ExecutionState.Running);
int taskEnqueuedCount = tasks.Count(task => task.state == TrangaTask.ExecutionState.Enqueued);
2023-05-19 16:27:56 +02:00
Console.Clear();
int tIndex = 0;
Console.WriteLine($"Tasks (Running/Queue/Total): {taskRunningCount}/{taskEnqueuedCount}/{taskCount}");
2023-05-20 16:35:08 +02:00
string header =
$"{"",-5}{"Task",-20} | {"Last Executed",-20} | {"Reoccurrence",-12} | {"State",-10} | {"Progress",-9} | {"Finished",-20} | {"Remaining",-12} | {"Connector",-15} | Publication/Manga ";
2023-05-20 16:35:08 +02:00
Console.WriteLine(header);
Console.WriteLine(new string('-', header.Length));
foreach (TrangaTask trangaTask in tasks)
{
string[] taskSplit = trangaTask.ToString().Split(", ");
Console.WriteLine($"{tIndex++:000}: {taskSplit[0],-20} | {taskSplit[1],-20} | {taskSplit[2],-12} | {taskSplit[3],-10} | {taskSplit[4],-9} | {taskSplit[5],-20} | {taskSplit[6][..12],-12} | {(taskSplit.Length > 7 ? taskSplit[7] : ""),-15} | {(taskSplit.Length > 8 ? taskSplit[8] : "")} {(taskSplit.Length > 9 ? taskSplit[9] : "")} {(taskSplit.Length > 10 ? taskSplit[10] : "")}");
}
2023-05-19 16:27:56 +02:00
}
2023-07-31 00:31:19 +02:00
private static TrangaTask[] SelectTasks(TrangaTask[] tasks, Logger? logger)
{
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "Menu: Select task");
if (tasks.Length < 1)
{
Console.Clear();
Console.WriteLine("There are no available Tasks.");
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "No available Tasks.");
return Array.Empty<TrangaTask>();
}
PrintTasks(tasks, logger);
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "Selecting Task to Remove (from queue)");
Console.WriteLine("Enter q to abort");
Console.WriteLine($"Select Task(s) (0-{tasks.Length - 1}):");
string? selectedTask = Console.ReadLine();
while(selectedTask is null || selectedTask.Length < 1)
selectedTask = Console.ReadLine();
if (selectedTask.Length == 1 && selectedTask.ToLower() == "q")
{
Console.Clear();
Console.WriteLine("aborted.");
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "aborted");
return Array.Empty<TrangaTask>();
}
if (selectedTask.Contains('-'))
{
int start = Convert.ToInt32(selectedTask.Split('-')[0]);
int end = Convert.ToInt32(selectedTask.Split('-')[1]);
return tasks[start..end];
}
else
{
int selectedTaskIndex = Convert.ToInt32(selectedTask);
return new[] { tasks[selectedTaskIndex] };
}
}
2023-05-22 00:15:08 +02:00
private static void AddMangaTaskToQueue(TaskManager taskManager, Logger logger)
{
Console.Clear();
logger.WriteLine("Tranga_CLI", "Menu: Add Manga Download to queue");
2023-05-31 21:18:41 +02:00
Connector? connector = SelectConnector(taskManager.GetAvailableConnectors().Values.ToArray(), logger);
2023-05-22 00:15:08 +02:00
if (connector is null)
return;
2023-07-31 00:31:19 +02:00
Publication? publication = SelectPublication(taskManager, connector);
2023-05-22 00:15:08 +02:00
if (publication is null)
return;
TimeSpan reoccurrence = SelectReoccurrence(logger);
logger.WriteLine("Tranga_CLI", "Sending Task to TaskManager");
TrangaTask nTask = new MonitorPublicationTask(connector.name, (Publication)publication, reoccurrence, "en");
taskManager.AddTask(nTask);
Console.WriteLine(nTask);
2023-05-22 00:15:08 +02:00
}
private static void AddTaskToQueue(TaskManager taskManager, Logger logger)
{
Console.Clear();
logger.WriteLine("Tranga_CLI", "Menu: Add Task to queue");
TrangaTask[] tasks = taskManager.GetAllTasks().Where(rTask =>
rTask.state is not TrangaTask.ExecutionState.Enqueued and not TrangaTask.ExecutionState.Running).ToArray();
TrangaTask[] selectedTasks = SelectTasks(tasks, logger);
logger.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager");
foreach(TrangaTask task in selectedTasks)
taskManager.AddTaskToQueue(task);
}
private static void RemoveTaskFromQueue(TaskManager taskManager, Logger logger)
{
Console.Clear();
logger.WriteLine("Tranga_CLI", "Menu: Remove Task from queue");
TrangaTask[] tasks = taskManager.GetAllTasks().Where(rTask => rTask.state is TrangaTask.ExecutionState.Enqueued).ToArray();
TrangaTask[] selectedTasks = SelectTasks(tasks, logger);
logger.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager");
foreach(TrangaTask task in selectedTasks)
taskManager.RemoveTaskFromQueue(task);
}
private static void TailLog(Logger logger)
2023-05-21 02:17:38 +02:00
{
logger.WriteLine("Tranga_CLI", "Menu: Show Log-lines");
Console.Clear();
2023-05-21 02:17:38 +02:00
string[] lines = logger.Tail(20);
foreach (string message in lines)
Console.Write(message);
2023-05-21 02:17:38 +02:00
while (!Console.KeyAvailable)
2023-05-21 02:17:38 +02:00
{
string[] newLines = logger.GetNewLines();
foreach(string message in newLines)
2023-05-21 02:17:38 +02:00
Console.Write(message);
Thread.Sleep(40);
2023-05-21 02:17:38 +02:00
}
}
2023-07-31 00:31:19 +02:00
private static void CreateTask(TaskManager taskManager)
{
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Creating Task");
TrangaTask.Task? tmpTask = SelectTaskType(taskManager.commonObjects.logger);
if (tmpTask is null)
return;
2023-05-31 21:18:41 +02:00
TrangaTask.Task task = (TrangaTask.Task)tmpTask;
Connector? connector = null;
if (task != TrangaTask.Task.UpdateLibraries)
{
2023-07-31 00:31:19 +02:00
connector = SelectConnector(taskManager.GetAvailableConnectors().Values.ToArray(), taskManager.commonObjects.logger);
if (connector is null)
return;
}
Publication? publication = null;
if (task != TrangaTask.Task.UpdateLibraries)
{
2023-07-31 00:31:19 +02:00
publication = SelectPublication(taskManager, connector!);
if (publication is null)
return;
}
if (task is TrangaTask.Task.MonitorPublication)
{
2023-07-31 00:31:19 +02:00
TimeSpan reoccurrence = SelectReoccurrence(taskManager.commonObjects.logger);
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Sending Task to TaskManager");
TrangaTask newTask = new MonitorPublicationTask(connector!.name, (Publication)publication!, reoccurrence, "en");
taskManager.AddTask(newTask);
Console.WriteLine(newTask);
}else if (task is TrangaTask.Task.DownloadChapter)
{
2023-07-31 00:31:19 +02:00
foreach (Chapter chapter in SelectChapters(connector!, (Publication)publication!, taskManager.commonObjects.logger))
{
TrangaTask newTask = new DownloadChapterTask(connector!.name, (Publication)publication, chapter, "en");
taskManager.AddTask(newTask);
Console.WriteLine(newTask);
}
}
}
2023-07-31 00:31:19 +02:00
private static void ExecuteTaskNow(TaskManager taskManager)
{
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Executing Task");
TrangaTask[] tasks = taskManager.GetAllTasks().Where(nTask => nTask.state is not TrangaTask.ExecutionState.Running).ToArray();
2023-07-31 00:31:19 +02:00
TrangaTask[] selectedTasks = SelectTasks(tasks, taskManager.commonObjects.logger);
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager");
foreach(TrangaTask task in selectedTasks)
taskManager.ExecuteTaskNow(task);
}
2023-07-31 00:31:19 +02:00
private static void DeleteTask(TaskManager taskManager)
2023-05-19 16:27:56 +02:00
{
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Delete Task");
2023-05-19 16:27:56 +02:00
TrangaTask[] tasks = taskManager.GetAllTasks();
2023-05-19 19:23:37 +02:00
2023-07-31 00:31:19 +02:00
TrangaTask[] selectedTasks = SelectTasks(tasks, taskManager.commonObjects.logger);
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager");
foreach(TrangaTask task in selectedTasks)
taskManager.DeleteTask(task);
2023-05-19 16:27:56 +02:00
}
2023-07-31 00:31:19 +02:00
private static TrangaTask.Task? SelectTaskType(Logger? logger)
2023-05-19 16:27:56 +02:00
{
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "Menu: Select TaskType");
2023-05-19 16:27:56 +02:00
Console.Clear();
string[] taskNames = Enum.GetNames<TrangaTask.Task>();
int tIndex = 0;
Console.WriteLine("Available Tasks:");
foreach (string taskName in taskNames)
Console.WriteLine($"{tIndex++}: {taskName}");
Console.WriteLine("Enter q to abort");
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();
if (selectedTask.Length == 1 && selectedTask.ToLower() == "q")
{
2023-05-20 16:13:19 +02:00
Console.Clear();
Console.WriteLine("aborted.");
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "aborted.");
return null;
}
try
{
int selectedTaskIndex = Convert.ToInt32(selectedTask);
string selectedTaskName = taskNames[selectedTaskIndex];
return Enum.Parse<TrangaTask.Task>(selectedTaskName);
}
catch (Exception e)
{
Console.WriteLine($"Exception: {e.Message}");
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", e.Message);
}
return null;
2023-05-19 16:27:56 +02:00
}
2023-07-31 00:31:19 +02:00
private static TimeSpan SelectReoccurrence(Logger? logger)
2023-05-19 16:27:56 +02:00
{
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "Menu: Select Reoccurrence");
Console.WriteLine("Select reoccurrence Timer (Format hh:mm:ss):");
return TimeSpan.Parse(Console.ReadLine()!, new CultureInfo("en-US"));
}
2023-07-31 00:31:19 +02:00
private static Chapter[] SelectChapters(Connector connector, Publication publication, Logger? logger)
{
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "Menu: Select Chapters");
Chapter[] availableChapters = connector.GetChapters(publication, "en");
int cIndex = 0;
Console.WriteLine("Chapters:");
2023-07-06 02:09:56 +02:00
System.Text.StringBuilder sb = new();
foreach(Chapter chapter in availableChapters)
2023-07-06 02:09:56 +02:00
{
sb.Append($"{cIndex++}: ");
if(string.IsNullOrWhiteSpace(chapter.volumeNumber) == false)
{
sb.Append($"Vol.{chapter.volumeNumber} ");
}
if(string.IsNullOrWhiteSpace(chapter.chapterNumber) == false)
{
sb.Append($"Ch.{chapter.chapterNumber} ");
}
if(string.IsNullOrWhiteSpace(chapter.name) == false)
{
sb.Append($" - {chapter.name}");
}
Console.WriteLine(sb.ToString());
sb.Clear();
}
Console.WriteLine("Enter q to abort");
Console.WriteLine($"Select Chapter(s):");
string? selectedChapters = Console.ReadLine();
while(selectedChapters is null || selectedChapters.Length < 1)
selectedChapters = Console.ReadLine();
return connector.SelectChapters(publication, selectedChapters);
}
2023-07-31 00:31:19 +02:00
private static Connector? SelectConnector(Connector[] connectors, Logger? logger)
{
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "Menu: Select Connector");
2023-05-19 16:27:56 +02:00
Console.Clear();
int cIndex = 0;
Console.WriteLine("Connectors:");
foreach (Connector connector in connectors)
Console.WriteLine($"{cIndex++}: {connector.name}");
Console.WriteLine("Enter q to abort");
2023-05-19 16:27:56 +02:00
Console.WriteLine($"Select Connector (0-{connectors.Length - 1}):");
string? selectedConnector = Console.ReadLine();
while(selectedConnector is null || selectedConnector.Length < 1)
selectedConnector = Console.ReadLine();
if (selectedConnector.Length == 1 && selectedConnector.ToLower() == "q")
{
2023-05-20 16:13:19 +02:00
Console.Clear();
Console.WriteLine("aborted.");
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", "aborted.");
return null;
}
2023-05-19 16:27:56 +02:00
try
{
int selectedConnectorIndex = Convert.ToInt32(selectedConnector);
return connectors[selectedConnectorIndex];
}
catch (Exception e)
{
Console.WriteLine($"Exception: {e.Message}");
2023-07-31 00:31:19 +02:00
logger?.WriteLine("Tranga_CLI", e.Message);
}
return null;
}
2023-07-31 00:31:19 +02:00
private static Publication? SelectPublication(TaskManager taskManager, Connector connector)
{
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Select Publication");
2023-05-20 21:48:08 +02:00
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(ref taskManager.collection, query ?? "");
2023-05-22 16:52:52 +02:00
if (publications.Length < 1)
{
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "No publications returned");
2023-05-22 16:52:52 +02:00
Console.WriteLine($"No publications for query '{query}' returned;");
return null;
}
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("Enter q to abort");
2023-05-18 17:42:47 +02:00
Console.WriteLine($"Select publication to Download (0-{publications.Length - 1}):");
string? selectedPublication = Console.ReadLine();
while(selectedPublication is null || selectedPublication.Length < 1)
selectedPublication = Console.ReadLine();
if (selectedPublication.Length == 1 && selectedPublication.ToLower() == "q")
{
2023-05-20 16:13:19 +02:00
Console.Clear();
Console.WriteLine("aborted.");
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "aborted.");
return null;
}
2023-05-18 17:42:47 +02:00
try
{
int selectedPublicationIndex = Convert.ToInt32(selectedPublication);
return publications[selectedPublicationIndex];
}
catch (Exception e)
{
Console.WriteLine($"Exception: {e.Message}");
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", e.Message);
}
return null;
}
2023-07-31 00:31:19 +02:00
private static void SearchTasks(TaskManager taskManager)
{
2023-07-31 00:31:19 +02:00
taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Search task");
Console.Clear();
Console.WriteLine("Enter search query:");
string? query = Console.ReadLine();
while (query is null || query.Length < 4)
query = Console.ReadLine();
PrintTasks(taskManager.GetAllTasks().Where(qTask =>
2023-07-31 00:31:19 +02:00
qTask.ToString().ToLower().Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray(), taskManager.commonObjects.logger);
}
2023-05-18 15:49:08 +02:00
}