jellyfin/MediaBrowser.Providers/MediaInfo/SubtitleScheduledTask.cs

139 lines
4.6 KiB
C#
Raw Normal View History

2014-08-10 15:13:17 -07:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.ScheduledTasks;
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 09:24:16 -07:00
using MediaBrowser.Model.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.MediaInfo
{
public class SubtitleScheduledTask : IScheduledTask
{
private readonly ILibraryManager _libraryManager;
private readonly IServerConfigurationManager _config;
private readonly ISubtitleManager _subtitleManager;
2015-03-31 09:24:16 -07:00
private readonly IMediaSourceManager _mediaSourceManager;
private readonly ILogger _logger;
2015-03-31 09:24:16 -07:00
public SubtitleScheduledTask(ILibraryManager libraryManager, IServerConfigurationManager config, ISubtitleManager subtitleManager, ILogger logger, IMediaSourceManager mediaSourceManager)
{
_libraryManager = libraryManager;
_config = config;
_subtitleManager = subtitleManager;
_logger = logger;
2015-03-31 09:24:16 -07:00
_mediaSourceManager = mediaSourceManager;
}
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 15:13:17 -07:00
private SubtitleOptions GetOptions()
{
return _config.GetConfiguration<SubtitleOptions>("subtitles");
}
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
{
2014-08-10 15:13:17 -07:00
var options = GetOptions();
var videos = _libraryManager.RootFolder
2015-01-24 23:34:50 -07:00
.GetRecursiveChildren(i =>
{
2015-01-24 23:34:50 -07:00
if (!(i is Video))
{
return false;
}
if (i.LocationType == LocationType.Remote || i.LocationType == LocationType.Virtual)
{
return false;
}
2014-08-10 15:13:17 -07:00
return (options.DownloadEpisodeSubtitles &&
i is Episode) ||
2014-08-10 15:13:17 -07:00
(options.DownloadMovieSubtitles &&
i is Movie);
})
2015-01-24 23:34:50 -07:00
.Cast<Video>()
.ToList();
var numComplete = 0;
foreach (var video in videos)
{
try
{
2014-08-10 15:13:17 -07:00
await DownloadSubtitles(video, options, cancellationToken).ConfigureAwait(false);
}
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 15:13:17 -07:00
private async Task DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
{
2014-08-10 15:13:17 -07:00
if ((options.DownloadEpisodeSubtitles &&
video is Episode) ||
2014-08-10 15:13:17 -07:00
(options.DownloadMovieSubtitles &&
video is Movie))
{
2015-03-31 09:24:16 -07:00
var mediaStreams = _mediaSourceManager.GetStaticMediaSources(video, false).First().MediaStreams;
var downloadedLanguages = await new SubtitleDownloader(_logger,
_subtitleManager)
.DownloadSubtitles(video,
mediaStreams,
2016-03-19 15:31:00 -07:00
options.SkipIfEmbeddedSubtitlesPresent,
2014-08-10 15:13:17 -07:00
options.SkipIfAudioTrackMatches,
options.DownloadLanguages,
cancellationToken).ConfigureAwait(false);
// Rescan
if (downloadedLanguages.Count > 0)
{
await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);
}
}
}
public IEnumerable<ITaskTrigger> GetDefaultTriggers()
{
return new ITaskTrigger[]
{
2015-08-26 18:31:54 -07:00
new IntervalTrigger{ Interval = TimeSpan.FromHours(8)}
};
}
}
}