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.
///
/// List of all available Connectors
/// 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(Connector[] connectors, TrangaTask trangaTask, Dictionary> chapterCollection)
{
//Get Connector from list of available Connectors and the required Connector of the TrangaTask
Connector? connector = connectors.FirstOrDefault(c => c.name == trangaTask.connectorName);
if (connector is null)
throw new ArgumentException($"Connector {trangaTask.connectorName} is not a known connector.");
if (trangaTask.isBeingExecuted)
return;
trangaTask.isBeingExecuted = true;
trangaTask.lastExecuted = DateTime.Now;
//Call appropriate Method based on TrangaTask.Task
switch (trangaTask.task)
{
case TrangaTask.Task.DownloadNewChapters:
DownloadNewChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
break;
case TrangaTask.Task.UpdateChapters:
UpdateChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
break;
case TrangaTask.Task.UpdatePublications:
UpdatePublications(connector, chapterCollection);
break;
}
trangaTask.isBeingExecuted = false;
}
///
/// Updates the available Publications from a Connector (all of them)
///
/// Connector to receive Publications from
///
private static void UpdatePublications(Connector connector, 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, Dictionary> chapterCollection)
{
List newChapters = UpdateChapters(connector, publication, language, chapterCollection);
connector.DownloadCover(publication);
//Check if Publication already has a Folder and a series.json
string publicationFolder = Path.Join(connector.downloadLocation, publication.folderName);
if(!Directory.Exists(publicationFolder))
Directory.CreateDirectory(publicationFolder);
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, Dictionary> chapterCollection)
{
List newChaptersList = new();
if (!chapterCollection.ContainsKey(publication))
return newChaptersList;
Chapter[] newChapters = connector.GetChapters(publication, language);
newChaptersList = newChapters.Where(nChapter => !connector.ChapterIsDownloaded(publication, nChapter)).ToList();
return newChaptersList;
}
}