SeriesPostScanTask.cs 4.6 KB

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