mirror of
https://github.com/C9Glax/tranga.git
synced 2025-07-06 10:54:17 +02:00
Compare commits
7 Commits
1.4.2
...
fcd81f03b3
Author | SHA1 | Date | |
---|---|---|---|
fcd81f03b3 | |||
76604d84d8 | |||
af822febbe | |||
8e207c3119 | |||
b6f8c8aab5 | |||
36f7cbd3e9 | |||
3b2643d949 |
@ -137,11 +137,11 @@ public abstract class Connector
|
||||
/// <param name="settings">TrangaSettings</param>
|
||||
public void CopyCoverFromCacheToDownloadLocation(Publication publication, TrangaSettings settings)
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} {publication.internalId}");
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Cloning cover {publication.sortName} -> {publication.internalId}");
|
||||
//Check if Publication already has a Folder and cover
|
||||
string publicationFolder = publication.CreatePublicationFolder(downloadLocation);
|
||||
DirectoryInfo dirInfo = new (publicationFolder);
|
||||
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover.")))
|
||||
if (dirInfo.EnumerateFiles().Any(info => info.Name.Contains("cover", StringComparison.InvariantCultureIgnoreCase)))
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Cover exists {publication.sortName}");
|
||||
return;
|
||||
|
@ -98,7 +98,7 @@ public class MangaDex : Connector
|
||||
{
|
||||
JsonArray relationships = manga["relationships"]!.AsArray();
|
||||
posterId = relationships.FirstOrDefault(relationship => relationship!["type"]!.GetValue<string>() == "cover_art")!["id"]!.GetValue<string>();
|
||||
foreach (JsonNode node in relationships.Where(relationship =>
|
||||
foreach (JsonNode? node in relationships.Where(relationship =>
|
||||
relationship!["type"]!.GetValue<string>() == "author"))
|
||||
authorIds.Add(node!["id"]!.GetValue<string>());
|
||||
}
|
||||
|
@ -128,7 +128,6 @@ public class TaskManager
|
||||
DateTime.Now.Subtract(removeTask.Key.lastChange) > TimeSpan.FromMinutes(3))//3 Minutes since last update to task -> remove
|
||||
{
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Disposing failed task {removeTask.Key}.");
|
||||
removeTask.Key.parentTask?.DecrementProgress(removeTask.Key.progress);
|
||||
removeTask.Value.Cancel();
|
||||
toRemove.Add(removeTask.Key);
|
||||
}
|
||||
@ -136,9 +135,9 @@ public class TaskManager
|
||||
foreach (DownloadChapterTask taskToRemove in toRemove)
|
||||
{
|
||||
DeleteTask(taskToRemove);
|
||||
DownloadChapterTask newTask = new DownloadChapterTask(taskToRemove.task, taskToRemove.connectorName,
|
||||
DownloadChapterTask newTask = new (taskToRemove.task, taskToRemove.connectorName,
|
||||
taskToRemove.publication, taskToRemove.chapter, taskToRemove.language,
|
||||
taskToRemove.parentTask);
|
||||
(DownloadNewChaptersTask?)taskToRemove.parentTask);
|
||||
AddTask(newTask);
|
||||
taskToRemove.parentTask?.ReplaceFailedChildTask(taskToRemove, newTask);
|
||||
}
|
||||
@ -428,13 +427,14 @@ public class TaskManager
|
||||
this._allTasks = JsonConvert.DeserializeObject<HashSet<TrangaTask>>(buffer, new JsonSerializerSettings() { Converters = { new TrangaTask.TrangaTaskJsonConverter() } })!;
|
||||
}
|
||||
|
||||
foreach (TrangaTask task in this._allTasks.Where(task => task.GetType() == typeof(DownloadChapterTask)))
|
||||
foreach (TrangaTask task in this._allTasks.Where(tTask => tTask.parentTaskId is not null))
|
||||
{
|
||||
DownloadChapterTask dcTask = (DownloadChapterTask)task;
|
||||
IEnumerable<TrangaTask> dncTasks = this._allTasks.Where(pTask => pTask.GetType() == typeof(DownloadNewChaptersTask));
|
||||
DownloadNewChaptersTask? parentTask = (DownloadNewChaptersTask?)dncTasks.FirstOrDefault(pTask => pTask.taskId.Equals(dcTask.parentTaskId));
|
||||
dcTask.parentTask = parentTask;
|
||||
parentTask?.AddChildTask(dcTask);
|
||||
TrangaTask? parentTask = this._allTasks.FirstOrDefault(pTask => pTask.taskId == task.parentTaskId);
|
||||
if (parentTask is not null)
|
||||
{
|
||||
task.parentTask = parentTask;
|
||||
parentTask.AddChildTask(task);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -22,51 +22,30 @@ public abstract class TrangaTask
|
||||
public DateTime lastExecuted { get; set; }
|
||||
public Task task { get; }
|
||||
public string taskId { get; set; }
|
||||
[Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; }
|
||||
[Newtonsoft.Json.JsonIgnore]public double progress { get; protected set; }
|
||||
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
|
||||
[Newtonsoft.Json.JsonIgnore] public ExecutionState state { get; set; }
|
||||
[Newtonsoft.Json.JsonIgnore] protected HashSet<TrangaTask> childTasks { get; }
|
||||
[Newtonsoft.Json.JsonIgnore] public TrangaTask? parentTask { get; set; }
|
||||
public string? parentTaskId { get; set; }
|
||||
[Newtonsoft.Json.JsonIgnore]public double progress { get; private set; }
|
||||
[Newtonsoft.Json.JsonIgnore]public DateTime executionStarted { get; private set; }
|
||||
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
public DateTime executionApproximatelyFinished => this.progress != 0
|
||||
? this.executionStarted.Add(DateTime.Now.Subtract(this.executionStarted) / this.progress)
|
||||
: DateTime.MaxValue;
|
||||
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
public TimeSpan executionApproximatelyRemaining => this.executionApproximatelyFinished.Subtract(DateTime.Now);
|
||||
|
||||
[Newtonsoft.Json.JsonIgnore]public DateTime lastChange { get; private set; }
|
||||
[Newtonsoft.Json.JsonIgnore]public DateTime executionApproximatelyFinished => progress != 0 ? lastChange.Add(GetRemainingTime()) : DateTime.MaxValue;
|
||||
[Newtonsoft.Json.JsonIgnore]public TimeSpan executionApproximatelyRemaining => executionApproximatelyFinished.Subtract(DateTime.Now);
|
||||
[Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence);
|
||||
|
||||
public enum ExecutionState
|
||||
{
|
||||
Waiting,
|
||||
Enqueued,
|
||||
Running
|
||||
};
|
||||
public enum ExecutionState { Waiting, Enqueued, Running }
|
||||
|
||||
protected TrangaTask(Task task, TimeSpan reoccurrence)
|
||||
protected TrangaTask(Task task, TimeSpan reoccurrence, TrangaTask? parentTask = null)
|
||||
{
|
||||
this.reoccurrence = reoccurrence;
|
||||
this.lastExecuted = DateTime.Now.Subtract(reoccurrence);
|
||||
this.task = task;
|
||||
this.progress = 0f;
|
||||
this.executionStarted = DateTime.Now;
|
||||
this.lastChange = DateTime.MaxValue;
|
||||
this.taskId = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(this.executionStarted.ToString(CultureInfo.InvariantCulture)));
|
||||
}
|
||||
|
||||
public double IncrementProgress(double amount)
|
||||
{
|
||||
this.progress += amount;
|
||||
this.lastChange = DateTime.Now;
|
||||
return this.progress;
|
||||
}
|
||||
|
||||
public double DecrementProgress(double amount)
|
||||
{
|
||||
this.progress -= amount;
|
||||
this.lastChange = DateTime.Now;
|
||||
return this.progress;
|
||||
this.childTasks = new();
|
||||
this.parentTask = parentTask;
|
||||
this.parentTaskId = parentTask?.taskId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -95,10 +74,44 @@ public abstract class TrangaTask
|
||||
logger?.WriteLine(this.GetType().ToString(), $"Finished Executing Task {this}");
|
||||
}
|
||||
|
||||
/// <returns>True if elapsed time since last execution is greater than set interval</returns>
|
||||
public bool ShouldExecute()
|
||||
public void ReplaceFailedChildTask(DownloadChapterTask failed, DownloadChapterTask newTask)
|
||||
{
|
||||
return nextExecution < DateTime.Now && state is ExecutionState.Waiting;
|
||||
if (!this.childTasks.Contains(failed))
|
||||
throw new ArgumentException($"Task {failed} is not childTask of {this}");
|
||||
this.childTasks.Remove(failed);
|
||||
this.childTasks.Add(newTask);
|
||||
this.DecrementProgress(failed.progress);
|
||||
}
|
||||
|
||||
public void AddChildTask(TrangaTask childTask)
|
||||
{
|
||||
this.childTasks.Add(childTask);
|
||||
}
|
||||
|
||||
public void IncrementProgress(double amount)
|
||||
{
|
||||
this.lastChange = DateTime.Now;
|
||||
this.progress += amount / (childTasks.Count > 0 ? childTasks.Count : 1);
|
||||
if (parentTask is not null)
|
||||
{
|
||||
parentTask.IncrementProgress(amount);
|
||||
parentTask.state = ExecutionState.Running;
|
||||
}
|
||||
}
|
||||
|
||||
public void DecrementProgress(double amount)
|
||||
{
|
||||
this.lastChange = DateTime.Now;
|
||||
this.progress -= amount / childTasks.Count > 0 ? childTasks.Count : 1;
|
||||
parentTask?.DecrementProgress(amount);
|
||||
}
|
||||
|
||||
private TimeSpan GetRemainingTime()
|
||||
{
|
||||
if(progress == 0)
|
||||
return DateTime.MaxValue.Subtract(DateTime.Now);
|
||||
TimeSpan elapsed = lastChange.Subtract(executionStarted);
|
||||
return elapsed.Divide(progress).Subtract(elapsed);
|
||||
}
|
||||
|
||||
public enum Task : byte
|
||||
|
@ -9,18 +9,13 @@ public class DownloadChapterTask : TrangaTask
|
||||
public Publication publication { get; }
|
||||
public string language { get; }
|
||||
public Chapter chapter { get; }
|
||||
[JsonIgnore]public DownloadNewChaptersTask? parentTask { get; set; }
|
||||
public string? parentTaskId { get; set; }
|
||||
|
||||
|
||||
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero)
|
||||
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero, parentTask)
|
||||
{
|
||||
this.chapter = chapter;
|
||||
this.connectorName = connectorName;
|
||||
this.publication = publication;
|
||||
this.language = language;
|
||||
this.parentTask = parentTask;
|
||||
this.parentTaskId = parentTask?.taskId;
|
||||
}
|
||||
|
||||
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
||||
@ -30,6 +25,7 @@ public class DownloadChapterTask : TrangaTask
|
||||
if(this.parentTask is not null)
|
||||
this.parentTask.state = ExecutionState.Running;
|
||||
Connector connector = taskManager.GetConnector(this.connectorName);
|
||||
connector.CopyCoverFromCacheToDownloadLocation(this.publication, taskManager.settings);
|
||||
connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
|
||||
if(this.parentTask is not null)
|
||||
this.parentTask.state = ExecutionState.Waiting;
|
||||
|
@ -8,15 +8,11 @@ public class DownloadNewChaptersTask : TrangaTask
|
||||
public string connectorName { get; }
|
||||
public Publication publication { get; }
|
||||
public string language { get; }
|
||||
[JsonIgnore]private HashSet<DownloadChapterTask> childTasks { get; }
|
||||
[JsonIgnore]public new double progress => childTasks.Count > 0 ? childTasks.Sum(childTask => childTask.progress) / childTasks.Count : 0;
|
||||
|
||||
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;
|
||||
this.childTasks = new();
|
||||
}
|
||||
|
||||
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
||||
@ -36,24 +32,11 @@ public class DownloadNewChaptersTask : TrangaTask
|
||||
|
||||
foreach (Chapter newChapter in newChapters)
|
||||
{
|
||||
DownloadChapterTask newTask = new (Task.DownloadChapter, this.connectorName!, pub, newChapter, this.language, this);
|
||||
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);
|
||||
}
|
||||
|
||||
public void AddChildTask(DownloadChapterTask childTask)
|
||||
{
|
||||
this.childTasks.Add(childTask);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the available Chapters of a Publication
|
||||
|
@ -14,6 +14,6 @@ public class UpdateLibrariesTask : TrangaTask
|
||||
return;
|
||||
foreach(LibraryManager lm in taskManager.settings.libraryManagers)
|
||||
lm.UpdateLibrary();
|
||||
this.progress = 1f;
|
||||
IncrementProgress(1);
|
||||
}
|
||||
}
|
@ -366,9 +366,14 @@ function ShowTasksQueue(){
|
||||
.then(json => {
|
||||
tagTasksRunning.innerText = json.length;
|
||||
json.forEach(task => {
|
||||
downloadTasksOutput.appendChild(CreateProgressChild(task));
|
||||
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
|
||||
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
|
||||
if(task.task == 2 || task.task == 4) {
|
||||
downloadTasksOutput.appendChild(CreateProgressChild(task));
|
||||
document.querySelector(`#progress${task.taskId.replaceAll('=', '')}`).value = task.progress;
|
||||
var finishedHours = task.executionApproximatelyRemaining.split(':')[0];
|
||||
var finishedMinutes = task.executionApproximatelyRemaining.split(':')[1];
|
||||
var finishedSeconds = task.executionApproximatelyRemaining.split(':')[2].split('.')[0];
|
||||
document.querySelector(`#progressStr${task.taskId.replaceAll('=', '')}`).innerText = `${finishedHours}:${finishedMinutes}:${finishedSeconds}`;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@ -399,7 +404,7 @@ function CreateProgressChild(task){
|
||||
child.appendChild(progress);
|
||||
|
||||
var progressStr = document.createElement("span");
|
||||
progressStr.innerText = "00.00%";
|
||||
progressStr.innerText = " \t∞";
|
||||
progressStr.className = "progressStr";
|
||||
progressStr.id = `progressStr${task.taskId.replaceAll('=','')}`;
|
||||
child.appendChild(progressStr);
|
||||
@ -484,8 +489,13 @@ setInterval(() => {
|
||||
setInterval(() => {
|
||||
GetRunningTasks().then((json) => {
|
||||
json.forEach(task => {
|
||||
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
|
||||
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = task.progress.toLocaleString(undefined,{style: 'percent', minimumFractionDigits:2});
|
||||
if(task.task == 2 || task.task == 4){
|
||||
document.querySelector(`#progress${task.taskId.replaceAll('=','')}`).value = task.progress;
|
||||
var finishedHours = task.executionApproximatelyRemaining.split(':')[0];
|
||||
var finishedMinutes = task.executionApproximatelyRemaining.split(':')[1];
|
||||
var finishedSeconds = task.executionApproximatelyRemaining.split(':')[2].split('.')[0];
|
||||
document.querySelector(`#progressStr${task.taskId.replaceAll('=','')}`).innerText = `${finishedHours}:${finishedMinutes}:${finishedSeconds}`;
|
||||
}
|
||||
});
|
||||
});
|
||||
},500);
|
Reference in New Issue
Block a user