SubtitleScheduledTask.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Entities.TV;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Subtitles;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.Providers;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.IO;
  17. using MediaBrowser.Controller.Dto;
  18. using MediaBrowser.Model.Serialization;
  19. using MediaBrowser.Model.Tasks;
  20. using MediaBrowser.Model.Extensions;
  21. namespace MediaBrowser.Providers.MediaInfo
  22. {
  23. public class SubtitleScheduledTask : IScheduledTask
  24. {
  25. private readonly ILibraryManager _libraryManager;
  26. private readonly IServerConfigurationManager _config;
  27. private readonly ISubtitleManager _subtitleManager;
  28. private readonly IMediaSourceManager _mediaSourceManager;
  29. private readonly ILogger _logger;
  30. private readonly IJsonSerializer _json;
  31. public SubtitleScheduledTask(ILibraryManager libraryManager, IJsonSerializer json, IServerConfigurationManager config, ISubtitleManager subtitleManager, ILogger logger, IMediaSourceManager mediaSourceManager)
  32. {
  33. _libraryManager = libraryManager;
  34. _config = config;
  35. _subtitleManager = subtitleManager;
  36. _logger = logger;
  37. _mediaSourceManager = mediaSourceManager;
  38. _json = json;
  39. }
  40. public string Name
  41. {
  42. get { return "Download missing subtitles"; }
  43. }
  44. public string Description
  45. {
  46. get { return "Searches the internet for missing subtitles based on metadata configuration."; }
  47. }
  48. public string Category
  49. {
  50. get { return "Library"; }
  51. }
  52. private SubtitleOptions GetOptions()
  53. {
  54. return _config.GetConfiguration<SubtitleOptions>("subtitles");
  55. }
  56. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  57. {
  58. var options = GetOptions();
  59. var types = new List<string>();
  60. if (options.DownloadEpisodeSubtitles)
  61. {
  62. types.Add("Episode");
  63. }
  64. if (options.DownloadMovieSubtitles)
  65. {
  66. types.Add("Movie");
  67. }
  68. if (types.Count == 0)
  69. {
  70. return;
  71. }
  72. var dict = new Dictionary<Guid, BaseItem>();
  73. foreach (var lang in options.DownloadLanguages)
  74. {
  75. var query = new InternalItemsQuery
  76. {
  77. MediaTypes = new string[] {MediaType.Video},
  78. IsVirtualItem = false,
  79. IncludeItemTypes = types.ToArray(types.Count),
  80. DtoOptions = new DtoOptions(true),
  81. SourceTypes = new[] {SourceType.Library}
  82. };
  83. if (options.SkipIfAudioTrackMatches)
  84. {
  85. query.HasNoAudioTrackWithLanguage = lang;
  86. }
  87. if (options.SkipIfEmbeddedSubtitlesPresent)
  88. {
  89. // Exclude if it already has any subtitles of the same language
  90. query.HasNoSubtitleTrackWithLanguage = lang;
  91. }
  92. else
  93. {
  94. // Exclude if it already has external subtitles of the same language
  95. query.HasNoExternalSubtitleTrackWithLanguage = lang;
  96. }
  97. var videosByLanguage = _libraryManager.GetItemList(query);
  98. foreach (var video in videosByLanguage)
  99. {
  100. dict[video.Id] = video;
  101. }
  102. }
  103. var videos = dict.Values.ToList();
  104. if (videos.Count == 0)
  105. {
  106. return;
  107. }
  108. var numComplete = 0;
  109. foreach (var video in videos)
  110. {
  111. cancellationToken.ThrowIfCancellationRequested();
  112. try
  113. {
  114. await DownloadSubtitles(video as Video, options, cancellationToken).ConfigureAwait(false);
  115. }
  116. catch (Exception ex)
  117. {
  118. _logger.ErrorException("Error downloading subtitles for {0}", ex, video.Path);
  119. }
  120. // Update progress
  121. numComplete++;
  122. double percent = numComplete;
  123. percent /= videos.Count;
  124. progress.Report(100 * percent);
  125. }
  126. }
  127. private async Task<bool> DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
  128. {
  129. if ((options.DownloadEpisodeSubtitles &&
  130. video is Episode) ||
  131. (options.DownloadMovieSubtitles &&
  132. video is Movie))
  133. {
  134. var mediaStreams = _mediaSourceManager.GetStaticMediaSources(video, false).First().MediaStreams;
  135. var downloadedLanguages = await new SubtitleDownloader(_logger,
  136. _subtitleManager)
  137. .DownloadSubtitles(video,
  138. mediaStreams,
  139. options.SkipIfEmbeddedSubtitlesPresent,
  140. options.SkipIfAudioTrackMatches,
  141. options.RequirePerfectMatch,
  142. options.DownloadLanguages,
  143. cancellationToken).ConfigureAwait(false);
  144. // Rescan
  145. if (downloadedLanguages.Count > 0)
  146. {
  147. await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  148. return false;
  149. }
  150. return true;
  151. }
  152. return false;
  153. }
  154. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  155. {
  156. return new[] {
  157. // Every so often
  158. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  159. };
  160. }
  161. public string Key
  162. {
  163. get { return "DownloadSubtitles"; }
  164. }
  165. }
  166. }