Tranga-Website/Tranga/TrangaTasks/DownloadChapterTask.cs
glax 9af5c1603e Using HttpStatusCode to signify Task-Success
When DownloadChapterTask returns notfound, do not retry.
2023-06-20 15:46:54 +02:00

56 lines
2.2 KiB
C#

using System.Net;
using Logging;
namespace Tranga.TrangaTasks;
public class DownloadChapterTask : TrangaTask
{
public string connectorName { get; }
public Publication publication { get; }
public string language { get; }
public Chapter chapter { get; }
private double _dctProgress = 0;
public DownloadChapterTask(string connectorName, Publication publication, Chapter chapter, string language = "en", MonitorPublicationTask? parentTask = null) : base(Task.DownloadChapter, TimeSpan.Zero, parentTask)
{
this.chapter = chapter;
this.connectorName = connectorName;
this.publication = publication;
this.language = language;
}
protected override HttpStatusCode ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
{
if (cancellationToken?.IsCancellationRequested ?? false)
return HttpStatusCode.RequestTimeout;
Connector connector = taskManager.GetConnector(this.connectorName);
connector.CopyCoverFromCacheToDownloadLocation(this.publication, taskManager.settings);
HttpStatusCode downloadSuccess = connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
if((int)downloadSuccess >= 200 && (int)downloadSuccess < 300 && parentTask is not null)
foreach(NotificationManager nm in taskManager.settings.notificationManagers)
nm.SendNotification("New Chapter downloaded", $"{this.publication.sortName} {this.chapter.chapterNumber} {this.chapter.name}");
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;
}
public override string ToString()
{
return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}, Vol.{chapter.volumeNumber} Ch.{chapter.chapterNumber}";
}
}