SeriesResolver.cs 8.7 KB

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