2
0

SeriesResolver.cs 8.7 KB

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