2023-05-17 23:23:01 +02:00
|
|
|
|
namespace Tranga;
|
|
|
|
|
|
|
|
|
|
public class TrangaTask
|
|
|
|
|
{
|
|
|
|
|
public TimeSpan reoccurrence { get; }
|
|
|
|
|
public DateTime lastExecuted { get; private set; }
|
2023-05-18 20:58:03 +02:00
|
|
|
|
public Connector connector { get; }
|
|
|
|
|
public availableTasks availableTaskToExecute { get; }
|
|
|
|
|
public enum availableTasks
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
downloadNewChapters,
|
|
|
|
|
updateChapters,
|
|
|
|
|
updatePublications
|
|
|
|
|
};
|
|
|
|
|
public Publication? publication { get; }
|
|
|
|
|
public string language { get; }
|
2023-05-17 23:23:01 +02:00
|
|
|
|
|
|
|
|
|
|
2023-05-18 20:58:03 +02:00
|
|
|
|
public TrangaTask(Connector connector, availableTasks availableTask, TimeSpan reoccurrence, Publication? publication = null, string language = "en")
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
this.connector = connector;
|
|
|
|
|
this.availableTaskToExecute = availableTask;
|
|
|
|
|
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
|
|
|
|
|
this.reoccurrence = reoccurrence;
|
|
|
|
|
this.publication = publication;
|
|
|
|
|
this.language = language;
|
|
|
|
|
if (publication is null && availableTask is availableTasks.updateChapters or availableTasks.downloadNewChapters)
|
|
|
|
|
{
|
|
|
|
|
if (publication is null)
|
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
"If task is updateChapters or downloadNewChapters, Argument publication can not be null!");
|
|
|
|
|
}
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 20:58:03 +02:00
|
|
|
|
public void Execute(ref Dictionary<Publication, Chapter[]> chapterCollection)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
switch (this.availableTaskToExecute)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
case availableTasks.updateChapters:
|
|
|
|
|
UpdateChapters(ref chapterCollection);
|
|
|
|
|
break;
|
|
|
|
|
case availableTasks.updatePublications:
|
|
|
|
|
UpdatePublications(ref chapterCollection);
|
|
|
|
|
break;
|
|
|
|
|
case availableTasks.downloadNewChapters:
|
|
|
|
|
DownloadNewChapters(UpdateChapters(ref chapterCollection));
|
|
|
|
|
break;
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
2023-05-18 20:58:03 +02:00
|
|
|
|
|
|
|
|
|
this.lastExecuted = DateTime.Now;
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
2023-05-18 20:58:03 +02:00
|
|
|
|
|
|
|
|
|
private Chapter[] UpdateChapters(ref Dictionary<Publication, Chapter[]> chapterCollection)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
Publication pPublication = (Publication)this.publication!;
|
|
|
|
|
Chapter[] presentChapters = chapterCollection[pPublication];
|
|
|
|
|
Chapter[] allChapters = connector.GetChapters(pPublication);
|
|
|
|
|
chapterCollection[pPublication] = allChapters;
|
2023-05-18 19:12:17 +02:00
|
|
|
|
|
2023-05-18 20:58:03 +02:00
|
|
|
|
Dictionary<string, Chapter> pChapter = presentChapters.ToDictionary(chapter => chapter.url, chapter => chapter);
|
|
|
|
|
Dictionary<string, Chapter> aChapter = allChapters.ToDictionary(chapter => chapter.url, chapter => chapter);
|
|
|
|
|
return aChapter.Except(pChapter).ToDictionary(pair => pair.Key, pair => pair.Value).Values.ToArray();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 20:58:03 +02:00
|
|
|
|
private void UpdatePublications(ref Dictionary<Publication, Chapter[]> chapterCollection)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 20:58:03 +02:00
|
|
|
|
Publication[] allPublications = connector.GetPublications();
|
|
|
|
|
foreach(Publication publication in allPublications)
|
|
|
|
|
chapterCollection.TryAdd(publication, Array.Empty<Chapter>());
|
|
|
|
|
}
|
2023-05-18 19:12:17 +02:00
|
|
|
|
|
2023-05-18 20:58:03 +02:00
|
|
|
|
private void DownloadNewChapters(Chapter[] newChapters)
|
|
|
|
|
{
|
|
|
|
|
Publication pPublication = (Publication)this.publication!;
|
|
|
|
|
foreach(Chapter chapter in newChapters)
|
|
|
|
|
connector.DownloadChapter(pPublication, chapter);
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|