SeriesResolver.cs 8.5 KB

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