SeriesResolver.cs 9.4 KB

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