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