2014-08-10 22:13:17 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Subtitles;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2015-03-31 16:24:16 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-05 07:15:29 +00:00
|
|
|
|
using System.IO;
|
2017-05-21 07:25:49 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2016-10-05 07:15:29 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2016-10-23 19:14:57 +00:00
|
|
|
|
using MediaBrowser.Model.Tasks;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
|
{
|
|
|
|
|
public class SubtitleScheduledTask : IScheduledTask
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly ISubtitleManager _subtitleManager;
|
2015-03-31 16:24:16 +00:00
|
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2016-10-22 14:50:45 +00:00
|
|
|
|
private readonly IJsonSerializer _json;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
|
2016-10-05 07:15:29 +00:00
|
|
|
|
public SubtitleScheduledTask(ILibraryManager libraryManager, IJsonSerializer json, IServerConfigurationManager config, ISubtitleManager subtitleManager, ILogger logger, IMediaSourceManager mediaSourceManager)
|
2014-07-05 04:01:30 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_config = config;
|
|
|
|
|
_subtitleManager = subtitleManager;
|
|
|
|
|
_logger = logger;
|
2015-03-31 16:24:16 +00:00
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2016-10-05 07:15:29 +00:00
|
|
|
|
_json = json;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Download missing subtitles"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Description
|
|
|
|
|
{
|
|
|
|
|
get { return "Searches the internet for missing subtitles based on metadata configuration."; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Category
|
|
|
|
|
{
|
|
|
|
|
get { return "Library"; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 22:13:17 +00:00
|
|
|
|
private SubtitleOptions GetOptions()
|
|
|
|
|
{
|
|
|
|
|
return _config.GetConfiguration<SubtitleOptions>("subtitles");
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-05 04:01:30 +00:00
|
|
|
|
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
|
|
|
|
|
{
|
2014-08-10 22:13:17 +00:00
|
|
|
|
var options = GetOptions();
|
|
|
|
|
|
2016-10-05 07:15:29 +00:00
|
|
|
|
var types = new List<string>();
|
2015-01-25 06:34:50 +00:00
|
|
|
|
|
2016-10-05 07:15:29 +00:00
|
|
|
|
if (options.DownloadEpisodeSubtitles)
|
|
|
|
|
{
|
|
|
|
|
types.Add("Episode");
|
|
|
|
|
}
|
|
|
|
|
if (options.DownloadMovieSubtitles)
|
|
|
|
|
{
|
|
|
|
|
types.Add("Movie");
|
|
|
|
|
}
|
2014-07-05 04:01:30 +00:00
|
|
|
|
|
2016-10-05 07:15:29 +00:00
|
|
|
|
if (types.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var videos = _libraryManager.GetItemList(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
MediaTypes = new string[] { MediaType.Video },
|
|
|
|
|
IsVirtualItem = false,
|
2017-05-21 07:25:49 +00:00
|
|
|
|
IncludeItemTypes = types.ToArray(),
|
|
|
|
|
DtoOptions = new DtoOptions(true)
|
2016-10-05 07:15:29 +00:00
|
|
|
|
|
|
|
|
|
}).OfType<Video>()
|
2017-01-09 17:05:34 +00:00
|
|
|
|
.Where(i => i.LocationType != LocationType.Remote)
|
2014-07-05 04:01:30 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
|
if (videos.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-05 04:01:30 +00:00
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var video in videos)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-10-29 04:10:11 +00:00
|
|
|
|
await DownloadSubtitles(video, options, cancellationToken).ConfigureAwait(false);
|
2014-07-05 04:01:30 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error downloading subtitles for {0}", ex, video.Path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update progress
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= videos.Count;
|
|
|
|
|
|
|
|
|
|
progress.Report(100 * percent);
|
|
|
|
|
}
|
2016-10-05 07:15:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<bool> DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
|
2014-07-05 04:01:30 +00:00
|
|
|
|
{
|
2014-08-10 22:13:17 +00:00
|
|
|
|
if ((options.DownloadEpisodeSubtitles &&
|
2014-07-05 04:01:30 +00:00
|
|
|
|
video is Episode) ||
|
2014-08-10 22:13:17 +00:00
|
|
|
|
(options.DownloadMovieSubtitles &&
|
2014-07-05 04:01:30 +00:00
|
|
|
|
video is Movie))
|
|
|
|
|
{
|
2015-03-31 16:24:16 +00:00
|
|
|
|
var mediaStreams = _mediaSourceManager.GetStaticMediaSources(video, false).First().MediaStreams;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
|
|
|
|
|
var downloadedLanguages = await new SubtitleDownloader(_logger,
|
|
|
|
|
_subtitleManager)
|
|
|
|
|
.DownloadSubtitles(video,
|
2014-09-06 04:21:23 +00:00
|
|
|
|
mediaStreams,
|
2016-03-19 22:31:00 +00:00
|
|
|
|
options.SkipIfEmbeddedSubtitlesPresent,
|
2014-08-10 22:13:17 +00:00
|
|
|
|
options.SkipIfAudioTrackMatches,
|
2016-04-07 04:09:32 +00:00
|
|
|
|
options.RequirePerfectMatch,
|
2014-08-10 22:13:17 +00:00
|
|
|
|
options.DownloadLanguages,
|
2014-07-05 04:01:30 +00:00
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Rescan
|
|
|
|
|
if (downloadedLanguages.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);
|
2016-10-05 07:15:29 +00:00
|
|
|
|
return false;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
}
|
2016-10-05 07:15:29 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
}
|
2016-10-05 07:15:29 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-23 19:14:57 +00:00
|
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
2014-07-05 04:01:30 +00:00
|
|
|
|
{
|
2016-10-23 19:14:57 +00:00
|
|
|
|
return new[] {
|
|
|
|
|
|
|
|
|
|
// Every so often
|
|
|
|
|
new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Key
|
|
|
|
|
{
|
|
|
|
|
get { return "DownloadSubtitles"; }
|
2014-07-05 04:01:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|