2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2019-01-13 12:26:31 -07:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 15:22:57 -07:00
|
|
|
using System.Globalization;
|
2019-01-13 12:26:31 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-05-06 19:28:19 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-05-16 21:24:10 -07:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
2014-05-11 15:38:10 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-05-06 19:28:19 -07:00
|
|
|
using MediaBrowser.Controller.Subtitles;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-05-06 19:28:19 -07:00
|
|
|
using MediaBrowser.Model.Entities;
|
2018-09-12 10:26:21 -07:00
|
|
|
using MediaBrowser.Model.Globalization;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Subtitles
|
|
|
|
{
|
|
|
|
public class SubtitleManager : ISubtitleManager
|
|
|
|
{
|
|
|
|
private ISubtitleProvider[] _subtitleProviders;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly ILibraryMonitor _monitor;
|
2015-02-07 14:03:09 -07:00
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
2014-08-10 15:13:17 -07:00
|
|
|
public event EventHandler<SubtitleDownloadFailureEventArgs> SubtitleDownloadFailure;
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
private ILocalizationManager _localization;
|
|
|
|
|
2019-01-17 15:55:05 -07:00
|
|
|
public SubtitleManager(
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
ILibraryMonitor monitor,
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
ILocalizationManager localizationManager)
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
2019-01-17 15:55:05 -07:00
|
|
|
_logger = loggerFactory.CreateLogger(nameof(SubtitleManager));
|
2014-05-06 19:28:19 -07:00
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_monitor = monitor;
|
2015-02-07 14:03:09 -07:00
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2018-09-12 10:26:21 -07:00
|
|
|
_localization = localizationManager;
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddParts(IEnumerable<ISubtitleProvider> subtitleProviders)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
_subtitleProviders = subtitleProviders
|
|
|
|
.OrderBy(i =>
|
|
|
|
{
|
|
|
|
var hasOrder = i as IHasOrder;
|
|
|
|
return hasOrder == null ? 0 : hasOrder.Order;
|
|
|
|
})
|
|
|
|
.ToArray();
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
public async Task<RemoteSubtitleInfo[]> SearchSubtitles(SubtitleSearchRequest request, CancellationToken cancellationToken)
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (request.Language != null)
|
|
|
|
{
|
|
|
|
var culture = _localization.FindLanguageInfo(request.Language);
|
|
|
|
|
|
|
|
if (culture != null)
|
|
|
|
{
|
|
|
|
request.TwoLetterISOLanguageName = culture.TwoLetterISOLanguageName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 21:24:10 -07:00
|
|
|
var contentType = request.ContentType;
|
2014-05-06 19:28:19 -07:00
|
|
|
var providers = _subtitleProviders
|
2014-05-16 21:24:10 -07:00
|
|
|
.Where(i => i.SupportedMediaTypes.Contains(contentType))
|
2018-09-12 10:26:21 -07:00
|
|
|
.Where(i => !request.DisabledSubtitleFetchers.Contains(i.Name, StringComparer.OrdinalIgnoreCase))
|
|
|
|
.OrderBy(i =>
|
|
|
|
{
|
|
|
|
var index = request.SubtitleFetcherOrder.ToList().IndexOf(i.Name);
|
|
|
|
return index == -1 ? int.MaxValue : index;
|
|
|
|
})
|
2017-08-19 12:43:35 -07:00
|
|
|
.ToArray();
|
2014-05-06 19:28:19 -07:00
|
|
|
|
2014-05-16 21:24:10 -07:00
|
|
|
// If not searching all, search one at a time until something is found
|
|
|
|
if (!request.SearchAllProviders)
|
|
|
|
{
|
|
|
|
foreach (var provider in providers)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var searchResults = await provider.Search(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
var list = searchResults.ToArray();
|
2014-05-16 21:24:10 -07:00
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
if (list.Length > 0)
|
2014-05-16 21:24:10 -07:00
|
|
|
{
|
|
|
|
Normalize(list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
_logger.LogError(ex, "Error downloading subtitles from {Provider}", provider.Name);
|
2014-05-16 21:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-19 12:43:35 -07:00
|
|
|
return new RemoteSubtitleInfo[] { };
|
2014-05-16 21:24:10 -07:00
|
|
|
}
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
var tasks = providers.Select(async i =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-05-16 21:24:10 -07:00
|
|
|
var searchResults = await i.Search(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
var list = searchResults.ToArray();
|
2014-05-16 21:24:10 -07:00
|
|
|
Normalize(list);
|
|
|
|
return list;
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
_logger.LogError(ex, "Error downloading subtitles from {0}", i.Name);
|
2017-08-19 12:43:35 -07:00
|
|
|
return new RemoteSubtitleInfo[] { };
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
return results.SelectMany(i => i).ToArray();
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public Task DownloadSubtitles(Video video, string subtitleId, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var libraryOptions = BaseItem.LibraryManager.GetLibraryOptions(video);
|
|
|
|
|
|
|
|
return DownloadSubtitles(video, libraryOptions, subtitleId, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
public async Task DownloadSubtitles(Video video,
|
2018-09-12 10:26:21 -07:00
|
|
|
LibraryOptions libraryOptions,
|
2014-05-06 19:28:19 -07:00
|
|
|
string subtitleId,
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2014-08-10 15:13:17 -07:00
|
|
|
var parts = subtitleId.Split(new[] { '_' }, 2);
|
|
|
|
var provider = GetProvider(parts.First());
|
2014-05-06 19:28:19 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var saveInMediaFolder = libraryOptions.SaveSubtitlesWithMedia;
|
2017-11-05 14:51:23 -07:00
|
|
|
|
2014-08-10 15:13:17 -07:00
|
|
|
try
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
2014-08-10 15:13:17 -07:00
|
|
|
var response = await GetRemoteSubtitles(subtitleId, cancellationToken).ConfigureAwait(false);
|
2014-05-16 21:24:10 -07:00
|
|
|
|
2014-08-10 15:13:17 -07:00
|
|
|
using (var stream = response.Stream)
|
2014-05-16 21:24:10 -07:00
|
|
|
{
|
2017-11-05 14:51:23 -07:00
|
|
|
using (var memoryStream = new MemoryStream())
|
2014-08-10 15:13:17 -07:00
|
|
|
{
|
2017-11-05 14:51:23 -07:00
|
|
|
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
memoryStream.Position = 0;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
var savePaths = new List<string>();
|
2019-01-30 08:57:15 -07:00
|
|
|
var saveFileName = Path.GetFileNameWithoutExtension(video.Path) + "." + response.Language.ToLowerInvariant();
|
2014-05-06 19:28:19 -07:00
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
if (response.IsForced)
|
|
|
|
{
|
|
|
|
saveFileName += ".forced";
|
|
|
|
}
|
2014-08-10 15:13:17 -07:00
|
|
|
|
2019-01-27 04:03:43 -07:00
|
|
|
saveFileName += "." + response.Format.ToLowerInvariant();
|
2015-11-14 12:56:56 -07:00
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
if (saveInMediaFolder)
|
2014-08-10 15:13:17 -07:00
|
|
|
{
|
2017-11-05 14:51:23 -07:00
|
|
|
savePaths.Add(Path.Combine(video.ContainingFolderPath, saveFileName));
|
2014-08-10 15:13:17 -07:00
|
|
|
}
|
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
savePaths.Add(Path.Combine(video.GetInternalMetadataPath(), saveFileName));
|
2014-08-10 15:13:17 -07:00
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
await TrySaveToFiles(memoryStream, savePaths).ConfigureAwait(false);
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-10 15:13:17 -07:00
|
|
|
}
|
2017-06-22 12:14:58 -07:00
|
|
|
catch (RateLimitExceededException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2014-08-10 15:13:17 -07:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-28 07:21:02 -07:00
|
|
|
SubtitleDownloadFailure?.Invoke(this, new SubtitleDownloadFailureEventArgs
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
2014-08-10 15:13:17 -07:00
|
|
|
Item = video,
|
|
|
|
Exception = ex,
|
|
|
|
Provider = provider.Name
|
2018-12-28 07:21:02 -07:00
|
|
|
});
|
2017-05-27 00:19:09 -07:00
|
|
|
|
2014-08-10 15:13:17 -07:00
|
|
|
throw;
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
private async Task TrySaveToFiles(Stream stream, List<string> savePaths)
|
|
|
|
{
|
|
|
|
Exception exceptionToThrow = null;
|
|
|
|
|
|
|
|
foreach (var savePath in savePaths)
|
|
|
|
{
|
2018-12-13 06:18:25 -07:00
|
|
|
_logger.LogInformation("Saving subtitles to {0}", savePath);
|
2017-11-05 14:51:23 -07:00
|
|
|
|
|
|
|
_monitor.ReportFileSystemChangeBeginning(savePath);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-01-26 14:08:04 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
using (var fs = _fileSystem.GetFileStream(savePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
|
|
|
|
{
|
|
|
|
await stream.CopyToAsync(fs).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
if (exceptionToThrow == null)
|
|
|
|
{
|
|
|
|
exceptionToThrow = ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_monitor.ReportFileSystemChangeComplete(savePath, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.Position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exceptionToThrow != null)
|
|
|
|
{
|
|
|
|
throw exceptionToThrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
public Task<RemoteSubtitleInfo[]> SearchSubtitles(Video video, string language, bool? isPerfectMatch, CancellationToken cancellationToken)
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (video.VideoType != VideoType.VideoFile)
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
2019-01-13 13:37:13 -07:00
|
|
|
return Task.FromResult(new RemoteSubtitleInfo[] { });
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
2014-05-11 15:38:10 -07:00
|
|
|
VideoContentType mediaType;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
|
|
if (video is Episode)
|
|
|
|
{
|
2014-05-11 15:38:10 -07:00
|
|
|
mediaType = VideoContentType.Episode;
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
else if (video is Movie)
|
|
|
|
{
|
2014-05-11 15:38:10 -07:00
|
|
|
mediaType = VideoContentType.Movie;
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// These are the only supported types
|
2019-01-13 13:37:13 -07:00
|
|
|
return Task.FromResult(new RemoteSubtitleInfo[] { });
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var request = new SubtitleSearchRequest
|
|
|
|
{
|
|
|
|
ContentType = mediaType,
|
|
|
|
IndexNumber = video.IndexNumber,
|
|
|
|
Language = language,
|
|
|
|
MediaPath = video.Path,
|
|
|
|
Name = video.Name,
|
|
|
|
ParentIndexNumber = video.ParentIndexNumber,
|
|
|
|
ProductionYear = video.ProductionYear,
|
2014-05-11 15:38:10 -07:00
|
|
|
ProviderIds = video.ProviderIds,
|
2017-06-30 12:58:53 -07:00
|
|
|
RuntimeTicks = video.RunTimeTicks,
|
|
|
|
IsPerfectMatch = isPerfectMatch ?? false
|
2014-05-06 19:28:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
var episode = video as Episode;
|
|
|
|
|
|
|
|
if (episode != null)
|
|
|
|
{
|
|
|
|
request.IndexNumberEnd = episode.IndexNumberEnd;
|
|
|
|
request.SeriesName = episode.SeriesName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SearchSubtitles(request, cancellationToken);
|
|
|
|
}
|
2014-05-16 21:24:10 -07:00
|
|
|
|
|
|
|
private void Normalize(IEnumerable<RemoteSubtitleInfo> subtitles)
|
|
|
|
{
|
|
|
|
foreach (var sub in subtitles)
|
|
|
|
{
|
|
|
|
sub.Id = GetProviderId(sub.ProviderName) + "_" + sub.Id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetProviderId(string name)
|
|
|
|
{
|
2019-02-28 15:22:57 -07:00
|
|
|
return name.ToLowerInvariant().GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
2014-05-16 21:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private ISubtitleProvider GetProvider(string id)
|
|
|
|
{
|
|
|
|
return _subtitleProviders.First(i => string.Equals(id, GetProviderId(i.Name)));
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public Task DeleteSubtitles(BaseItem item, int index)
|
2014-05-16 21:24:10 -07:00
|
|
|
{
|
2015-02-07 14:03:09 -07:00
|
|
|
var stream = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery
|
2014-05-16 21:24:10 -07:00
|
|
|
{
|
|
|
|
Index = index,
|
2018-09-12 10:26:21 -07:00
|
|
|
ItemId = item.Id,
|
2014-05-16 21:24:10 -07:00
|
|
|
Type = MediaStreamType.Subtitle
|
|
|
|
|
|
|
|
}).First();
|
|
|
|
|
|
|
|
var path = stream.Path;
|
|
|
|
_monitor.ReportFileSystemChangeBeginning(path);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-01-12 20:46:44 -07:00
|
|
|
_fileSystem.DeleteFile(path);
|
2014-05-16 21:24:10 -07:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_monitor.ReportFileSystemChangeComplete(path, false);
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
return item.RefreshMetadata(CancellationToken.None);
|
2014-05-16 21:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<SubtitleResponse> GetRemoteSubtitles(string id, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var parts = id.Split(new[] { '_' }, 2);
|
|
|
|
|
|
|
|
var provider = GetProvider(parts.First());
|
|
|
|
id = parts.Last();
|
|
|
|
|
|
|
|
return provider.GetSubtitles(id, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public SubtitleProviderInfo[] GetSupportedProviders(BaseItem video)
|
2014-05-16 21:24:10 -07:00
|
|
|
{
|
|
|
|
VideoContentType mediaType;
|
|
|
|
|
|
|
|
if (video is Episode)
|
|
|
|
{
|
|
|
|
mediaType = VideoContentType.Episode;
|
|
|
|
}
|
|
|
|
else if (video is Movie)
|
|
|
|
{
|
|
|
|
mediaType = VideoContentType.Movie;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// These are the only supported types
|
2017-08-19 12:43:35 -07:00
|
|
|
return new SubtitleProviderInfo[] { };
|
2014-05-16 21:24:10 -07:00
|
|
|
}
|
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
return _subtitleProviders
|
|
|
|
.Where(i => i.SupportedMediaTypes.Contains(mediaType))
|
|
|
|
.Select(i => new SubtitleProviderInfo
|
|
|
|
{
|
|
|
|
Name = i.Name,
|
|
|
|
Id = GetProviderId(i.Name)
|
2014-05-16 21:24:10 -07:00
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
}).ToArray();
|
2014-05-16 21:24:10 -07:00
|
|
|
}
|
2014-08-10 15:13:17 -07:00
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
}
|