Tranga-Website/Tranga/TaskManager.cs

92 lines
3.1 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using Tranga.Connectors;
namespace Tranga;
public class TaskManager
{
private readonly Dictionary<Publication, List<Chapter>> _chapterCollection;
private readonly HashSet<TrangaTask> _allTasks;
private bool _continueRunning = true;
2023-05-19 16:27:56 +02:00
private readonly Connector[] connectors;
private readonly string folderPath;
public TaskManager(string folderPath)
{
2023-05-19 16:27:56 +02:00
this.folderPath = folderPath;
this.connectors = new Connector[]{ new MangaDex(folderPath) };
_chapterCollection = new();
_allTasks = ImportTasks(Directory.GetCurrentDirectory());
Thread taskChecker = new(TaskCheckerThread);
taskChecker.Start();
}
private void TaskCheckerThread()
{
while (_continueRunning)
{
2023-05-19 16:27:56 +02:00
foreach (TrangaTask task in _allTasks)
{
2023-05-19 16:27:56 +02:00
if(task.ShouldExecute())
TaskExecutor.Execute(this.connectors, task, this._chapterCollection);
}
Thread.Sleep(1000);
}
}
2023-05-19 16:27:56 +02:00
public bool AddTask(TrangaTask.Task task, Connector connector, Publication? publication, TimeSpan reoccurrence,
2023-05-19 14:15:17 +02:00
string language = "")
{
2023-05-19 16:27:56 +02:00
if(!_allTasks.Any(trangaTask => trangaTask.task != task && trangaTask.connectorName != connector.name && trangaTask.publication?.downloadUrl != publication?.downloadUrl))
return _allTasks.Add(new TrangaTask(connector.name, task, publication, reoccurrence, language));
2023-05-19 14:15:17 +02:00
return false;
}
2023-05-19 16:27:56 +02:00
public bool RemoveTask(TrangaTask.Task task, string connectorName, Publication? publication)
2023-05-19 14:15:17 +02:00
{
return (_allTasks.RemoveWhere(trangaTask =>
2023-05-19 16:27:56 +02:00
trangaTask.task == task && trangaTask.connectorName == connectorName && trangaTask.publication?.downloadUrl == publication?.downloadUrl)
2023-05-19 14:15:17 +02:00
> 0);
}
2023-05-19 16:27:56 +02:00
public Dictionary<string, Connector> GetAvailableConnectors()
{
return this.connectors.ToDictionary(connector => connector.name, connector => connector);
}
public TrangaTask[] GetAllTasks()
{
TrangaTask[] ret = new TrangaTask[_allTasks.Count];
_allTasks.CopyTo(ret);
return ret;
}
public void Shutdown()
{
_continueRunning = false;
ExportTasks(Directory.GetCurrentDirectory());
}
public HashSet<TrangaTask> ImportTasks(string importFolderPath)
{
string filePath = Path.Join(importFolderPath, "tasks.json");
if (!File.Exists(filePath))
return new HashSet<TrangaTask>();
string toRead = File.ReadAllText(filePath);
TrangaTask[] importTasks = JsonConvert.DeserializeObject<TrangaTask[]>(toRead)!;
foreach(TrangaTask task in importTasks.Where(task => task.publication is not null))
this._chapterCollection.Add((Publication)task.publication!, new List<Chapter>());
return importTasks.ToHashSet();
}
public void ExportTasks(string exportFolderPath)
{
string filePath = Path.Join(exportFolderPath, "tasks.json");
string toWrite = JsonConvert.SerializeObject(_allTasks.ToArray());
File.WriteAllText(filePath,toWrite);
}
}