SeriesResolver.cs 9.1 KB

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