2023-05-31 22:32:37 +02:00
|
|
|
|
using Logging;
|
2023-06-06 20:54:21 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2023-05-31 22:32:37 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga.TrangaTasks;
|
2023-05-31 21:15:32 +02:00
|
|
|
|
|
|
|
|
|
public class DownloadNewChaptersTask : TrangaTask
|
|
|
|
|
{
|
2023-06-05 00:35:57 +02:00
|
|
|
|
public string connectorName { get; }
|
|
|
|
|
public Publication publication { get; }
|
|
|
|
|
public string language { get; }
|
2023-06-10 14:27:09 +02:00
|
|
|
|
[JsonIgnore]private HashSet<DownloadChapterTask> childTasks { get; }
|
2023-06-06 20:54:21 +02:00
|
|
|
|
|
2023-06-05 00:35:57 +02:00
|
|
|
|
public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, reoccurrence)
|
2023-05-31 21:15:32 +02:00
|
|
|
|
{
|
2023-06-05 00:35:57 +02:00
|
|
|
|
this.connectorName = connectorName;
|
|
|
|
|
this.publication = publication;
|
|
|
|
|
this.language = language;
|
2023-06-10 14:27:09 +02:00
|
|
|
|
this.childTasks = new();
|
2023-06-06 20:54:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new float IncrementProgress(float amount)
|
|
|
|
|
{
|
2023-06-10 14:27:09 +02:00
|
|
|
|
this.progress += amount / this.childTasks.Count;
|
2023-06-07 00:27:53 +02:00
|
|
|
|
this.lastChange = DateTime.Now;
|
2023-06-06 20:54:21 +02:00
|
|
|
|
return this.progress;
|
2023-06-09 23:38:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new float DecrementProgress(float amount)
|
|
|
|
|
{
|
2023-06-10 14:27:09 +02:00
|
|
|
|
this.progress -= amount / this.childTasks.Count;
|
2023-06-09 23:38:28 +02:00
|
|
|
|
this.lastChange = DateTime.Now;
|
|
|
|
|
return this.progress;
|
2023-05-31 21:15:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 14:27:09 +02:00
|
|
|
|
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
2023-05-31 21:15:32 +02:00
|
|
|
|
{
|
2023-06-10 14:27:09 +02:00
|
|
|
|
if (cancellationToken?.IsCancellationRequested??false)
|
|
|
|
|
return;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
Publication pub = publication!;
|
2023-05-31 21:15:32 +02:00
|
|
|
|
Connector connector = taskManager.GetConnector(this.connectorName);
|
2023-06-05 00:35:57 +02:00
|
|
|
|
|
2023-05-31 21:15:32 +02:00
|
|
|
|
//Check if Publication already has a Folder
|
2023-06-01 22:59:04 +02:00
|
|
|
|
pub.CreatePublicationFolder(taskManager.settings.downloadLocation);
|
2023-06-05 00:35:57 +02:00
|
|
|
|
List<Chapter> newChapters = GetNewChaptersList(connector, pub, language!, ref taskManager.chapterCollection);
|
2023-05-31 21:15:32 +02:00
|
|
|
|
|
|
|
|
|
connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings);
|
|
|
|
|
|
|
|
|
|
pub.SaveSeriesInfoJson(connector.downloadLocation);
|
|
|
|
|
|
2023-06-05 00:35:57 +02:00
|
|
|
|
foreach (Chapter newChapter in newChapters)
|
2023-06-10 14:27:09 +02:00
|
|
|
|
{
|
|
|
|
|
DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName!, pub, newChapter, this.language, this);
|
|
|
|
|
taskManager.AddTask(newTask);
|
|
|
|
|
this.childTasks.Add(newTask);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReplaceFailedChildTask(DownloadChapterTask failed, DownloadChapterTask newTask)
|
|
|
|
|
{
|
|
|
|
|
if (!this.childTasks.Contains(failed))
|
|
|
|
|
throw new ArgumentException($"Task {failed} is not childTask of {this}");
|
|
|
|
|
this.childTasks.Remove(failed);
|
|
|
|
|
this.childTasks.Add(newTask);
|
2023-05-31 21:15:32 +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-06-05 00:35:57 +02:00
|
|
|
|
private static List<Chapter> GetNewChaptersList(Connector connector, Publication publication, string language, ref Dictionary<Publication, List<Chapter>> chapterCollection)
|
2023-05-31 21:15:32 +02:00
|
|
|
|
{
|
|
|
|
|
List<Chapter> newChaptersList = new();
|
|
|
|
|
chapterCollection.TryAdd(publication, newChaptersList); //To ensure publication is actually in collection
|
|
|
|
|
|
|
|
|
|
Chapter[] newChapters = connector.GetChapters(publication, language);
|
|
|
|
|
newChaptersList = newChapters.Where(nChapter => !connector.CheckChapterIsDownloaded(publication, nChapter)).ToList();
|
|
|
|
|
|
|
|
|
|
return newChaptersList;
|
|
|
|
|
}
|
2023-06-05 00:35:57 +02:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}";
|
|
|
|
|
}
|
2023-05-31 21:15:32 +02:00
|
|
|
|
}
|