SeriesResolver.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using Emby.Naming.Common;
  7. using Emby.Naming.TV;
  8. using Emby.Naming.Video;
  9. using MediaBrowser.Controller.Entities.TV;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Resolvers;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.IO;
  14. using Microsoft.Extensions.Logging;
  15. namespace Emby.Server.Implementations.Library.Resolvers.TV
  16. {
  17. /// <summary>
  18. /// Class SeriesResolver.
  19. /// </summary>
  20. public class SeriesResolver : GenericFolderResolver<Series>
  21. {
  22. private readonly ILogger<SeriesResolver> _logger;
  23. private readonly NamingOptions _namingOptions;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="SeriesResolver"/> class.
  26. /// </summary>
  27. /// <param name="logger">The logger.</param>
  28. /// <param name="namingOptions">The naming options.</param>
  29. public SeriesResolver(ILogger<SeriesResolver> logger, NamingOptions namingOptions)
  30. {
  31. _logger = logger;
  32. _namingOptions = namingOptions;
  33. }
  34. /// <summary>
  35. /// Gets the priority.
  36. /// </summary>
  37. /// <value>The priority.</value>
  38. public override ResolverPriority Priority => ResolverPriority.Second;
  39. /// <summary>
  40. /// Resolves the specified args.
  41. /// </summary>
  42. /// <param name="args">The args.</param>
  43. /// <returns>Series.</returns>
  44. protected override Series Resolve(ItemResolveArgs args)
  45. {
  46. if (args.IsDirectory)
  47. {
  48. if (args.HasParent<Series>() || args.HasParent<Season>())
  49. {
  50. return null;
  51. }
  52. var seriesInfo = Naming.TV.SeriesResolver.Resolve(_namingOptions, args.Path);
  53. var collectionType = args.GetCollectionType();
  54. if (string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  55. {
  56. // TODO refactor into separate class or something, this is copied from LibraryManager.GetConfiguredContentType
  57. var configuredContentType = args.GetConfiguredContentType();
  58. if (!string.Equals(configuredContentType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  59. {
  60. return new Series
  61. {
  62. Path = args.Path,
  63. Name = seriesInfo.Name
  64. };
  65. }
  66. }
  67. else if (string.IsNullOrEmpty(collectionType))
  68. {
  69. if (args.ContainsFileSystemEntryByName("tvshow.nfo"))
  70. {
  71. if (args.Parent is not null && args.Parent.IsRoot)
  72. {
  73. // 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
  74. return null;
  75. }
  76. return new Series
  77. {
  78. Path = args.Path,
  79. Name = seriesInfo.Name
  80. };
  81. }
  82. if (args.Parent is not null && args.Parent.IsRoot)
  83. {
  84. return null;
  85. }
  86. if (IsSeriesFolder(args.Path, args.FileSystemChildren, false))
  87. {
  88. return new Series
  89. {
  90. Path = args.Path,
  91. Name = seriesInfo.Name
  92. };
  93. }
  94. }
  95. }
  96. return null;
  97. }
  98. private bool IsSeriesFolder(
  99. string path,
  100. IEnumerable<FileSystemMetadata> fileSystemChildren,
  101. bool isTvContentType)
  102. {
  103. foreach (var child in fileSystemChildren)
  104. {
  105. if (child.IsDirectory)
  106. {
  107. if (IsSeasonFolder(child.FullName, isTvContentType))
  108. {
  109. _logger.LogDebug("{Path} is a series because of season folder {Dir}.", path, child.FullName);
  110. return true;
  111. }
  112. }
  113. else
  114. {
  115. string fullName = child.FullName;
  116. if (VideoResolver.IsVideoFile(path, _namingOptions))
  117. {
  118. if (isTvContentType)
  119. {
  120. return true;
  121. }
  122. var namingOptions = _namingOptions;
  123. var episodeResolver = new Naming.TV.EpisodeResolver(namingOptions);
  124. var episodeInfo = episodeResolver.Resolve(fullName, false, true, false, fillExtendedInfo: false);
  125. if (episodeInfo is not null && episodeInfo.EpisodeNumber.HasValue)
  126. {
  127. return true;
  128. }
  129. }
  130. }
  131. }
  132. _logger.LogDebug("{Path} is not a series folder.", path);
  133. return false;
  134. }
  135. /// <summary>
  136. /// Determines whether [is season folder] [the specified path].
  137. /// </summary>
  138. /// <param name="path">The path.</param>
  139. /// <param name="isTvContentType">if set to <c>true</c> [is tv content type].</param>
  140. /// <returns><c>true</c> if [is season folder] [the specified path]; otherwise, <c>false</c>.</returns>
  141. private static bool IsSeasonFolder(string path, bool isTvContentType)
  142. {
  143. var seasonNumber = SeasonPathParser.Parse(path, isTvContentType, isTvContentType).SeasonNumber;
  144. return seasonNumber.HasValue;
  145. }
  146. /// <summary>
  147. /// Sets the initial item values.
  148. /// </summary>
  149. /// <param name="item">The item.</param>
  150. /// <param name="args">The args.</param>
  151. protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
  152. {
  153. base.SetInitialItemValues(item, args);
  154. SetProviderIdFromPath(item, args.Path);
  155. }
  156. /// <summary>
  157. /// Sets the provider id from path.
  158. /// </summary>
  159. /// <param name="item">The item.</param>
  160. /// <param name="path">The path.</param>
  161. private static void SetProviderIdFromPath(Series item, string path)
  162. {
  163. var justName = Path.GetFileName(path.AsSpan());
  164. var tvdbId = justName.GetAttributeValue("tvdbid");
  165. if (!string.IsNullOrEmpty(tvdbId))
  166. {
  167. item.SetProviderId(MetadataProvider.Tvdb, tvdbId);
  168. }
  169. var tvmazeId = justName.GetAttributeValue("tvmazeid");
  170. if (!string.IsNullOrEmpty(tvmazeId))
  171. {
  172. item.SetProviderId(MetadataProvider.TvMaze, tvmazeId);
  173. }
  174. var tmdbId = justName.GetAttributeValue("tmdbid");
  175. if (!string.IsNullOrEmpty(tmdbId))
  176. {
  177. item.SetProviderId(MetadataProvider.Tmdb, tmdbId);
  178. }
  179. var anidbId = justName.GetAttributeValue("anidbid");
  180. if (!string.IsNullOrEmpty(anidbId))
  181. {
  182. item.SetProviderId("AniDB", anidbId);
  183. }
  184. var aniListId = justName.GetAttributeValue("anilistid");
  185. if (!string.IsNullOrEmpty(aniListId))
  186. {
  187. item.SetProviderId("AniList", aniListId);
  188. }
  189. var aniSearchId = justName.GetAttributeValue("anisearchid");
  190. if (!string.IsNullOrEmpty(aniSearchId))
  191. {
  192. item.SetProviderId("AniSearch", aniSearchId);
  193. }
  194. }
  195. }
  196. }