SeriesResolver.cs 4.0 KB

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