SeriesPostScanTask.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities.TV;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Localization;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using CommonIO;
  14. namespace MediaBrowser.Providers.TV
  15. {
  16. class SeriesGroup : List<Series>, IGrouping<string, Series>
  17. {
  18. public string Key { get; set; }
  19. }
  20. class SeriesPostScanTask : ILibraryPostScanTask, IHasOrder
  21. {
  22. /// <summary>
  23. /// The _library manager
  24. /// </summary>
  25. private readonly ILibraryManager _libraryManager;
  26. private readonly IServerConfigurationManager _config;
  27. private readonly ILogger _logger;
  28. private readonly ILocalizationManager _localization;
  29. private readonly IFileSystem _fileSystem;
  30. public SeriesPostScanTask(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, ILocalizationManager localization, IFileSystem fileSystem)
  31. {
  32. _libraryManager = libraryManager;
  33. _logger = logger;
  34. _config = config;
  35. _localization = localization;
  36. _fileSystem = fileSystem;
  37. }
  38. public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  39. {
  40. return RunInternal(progress, cancellationToken);
  41. }
  42. private async Task RunInternal(IProgress<double> progress, CancellationToken cancellationToken)
  43. {
  44. var seriesList = _libraryManager.RootFolder
  45. .GetRecursiveChildren(i => i is Series)
  46. .Cast<Series>()
  47. .ToList();
  48. var seriesGroups = FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList();
  49. await new MissingEpisodeProvider(_logger, _config, _libraryManager, _localization, _fileSystem).Run(seriesGroups, cancellationToken).ConfigureAwait(false);
  50. var numComplete = 0;
  51. foreach (var series in seriesList)
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. var episodes = series.GetRecursiveChildren(i => i is Episode)
  55. .Cast<Episode>()
  56. .ToList();
  57. var physicalEpisodes = episodes.Where(i => i.LocationType != LocationType.Virtual)
  58. .ToList();
  59. series.SpecialFeatureIds = physicalEpisodes
  60. .Where(i => i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == 0)
  61. .Select(i => i.Id)
  62. .ToList();
  63. numComplete++;
  64. double percent = numComplete;
  65. percent /= seriesList.Count;
  66. percent *= 100;
  67. progress.Report(percent);
  68. }
  69. }
  70. private IEnumerable<IGrouping<string, Series>> FindSeriesGroups(List<Series> seriesList)
  71. {
  72. var links = seriesList.ToDictionary(s => s, s => seriesList.Where(c => c != s && ShareProviderId(s, c)).ToList());
  73. var visited = new HashSet<Series>();
  74. foreach (var series in seriesList)
  75. {
  76. if (!visited.Contains(series))
  77. {
  78. var group = new SeriesGroup();
  79. FindAllLinked(series, visited, links, group);
  80. group.Key = group.Select(s => s.GetProviderId(MetadataProviders.Tvdb)).FirstOrDefault(id => !string.IsNullOrEmpty(id));
  81. yield return group;
  82. }
  83. }
  84. }
  85. private void FindAllLinked(Series series, HashSet<Series> visited, IDictionary<Series, List<Series>> linksMap, List<Series> results)
  86. {
  87. results.Add(series);
  88. visited.Add(series);
  89. var links = linksMap[series];
  90. foreach (var s in links)
  91. {
  92. if (!visited.Contains(s))
  93. {
  94. FindAllLinked(s, visited, linksMap, results);
  95. }
  96. }
  97. }
  98. private bool ShareProviderId(Series a, Series b)
  99. {
  100. return a.ProviderIds.Any(id =>
  101. {
  102. string value;
  103. return b.ProviderIds.TryGetValue(id.Key, out value) && id.Value == value;
  104. });
  105. }
  106. public int Order
  107. {
  108. get
  109. {
  110. // Run after tvdb update task
  111. return 1;
  112. }
  113. }
  114. }
  115. }