SeriesResolver.cs 8.6 KB

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