Re enabling the metadata provider loader task

This commit is contained in:
Zoe Roux 2021-07-20 00:06:26 +02:00
parent 4c0179ad4d
commit 7875c72dd9
4 changed files with 83 additions and 47 deletions

View File

@ -20,6 +20,12 @@ namespace Kyoo.Models
/// </summary>
public string Link { get; set; }
/// <summary>
/// A shortcut to access the provider of this metadata.
/// Unlike the <see cref="Link{T, T2}.Second"/> property, this is serializable.
/// </summary>
public Provider Provider => Second;
/// <summary>
/// The expression to retrieve the unique ID of a MetadataID. This is an aggregate of the two resources IDs.
/// </summary>

@ -1 +1 @@
Subproject commit dcdebad14cbcdf1f9486cb9178e6518d10c0e97f
Subproject commit 87783a5bfdd79f21d72bad13da1d96ec8e08cd42

View File

@ -115,6 +115,7 @@ namespace Kyoo
builder.RegisterTask<Housekeeping>();
builder.RegisterTask<RegisterEpisode>();
builder.RegisterTask<RegisterSubtitle>();
builder.RegisterTask<MetadataProviderLoader>();
static bool DatabaseIsPresent(IComponentRegistryBuilder x)
=> x.IsRegistered(new TypedService(typeof(DatabaseContext)));

View File

@ -1,46 +1,75 @@
// using System;
// using System.Collections.Generic;
// using System.Threading;
// using System.Threading.Tasks;
// using Kyoo.Controllers;
// using Kyoo.Models;
// using Microsoft.Extensions.DependencyInjection;
//
// namespace Kyoo.Tasks
// {
// public class MetadataProviderLoader : ITask
// {
// public string Slug => "reload-metdata";
// public string Name => "Reload Metadata Providers";
// public string Description => "Add every loaded metadata provider to the database.";
// public string HelpMessage => null;
// public bool RunOnStartup => true;
// public int Priority => 1000;
//
// public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string arguments = null)
// {
// using IServiceScope serviceScope = serviceProvider.CreateScope();
// IProviderRepository providers = serviceScope.ServiceProvider.GetService<IProviderRepository>();
// IThumbnailsManager thumbnails = serviceScope.ServiceProvider.GetService<IThumbnailsManager>();
// IPluginManager pluginManager = serviceScope.ServiceProvider.GetService<IPluginManager>();
//
// foreach (IMetadataProvider provider in pluginManager!.GetPlugins<IMetadataProvider>())
// {
// if (string.IsNullOrEmpty(provider.Provider.Slug))
// throw new ArgumentException($"Empty provider slug (name: {provider.Provider.Name}).");
// await providers!.CreateIfNotExists(provider.Provider);
// await thumbnails!.Validate(provider.Provider);
// }
// }
//
// public Task<IEnumerable<string>> GetPossibleParameters()
// {
// return Task.FromResult<IEnumerable<string>>(null);
// }
//
// public int? Progress()
// {
// return null;
// }
// }
// }
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Kyoo.Controllers;
using Kyoo.Models.Attributes;
using Kyoo.Models.Exceptions;
namespace Kyoo.Tasks
{
/// <summary>
/// A task that download metadata providers images.
/// </summary>
public class MetadataProviderLoader : ITask
{
/// <inheritdoc />
public string Slug => "reload-metdata";
/// <inheritdoc />
public string Name => "Reload Metadata Providers";
/// <inheritdoc />
public string Description => "Add every loaded metadata provider to the database.";
/// <inheritdoc />
public string HelpMessage => null;
/// <inheritdoc />
public bool RunOnStartup => true;
/// <inheritdoc />
public int Priority => 1000;
/// <inheritdoc />
public bool IsHidden => true;
/// <summary>
/// The provider repository used to create in-db providers from metadata providers.
/// </summary>
[Injected] public IProviderRepository Providers { private get; set; }
/// <summary>
/// The thumbnail manager used to download providers logo.
/// </summary>
[Injected] public IThumbnailsManager Thumbnails { private get; set; }
/// <summary>
/// The list of metadata providers to register.
/// </summary>
[Injected] public ICollection<IMetadataProvider> MetadataProviders { private get; set; }
/// <inheritdoc />
public TaskParameters GetParameters()
{
return new();
}
/// <inheritdoc />
public async Task Run(TaskParameters arguments, IProgress<float> progress, CancellationToken cancellationToken)
{
float percent = 0;
progress.Report(0);
foreach (IMetadataProvider provider in MetadataProviders)
{
if (string.IsNullOrEmpty(provider.Provider.Slug))
throw new TaskFailedException($"Empty provider slug (name: {provider.Provider.Name}).");
await Providers.CreateIfNotExists(provider.Provider);
await Thumbnails.DownloadImages(provider.Provider);
percent += 100f / MetadataProviders.Count;
progress.Report(percent);
}
progress.Report(100);
}
}
}