using Logging; namespace Tranga; /// /// Executes TrangaTasks /// Based on the TrangaTask.Task a method is called. /// The chapterCollection is updated with new Publications/Chapters. /// public static class TaskExecutor { /// /// Executes TrangaTask. /// /// Parent /// Task to execute /// Current chapterCollection to update /// /// Is thrown when there is no Connector available with the name of the TrangaTask.connectorName public static void Execute(TaskManager taskManager, TrangaTask trangaTask, Logger? logger) { //Only execute task if it is not already being executed. if (trangaTask.state == TrangaTask.ExecutionState.Running) { logger?.WriteLine("TaskExecutor", $"Task already running {trangaTask}"); return; } trangaTask.state = TrangaTask.ExecutionState.Running; logger?.WriteLine("TaskExecutor", $"Starting Task {trangaTask}"); //Connector is not needed for all tasks Connector? connector = null; if (trangaTask.task != TrangaTask.Task.UpdateKomgaLibrary) connector = taskManager.GetConnector(trangaTask.connectorName!); //Call appropriate Method based on TrangaTask.Task switch (trangaTask.task) { case TrangaTask.Task.DownloadNewChapters: DownloadNewChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, ref taskManager._chapterCollection); break; case TrangaTask.Task.UpdateChapters: UpdateChapters(connector!, (Publication)trangaTask.publication!, trangaTask.language, ref taskManager._chapterCollection); break; case TrangaTask.Task.UpdatePublications: UpdatePublications(connector!, ref taskManager._chapterCollection); break; case TrangaTask.Task.UpdateKomgaLibrary: UpdateKomgaLibrary(taskManager); break; } logger?.WriteLine("TaskExecutor", $"Task finished! {trangaTask}"); trangaTask.lastExecuted = DateTime.Now; trangaTask.state = TrangaTask.ExecutionState.Waiting; } /// /// Updates all Komga-Libraries /// /// Parent private static void UpdateKomgaLibrary(TaskManager taskManager) { if (taskManager.komga is null) return; Komga komga = taskManager.komga; Komga.KomgaLibrary[] allLibraries = komga.GetLibraries(); foreach (Komga.KomgaLibrary lib in allLibraries) komga.UpdateLibrary(lib.id); } /// /// Updates the available Publications from a Connector (all of them) /// /// Connector to receive Publications from /// private static void UpdatePublications(Connector connector, ref Dictionary> chapterCollection) { Publication[] publications = connector.GetPublications(); foreach (Publication publication in publications) chapterCollection.TryAdd(publication, new List()); } /// /// Checks for new Chapters and Downloads new ones. /// If no Chapters had been downloaded previously, download also cover and create series.json /// /// Connector to use /// Publication to check /// Language to receive chapters for /// private static void DownloadNewChapters(Connector connector, Publication publication, string language, ref Dictionary> chapterCollection) { //Check if Publication already has a Folder string publicationFolder = Path.Join(connector.downloadLocation, publication.folderName); if(!Directory.Exists(publicationFolder)) Directory.CreateDirectory(publicationFolder); List newChapters = UpdateChapters(connector, publication, language, ref chapterCollection); connector.DownloadCover(publication); string seriesInfoPath = Path.Join(publicationFolder, "series.json"); if(!File.Exists(seriesInfoPath)) File.WriteAllText(seriesInfoPath,publication.GetSeriesInfoJson()); foreach(Chapter newChapter in newChapters) connector.DownloadChapter(publication, newChapter); } /// /// Updates the available Chapters of a Publication /// /// Connector to use /// Publication to check /// Language to receive chapters for /// /// List of Chapters that were previously not in collection private static List UpdateChapters(Connector connector, Publication publication, string language, ref Dictionary> chapterCollection) { List newChaptersList = new(); chapterCollection.TryAdd(publication, newChaptersList); //To ensure publication is actually in collection Chapter[] newChapters = connector.GetChapters(publication, language); newChaptersList = newChapters.Where(nChapter => !connector.ChapterIsDownloaded(publication, nChapter)).ToList(); return newChaptersList; } }