SeriesResolver.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using Emby.Naming.Common;
  8. using Emby.Naming.TV;
  9. using Emby.Naming.Video;
  10. using MediaBrowser.Common.Configuration;
  11. using MediaBrowser.Controller.Configuration;
  12. using MediaBrowser.Controller.Entities.TV;
  13. using MediaBrowser.Controller.Library;
  14. using MediaBrowser.Controller.Resolvers;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.IO;
  17. using Microsoft.Extensions.Logging;
  18. namespace Emby.Server.Implementations.Library.Resolvers.TV
  19. {
  20. /// <summary>
  21. /// Class SeriesResolver.
  22. /// </summary>
  23. public class SeriesResolver : GenericFolderResolver<Series>
  24. {
  25. private readonly ILogger<SeriesResolver> _logger;
  26. private readonly NamingOptions _namingOptions;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="SeriesResolver"/> class.
  29. /// </summary>
  30. /// <param name="logger">The logger.</param>
  31. /// <param name="namingOptions">The naming options.</param>
  32. public SeriesResolver(ILogger<SeriesResolver> logger, NamingOptions namingOptions)
  33. {
  34. _logger = logger;
  35. _namingOptions = namingOptions;
  36. }
  37. /// <summary>
  38. /// Gets the priority.
  39. /// </summary>
  40. /// <value>The priority.</value>
  41. public override ResolverPriority Priority => ResolverPriority.Second;
  42. /// <summary>
  43. /// Resolves the specified args.
  44. /// </summary>
  45. /// <param name="args">The args.</param>
  46. /// <returns>Series.</returns>
  47. protected override Series Resolve(ItemResolveArgs args)
  48. {
  49. if (args.IsDirectory)
  50. {
  51. if (args.HasParent<Series>() || args.HasParent<Season>())
  52. {
  53. return null;
  54. }
  55. var seriesInfo = Naming.TV.SeriesResolver.Resolve(_namingOptions, args.Path);
  56. var collectionType = args.GetCollectionType();
  57. if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  58. {
  59. // TODO refactor into separate class or something, this is copied from LibraryManager.GetConfiguredContentType
  60. var configuredContentType = args.GetConfiguredContentType();
  61. if (!string.Equals(configuredContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  62. {
  63. return new Series
  64. {
  65. Path = args.Path,
  66. Name = seriesInfo.Name
  67. };
  68. }
  69. }
  70. else if (string.IsNullOrEmpty(collectionType))
  71. {
  72. if (args.ContainsFileSystemEntryByName("tvshow.nfo"))
  73. {
  74. if (args.Parent != null && args.Parent.IsRoot)
  75. {
  76. // For now, return null, but if we want to allow this in the future then add some additional checks to guard against a misplaced tvshow.nfo
  77. return null;
  78. }
  79. return new Series
  80. {
  81. Path = args.Path,
  82. Name = seriesInfo.Name
  83. };
  84. }
  85. if (args.Parent != null && args.Parent.IsRoot)
  86. {
  87. return null;
  88. }
  89. if (IsSeriesFolder(args.Path, args.FileSystemChildren, false))
  90. {
  91. return new Series
  92. {
  93. Path = args.Path,
  94. Name = seriesInfo.Name
  95. };
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. private bool IsSeriesFolder(
  102. string path,
  103. IEnumerable<FileSystemMetadata> fileSystemChildren,
  104. bool isTvContentType)
  105. {
  106. foreach (var child in fileSystemChildren)
  107. {
  108. if (child.IsDirectory)
  109. {
  110. if (IsSeasonFolder(child.FullName, isTvContentType))
  111. {
  112. _logger.LogDebug("{Path} is a series because of season folder {Dir}.", path, child.FullName);
  113. return true;
  114. }
  115. }
  116. else
  117. {
  118. string fullName = child.FullName;
  119. if (VideoResolver.IsVideoFile(path, _namingOptions))
  120. {
  121. if (isTvContentType)
  122. {
  123. return true;
  124. }
  125. var namingOptions = _namingOptions;
  126. var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions);
  127. var episodeInfo = episodeResolver.Resolve(fullName, false, true, false, fillExtendedInfo: false);
  128. if (episodeInfo != null && episodeInfo.EpisodeNumber.HasValue)
  129. {
  130. return true;
  131. }
  132. }
  133. }
  134. }
  135. _logger.LogDebug("{Path} is not a series folder.", path);
  136. return false;
  137. }
  138. /// <summary>
  139. /// Determines whether [is season folder] [the specified path].
  140. /// </summary>
  141. /// <param name="path">The path.</param>
  142. /// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
  143. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  144. private static bool IsSeasonFolder(string path, bool isTvContentType)
  145. {
  146. var seasonNumber = SeasonPathParser.Parse(path, isTvContentType, isTvContentType).SeasonNumber;
  147. return seasonNumber.HasValue;
  148. }
  149. /// <summary>
  150. /// Sets the initial item values.
  151. /// </summary>
  152. /// <param name="item">The item.</param>
  153. /// <param name="args">The args.</param>
  154. protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
  155. {
  156. base.SetInitialItemValues(item, args);
  157. SetProviderIdFromPath(item, args.Path);
  158. }
  159. /// <summary>
  160. /// Sets the provider id from path.
  161. /// </summary>
  162. /// <param name="item">The item.</param>
  163. /// <param name="path">The path.</param>
  164. private static void SetProviderIdFromPath(Series item, string path)
  165. {
  166. var justName = Path.GetFileName(path);
  167. var id = justName.GetAttributeValue("tvdbid");
  168. if (!string.IsNullOrEmpty(id))
  169. {
  170. item.SetProviderId(MetadataProvider.Tvdb, id);
  171. }
  172. }
  173. }
  174. }