using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Core.Models.Watch;
using Microsoft.Extensions.Logging;
namespace Kyoo.Core.Tasks
{
///
/// A task to add new video files.
///
[TaskMetadata("scan", "Scan libraries", "Scan your libraries and load data for new shows.", RunOnStartup = true)]
public class Crawler : ITask
{
///
/// The library manager used to get libraries and providers to use.
///
private readonly ILibraryManager _libraryManager;
///
/// The file manager used walk inside directories and check they existences.
///
private readonly IFileSystem _fileSystem;
///
/// A task manager used to create sub tasks for each episode to add to the database.
///
private readonly ITaskManager _taskManager;
///
/// The logger used to inform the current status to the console.
///
private readonly ILogger _logger;
///
/// Create a new .
///
/// The library manager to retrieve existing episodes/library/tracks
/// The file system to glob files
/// The task manager used to start .
/// The logger used print messages.
public Crawler(ILibraryManager libraryManager,
IFileSystem fileSystem,
ITaskManager taskManager,
ILogger logger)
{
_libraryManager = libraryManager;
_fileSystem = fileSystem;
_taskManager = taskManager;
_logger = logger;
}
///
public TaskParameters GetParameters()
{
return new()
{
TaskParameter.Create("slug", "A library slug to restrict the scan to this library.")
};
}
///
public async Task Run(TaskParameters arguments, IProgress progress, CancellationToken cancellationToken)
{
string argument = arguments["slug"].As();
ICollection libraries = argument == null
? await _libraryManager.GetAll()
: new [] { await _libraryManager.GetOrDefault(argument)};
if (argument != null && libraries.First() == null)
throw new ArgumentException($"No library found with the name {argument}");
foreach (Library library in libraries)
await _libraryManager.Load(library, x => x.Providers);
progress.Report(0);
float percent = 0;
ICollection episodes = await _libraryManager.GetAll();
ICollection