SeriesResolver.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Entities.TV;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Controller.Resolvers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Naming.Common;
  9. using MediaBrowser.Naming.TV;
  10. using MediaBrowser.Server.Implementations.Logging;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
  16. {
  17. /// <summary>
  18. /// Class SeriesResolver
  19. /// </summary>
  20. public class SeriesResolver : FolderResolver<Series>
  21. {
  22. private readonly IFileSystem _fileSystem;
  23. private readonly ILogger _logger;
  24. private readonly ILibraryManager _libraryManager;
  25. public SeriesResolver(IFileSystem fileSystem, ILogger logger, ILibraryManager libraryManager)
  26. {
  27. _fileSystem = fileSystem;
  28. _logger = logger;
  29. _libraryManager = libraryManager;
  30. }
  31. /// <summary>
  32. /// Gets the priority.
  33. /// </summary>
  34. /// <value>The priority.</value>
  35. public override ResolverPriority Priority
  36. {
  37. get
  38. {
  39. return ResolverPriority.Second;
  40. }
  41. }
  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. var collectionType = args.GetCollectionType();
  52. if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  53. {
  54. if (args.HasParent<Series>())
  55. {
  56. return null;
  57. }
  58. var configuredContentType = _libraryManager.GetConfiguredContentType(args.Path);
  59. if (!string.Equals(configuredContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  60. {
  61. return new Series
  62. {
  63. Path = args.Path,
  64. Name = Path.GetFileName(args.Path)
  65. };
  66. }
  67. }
  68. else
  69. {
  70. if (string.IsNullOrWhiteSpace(collectionType))
  71. {
  72. if (args.HasParent<Series>())
  73. {
  74. return null;
  75. }
  76. if (args.Parent.IsRoot)
  77. {
  78. return null;
  79. }
  80. if (IsSeriesFolder(args.Path, args.FileSystemChildren, args.DirectoryService, _fileSystem, _logger, _libraryManager, false))
  81. {
  82. return new Series
  83. {
  84. Path = args.Path,
  85. Name = Path.GetFileName(args.Path)
  86. };
  87. }
  88. }
  89. }
  90. }
  91. return null;
  92. }
  93. public static bool IsSeriesFolder(string path,
  94. IEnumerable<FileSystemInfo> fileSystemChildren,
  95. IDirectoryService directoryService,
  96. IFileSystem fileSystem,
  97. ILogger logger,
  98. ILibraryManager libraryManager,
  99. bool isTvContentType)
  100. {
  101. foreach (var child in fileSystemChildren)
  102. {
  103. var attributes = child.Attributes;
  104. //if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  105. //{
  106. // //logger.Debug("Igoring series file or folder marked hidden: {0}", child.FullName);
  107. // continue;
  108. //}
  109. // Can't enforce this because files saved by Bitcasa are always marked System
  110. //if ((attributes & FileAttributes.System) == FileAttributes.System)
  111. //{
  112. // logger.Debug("Igoring series subfolder marked system: {0}", child.FullName);
  113. // continue;
  114. //}
  115. if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
  116. {
  117. if (IsSeasonFolder(child.FullName, isTvContentType, libraryManager))
  118. {
  119. //logger.Debug("{0} is a series because of season folder {1}.", path, child.FullName);
  120. return true;
  121. }
  122. }
  123. else
  124. {
  125. string fullName = child.FullName;
  126. if (libraryManager.IsVideoFile(fullName))
  127. {
  128. if (isTvContentType)
  129. {
  130. return true;
  131. }
  132. var namingOptions = ((LibraryManager)libraryManager).GetNamingOptions();
  133. // In mixed folders we need to be conservative and avoid expressions that may result in false positives (e.g. movies with numbers in the title)
  134. if (!isTvContentType)
  135. {
  136. namingOptions.EpisodeExpressions = namingOptions.EpisodeExpressions
  137. .Where(i => i.IsNamed && !i.IsOptimistic)
  138. .ToList();
  139. }
  140. var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions, new PatternsLogger());
  141. var episodeInfo = episodeResolver.Resolve(fullName, false, false);
  142. if (episodeInfo != null && episodeInfo.EpisodeNumber.HasValue)
  143. {
  144. return true;
  145. }
  146. }
  147. }
  148. }
  149. logger.Debug("{0} is not a series folder.", path);
  150. return false;
  151. }
  152. /// <summary>
  153. /// Determines whether [is place holder] [the specified path].
  154. /// </summary>
  155. /// <param name="path">The path.</param>
  156. /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
  157. /// <exception cref="System.ArgumentNullException">path</exception>
  158. private static bool IsVideoPlaceHolder(string path)
  159. {
  160. if (string.IsNullOrEmpty(path))
  161. {
  162. throw new ArgumentNullException("path");
  163. }
  164. var extension = Path.GetExtension(path);
  165. return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
  166. }
  167. /// <summary>
  168. /// Determines whether [is season folder] [the specified path].
  169. /// </summary>
  170. /// <param name="path">The path.</param>
  171. /// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
  172. /// <param name="libraryManager">The library manager.</param>
  173. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  174. private static bool IsSeasonFolder(string path, bool isTvContentType, ILibraryManager libraryManager)
  175. {
  176. var namingOptions = ((LibraryManager)libraryManager).GetNamingOptions();
  177. var seasonNumber = new SeasonPathParser(namingOptions, new RegexProvider()).Parse(path, isTvContentType, isTvContentType).SeasonNumber;
  178. return seasonNumber.HasValue;
  179. }
  180. /// <summary>
  181. /// Sets the initial item values.
  182. /// </summary>
  183. /// <param name="item">The item.</param>
  184. /// <param name="args">The args.</param>
  185. protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
  186. {
  187. base.SetInitialItemValues(item, args);
  188. SetProviderIdFromPath(item, args.Path);
  189. }
  190. /// <summary>
  191. /// Sets the provider id from path.
  192. /// </summary>
  193. /// <param name="item">The item.</param>
  194. /// <param name="path">The path.</param>
  195. private void SetProviderIdFromPath(Series item, string path)
  196. {
  197. var justName = Path.GetFileName(path);
  198. var id = justName.GetAttributeValue("tvdbid");
  199. if (!string.IsNullOrEmpty(id))
  200. {
  201. item.SetProviderId(MetadataProviders.Tvdb, id);
  202. }
  203. }
  204. }
  205. }