diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index b8ea1bec4..7ba2547c1 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -32,6 +32,7 @@ namespace MediaBrowser.Controller.Entities ProviderIds = new Dictionary(StringComparer.OrdinalIgnoreCase); LockedFields = new List(); ImageInfos = new List(); + Identities = new List(); } /// @@ -244,6 +245,9 @@ namespace MediaBrowser.Controller.Entities public bool IsUnidentified { get; set; } + [IgnoreDataMember] + public List Identities { get; set; } + /// /// Gets or sets the locked fields. /// diff --git a/MediaBrowser.Controller/Entities/IHasMetadata.cs b/MediaBrowser.Controller/Entities/IHasMetadata.cs index 91f37135f..4e90fb155 100644 --- a/MediaBrowser.Controller/Entities/IHasMetadata.cs +++ b/MediaBrowser.Controller/Entities/IHasMetadata.cs @@ -1,4 +1,5 @@ using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using System; using System.Collections.Generic; @@ -55,5 +56,10 @@ namespace MediaBrowser.Controller.Entities /// /// true if this instance is unidentified; otherwise, false. bool IsUnidentified { get; set; } + + /// + /// Gets the item identities. + /// + List Identities { get; set; } } } diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 6d09fe163..de112c305 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -194,6 +194,7 @@ + diff --git a/MediaBrowser.Controller/Providers/IProviderManager.cs b/MediaBrowser.Controller/Providers/IProviderManager.cs index 242a16ee9..883fa12b4 100644 --- a/MediaBrowser.Controller/Providers/IProviderManager.cs +++ b/MediaBrowser.Controller/Providers/IProviderManager.cs @@ -138,5 +138,12 @@ namespace MediaBrowser.Controller.Providers /// The cancellation token. /// Task{HttpResponseInfo}. Task GetSearchImage(string providerName, string url, CancellationToken cancellationToken); + + IEnumerable> GetItemIdentityProviders() + where TLookupInfo : ItemLookupInfo + where TIdentity : IItemIdentity; + + IEnumerable> GetItemIdentityConverters() + where TIdentity : IItemIdentity; } } \ No newline at end of file diff --git a/MediaBrowser.Providers/Manager/ItemIdentifier.cs b/MediaBrowser.Controller/Providers/ItemIdentifier.cs similarity index 75% rename from MediaBrowser.Providers/Manager/ItemIdentifier.cs rename to MediaBrowser.Controller/Providers/ItemIdentifier.cs index 0bf983b69..37a8489c6 100644 --- a/MediaBrowser.Providers/Manager/ItemIdentifier.cs +++ b/MediaBrowser.Controller/Providers/ItemIdentifier.cs @@ -1,18 +1,15 @@ -using System; -using System.Collections.Generic; +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 +namespace MediaBrowser.Controller.Providers { public class ItemIdentifier where TLookupInfo : ItemLookupInfo where TIdentity : IItemIdentity { - public async Task> FindIdentities(TLookupInfo item, ProviderManager providerManager, CancellationToken cancellationToken) + public async Task> FindIdentities(TLookupInfo item, IProviderManager providerManager, CancellationToken cancellationToken) { var providers = providerManager.GetItemIdentityProviders(); var converters = providerManager.GetItemIdentityConverters(); @@ -27,7 +24,10 @@ namespace MediaBrowser.Providers.Manager Order = provider.Order }; - identities.Add(result); + if (!Equals(result.Identity, default(TIdentity))) + { + identities.Add(result); + } } var convertersAvailable = new List>(converters); @@ -43,7 +43,7 @@ namespace MediaBrowser.Providers.Manager 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)) + if (input != null && !existing.Any(id => id.Order <= converter.Order)) { var result = new IdentityPair { @@ -51,9 +51,12 @@ namespace MediaBrowser.Providers.Manager Order = converter.Order }; - identities.Add(result); - convertersAvailable.RemoveAt(i); - changesMade = true; + if (!Equals(result.Identity, default(TIdentity))) + { + identities.Add(result); + convertersAvailable.RemoveAt(i); + changesMade = true; + } } } } while (changesMade); diff --git a/MediaBrowser.Controller/Providers/ItemLookupInfo.cs b/MediaBrowser.Controller/Providers/ItemLookupInfo.cs index 65ee94556..20bc572b4 100644 --- a/MediaBrowser.Controller/Providers/ItemLookupInfo.cs +++ b/MediaBrowser.Controller/Providers/ItemLookupInfo.cs @@ -1,4 +1,7 @@ -using MediaBrowser.Model.Entities; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Model.Entities; using System; using System.Collections.Generic; @@ -96,8 +99,10 @@ namespace MediaBrowser.Controller.Providers public string Path { get; set; } } - public class EpisodeInfo : ItemLookupInfo + public class EpisodeInfo : ItemLookupInfo, IHasIdentities { + private List _identities = new List(); + public Dictionary SeriesProviderIds { get; set; } public int? IndexNumberEnd { get; set; } @@ -107,6 +112,27 @@ namespace MediaBrowser.Controller.Providers { SeriesProviderIds = new Dictionary(StringComparer.OrdinalIgnoreCase); } + + public IEnumerable Identities + { + get { return _identities; } + } + + public async Task FindIdentities(IProviderManager providerManager, CancellationToken cancellationToken) + { + var identifier = new ItemIdentifier(); + _identities = (await identifier.FindIdentities(this, providerManager, cancellationToken)).ToList(); + } + } + + public class EpisodeIdentity : IItemIdentity + { + public string Type { get; set; } + + public string SeriesId { get; set; } + public int? SeasonIndex { get; set; } + public int IndexNumber { get; set; } + public int? IndexNumberEnd { get; set; } } public class SongInfo : ItemLookupInfo @@ -116,9 +142,29 @@ namespace MediaBrowser.Controller.Providers public List Artists { get; set; } } - public class SeriesInfo : ItemLookupInfo + public class SeriesInfo : ItemLookupInfo, IHasIdentities { + private List _identities = new List(); + public int? AnimeSeriesIndex { get; set; } + + public IEnumerable Identities + { + get { return _identities; } + } + + public async Task FindIdentities(IProviderManager providerManager, CancellationToken cancellationToken) + { + var identifier = new ItemIdentifier(); + _identities = (await identifier.FindIdentities(this, providerManager, cancellationToken)).ToList(); + } + } + + public class SeriesIdentity : IItemIdentity + { + public string Type { get; set; } + + public string Id { get; set; } } public class PersonLookupInfo : ItemLookupInfo @@ -151,8 +197,10 @@ namespace MediaBrowser.Controller.Providers public string SeriesName { get; set; } } - public class SeasonInfo : ItemLookupInfo + public class SeasonInfo : ItemLookupInfo, IHasIdentities { + private List _identities = new List(); + public Dictionary SeriesProviderIds { get; set; } public int? AnimeSeriesIndex { get; set; } @@ -160,5 +208,25 @@ namespace MediaBrowser.Controller.Providers { SeriesProviderIds = new Dictionary(StringComparer.OrdinalIgnoreCase); } + + public IEnumerable Identities + { + get { return _identities; } + } + + public async Task FindIdentities(IProviderManager providerManager, CancellationToken cancellationToken) + { + var identifier = new ItemIdentifier(); + _identities = (await identifier.FindIdentities(this, providerManager, cancellationToken)).ToList(); + } + } + + public class SeasonIdentity : IItemIdentity + { + public string Type { get; set; } + + public string SeriesId { get; set; } + + public int SeasonIndex { get; set; } } } diff --git a/MediaBrowser.Providers/Manager/MetadataService.cs b/MediaBrowser.Providers/Manager/MetadataService.cs index c0ef7e144..992a519e7 100644 --- a/MediaBrowser.Providers/Manager/MetadataService.cs +++ b/MediaBrowser.Providers/Manager/MetadataService.cs @@ -104,6 +104,9 @@ namespace MediaBrowser.Providers.Manager refreshResult.AddStatus(ProviderRefreshStatus.Failure, ex.Message); } + // Identify item + TIdType id = null; + // Next run metadata providers if (refreshOptions.MetadataRefreshMode != MetadataRefreshMode.None) { @@ -120,7 +123,8 @@ namespace MediaBrowser.Providers.Manager if (providers.Count > 0) { - var result = await RefreshWithProviders(itemOfType, refreshOptions, providers, itemImageProvider, cancellationToken).ConfigureAwait(false); + id = await CreateInitialLookupInfo(itemOfType, cancellationToken).ConfigureAwait(false); + var result = await RefreshWithProviders(itemOfType, id, refreshOptions, providers, itemImageProvider, cancellationToken).ConfigureAwait(false); updateType = updateType | result.UpdateType; refreshResult.AddStatus(result.Status, result.ErrorMessage); @@ -129,6 +133,13 @@ namespace MediaBrowser.Providers.Manager } } + if (id == null) + { + id = await CreateInitialLookupInfo(itemOfType, cancellationToken).ConfigureAwait(false); + } + + MergeIdentities(itemOfType, id); + // Next run remote image providers, but only if local image providers didn't throw an exception if (!localImagesFailed && refreshOptions.ImageRefreshMode != ImageRefreshMode.ValidationOnly) { @@ -162,6 +173,15 @@ namespace MediaBrowser.Providers.Manager } } + private void MergeIdentities(TItemType item, TIdType id) + { + var hasIdentity = id as IHasIdentities; + if (hasIdentity != null) + { + item.Identities = hasIdentity.Identities.ToList(); + } + } + /// /// Befores the save. /// @@ -267,7 +287,7 @@ namespace MediaBrowser.Providers.Manager return item is TItemType; } - protected virtual async Task RefreshWithProviders(TItemType item, MetadataRefreshOptions options, List providers, ItemImageProvider imageService, CancellationToken cancellationToken) + protected virtual async Task RefreshWithProviders(TItemType item, TIdType id, MetadataRefreshOptions options, List providers, ItemImageProvider imageService, CancellationToken cancellationToken) { var refreshResult = new RefreshResult { @@ -288,7 +308,7 @@ namespace MediaBrowser.Providers.Manager // If replacing all metadata, run internet providers first if (options.ReplaceAllMetadata) { - await ExecuteRemoteProviders(item, temp, providers.OfType>(), refreshResult, cancellationToken).ConfigureAwait(false); + await ExecuteRemoteProviders(item, temp, id, providers.OfType>(), refreshResult, cancellationToken).ConfigureAwait(false); } var hasLocalMetadata = false; @@ -346,7 +366,7 @@ namespace MediaBrowser.Providers.Manager // Local metadata is king - if any is found don't run remote providers if (!options.ReplaceAllMetadata && (!hasLocalMetadata || options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh)) { - await ExecuteRemoteProviders(item, temp, providers.OfType>(), refreshResult, cancellationToken).ConfigureAwait(false); + await ExecuteRemoteProviders(item, temp, id, providers.OfType>(), refreshResult, cancellationToken).ConfigureAwait(false); } if (refreshResult.UpdateType > ItemUpdateType.None) @@ -387,10 +407,8 @@ namespace MediaBrowser.Providers.Manager return new TItemType(); } - private async Task ExecuteRemoteProviders(TItemType item, TItemType temp, IEnumerable> providers, RefreshResult refreshResult, CancellationToken cancellationToken) + private async Task ExecuteRemoteProviders(TItemType item, TItemType temp, TIdType id, IEnumerable> providers, RefreshResult refreshResult, CancellationToken cancellationToken) { - TIdType id = await CreateInitialLookupInfo(item, cancellationToken).ConfigureAwait(false); - var unidentifiedCount = 0; var identifiedCount = 0; diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index 0549da557..3011b5c12 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -95,7 +95,6 @@ - @@ -204,6 +203,7 @@ + diff --git a/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs b/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs index 922c29fe9..9ce6dfaf6 100644 --- a/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs @@ -23,7 +23,7 @@ namespace MediaBrowser.Providers.TV /// /// Class RemoteEpisodeProvider /// - class TvdbEpisodeProvider : IRemoteMetadataProvider, IHasChangeMonitor + class TvdbEpisodeProvider : IRemoteMetadataProvider, IItemIdentityProvider, IHasChangeMonitor { internal static TvdbEpisodeProvider Current; private readonly IFileSystem _fileSystem; @@ -40,19 +40,18 @@ namespace MediaBrowser.Providers.TV { var list = new List(); - string seriesTvdbId; - searchInfo.SeriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out seriesTvdbId); + var identity = searchInfo.Identities.FirstOrDefault(id => id.Type == MetadataProviders.Tvdb.ToString()) ?? await FindIdentity(searchInfo).ConfigureAwait(false); - if (!string.IsNullOrEmpty(seriesTvdbId)) + if (identity != null) { - await TvdbSeriesProvider.Current.EnsureSeriesInfo(seriesTvdbId, searchInfo.MetadataLanguage, + await TvdbSeriesProvider.Current.EnsureSeriesInfo(identity.SeriesId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false); - var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesTvdbId); + var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, identity.SeriesId); try { - var item = FetchEpisodeData(searchInfo, seriesDataPath, searchInfo.SeriesProviderIds, cancellationToken); + var item = FetchEpisodeData(searchInfo, identity, seriesDataPath, searchInfo.SeriesProviderIds, cancellationToken); if (item != null) { @@ -83,20 +82,19 @@ namespace MediaBrowser.Providers.TV get { return "TheTVDB"; } } - public Task> GetMetadata(EpisodeInfo searchInfo, CancellationToken cancellationToken) + public async Task> GetMetadata(EpisodeInfo searchInfo, CancellationToken cancellationToken) { - string seriesTvdbId; - searchInfo.SeriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out seriesTvdbId); + var identity = searchInfo.Identities.FirstOrDefault(id => id.Type == MetadataProviders.Tvdb.ToString()) ?? await FindIdentity(searchInfo).ConfigureAwait(false); var result = new MetadataResult(); - if (!string.IsNullOrEmpty(seriesTvdbId)) + if (identity != null) { - var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesTvdbId); + var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, identity.SeriesId); try { - result.Item = FetchEpisodeData(searchInfo, seriesDataPath, searchInfo.SeriesProviderIds, cancellationToken); + result.Item = FetchEpisodeData(searchInfo, identity, seriesDataPath, searchInfo.SeriesProviderIds, cancellationToken); result.HasMetadata = result.Item != null; } catch (FileNotFoundException) @@ -105,7 +103,7 @@ namespace MediaBrowser.Providers.TV } } - return Task.FromResult(result); + return result; } public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date) @@ -211,18 +209,14 @@ namespace MediaBrowser.Providers.TV /// /// The identifier. /// The series data path. + /// /// The cancellation token. /// Task{System.Boolean}. - private Episode FetchEpisodeData(EpisodeInfo id, string seriesDataPath, Dictionary seriesProviderIds, CancellationToken cancellationToken) + private Episode FetchEpisodeData(EpisodeInfo id, EpisodeIdentity identity, string seriesDataPath, Dictionary seriesProviderIds, CancellationToken cancellationToken) { - if (id.IndexNumber == null) - { - return null; - } - - var episodeNumber = id.IndexNumber.Value; + var episodeNumber = identity.IndexNumber; var seasonOffset = TvdbSeriesProvider.GetSeriesOffset(seriesProviderIds) ?? 0; - var seasonNumber = id.ParentIndexNumber + seasonOffset; + var seasonNumber = identity.SeasonIndex + seasonOffset; if (seasonNumber == null) { @@ -263,7 +257,7 @@ namespace MediaBrowser.Providers.TV usingAbsoluteData = true; } - var end = id.IndexNumberEnd ?? episodeNumber; + var end = identity.IndexNumberEnd ?? episodeNumber; episodeNumber++; while (episodeNumber <= end) @@ -722,5 +716,29 @@ namespace MediaBrowser.Providers.TV { throw new NotImplementedException(); } + + public Task FindIdentity(EpisodeInfo info) + { + string seriesTvdbId; + info.SeriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out seriesTvdbId); + + if (string.IsNullOrEmpty(seriesTvdbId) || info.IndexNumber == null) + { + return Task.FromResult(null); + } + + var id = new EpisodeIdentity + { + Type = MetadataProviders.Tvdb.ToString(), + SeriesId = seriesTvdbId, + SeasonIndex = info.ParentIndexNumber, + IndexNumber = info.IndexNumber.Value, + IndexNumberEnd = info.IndexNumberEnd + }; + + return Task.FromResult(id); + } + + public int Order { get { return 0; } } } -} +} \ No newline at end of file diff --git a/MediaBrowser.Providers/TV/TvdbSeasonIdentityProvider.cs b/MediaBrowser.Providers/TV/TvdbSeasonIdentityProvider.cs new file mode 100644 index 000000000..5876c9998 --- /dev/null +++ b/MediaBrowser.Providers/TV/TvdbSeasonIdentityProvider.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; + +namespace MediaBrowser.Providers.TV +{ + public class TvdbSeasonIdentityProvider : IItemIdentityProvider + { + public Task FindIdentity(SeasonInfo info) + { + string tvdbSeriesId; + if (!info.SeriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out tvdbSeriesId) || string.IsNullOrEmpty(tvdbSeriesId) || info.IndexNumber == null) + { + return Task.FromResult(null); + } + + var result = new SeasonIdentity + { + Type = MetadataProviders.Tvdb.ToString(), + SeriesId = tvdbSeriesId, + SeasonIndex = info.IndexNumber.Value + }; + + return Task.FromResult(result); + } + + public int Order { get { return 0; } } + } +} \ No newline at end of file diff --git a/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs b/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs index 5a9981c6e..af1edeed0 100644 --- a/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs @@ -65,7 +65,8 @@ namespace MediaBrowser.Providers.TV var season = (Season)item; var series = season.Series; - var seriesId = series != null ? series.GetProviderId(MetadataProviders.Tvdb) : null; + var identity = season.Identities.OfType().FirstOrDefault(id => id.Type == MetadataProviders.Tvdb.ToString()); + var seriesId = identity != null ? identity.SeriesId : null; if (!string.IsNullOrEmpty(seriesId) && season.IndexNumber.HasValue) { @@ -78,7 +79,7 @@ namespace MediaBrowser.Providers.TV try { - int seasonNumber = AdjustForSeriesOffset(series, season.IndexNumber.Value); + int seasonNumber = AdjustForSeriesOffset(series, identity.SeasonIndex); return GetImages(path, item.GetPreferredMetadataLanguage(), seasonNumber, cancellationToken); } catch (FileNotFoundException) diff --git a/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs b/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs index f9ebaddf8..6ca548829 100644 --- a/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbSeriesProvider.cs @@ -23,7 +23,7 @@ using System.Xml; namespace MediaBrowser.Providers.TV { - public class TvdbSeriesProvider : IRemoteMetadataProvider, IHasOrder + public class TvdbSeriesProvider : IRemoteMetadataProvider, IItemIdentityProvider, IHasOrder { private const string TvdbSeriesOffset = "TvdbSeriesOffset"; private const string TvdbSeriesOffsetFormat = "{0}-{1}"; @@ -72,7 +72,15 @@ namespace MediaBrowser.Providers.TV if (string.IsNullOrEmpty(seriesId)) { - seriesId = await FindSeries(itemId.Name, cancellationToken).ConfigureAwait(false); + seriesId = itemId.Identities + .Where(id => id.Type == MetadataProviders.Tvdb.ToString()) + .Select(id => id.Id) + .FirstOrDefault(); + + if (string.IsNullOrEmpty(seriesId)) + { + seriesId = await FindSeries(itemId.Name, cancellationToken).ConfigureAwait(false); + } if (string.IsNullOrEmpty(seriesId)) { @@ -1121,6 +1129,20 @@ namespace MediaBrowser.Providers.TV get { return "TheTVDB"; } } + public async Task FindIdentity(SeriesInfo info) + { + string tvdbId; + if (!info.ProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out tvdbId)) + tvdbId = await FindSeries(info.Name, CancellationToken.None); + + if (!string.IsNullOrEmpty(tvdbId)) + { + return new SeriesIdentity {Type = MetadataProviders.Tvdb.ToString(), Id = tvdbId}; + } + + return null; + } + public int Order { get diff --git a/MediaBrowser.sln b/MediaBrowser.sln index 4eec81cff..1b132e07f 100644 --- a/MediaBrowser.sln +++ b/MediaBrowser.sln @@ -1,6 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.Controller", "MediaBrowser.Controller\MediaBrowser.Controller.csproj", "{17E1F4E6-8ABD-4FE5-9ECF-43D4B6087BA2}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.Api", "MediaBrowser.Api\MediaBrowser.Api.csproj", "{4FD51AC5-2C16-4308-A993-C3A84F3B4582}" @@ -283,7 +285,4 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(Performance) = preSolution - HasPerformanceSessions = true - EndGlobalSection EndGlobal