2014-08-10 22:13:17 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
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;
|
2014-08-10 22:13:17 +00:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2014-07-05 04:01:30 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2014-08-10 22:13:17 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
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;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public SubtitleScheduledTask(ILibraryManager libraryManager, IServerConfigurationManager config, ISubtitleManager subtitleManager, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_config = config;
|
|
|
|
|
_subtitleManager = subtitleManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2014-07-05 04:01:30 +00:00
|
|
|
|
var videos = _libraryManager.RootFolder
|
2015-01-25 06:34:50 +00:00
|
|
|
|
.GetRecursiveChildren(i =>
|
2014-07-05 04:01:30 +00:00
|
|
|
|
{
|
2015-01-25 06:34:50 +00:00
|
|
|
|
if (!(i is Video))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-05 04:01:30 +00:00
|
|
|
|
if (i.LocationType == LocationType.Remote || i.LocationType == LocationType.Virtual)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 22:13:17 +00:00
|
|
|
|
return (options.DownloadEpisodeSubtitles &&
|
2014-07-05 04:01:30 +00:00
|
|
|
|
i is Episode) ||
|
2014-08-10 22:13:17 +00:00
|
|
|
|
(options.DownloadMovieSubtitles &&
|
2014-07-05 04:01:30 +00:00
|
|
|
|
i is Movie);
|
|
|
|
|
})
|
2015-01-25 06:34:50 +00:00
|
|
|
|
.Cast<Video>()
|
2014-07-05 04:01:30 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var video in videos)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-08-10 22:13:17 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 22:13:17 +00:00
|
|
|
|
private async Task 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))
|
|
|
|
|
{
|
|
|
|
|
var mediaStreams = video.GetMediaSources(false).First().MediaStreams;
|
|
|
|
|
|
|
|
|
|
var downloadedLanguages = await new SubtitleDownloader(_logger,
|
|
|
|
|
_subtitleManager)
|
|
|
|
|
.DownloadSubtitles(video,
|
2014-09-06 04:21:23 +00:00
|
|
|
|
mediaStreams,
|
2014-08-10 22:13:17 +00:00
|
|
|
|
options.SkipIfGraphicalSubtitlesPresent,
|
|
|
|
|
options.SkipIfAudioTrackMatches,
|
|
|
|
|
options.DownloadLanguages,
|
2014-07-05 04:01:30 +00:00
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Rescan
|
|
|
|
|
if (downloadedLanguages.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ITaskTrigger> GetDefaultTriggers()
|
|
|
|
|
{
|
|
|
|
|
return new ITaskTrigger[]
|
|
|
|
|
{
|
|
|
|
|
new DailyTrigger { TimeOfDay = TimeSpan.FromHours(3) },
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|