2023-06-20 15:46:54 +02:00
|
|
|
|
using System.Net;
|
|
|
|
|
using Logging;
|
2023-07-30 16:26:29 +02:00
|
|
|
|
using Tranga.NotificationManagers;
|
|
|
|
|
using Tranga.LibraryManagers;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga.TrangaTasks;
|
|
|
|
|
|
|
|
|
|
public class DownloadChapterTask : TrangaTask
|
|
|
|
|
{
|
|
|
|
|
public string connectorName { get; }
|
|
|
|
|
public Publication publication { get; }
|
|
|
|
|
public string language { get; }
|
|
|
|
|
public Chapter chapter { get; }
|
2023-06-10 15:59:23 +02:00
|
|
|
|
|
2023-06-20 14:57:44 +02:00
|
|
|
|
private double _dctProgress = 0;
|
|
|
|
|
|
|
|
|
|
public DownloadChapterTask(string connectorName, Publication publication, Chapter chapter, string language = "en", MonitorPublicationTask? parentTask = null) : base(Task.DownloadChapter, TimeSpan.Zero, parentTask)
|
2023-06-05 00:35:57 +02:00
|
|
|
|
{
|
|
|
|
|
this.chapter = chapter;
|
|
|
|
|
this.connectorName = connectorName;
|
|
|
|
|
this.publication = publication;
|
|
|
|
|
this.language = language;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 15:46:54 +02:00
|
|
|
|
protected override HttpStatusCode ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
2023-06-05 00:35:57 +02:00
|
|
|
|
{
|
2023-06-20 15:46:54 +02:00
|
|
|
|
if (cancellationToken?.IsCancellationRequested ?? false)
|
|
|
|
|
return HttpStatusCode.RequestTimeout;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
Connector connector = taskManager.GetConnector(this.connectorName);
|
2023-06-11 19:05:08 +02:00
|
|
|
|
connector.CopyCoverFromCacheToDownloadLocation(this.publication, taskManager.settings);
|
2023-06-20 15:46:54 +02:00
|
|
|
|
HttpStatusCode downloadSuccess = connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
|
2023-07-03 00:01:08 +02:00
|
|
|
|
if ((int)downloadSuccess >= 200 && (int)downloadSuccess < 300)
|
|
|
|
|
{
|
2023-07-02 23:06:24 +02:00
|
|
|
|
foreach(NotificationManager nm in taskManager.settings.notificationManagers)
|
|
|
|
|
nm.SendNotification("Chapter downloaded", $"{this.publication.sortName} {this.chapter.chapterNumber} {this.chapter.name}");
|
2023-07-03 00:01:08 +02:00
|
|
|
|
|
|
|
|
|
foreach (LibraryManager lm in taskManager.settings.libraryManagers)
|
|
|
|
|
lm.UpdateLibrary();
|
|
|
|
|
}
|
2023-06-20 14:57:44 +02:00
|
|
|
|
return downloadSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TrangaTask Clone()
|
|
|
|
|
{
|
|
|
|
|
return new DownloadChapterTask(this.connectorName, this.publication, this.chapter,
|
|
|
|
|
this.language, (MonitorPublicationTask?)this.parentTask);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override double GetProgress()
|
|
|
|
|
{
|
|
|
|
|
return _dctProgress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void IncrementProgress(double amount)
|
|
|
|
|
{
|
|
|
|
|
this._dctProgress += amount;
|
2023-06-21 17:30:31 +02:00
|
|
|
|
this.lastChange = DateTime.Now;
|
|
|
|
|
if(this.parentTask is not null)
|
|
|
|
|
this.parentTask.lastChange = DateTime.Now;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
}
|
2023-06-06 20:54:21 +02:00
|
|
|
|
|
2023-06-05 00:35:57 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}, Vol.{chapter.volumeNumber} Ch.{chapter.chapterNumber}";
|
|
|
|
|
}
|
|
|
|
|
}
|