SeriesResolver.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Controller.Resolvers;
  8. using MediaBrowser.Model.Entities;
  9. using System;
  10. using System.IO;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Naming.Common;
  13. using MediaBrowser.Naming.IO;
  14. using MediaBrowser.Naming.TV;
  15. using MediaBrowser.Server.Implementations.Logging;
  16. using EpisodeInfo = MediaBrowser.Controller.Providers.EpisodeInfo;
  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. var collectionType = args.GetCollectionType();
  54. if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  55. {
  56. if (args.HasParent<Series>())
  57. {
  58. return null;
  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.HasParent<Series>())
  75. {
  76. return null;
  77. }
  78. if (args.Parent.IsRoot)
  79. {
  80. return null;
  81. }
  82. if (IsSeriesFolder(args.Path, args.FileSystemChildren, args.DirectoryService, _fileSystem, _logger, _libraryManager, false))
  83. {
  84. return new Series
  85. {
  86. Path = args.Path,
  87. Name = Path.GetFileName(args.Path)
  88. };
  89. }
  90. }
  91. }
  92. }
  93. return null;
  94. }
  95. public static bool IsSeriesFolder(string path,
  96. IEnumerable<FileSystemInfo> fileSystemChildren,
  97. IDirectoryService directoryService,
  98. IFileSystem fileSystem,
  99. ILogger logger,
  100. ILibraryManager libraryManager,
  101. bool isTvContentType)
  102. {
  103. foreach (var child in fileSystemChildren)
  104. {
  105. var attributes = child.Attributes;
  106. if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
  107. {
  108. //logger.Debug("Igoring series file or folder marked hidden: {0}", child.FullName);
  109. continue;
  110. }
  111. // Can't enforce this because files saved by Bitcasa are always marked System
  112. //if ((attributes & FileAttributes.System) == FileAttributes.System)
  113. //{
  114. // logger.Debug("Igoring series subfolder marked system: {0}", child.FullName);
  115. // continue;
  116. //}
  117. if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
  118. {
  119. if (IsSeasonFolder(child.FullName, isTvContentType))
  120. {
  121. //logger.Debug("{0} is a series because of season folder {1}.", path, child.FullName);
  122. return true;
  123. }
  124. }
  125. else
  126. {
  127. string fullName = child.FullName;
  128. if (libraryManager.IsVideoFile(fullName))
  129. {
  130. if (isTvContentType)
  131. {
  132. return true;
  133. }
  134. var namingOptions = ((LibraryManager)libraryManager).GetNamingOptions();
  135. // 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)
  136. if (!isTvContentType)
  137. {
  138. namingOptions.EpisodeExpressions = namingOptions.EpisodeExpressions
  139. .Where(i => i.IsNamed && !i.IsOptimistic)
  140. .ToList();
  141. }
  142. var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions, new PatternsLogger());
  143. var episodeInfo = episodeResolver.Resolve(fullName, FileInfoType.File, false);
  144. if (episodeInfo != null && episodeInfo.EpisodeNumber.HasValue)
  145. {
  146. return true;
  147. }
  148. }
  149. }
  150. }
  151. logger.Debug("{0} is not a series folder.", path);
  152. return false;
  153. }
  154. /// <summary>
  155. /// Determines whether [is place holder] [the specified path].
  156. /// </summary>
  157. /// <param name="path">The path.</param>
  158. /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
  159. /// <exception cref="System.ArgumentNullException">path</exception>
  160. private static bool IsVideoPlaceHolder(string path)
  161. {
  162. if (string.IsNullOrEmpty(path))
  163. {
  164. throw new ArgumentNullException("path");
  165. }
  166. var extension = Path.GetExtension(path);
  167. return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
  168. }
  169. /// <summary>
  170. /// Determines whether [is season folder] [the specified path].
  171. /// </summary>
  172. /// <param name="path">The path.</param>
  173. /// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</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)
  176. {
  177. var seasonNumber = new SeasonPathParser(new ExtendedNamingOptions(), 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. }