SeriesResolver.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Resolvers;
  7. using MediaBrowser.Model.Entities;
  8. using System;
  9. using System.IO;
  10. namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV
  11. {
  12. /// <summary>
  13. /// Class SeriesResolver
  14. /// </summary>
  15. public class SeriesResolver : FolderResolver<Series>
  16. {
  17. /// <summary>
  18. /// Gets the priority.
  19. /// </summary>
  20. /// <value>The priority.</value>
  21. public override ResolverPriority Priority
  22. {
  23. get
  24. {
  25. return ResolverPriority.Second;
  26. }
  27. }
  28. /// <summary>
  29. /// Resolves the specified args.
  30. /// </summary>
  31. /// <param name="args">The args.</param>
  32. /// <returns>Series.</returns>
  33. protected override Series Resolve(ItemResolveArgs args)
  34. {
  35. if (args.IsDirectory)
  36. {
  37. // Avoid expensive tests against VF's and all their children by not allowing this
  38. if (args.Parent == null || args.Parent.IsRoot)
  39. {
  40. return null;
  41. }
  42. // Optimization to avoid running these tests against Seasons
  43. if (args.Parent is Series || args.Parent is MusicArtist || args.Parent is MusicAlbum)
  44. {
  45. return null;
  46. }
  47. var collectionType = args.GetCollectionType();
  48. // If there's a collection type and it's not tv, it can't be a series
  49. if (!string.IsNullOrEmpty(collectionType) &&
  50. !string.Equals(collectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase) &&
  51. !string.Equals(collectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase))
  52. {
  53. return null;
  54. }
  55. // It's a Series if any of the following conditions are met:
  56. // series.xml exists
  57. // [tvdbid= is present in the path
  58. // TVUtils.IsSeriesFolder returns true
  59. var filename = Path.GetFileName(args.Path);
  60. if (string.IsNullOrEmpty(filename))
  61. {
  62. return null;
  63. }
  64. // Without these movies that have the name season in them could cause the parent folder to be resolved as a series
  65. if (filename.IndexOf("[boxset]", StringComparison.OrdinalIgnoreCase) != -1 || filename.IndexOf("[tmdbid=", StringComparison.OrdinalIgnoreCase) != -1)
  66. {
  67. return null;
  68. }
  69. if (args.ContainsMetaFileByName("series.xml") || filename.IndexOf("[tvdbid=", StringComparison.OrdinalIgnoreCase) != -1 || TVUtils.IsSeriesFolder(args.Path, args.FileSystemChildren))
  70. {
  71. return new Series();
  72. }
  73. }
  74. return null;
  75. }
  76. /// <summary>
  77. /// Sets the initial item values.
  78. /// </summary>
  79. /// <param name="item">The item.</param>
  80. /// <param name="args">The args.</param>
  81. protected override void SetInitialItemValues(Series item, ItemResolveArgs args)
  82. {
  83. base.SetInitialItemValues(item, args);
  84. Season.AddMetadataFiles(args);
  85. SetProviderIdFromPath(item, args.Path);
  86. }
  87. /// <summary>
  88. /// Sets the provider id from path.
  89. /// </summary>
  90. /// <param name="item">The item.</param>
  91. /// <param name="path">The path.</param>
  92. private void SetProviderIdFromPath(Series item, string path)
  93. {
  94. var justName = Path.GetFileName(path);
  95. var id = justName.GetAttributeValue("tvdbid");
  96. if (!string.IsNullOrEmpty(id))
  97. {
  98. item.SetProviderId(MetadataProviders.Tvdb, id);
  99. }
  100. }
  101. }
  102. }