Cleaning uo

This commit is contained in:
Zoe Roux 2021-07-17 02:32:50 +02:00
parent e66e59cf32
commit 84458d3413
7 changed files with 66 additions and 8 deletions

View File

@ -20,6 +20,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
<PackageReference Include="TvDbSharper" Version="3.2.2" />
</ItemGroup>

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Autofac;
using Kyoo.Controllers;
using TvDbSharper;
namespace Kyoo.TheTvdb
{
@ -35,6 +36,7 @@ namespace Kyoo.TheTvdb
/// <inheritdoc />
public void Configure(ContainerBuilder builder)
{
builder.RegisterType<TvDbClient>().As<ITvDbClient>();
builder.RegisterProvider<ProviderTvdb>();
}
}

View File

@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Kyoo.Authentication.Models;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.Extensions.Options;
using TvDbSharper;
using TvDbSharper.Dto;
@ -18,12 +20,12 @@ namespace Kyoo.TheTvdb
/// <summary>
/// The internal tvdb client used to make requests.
/// </summary>
private readonly TvDbClient _client = new();
private readonly ITvDbClient _client;
/// <summary>
/// The API key used to authenticate with the tvdb API.
/// </summary>
private readonly string _apiKey;
private readonly IOptions<TvdbOption> _apiKey;
/// <inheritdoc />
public Provider Provider => new()
@ -35,15 +37,24 @@ namespace Kyoo.TheTvdb
};
public ProviderTvdb(string apiKey)
/// <summary>
/// Create a new <see cref="ProviderTvdb"/> using a tvdb client and an api key.
/// </summary>
/// <param name="client">The tvdb client to use</param>
/// <param name="apiKey">The api key</param>
public ProviderTvdb(ITvDbClient client, IOptions<TvdbOption> apiKey)
{
_client = client;
_apiKey = apiKey;
}
/// <summary>
/// Authenticate and refresh the token of the tvdb client.
/// </summary>
private Task _Authenticate()
{
if (_client.Authentication.Token == null)
return _client.Authentication.AuthenticateAsync(_apiKey);
return _client.Authentication.AuthenticateAsync(_apiKey.Value.ApiKey);
return _client.Authentication.RefreshTokenAsync();
}

View File

@ -0,0 +1,18 @@
namespace Kyoo.Authentication.Models
{
/// <summary>
/// The option containing the api key for the tvdb.
/// </summary>
public class TvdbOption
{
/// <summary>
/// The path to get this option from the root configuration.
/// </summary>
public const string Path = "tvdb";
/// <summary>
/// The api key of the tvdb.
/// </summary>
public string ApiKey { get; set; }
}
}

View File

@ -144,7 +144,7 @@ namespace Kyoo.Controllers
ICollection<TaskParameter> all = task.GetParameters();
ICollection<string> invalids = arguments.Keys
.Where(x => all.Any(y => x != y.Name))
.Where(x => all.All(y => x != y.Name))
.ToArray();
if (invalids.Any())
{

View File

@ -38,7 +38,9 @@ namespace Kyoo
typeof(IThumbnailsManager),
typeof(IMetadataProvider),
typeof(ITaskManager),
typeof(ILibraryManager)
typeof(ILibraryManager),
typeof(IIdentifier),
typeof(AProviderComposite)
};
/// <inheritdoc />
@ -99,9 +101,14 @@ namespace Kyoo
builder.RegisterType<ThumbnailsManager>().As<IThumbnailsManager>().SingleInstance();
builder.RegisterType<TaskManager>().As<ITaskManager>().SingleInstance();
builder.RegisterType<LibraryManager>().As<ILibraryManager>().InstancePerLifetimeScope();
builder.RegisterComposite<ProviderComposite, IMetadataProvider>().InstancePerLifetimeScope();
builder.RegisterType<RegexIdentifier>().As<IIdentifier>().SingleInstance();
builder.RegisterComposite<ProviderComposite, IMetadataProvider>();
builder.Register(x => (AProviderComposite)x.Resolve<IMetadataProvider>());
builder.RegisterTask<Crawler>();
builder.RegisterTask<Housekeeping>();
builder.RegisterTask<RegisterEpisode>();
builder.RegisterTask<RegisterSubtitle>();
static bool DatabaseIsPresent(IComponentRegistryBuilder x)
=> x.IsRegistered(new TypedService(typeof(DatabaseContext)));

View File

@ -4,8 +4,14 @@ using System.Linq;
namespace Kyoo.Models.Watch
{
/// <summary>
/// A static class allowing one to identify files extensions.
/// </summary>
public static class FileExtensions
{
/// <summary>
/// The list of known video extensions
/// </summary>
public static readonly string[] VideoExtensions =
{
".webm",
@ -34,17 +40,30 @@ namespace Kyoo.Models.Watch
".3g2"
};
/// <summary>
/// Check if a file represent a video file (only by checking the extension of the file)
/// </summary>
/// <param name="filePath">The path of the file to check</param>
/// <returns><c>true</c> if the file is a video file, <c>false</c> otherwise.</returns>
public static bool IsVideo(string filePath)
{
return VideoExtensions.Contains(Path.GetExtension(filePath));
}
/// <summary>
/// The dictionary of known subtitles extensions and the name of the subtitle codec.
/// </summary>
public static readonly Dictionary<string, string> SubtitleExtensions = new()
{
{".ass", "ass"},
{".str", "subrip"}
};
/// <summary>
/// Check if a file represent a subtitle file (only by checking the extension of the file)
/// </summary>
/// <param name="filePath">The path of the file to check</param>
/// <returns><c>true</c> if the file is a subtitle file, <c>false</c> otherwise.</returns>
public static bool IsSubtitle(string filePath)
{
return SubtitleExtensions.ContainsKey(Path.GetExtension(filePath));