2023-05-31 22:32:37 +02:00
|
|
|
|
using Logging;
|
|
|
|
|
|
|
|
|
|
namespace Tranga.TrangaTasks;
|
2023-05-31 21:15:32 +02:00
|
|
|
|
|
|
|
|
|
public class DownloadNewChaptersTask : TrangaTask
|
|
|
|
|
{
|
|
|
|
|
public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, connectorName, publication, reoccurrence, language)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 22:32:37 +02:00
|
|
|
|
public override void Execute(TaskManager taskManager, Logger? logger)
|
2023-05-31 21:15:32 +02:00
|
|
|
|
{
|
2023-05-31 22:32:37 +02:00
|
|
|
|
StartExecutionChores(logger);
|
2023-05-31 21:15:32 +02:00
|
|
|
|
Publication pub = (Publication)this.publication!;
|
|
|
|
|
Connector connector = taskManager.GetConnector(this.connectorName);
|
|
|
|
|
|
|
|
|
|
//Check if Publication already has a Folder
|
|
|
|
|
string publicationFolder = Path.Join(connector.downloadLocation, pub.folderName);
|
|
|
|
|
if(!Directory.Exists(publicationFolder))
|
|
|
|
|
Directory.CreateDirectory(publicationFolder);
|
2023-05-31 21:18:41 +02:00
|
|
|
|
List<Chapter> newChapters = UpdateChapters(connector, pub, language!, ref taskManager.chapterCollection);
|
2023-05-31 21:15:32 +02:00
|
|
|
|
|
|
|
|
|
connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings);
|
|
|
|
|
|
|
|
|
|
pub.SaveSeriesInfoJson(connector.downloadLocation);
|
|
|
|
|
|
|
|
|
|
foreach(Chapter newChapter in newChapters)
|
|
|
|
|
connector.DownloadChapter(pub, newChapter);
|
2023-05-31 21:52:50 +02:00
|
|
|
|
|
2023-05-31 22:32:37 +02:00
|
|
|
|
EndExecutionChores(logger);
|
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>
|
|
|
|
|
private static List<Chapter> UpdateChapters(Connector connector, Publication publication, string language, ref Dictionary<Publication, List<Chapter>> chapterCollection)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|