jellyfin/MediaBrowser.Providers/Chapters/ChapterManager.cs

288 lines
9.2 KiB
C#
Raw Normal View History

2014-06-29 17:35:05 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
2014-06-09 19:16:14 +00:00
using MediaBrowser.Controller.Chapters;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-06-10 17:36:06 +00:00
using MediaBrowser.Controller.Persistence;
2014-06-09 19:16:14 +00:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Chapters;
2014-06-29 17:35:05 +00:00
using MediaBrowser.Model.Configuration;
2014-06-10 17:36:06 +00:00
using MediaBrowser.Model.Entities;
2014-06-09 19:16:14 +00:00
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Chapters
{
public class ChapterManager : IChapterManager
{
private IChapterProvider[] _providers;
private readonly ILibraryManager _libraryManager;
private readonly ILogger _logger;
private readonly IServerConfigurationManager _config;
2014-06-10 17:36:06 +00:00
private readonly IItemRepository _itemRepo;
2014-06-09 19:16:14 +00:00
2014-06-10 17:36:06 +00:00
public ChapterManager(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IItemRepository itemRepo)
2014-06-09 19:16:14 +00:00
{
_libraryManager = libraryManager;
_logger = logger;
_config = config;
2014-06-10 17:36:06 +00:00
_itemRepo = itemRepo;
2014-06-09 19:16:14 +00:00
}
public void AddParts(IEnumerable<IChapterProvider> chapterProviders)
{
_providers = chapterProviders.ToArray();
}
public Task<IEnumerable<RemoteChapterResult>> Search(Video video, CancellationToken cancellationToken)
{
VideoContentType mediaType;
if (video is Episode)
{
mediaType = VideoContentType.Episode;
}
else if (video is Movie)
{
mediaType = VideoContentType.Movie;
}
else
{
// These are the only supported types
return Task.FromResult<IEnumerable<RemoteChapterResult>>(new List<RemoteChapterResult>());
}
var request = new ChapterSearchRequest
{
ContentType = mediaType,
IndexNumber = video.IndexNumber,
2014-06-12 02:39:52 +00:00
Language = video.GetPreferredMetadataLanguage(),
2014-06-09 19:16:14 +00:00
MediaPath = video.Path,
Name = video.Name,
ParentIndexNumber = video.ParentIndexNumber,
ProductionYear = video.ProductionYear,
ProviderIds = video.ProviderIds,
2014-06-12 02:38:40 +00:00
RuntimeTicks = video.RunTimeTicks,
SearchAllProviders = false
2014-06-09 19:16:14 +00:00
};
var episode = video as Episode;
if (episode != null)
{
request.IndexNumberEnd = episode.IndexNumberEnd;
request.SeriesName = episode.SeriesName;
}
return Search(request, cancellationToken);
}
public async Task<IEnumerable<RemoteChapterResult>> Search(ChapterSearchRequest request, CancellationToken cancellationToken)
{
var contentType = request.ContentType;
var providers = GetInternalProviders(false)
.Where(i => i.SupportedMediaTypes.Contains(contentType))
.ToList();
// If not searching all, search one at a time until something is found
if (!request.SearchAllProviders)
{
foreach (var provider in providers)
{
try
{
2014-06-12 02:38:40 +00:00
var currentResults = await Search(request, provider, cancellationToken).ConfigureAwait(false);
2014-06-09 19:16:14 +00:00
2014-06-12 02:38:40 +00:00
if (currentResults.Count > 0)
{
return currentResults;
}
2014-06-09 19:16:14 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles from {0}", ex, provider.Name);
}
}
return new List<RemoteChapterResult>();
}
var tasks = providers.Select(async i =>
{
try
{
return await Search(request, i, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles from {0}", ex, i.Name);
return new List<RemoteChapterResult>();
}
});
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
return results.SelectMany(i => i);
}
2014-06-12 02:38:40 +00:00
private async Task<List<RemoteChapterResult>> Search(ChapterSearchRequest request,
2014-06-09 19:16:14 +00:00
IChapterProvider provider,
CancellationToken cancellationToken)
{
var searchResults = await provider.Search(request, cancellationToken).ConfigureAwait(false);
2014-06-12 02:38:40 +00:00
var list = searchResults.ToList();
foreach (var result in list)
2014-06-09 19:16:14 +00:00
{
result.Id = GetProviderId(provider.Name) + "_" + result.Id;
result.ProviderName = provider.Name;
}
2014-06-12 02:38:40 +00:00
return list;
2014-06-09 19:16:14 +00:00
}
public Task<ChapterResponse> GetChapters(string id, CancellationToken cancellationToken)
{
var parts = id.Split(new[] { '_' }, 2);
var provider = GetProvider(parts.First());
id = parts.Last();
return provider.GetChapters(id, cancellationToken);
}
public IEnumerable<ChapterProviderInfo> GetProviders(string itemId)
{
var video = _libraryManager.GetItemById(itemId) as Video;
VideoContentType mediaType;
if (video is Episode)
{
mediaType = VideoContentType.Episode;
}
else if (video is Movie)
{
mediaType = VideoContentType.Movie;
}
else
{
// These are the only supported types
return new List<ChapterProviderInfo>();
}
var providers = GetInternalProviders(false)
.Where(i => i.SupportedMediaTypes.Contains(mediaType));
return GetInfos(providers);
}
public IEnumerable<ChapterProviderInfo> GetProviders()
{
return GetInfos(GetInternalProviders(true));
}
private IEnumerable<IChapterProvider> GetInternalProviders(bool includeDisabledProviders)
{
var providers = _providers;
if (!includeDisabledProviders)
{
2014-06-29 17:35:05 +00:00
var options = GetConfiguration();
2014-06-09 19:16:14 +00:00
providers = providers
2014-06-29 17:35:05 +00:00
.Where(i => !options.DisabledFetchers.Contains(i.Name))
2014-06-09 19:16:14 +00:00
.ToArray();
}
return providers
.OrderBy(GetConfiguredOrder)
.ThenBy(GetDefaultOrder)
.ToArray();
}
private IEnumerable<ChapterProviderInfo> GetInfos(IEnumerable<IChapterProvider> providers)
{
return providers.Select(i => new ChapterProviderInfo
{
Name = i.Name,
Id = GetProviderId(i.Name)
});
}
private string GetProviderId(string name)
{
return name.ToLower().GetMD5().ToString("N");
}
private IChapterProvider GetProvider(string id)
{
return _providers.First(i => string.Equals(id, GetProviderId(i.Name)));
}
private int GetConfiguredOrder(IChapterProvider provider)
{
2014-06-29 17:35:05 +00:00
var options = GetConfiguration();
2014-06-09 19:16:14 +00:00
// See if there's a user-defined order
2014-06-29 17:35:05 +00:00
var index = Array.IndexOf(options.FetcherOrder, provider.Name);
2014-06-09 19:16:14 +00:00
if (index != -1)
{
return index;
}
// Not configured. Just return some high number to put it at the end.
return 100;
}
private int GetDefaultOrder(IChapterProvider provider)
{
var hasOrder = provider as IHasOrder;
if (hasOrder != null)
{
return hasOrder.Order;
}
return 0;
}
2014-06-10 17:36:06 +00:00
public IEnumerable<ChapterInfo> GetChapters(string itemId)
{
return _itemRepo.GetChapters(new Guid(itemId));
}
public Task SaveChapters(string itemId, IEnumerable<ChapterInfo> chapters, CancellationToken cancellationToken)
{
return _itemRepo.SaveChapters(new Guid(itemId), chapters, cancellationToken);
}
2014-06-29 17:35:05 +00:00
public ChapterOptions GetConfiguration()
{
return _config.GetConfiguration<ChapterOptions>("chapters");
}
}
public class ChapterConfigurationStore : IConfigurationFactory
{
public IEnumerable<ConfigurationStore> GetConfigurations()
{
return new List<ConfigurationStore>
{
new ConfigurationStore
{
Key = "chapters",
ConfigurationType = typeof (ChapterOptions)
}
};
}
2014-06-09 19:16:14 +00:00
}
}