SeriesPostScanTask.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Entities.TV;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Plugins;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Globalization;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Logging;
  16. using MediaBrowser.Model.Tasks;
  17. using MediaBrowser.Model.Threading;
  18. using MediaBrowser.Model.Xml;
  19. using MediaBrowser.Providers.TV;
  20. namespace Emby.Server.Implementations.TV
  21. {
  22. class SeriesGroup : List<Series>, IGrouping<string, Series>
  23. {
  24. public string Key { get; set; }
  25. }
  26. class SeriesPostScanTask : ILibraryPostScanTask, IHasOrder
  27. {
  28. /// <summary>
  29. /// The _library manager
  30. /// </summary>
  31. private readonly ILibraryManager _libraryManager;
  32. private readonly IServerConfigurationManager _config;
  33. private readonly ILogger _logger;
  34. private readonly ILocalizationManager _localization;
  35. private readonly IFileSystem _fileSystem;
  36. private readonly IXmlReaderSettingsFactory _xmlSettings;
  37. public SeriesPostScanTask(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, ILocalizationManager localization, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
  38. {
  39. _libraryManager = libraryManager;
  40. _logger = logger;
  41. _config = config;
  42. _localization = localization;
  43. _fileSystem = fileSystem;
  44. _xmlSettings = xmlSettings;
  45. }
  46. public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  47. {
  48. return RunInternal(progress, cancellationToken);
  49. }
  50. private Task RunInternal(IProgress<double> progress, CancellationToken cancellationToken)
  51. {
  52. var seriesList = _libraryManager.GetItemList(new InternalItemsQuery()
  53. {
  54. IncludeItemTypes = new[] { typeof(Series).Name },
  55. Recursive = true,
  56. GroupByPresentationUniqueKey = false
  57. }).Cast<Series>().ToList();
  58. var seriesGroups = FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList();
  59. return new MissingEpisodeProvider(_logger, _config, _libraryManager, _localization, _fileSystem, _xmlSettings).Run(seriesGroups, true, cancellationToken);
  60. }
  61. internal static IEnumerable<IGrouping<string, Series>> FindSeriesGroups(List<Series> seriesList)
  62. {
  63. var links = seriesList.ToDictionary(s => s, s => seriesList.Where(c => c != s && ShareProviderId(s, c)).ToList());
  64. var visited = new HashSet<Series>();
  65. foreach (var series in seriesList)
  66. {
  67. if (!visited.Contains(series))
  68. {
  69. var group = new SeriesGroup();
  70. FindAllLinked(series, visited, links, group);
  71. group.Key = group.Select(s => s.PresentationUniqueKey).FirstOrDefault(id => !string.IsNullOrEmpty(id));
  72. yield return group;
  73. }
  74. }
  75. }
  76. private static void FindAllLinked(Series series, HashSet<Series> visited, IDictionary<Series, List<Series>> linksMap, List<Series> results)
  77. {
  78. results.Add(series);
  79. visited.Add(series);
  80. var links = linksMap[series];
  81. foreach (var s in links)
  82. {
  83. if (!visited.Contains(s))
  84. {
  85. FindAllLinked(s, visited, linksMap, results);
  86. }
  87. }
  88. }
  89. private static bool ShareProviderId(Series a, Series b)
  90. {
  91. return string.Equals(a.PresentationUniqueKey, b.PresentationUniqueKey, StringComparison.Ordinal);
  92. }
  93. public int Order
  94. {
  95. get
  96. {
  97. // Run after tvdb update task
  98. return 1;
  99. }
  100. }
  101. }
  102. public class CleanMissingEpisodesEntryPoint : IServerEntryPoint
  103. {
  104. private readonly ILibraryManager _libraryManager;
  105. private readonly IServerConfigurationManager _config;
  106. private readonly ILogger _logger;
  107. private readonly ILocalizationManager _localization;
  108. private readonly IFileSystem _fileSystem;
  109. private readonly object _libraryChangedSyncLock = new object();
  110. private const int LibraryUpdateDuration = 180000;
  111. private readonly ITaskManager _taskManager;
  112. private readonly IXmlReaderSettingsFactory _xmlSettings;
  113. private readonly ITimerFactory _timerFactory;
  114. public CleanMissingEpisodesEntryPoint(ILibraryManager libraryManager, IServerConfigurationManager config, ILogger logger, ILocalizationManager localization, IFileSystem fileSystem, ITaskManager taskManager, IXmlReaderSettingsFactory xmlSettings, ITimerFactory timerFactory)
  115. {
  116. _libraryManager = libraryManager;
  117. _config = config;
  118. _logger = logger;
  119. _localization = localization;
  120. _fileSystem = fileSystem;
  121. _taskManager = taskManager;
  122. _xmlSettings = xmlSettings;
  123. _timerFactory = timerFactory;
  124. }
  125. private ITimer LibraryUpdateTimer { get; set; }
  126. public void Run()
  127. {
  128. _libraryManager.ItemAdded += _libraryManager_ItemAdded;
  129. }
  130. private void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
  131. {
  132. if (!FilterItem(e.Item))
  133. {
  134. return;
  135. }
  136. lock (_libraryChangedSyncLock)
  137. {
  138. if (LibraryUpdateTimer == null)
  139. {
  140. LibraryUpdateTimer = _timerFactory.Create(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, Timeout.Infinite);
  141. }
  142. else
  143. {
  144. LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite);
  145. }
  146. }
  147. }
  148. private async void LibraryUpdateTimerCallback(object state)
  149. {
  150. try
  151. {
  152. if (MissingEpisodeProvider.IsRunning)
  153. {
  154. return;
  155. }
  156. if (_libraryManager.IsScanRunning)
  157. {
  158. return;
  159. }
  160. var seriesList = _libraryManager.GetItemList(new InternalItemsQuery()
  161. {
  162. IncludeItemTypes = new[] { typeof(Series).Name },
  163. Recursive = true,
  164. GroupByPresentationUniqueKey = false
  165. }).Cast<Series>().ToList();
  166. var seriesGroups = SeriesPostScanTask.FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList();
  167. await new MissingEpisodeProvider(_logger, _config, _libraryManager, _localization, _fileSystem, _xmlSettings)
  168. .Run(seriesGroups, false, CancellationToken.None).ConfigureAwait(false);
  169. }
  170. catch (Exception ex)
  171. {
  172. _logger.ErrorException("Error in SeriesPostScanTask", ex);
  173. }
  174. }
  175. private bool FilterItem(BaseItem item)
  176. {
  177. return item is Episode && item.LocationType != LocationType.Virtual;
  178. }
  179. /// <summary>
  180. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  181. /// </summary>
  182. public void Dispose()
  183. {
  184. Dispose(true);
  185. }
  186. /// <summary>
  187. /// Releases unmanaged and - optionally - managed resources.
  188. /// </summary>
  189. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  190. protected virtual void Dispose(bool dispose)
  191. {
  192. if (dispose)
  193. {
  194. if (LibraryUpdateTimer != null)
  195. {
  196. LibraryUpdateTimer.Dispose();
  197. LibraryUpdateTimer = null;
  198. }
  199. _libraryManager.ItemAdded -= _libraryManager_ItemAdded;
  200. }
  201. }
  202. }
  203. }