2023-05-19 14:00:30 +02:00
|
|
|
|
namespace Tranga;
|
|
|
|
|
|
2023-05-19 20:03:17 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes TrangaTasks
|
|
|
|
|
/// Based on the TrangaTask.Task a method is called.
|
|
|
|
|
/// The chapterCollection is updated with new Publications/Chapters.
|
|
|
|
|
/// </summary>
|
2023-05-19 14:00:30 +02:00
|
|
|
|
public static class TaskExecutor
|
|
|
|
|
{
|
2023-05-19 20:03:17 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes TrangaTask.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="connectors">List of all available Connectors</param>
|
|
|
|
|
/// <param name="trangaTask">Task to execute</param>
|
|
|
|
|
/// <param name="chapterCollection">Current chapterCollection to update</param>
|
|
|
|
|
/// <exception cref="ArgumentException">Is thrown when there is no Connector available with the name of the TrangaTask.connectorName</exception>
|
2023-05-19 16:27:56 +02:00
|
|
|
|
public static void Execute(Connector[] connectors, TrangaTask trangaTask, Dictionary<Publication, List<Chapter>> chapterCollection)
|
2023-05-19 14:00:30 +02:00
|
|
|
|
{
|
2023-05-19 20:22:13 +02:00
|
|
|
|
//Get Connector from list of available Connectors and the required Connector of the TrangaTask
|
2023-05-19 16:27:56 +02:00
|
|
|
|
Connector? connector = connectors.FirstOrDefault(c => c.name == trangaTask.connectorName);
|
|
|
|
|
if (connector is null)
|
|
|
|
|
throw new ArgumentException($"Connector {trangaTask.connectorName} is not a known connector.");
|
2023-05-19 19:20:06 +02:00
|
|
|
|
|
|
|
|
|
if (trangaTask.isBeingExecuted)
|
|
|
|
|
return;
|
|
|
|
|
trangaTask.isBeingExecuted = true;
|
2023-05-19 16:35:27 +02:00
|
|
|
|
trangaTask.lastExecuted = DateTime.Now;
|
2023-05-19 16:27:56 +02:00
|
|
|
|
|
2023-05-19 20:22:13 +02:00
|
|
|
|
//Call appropriate Method based on TrangaTask.Task
|
2023-05-19 14:00:30 +02:00
|
|
|
|
switch (trangaTask.task)
|
|
|
|
|
{
|
|
|
|
|
case TrangaTask.Task.DownloadNewChapters:
|
2023-05-19 16:27:56 +02:00
|
|
|
|
DownloadNewChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
|
2023-05-19 14:00:30 +02:00
|
|
|
|
break;
|
|
|
|
|
case TrangaTask.Task.UpdateChapters:
|
2023-05-19 16:27:56 +02:00
|
|
|
|
UpdateChapters(connector, (Publication)trangaTask.publication!, trangaTask.language, chapterCollection);
|
2023-05-19 14:00:30 +02:00
|
|
|
|
break;
|
|
|
|
|
case TrangaTask.Task.UpdatePublications:
|
2023-05-19 16:27:56 +02:00
|
|
|
|
UpdatePublications(connector, chapterCollection);
|
2023-05-19 14:00:30 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-05-19 19:20:06 +02:00
|
|
|
|
|
|
|
|
|
trangaTask.isBeingExecuted = false;
|
2023-05-19 14:00:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 20:22:13 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates the available Publications from a Connector (all of them)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="connector">Connector to receive Publications from</param>
|
|
|
|
|
/// <param name="chapterCollection"></param>
|
2023-05-19 14:00:30 +02:00
|
|
|
|
private static void UpdatePublications(Connector connector, Dictionary<Publication, List<Chapter>> chapterCollection)
|
|
|
|
|
{
|
|
|
|
|
Publication[] publications = connector.GetPublications();
|
|
|
|
|
foreach (Publication publication in publications)
|
|
|
|
|
chapterCollection.TryAdd(publication, new List<Chapter>());
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 20:22:13 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks for new Chapters and Downloads new ones.
|
|
|
|
|
/// If no Chapters had been downloaded previously, download also cover and create series.json
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="connector">Connector to use</param>
|
|
|
|
|
/// <param name="publication">Publication to check</param>
|
|
|
|
|
/// <param name="language">Language to receive chapters for</param>
|
|
|
|
|
/// <param name="chapterCollection"></param>
|
2023-05-19 14:00:30 +02:00
|
|
|
|
private static void DownloadNewChapters(Connector connector, Publication publication, string language, Dictionary<Publication, List<Chapter>> chapterCollection)
|
|
|
|
|
{
|
|
|
|
|
List<Chapter> newChapters = UpdateChapters(connector, publication, language, chapterCollection);
|
2023-05-19 16:24:05 +02:00
|
|
|
|
connector.DownloadCover(publication);
|
2023-05-19 23:01:34 +02:00
|
|
|
|
|
|
|
|
|
//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.GetSeriesInfo());
|
|
|
|
|
|
2023-05-19 20:55:04 +02:00
|
|
|
|
foreach(Chapter newChapter in newChapters)
|
|
|
|
|
connector.DownloadChapter(publication, newChapter);
|
2023-05-19 14:00:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 20:22:13 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates the available Chapters of a Publication
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="connector">Connector to use</param>
|
|
|
|
|
/// <param name="publication">Publication to check</param>
|
|
|
|
|
/// <param name="language">Language to receive chapters for</param>
|
|
|
|
|
/// <param name="chapterCollection"></param>
|
|
|
|
|
/// <returns>List of Chapters that were previously not in collection</returns>
|
2023-05-19 14:00:30 +02:00
|
|
|
|
private static List<Chapter> UpdateChapters(Connector connector, Publication publication, string language, Dictionary<Publication, List<Chapter>> chapterCollection)
|
|
|
|
|
{
|
|
|
|
|
List<Chapter> newChaptersList = new();
|
|
|
|
|
if (!chapterCollection.ContainsKey(publication))
|
|
|
|
|
return newChaptersList;
|
|
|
|
|
|
|
|
|
|
List<Chapter> currentChapters = chapterCollection[publication];
|
|
|
|
|
Chapter[] newChapters = connector.GetChapters(publication, language);
|
|
|
|
|
|
|
|
|
|
newChaptersList = newChapters.ToList()
|
|
|
|
|
.ExceptBy(currentChapters.Select(cChapter => cChapter.url), nChapter => nChapter.url).ToList();
|
|
|
|
|
return newChaptersList;
|
|
|
|
|
}
|
|
|
|
|
}
|