2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2019-01-13 12:26:31 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-05-06 19:28:19 -07:00
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-05-11 15:38:10 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-05-06 19:28:19 -07:00
|
|
|
using MediaBrowser.Controller.Subtitles;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2018-12-13 06:18:25 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
|
|
|
public class SubtitleDownloader
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly ISubtitleManager _subtitleManager;
|
|
|
|
|
|
|
|
public SubtitleDownloader(ILogger logger, ISubtitleManager subtitleManager)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_subtitleManager = subtitleManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<List<string>> DownloadSubtitles(Video video,
|
2014-09-05 21:21:23 -07:00
|
|
|
List<MediaStream> mediaStreams,
|
2016-03-19 15:31:00 -07:00
|
|
|
bool skipIfEmbeddedSubtitlesPresent,
|
2014-05-07 19:16:11 -07:00
|
|
|
bool skipIfAudioTrackMatches,
|
2016-04-06 21:09:32 -07:00
|
|
|
bool requirePerfectMatch,
|
2014-05-06 19:28:19 -07:00
|
|
|
IEnumerable<string> languages,
|
2018-09-12 10:26:21 -07:00
|
|
|
string[] disabledSubtitleFetchers,
|
|
|
|
string[] subtitleFetcherOrder,
|
2014-05-06 19:28:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
2017-09-09 20:18:23 -07:00
|
|
|
{
|
|
|
|
var downloadedLanguages = new List<string>();
|
|
|
|
|
|
|
|
foreach (var lang in languages)
|
|
|
|
{
|
|
|
|
var downloaded = await DownloadSubtitles(video, mediaStreams, skipIfEmbeddedSubtitlesPresent,
|
2018-09-12 10:26:21 -07:00
|
|
|
skipIfAudioTrackMatches, requirePerfectMatch, lang, disabledSubtitleFetchers, subtitleFetcherOrder, cancellationToken).ConfigureAwait(false);
|
2017-09-09 20:18:23 -07:00
|
|
|
|
|
|
|
if (downloaded)
|
|
|
|
{
|
|
|
|
downloadedLanguages.Add(lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return downloadedLanguages;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task<bool> DownloadSubtitles(Video video,
|
|
|
|
List<MediaStream> mediaStreams,
|
|
|
|
bool skipIfEmbeddedSubtitlesPresent,
|
|
|
|
bool skipIfAudioTrackMatches,
|
|
|
|
bool requirePerfectMatch,
|
|
|
|
string lang,
|
2018-09-12 10:26:21 -07:00
|
|
|
string[] disabledSubtitleFetchers,
|
|
|
|
string[] subtitleFetcherOrder,
|
2017-09-09 20:18:23 -07:00
|
|
|
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
|
|
|
{
|
2017-09-09 20:18:23 -07:00
|
|
|
return Task.FromResult(false);
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
2017-08-26 12:50:02 -07:00
|
|
|
if (!video.IsCompleteMedia)
|
|
|
|
{
|
2017-09-09 20:18:23 -07:00
|
|
|
return Task.FromResult(false);
|
2017-08-26 12:50:02 -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
|
2017-09-09 20:18:23 -07:00
|
|
|
return Task.FromResult(false);
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
2017-09-09 20:18:23 -07:00
|
|
|
return DownloadSubtitles(video, mediaStreams, skipIfEmbeddedSubtitlesPresent, skipIfAudioTrackMatches,
|
2018-09-12 10:26:21 -07:00
|
|
|
requirePerfectMatch, lang, disabledSubtitleFetchers, subtitleFetcherOrder, mediaType, cancellationToken);
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<bool> DownloadSubtitles(Video video,
|
2014-09-05 21:21:23 -07:00
|
|
|
List<MediaStream> mediaStreams,
|
2016-03-19 15:31:00 -07:00
|
|
|
bool skipIfEmbeddedSubtitlesPresent,
|
2014-05-07 19:16:11 -07:00
|
|
|
bool skipIfAudioTrackMatches,
|
2016-04-06 21:09:32 -07:00
|
|
|
bool requirePerfectMatch,
|
2014-05-06 19:28:19 -07:00
|
|
|
string language,
|
2018-09-12 10:26:21 -07:00
|
|
|
string[] disabledSubtitleFetchers,
|
|
|
|
string[] subtitleFetcherOrder,
|
2014-05-11 15:38:10 -07:00
|
|
|
VideoContentType mediaType,
|
2014-05-06 19:28:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// There's already subtitles for this language
|
2014-09-05 21:21:23 -07:00
|
|
|
if (mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle && i.IsTextSubtitleStream && string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-05 21:21:23 -07:00
|
|
|
var audioStreams = mediaStreams.Where(i => i.Type == MediaStreamType.Audio).ToList();
|
2014-05-08 13:09:53 -07:00
|
|
|
var defaultAudioStreams = audioStreams.Where(i => i.IsDefault).ToList();
|
|
|
|
|
|
|
|
// If none are marked as default, just take a guess
|
|
|
|
if (defaultAudioStreams.Count == 0)
|
|
|
|
{
|
|
|
|
defaultAudioStreams = audioStreams.Take(1).ToList();
|
|
|
|
}
|
|
|
|
|
2014-05-07 22:04:39 -07:00
|
|
|
// There's already a default audio stream for this language
|
2014-05-07 19:16:11 -07:00
|
|
|
if (skipIfAudioTrackMatches &&
|
2014-05-08 13:09:53 -07:00
|
|
|
defaultAudioStreams.Any(i => string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
|
2014-05-07 13:28:28 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 13:24:34 -07:00
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
// There's an internal subtitle stream for this language
|
2016-03-19 15:31:00 -07:00
|
|
|
if (skipIfEmbeddedSubtitlesPresent &&
|
|
|
|
mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal && string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
|
2014-05-06 19:28:19 -07:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-16 21:24:10 -07:00
|
|
|
ProviderIds = video.ProviderIds,
|
|
|
|
|
|
|
|
// Stop as soon as we find something
|
2016-04-06 21:09:32 -07:00
|
|
|
SearchAllProviders = false,
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
IsPerfectMatch = requirePerfectMatch,
|
|
|
|
DisabledSubtitleFetchers = disabledSubtitleFetchers,
|
|
|
|
SubtitleFetcherOrder = subtitleFetcherOrder
|
2014-05-06 19:28:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
var episode = video as Episode;
|
|
|
|
|
|
|
|
if (episode != null)
|
|
|
|
{
|
|
|
|
request.IndexNumberEnd = episode.IndexNumberEnd;
|
|
|
|
request.SeriesName = episode.SeriesName;
|
|
|
|
}
|
2017-06-22 12:14:58 -07:00
|
|
|
|
2014-05-06 19:28:19 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var searchResults = await _subtitleManager.SearchSubtitles(request, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var result = searchResults.FirstOrDefault();
|
|
|
|
|
|
|
|
if (result != null)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
await _subtitleManager.DownloadSubtitles(video, result.Id, cancellationToken).ConfigureAwait(false);
|
2014-05-06 19:28:19 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-06-22 12:14:58 -07:00
|
|
|
catch (RateLimitExceededException)
|
|
|
|
{
|
|
|
|
}
|
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");
|
2014-05-06 19:28:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|