mirror of
https://github.com/C9Glax/tranga.git
synced 2025-06-15 07:47:54 +02:00
Rewrite Task-Structure for serialization.
TrangaTask include information on what to execute where, do not execute tasks. TaskExecutor executes Tasks on information from TrangaTask
This commit is contained in:
48
Tranga/TaskExecutor.cs
Normal file
48
Tranga/TaskExecutor.cs
Normal file
@ -0,0 +1,48 @@
|
||||
namespace Tranga;
|
||||
|
||||
public static class TaskExecutor
|
||||
{
|
||||
public static void Execute(TrangaTask trangaTask, Dictionary<Publication, List<Chapter>> chapterCollection)
|
||||
{
|
||||
switch (trangaTask.task)
|
||||
{
|
||||
case TrangaTask.Task.DownloadNewChapters:
|
||||
DownloadNewChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection);
|
||||
break;
|
||||
case TrangaTask.Task.UpdateChapters:
|
||||
UpdateChapters(trangaTask.connector, trangaTask.publication, trangaTask.language, chapterCollection);
|
||||
break;
|
||||
case TrangaTask.Task.UpdatePublications:
|
||||
UpdatePublications(trangaTask.connector, chapterCollection);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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>());
|
||||
}
|
||||
|
||||
private static void DownloadNewChapters(Connector connector, Publication publication, string language, Dictionary<Publication, List<Chapter>> chapterCollection)
|
||||
{
|
||||
List<Chapter> newChapters = UpdateChapters(connector, publication, language, chapterCollection);
|
||||
foreach(Chapter newChapter in newChapters)
|
||||
connector.DownloadChapter(publication, newChapter);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user