using Logging; using Newtonsoft.Json; namespace Tranga.TrangaTasks; public class DownloadNewChaptersTask : TrangaTask { public string connectorName { get; } public Publication publication { get; } public string language { get; } [JsonIgnore]private int childTaskAmount { get; set; } public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, reoccurrence) { this.connectorName = connectorName; this.publication = publication; this.language = language; childTaskAmount = 0; } public new float IncrementProgress(float amount) { this.progress += amount / this.childTaskAmount; return this.progress; } protected override void ExecuteTask(TaskManager taskManager, Logger? logger) { Publication pub = publication!; Connector connector = taskManager.GetConnector(this.connectorName); //Check if Publication already has a Folder pub.CreatePublicationFolder(taskManager.settings.downloadLocation); List newChapters = GetNewChaptersList(connector, pub, language!, ref taskManager.chapterCollection); this.childTaskAmount = newChapters.Count; connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings); pub.SaveSeriesInfoJson(connector.downloadLocation); foreach (Chapter newChapter in newChapters) taskManager.AddTask(new DownloadChapterTask(Task.DownloadChapter, this.connectorName!, pub, newChapter, this.language, this)); } /// /// 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 GetNewChaptersList(Connector connector, Publication publication, string language, ref Dictionary> chapterCollection) { List 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; } public override string ToString() { return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}"; } }