SortNameProvider.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Model.Logging;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Controller.Providers
  10. {
  11. /// <summary>
  12. /// Class SortNameProvider
  13. /// </summary>
  14. public class SortNameProvider : BaseMetadataProvider
  15. {
  16. public SortNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  17. : base(logManager, configurationManager)
  18. {
  19. }
  20. /// <summary>
  21. /// Supportses the specified item.
  22. /// </summary>
  23. /// <param name="item">The item.</param>
  24. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  25. public override bool Supports(BaseItem item)
  26. {
  27. return true;
  28. }
  29. /// <summary>
  30. /// Gets the priority.
  31. /// </summary>
  32. /// <value>The priority.</value>
  33. public override MetadataProviderPriority Priority
  34. {
  35. get { return MetadataProviderPriority.Last; }
  36. }
  37. /// <summary>
  38. /// Needses the refresh internal.
  39. /// </summary>
  40. /// <param name="item">The item.</param>
  41. /// <param name="providerInfo">The provider info.</param>
  42. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  43. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  44. {
  45. return !string.IsNullOrEmpty(item.Name) && string.IsNullOrEmpty(item.SortName);
  46. }
  47. // Cache these since they will be used a lot
  48. /// <summary>
  49. /// The false task result
  50. /// </summary>
  51. protected static readonly Task<bool> FalseTaskResult = Task.FromResult(false);
  52. /// <summary>
  53. /// The true task result
  54. /// </summary>
  55. protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
  56. /// <summary>
  57. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  58. /// </summary>
  59. /// <param name="item">The item.</param>
  60. /// <param name="force">if set to <c>true</c> [force].</param>
  61. /// <param name="cancellationToken">The cancellation token.</param>
  62. /// <returns>Task{System.Boolean}.</returns>
  63. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  64. {
  65. return SetSortName(item, cancellationToken) ? TrueTaskResult : FalseTaskResult;
  66. }
  67. /// <summary>
  68. /// Sets the name of the sort.
  69. /// </summary>
  70. /// <param name="item">The item.</param>
  71. /// <param name="cancellationToken">The cancellation token.</param>
  72. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  73. protected bool SetSortName(BaseItem item, CancellationToken cancellationToken)
  74. {
  75. if (!string.IsNullOrWhiteSpace(item.SortName)) return false; //let the earlier provider win
  76. cancellationToken.ThrowIfCancellationRequested();
  77. if (item is Episode)
  78. {
  79. //special handling for TV episodes season and episode number
  80. item.SortName = (item.ParentIndexNumber != null ? item.ParentIndexNumber.Value.ToString("000-") : "")
  81. + (item.IndexNumber != null ? item.IndexNumber.Value.ToString("0000 - ") : "") + item.Name;
  82. }
  83. else if (item is Season)
  84. {
  85. //sort seasons by season number - numerically
  86. item.SortName = item.IndexNumber != null ? item.IndexNumber.Value.ToString("0000") : item.Name;
  87. }
  88. else if (item is Audio)
  89. {
  90. //sort tracks by production year and index no so they will sort in order if in a multi-album list
  91. item.SortName = (item.ProductionYear != null ? item.ProductionYear.Value.ToString("000-") : "")
  92. + (item.IndexNumber != null ? item.IndexNumber.Value.ToString("0000 - ") : "") + item.Name;
  93. }
  94. else if (item is MusicAlbum)
  95. {
  96. //sort albums by year
  97. item.SortName = item.ProductionYear != null ? item.ProductionYear.Value.ToString("0000") : item.Name;
  98. }
  99. else
  100. {
  101. if (item.Name == null) return false; //some items may not have name filled in properly
  102. var sortable = item.Name.Trim().ToLower();
  103. sortable = ConfigurationManager.Configuration.SortRemoveCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), string.Empty));
  104. sortable = ConfigurationManager.Configuration.SortReplaceCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), " "));
  105. foreach (var search in ConfigurationManager.Configuration.SortRemoveWords)
  106. {
  107. cancellationToken.ThrowIfCancellationRequested();
  108. var searchLower = search.ToLower();
  109. // Remove from beginning if a space follows
  110. if (sortable.StartsWith(searchLower + " "))
  111. {
  112. sortable = sortable.Remove(0, searchLower.Length + 1);
  113. }
  114. // Remove from middle if surrounded by spaces
  115. sortable = sortable.Replace(" " + searchLower + " ", " ");
  116. // Remove from end if followed by a space
  117. if (sortable.EndsWith(" " + searchLower))
  118. {
  119. sortable = sortable.Remove(sortable.Length - (searchLower.Length + 1));
  120. }
  121. }
  122. item.SortName = sortable;
  123. }
  124. return true;
  125. }
  126. }
  127. }