jellyfin-server/MediaBrowser.Providers/Subtitles/SubtitleManager.cs

309 lines
10 KiB
C#
Raw Normal View History

2014-08-10 22:13:17 +00:00
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Extensions;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-05-17 04:24:10 +00:00
using MediaBrowser.Controller.Persistence;
2014-05-11 22:38:10 +00:00
using MediaBrowser.Controller.Providers;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Providers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2017-05-26 06:48:54 +00:00
2016-10-25 19:02:04 +00:00
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2014-05-07 02:28:19 +00:00
namespace MediaBrowser.Providers.Subtitles
{
public class SubtitleManager : ISubtitleManager
{
private ISubtitleProvider[] _subtitleProviders;
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly ILibraryMonitor _monitor;
2014-05-17 04:24:10 +00:00
private readonly ILibraryManager _libraryManager;
private readonly IMediaSourceManager _mediaSourceManager;
2014-05-07 02:28:19 +00:00
2014-08-10 22:13:17 +00:00
public event EventHandler<SubtitleDownloadEventArgs> SubtitlesDownloaded;
public event EventHandler<SubtitleDownloadFailureEventArgs> SubtitleDownloadFailure;
public SubtitleManager(ILogger logger, IFileSystem fileSystem, ILibraryMonitor monitor, ILibraryManager libraryManager, IMediaSourceManager mediaSourceManager)
2014-05-07 02:28:19 +00:00
{
_logger = logger;
_fileSystem = fileSystem;
_monitor = monitor;
2014-05-17 04:24:10 +00:00
_libraryManager = libraryManager;
_mediaSourceManager = mediaSourceManager;
2014-05-07 02:28:19 +00:00
}
public void AddParts(IEnumerable<ISubtitleProvider> subtitleProviders)
{
_subtitleProviders = subtitleProviders.ToArray();
}
2017-08-19 19:43:35 +00:00
public async Task<RemoteSubtitleInfo[]> SearchSubtitles(SubtitleSearchRequest request, CancellationToken cancellationToken)
2014-05-07 02:28:19 +00:00
{
2014-05-17 04:24:10 +00:00
var contentType = request.ContentType;
2014-05-07 02:28:19 +00:00
var providers = _subtitleProviders
2014-05-17 04:24:10 +00:00
.Where(i => i.SupportedMediaTypes.Contains(contentType))
2017-08-19 19:43:35 +00:00
.ToArray();
2014-05-07 02:28:19 +00:00
2014-05-17 04:24:10 +00: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 19:43:35 +00:00
var list = searchResults.ToArray();
2014-05-17 04:24:10 +00:00
2017-08-19 19:43:35 +00:00
if (list.Length > 0)
2014-05-17 04:24:10 +00:00
{
Normalize(list);
return list;
}
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles from {0}", ex, provider.Name);
}
}
2017-08-19 19:43:35 +00:00
return new RemoteSubtitleInfo[] { };
2014-05-17 04:24:10 +00:00
}
2014-05-07 02:28:19 +00:00
var tasks = providers.Select(async i =>
{
try
{
2014-05-17 04:24:10 +00:00
var searchResults = await i.Search(request, cancellationToken).ConfigureAwait(false);
2017-08-19 19:43:35 +00:00
var list = searchResults.ToArray();
2014-05-17 04:24:10 +00:00
Normalize(list);
return list;
2014-05-07 02:28:19 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles from {0}", ex, i.Name);
2017-08-19 19:43:35 +00:00
return new RemoteSubtitleInfo[] { };
2014-05-07 02:28:19 +00:00
}
});
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
2017-08-19 19:43:35 +00:00
return results.SelectMany(i => i).ToArray();
2014-05-07 02:28:19 +00:00
}
public async Task DownloadSubtitles(Video video,
string subtitleId,
CancellationToken cancellationToken)
{
2014-08-10 22:13:17 +00:00
var parts = subtitleId.Split(new[] { '_' }, 2);
var provider = GetProvider(parts.First());
2014-05-07 02:28:19 +00:00
2014-08-10 22:13:17 +00:00
try
2014-05-07 02:28:19 +00:00
{
2014-08-10 22:13:17 +00:00
var response = await GetRemoteSubtitles(subtitleId, cancellationToken).ConfigureAwait(false);
2014-05-17 04:24:10 +00:00
2014-08-10 22:13:17 +00:00
using (var stream = response.Stream)
2014-05-17 04:24:10 +00:00
{
2017-05-04 18:14:45 +00:00
var savePath = Path.Combine(_fileSystem.GetDirectoryName(video.Path),
2014-08-10 22:13:17 +00:00
_fileSystem.GetFileNameWithoutExtension(video.Path) + "." + response.Language.ToLower());
2014-05-17 04:24:10 +00:00
2014-08-10 22:13:17 +00:00
if (response.IsForced)
{
savePath += ".forced";
}
2014-05-07 02:28:19 +00:00
2014-08-10 22:13:17 +00:00
savePath += "." + response.Format.ToLower();
2014-05-07 02:28:19 +00:00
2014-08-10 22:13:17 +00:00
_logger.Info("Saving subtitles to {0}", savePath);
2014-05-07 02:28:19 +00:00
2014-08-10 22:13:17 +00:00
_monitor.ReportFileSystemChangeBeginning(savePath);
try
{
2015-11-14 19:56:56 +00:00
//var isText = MediaStream.IsTextFormat(response.Format);
using (var fs = _fileSystem.GetFileStream(savePath, FileOpenMode.Create, FileAccessMode.Write,
FileShareMode.Read, true))
2014-08-10 22:13:17 +00:00
{
await stream.CopyToAsync(fs).ConfigureAwait(false);
}
EventHelper.FireEventIfNotNull(SubtitlesDownloaded, this, new SubtitleDownloadEventArgs
{
Item = video,
Format = response.Format,
Language = response.Language,
IsForced = response.IsForced,
Provider = provider.Name
}, _logger);
}
finally
2014-05-07 02:28:19 +00:00
{
2014-08-10 22:13:17 +00:00
_monitor.ReportFileSystemChangeComplete(savePath, false);
2014-05-07 02:28:19 +00:00
}
}
2014-08-10 22:13:17 +00:00
}
catch (RateLimitExceededException)
{
throw;
}
2014-08-10 22:13:17 +00:00
catch (Exception ex)
{
EventHelper.FireEventIfNotNull(SubtitleDownloadFailure, this, new SubtitleDownloadFailureEventArgs
2014-05-07 02:28:19 +00:00
{
2014-08-10 22:13:17 +00:00
Item = video,
Exception = ex,
Provider = provider.Name
}, _logger);
2017-05-27 07:19:09 +00:00
2014-08-10 22:13:17 +00:00
throw;
2014-05-07 02:28:19 +00:00
}
}
2017-08-19 19:43:35 +00:00
public Task<RemoteSubtitleInfo[]> SearchSubtitles(Video video, string language, bool? isPerfectMatch, CancellationToken cancellationToken)
2014-05-07 02:28:19 +00:00
{
if (video.LocationType != LocationType.FileSystem ||
video.VideoType != VideoType.VideoFile)
{
2017-08-19 19:43:35 +00:00
return Task.FromResult<RemoteSubtitleInfo[]>(new RemoteSubtitleInfo[] { });
2014-05-07 02:28:19 +00:00
}
2014-05-11 22:38:10 +00:00
VideoContentType mediaType;
2014-05-07 02:28:19 +00:00
if (video is Episode)
{
2014-05-11 22:38:10 +00:00
mediaType = VideoContentType.Episode;
2014-05-07 02:28:19 +00:00
}
else if (video is Movie)
{
2014-05-11 22:38:10 +00:00
mediaType = VideoContentType.Movie;
2014-05-07 02:28:19 +00:00
}
else
{
// These are the only supported types
2017-08-19 19:43:35 +00:00
return Task.FromResult<RemoteSubtitleInfo[]>(new RemoteSubtitleInfo[] { });
2014-05-07 02:28:19 +00: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 22:38:10 +00:00
ProviderIds = video.ProviderIds,
RuntimeTicks = video.RunTimeTicks,
IsPerfectMatch = isPerfectMatch ?? false
2014-05-07 02:28:19 +00:00
};
var episode = video as Episode;
if (episode != null)
{
request.IndexNumberEnd = episode.IndexNumberEnd;
request.SeriesName = episode.SeriesName;
}
return SearchSubtitles(request, cancellationToken);
}
2014-05-17 04:24:10 +00:00
private void Normalize(IEnumerable<RemoteSubtitleInfo> subtitles)
{
foreach (var sub in subtitles)
{
sub.Id = GetProviderId(sub.ProviderName) + "_" + sub.Id;
}
}
private string GetProviderId(string name)
{
return name.ToLower().GetMD5().ToString("N");
}
private ISubtitleProvider GetProvider(string id)
{
return _subtitleProviders.First(i => string.Equals(id, GetProviderId(i.Name)));
}
public Task DeleteSubtitles(string itemId, int index)
{
var stream = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery
2014-05-17 04:24:10 +00:00
{
Index = index,
ItemId = new Guid(itemId),
Type = MediaStreamType.Subtitle
}).First();
var path = stream.Path;
_monitor.ReportFileSystemChangeBeginning(path);
try
{
_fileSystem.DeleteFile(path);
2014-05-17 04:24:10 +00:00
}
finally
{
_monitor.ReportFileSystemChangeComplete(path, false);
}
return _libraryManager.GetItemById(itemId).RefreshMetadata(CancellationToken.None);
2014-05-17 04:24:10 +00: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);
}
2017-08-19 19:43:35 +00:00
public SubtitleProviderInfo[] GetProviders(string itemId)
2014-05-17 04:24:10 +00:00
{
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
2017-08-19 19:43:35 +00:00
return new SubtitleProviderInfo[] { };
2014-05-17 04:24:10 +00:00
}
2017-08-19 19:43:35 +00:00
return _subtitleProviders
.Where(i => i.SupportedMediaTypes.Contains(mediaType))
.Select(i => new SubtitleProviderInfo
{
Name = i.Name,
Id = GetProviderId(i.Name)
2014-05-17 04:24:10 +00:00
2017-08-19 19:43:35 +00:00
}).ToArray();
2014-05-17 04:24:10 +00:00
}
2014-08-10 22:13:17 +00:00
2014-05-07 02:28:19 +00:00
}
}