Added item identity providers and converters

This commit is contained in:
Thomas Gillen 2014-05-05 20:53:05 +01:00
parent e45001f99c
commit a14e919a85
8 changed files with 164 additions and 14 deletions

View File

@ -194,6 +194,7 @@
<Compile Include="Security\IEncryptionManager.cs" />
<Compile Include="Subtitles\ISubtitleManager.cs" />
<Compile Include="Subtitles\ISubtitleProvider.cs" />
<Compile Include="Providers\ItemIdentities.cs" />
<Compile Include="Providers\ItemLookupInfo.cs" />
<Compile Include="Providers\MetadataRefreshOptions.cs" />
<Compile Include="Providers\NameParser.cs" />

View File

@ -54,11 +54,13 @@ namespace MediaBrowser.Controller.Providers
/// </summary>
/// <param name="imageProviders">The image providers.</param>
/// <param name="metadataServices">The metadata services.</param>
/// <param name="identityProviders">The identity providers.</param>
/// <param name="identityConverters">The identity converters.</param>
/// <param name="metadataProviders">The metadata providers.</param>
/// <param name="savers">The savers.</param>
/// <param name="imageSavers">The image savers.</param>
/// <param name="externalIds">The external ids.</param>
void AddParts(IEnumerable<IImageProvider> imageProviders, IEnumerable<IMetadataService> metadataServices, IEnumerable<IMetadataProvider> metadataProviders,
void AddParts(IEnumerable<IImageProvider> imageProviders, IEnumerable<IMetadataService> metadataServices, IEnumerable<IItemIdentityProvider> identityProviders, IEnumerable<IItemIdentityConverter> identityConverters, IEnumerable<IMetadataProvider> metadataProviders,
IEnumerable<IMetadataSaver> savers,
IEnumerable<IImageSaver> imageSavers,
IEnumerable<IExternalId> externalIds);

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers
{
public interface IItemIdentity
{
string Type { get; }
}
public interface IHasIdentities<out TIdentity>
where TIdentity : IItemIdentity
{
IEnumerable<TIdentity> Identities { get; }
Task FindIdentities(IProviderManager providerManager, CancellationToken cancellationToken);
}
public interface IItemIdentityProvider : IHasOrder { }
public interface IItemIdentityProvider<in TLookupInfo, TIdentity> : IItemIdentityProvider
where TLookupInfo : ItemLookupInfo
where TIdentity : IItemIdentity
{
Task<TIdentity> FindIdentity(TLookupInfo info);
}
public interface IItemIdentityConverter : IHasOrder { }
public interface IItemIdentityConverter<TIdentity> : IItemIdentityConverter
where TIdentity : IItemIdentity
{
Task<TIdentity> Convert(TIdentity identity);
string SourceType { get; }
string ResultType { get; }
}
}

View File

@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Providers;
namespace MediaBrowser.Providers.Manager
{
public class ItemIdentifier<TLookupInfo, TIdentity>
where TLookupInfo : ItemLookupInfo
where TIdentity : IItemIdentity
{
public async Task<IEnumerable<TIdentity>> FindIdentities(TLookupInfo item, ProviderManager providerManager, CancellationToken cancellationToken)
{
var providers = providerManager.GetItemIdentityProviders<TLookupInfo, TIdentity>();
var converters = providerManager.GetItemIdentityConverters<TIdentity>();
var identities = new List<IdentityPair>();
foreach (var provider in providers)
{
var result = new IdentityPair
{
Identity = await provider.FindIdentity(item),
Order = provider.Order
};
identities.Add(result);
}
var convertersAvailable = new List<IItemIdentityConverter<TIdentity>>(converters);
bool changesMade;
do
{
changesMade = false;
for (int i = convertersAvailable.Count - 1; i >= 0; i--)
{
var converter = convertersAvailable[i];
var input = identities.FirstOrDefault(id => id.Identity.Type == converter.SourceType);
var existing = identities.Where(id => id.Identity.Type == converter.ResultType);
if (input != null && !existing.Any(id => id.Order >= converter.Order))
{
var result = new IdentityPair
{
Identity = await converter.Convert(input.Identity).ConfigureAwait(false),
Order = converter.Order
};
identities.Add(result);
convertersAvailable.RemoveAt(i);
changesMade = true;
}
}
} while (changesMade);
return identities.Select(id => id.Identity);
}
private class IdentityPair
{
public TIdentity Identity;
public int Order;
}
}
}

View File

@ -389,7 +389,7 @@ namespace MediaBrowser.Providers.Manager
private async Task ExecuteRemoteProviders(TItemType item, TItemType temp, IEnumerable<IRemoteMetadataProvider<TItemType, TIdType>> providers, RefreshResult refreshResult, CancellationToken cancellationToken)
{
TIdType id = null;
TIdType id = await CreateInitialLookupInfo(item, cancellationToken).ConfigureAwait(false);
var unidentifiedCount = 0;
var identifiedCount = 0;
@ -399,11 +399,7 @@ namespace MediaBrowser.Providers.Manager
var providerName = provider.GetType().Name;
Logger.Debug("Running {0} for {1}", providerName, item.Path ?? item.Name);
if (id == null)
{
id = item.GetLookupInfo();
}
else
if (id != null)
{
MergeNewData(temp, id);
}
@ -448,6 +444,19 @@ namespace MediaBrowser.Providers.Manager
}
}
private async Task<TIdType> CreateInitialLookupInfo(TItemType item, CancellationToken cancellationToken)
{
var info = item.GetLookupInfo();
var hasIdentity = info as IHasIdentities<IItemIdentity>;
if (hasIdentity != null)
{
await hasIdentity.FindIdentities(ProviderManager, cancellationToken).ConfigureAwait(false);
}
return info;
}
private void MergeNewData(TItemType source, TIdType lookupInfo)
{
// Copy new provider id's that may have been obtained

View File

@ -53,6 +53,8 @@ namespace MediaBrowser.Providers.Manager
private readonly IFileSystem _fileSystem;
private IMetadataService[] _metadataServices = { };
private IItemIdentityProvider[] _identityProviders = { };
private IItemIdentityConverter[] _identityConverters = { };
private IMetadataProvider[] _metadataProviders = { };
private IEnumerable<IMetadataSaver> _savers;
private IImageSaver[] _imageSavers;
@ -81,17 +83,22 @@ namespace MediaBrowser.Providers.Manager
/// </summary>
/// <param name="imageProviders">The image providers.</param>
/// <param name="metadataServices">The metadata services.</param>
/// <param name="identityProviders">The identity providers.</param>
/// <param name="identityConverters">The identity converters.</param>
/// <param name="metadataProviders">The metadata providers.</param>
/// <param name="metadataSavers">The metadata savers.</param>
/// <param name="imageSavers">The image savers.</param>
/// <param name="externalIds">The external ids.</param>
public void AddParts(IEnumerable<IImageProvider> imageProviders, IEnumerable<IMetadataService> metadataServices, IEnumerable<IMetadataProvider> metadataProviders, IEnumerable<IMetadataSaver> metadataSavers,
IEnumerable<IImageSaver> imageSavers,
IEnumerable<IExternalId> externalIds)
public void AddParts(IEnumerable<IImageProvider> imageProviders, IEnumerable<IMetadataService> metadataServices,
IEnumerable<IItemIdentityProvider> identityProviders, IEnumerable<IItemIdentityConverter> identityConverters,
IEnumerable<IMetadataProvider> metadataProviders, IEnumerable<IMetadataSaver> metadataSavers,
IEnumerable<IImageSaver> imageSavers, IEnumerable<IExternalId> externalIds)
{
ImageProviders = imageProviders.ToArray();
_metadataServices = metadataServices.OrderBy(i => i.Order).ToArray();
_identityProviders = identityProviders.ToArray();
_identityConverters = identityConverters.ToArray();
_metadataProviders = metadataProviders.ToArray();
_savers = metadataSavers.ToArray();
_imageSavers = imageSavers.ToArray();
@ -257,6 +264,19 @@ namespace MediaBrowser.Providers.Manager
.ThenBy(GetDefaultOrder);
}
public IEnumerable<IItemIdentityProvider<TLookupInfo, TIdentity>> GetItemIdentityProviders<TLookupInfo, TIdentity>()
where TLookupInfo : ItemLookupInfo
where TIdentity : IItemIdentity
{
return _identityProviders.OfType<IItemIdentityProvider<TLookupInfo, TIdentity>>();
}
public IEnumerable<IItemIdentityConverter<TIdentity>> GetItemIdentityConverters<TIdentity>()
where TIdentity : IItemIdentity
{
return _identityConverters.OfType<IItemIdentityConverter<TIdentity>>();
}
private IEnumerable<IRemoteImageProvider> GetRemoteImageProviders(IHasImages item, bool includeDisabled)
{
var options = GetMetadataOptions(item);

View File

@ -95,6 +95,7 @@
<Compile Include="LiveTv\ProgramMetadataService.cs" />
<Compile Include="LiveTv\VideoRecordingService.cs" />
<Compile Include="Manager\ImageSaver.cs" />
<Compile Include="Manager\ItemIdentifier.cs" />
<Compile Include="Manager\ItemImageProvider.cs" />
<Compile Include="Manager\ProviderManager.cs" />
<Compile Include="Manager\MetadataService.cs" />

View File

@ -708,10 +708,14 @@ namespace MediaBrowser.ServerApplication
GetExports<IBaseItemComparer>(),
GetExports<ILibraryPostScanTask>());
ProviderManager.AddParts(GetExports<IImageProvider>(), GetExports<IMetadataService>(), GetExports<IMetadataProvider>(),
GetExports<IMetadataSaver>(),
GetExports<IImageSaver>(),
GetExports<IExternalId>());
ProviderManager.AddParts(GetExports<IImageProvider>(),
GetExports<IMetadataService>(),
GetExports<IItemIdentityProvider>(),
GetExports<IItemIdentityConverter>(),
GetExports<IMetadataProvider>(),
GetExports<IMetadataSaver>(),
GetExports<IImageSaver>(),
GetExports<IExternalId>());
SeriesOrderManager.AddParts(GetExports<ISeriesOrderProvider>());