SeriesResolver.cs 9.3 KB

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