SortNameProvider.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /// <summary>
  48. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  49. /// </summary>
  50. /// <param name="item">The item.</param>
  51. /// <param name="force">if set to <c>true</c> [force].</param>
  52. /// <param name="cancellationToken">The cancellation token.</param>
  53. /// <returns>Task{System.Boolean}.</returns>
  54. protected override Task<bool> FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
  55. {
  56. return SetSortName(item, cancellationToken) ? TrueTaskResult : FalseTaskResult;
  57. }
  58. /// <summary>
  59. /// Sets the name of the sort.
  60. /// </summary>
  61. /// <param name="item">The item.</param>
  62. /// <param name="cancellationToken">The cancellation token.</param>
  63. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  64. protected bool SetSortName(BaseItem item, CancellationToken cancellationToken)
  65. {
  66. if (!string.IsNullOrWhiteSpace(item.SortName)) return false; //let the earlier provider win
  67. cancellationToken.ThrowIfCancellationRequested();
  68. if (item is Episode)
  69. {
  70. //special handling for TV episodes season and episode number
  71. item.SortName = (item.ParentIndexNumber != null ? item.ParentIndexNumber.Value.ToString("000-") : "")
  72. + (item.IndexNumber != null ? item.IndexNumber.Value.ToString("0000 - ") : "") + item.Name;
  73. }
  74. else if (item is Season)
  75. {
  76. //sort seasons by season number - numerically
  77. item.SortName = item.IndexNumber != null ? item.IndexNumber.Value.ToString("0000") : item.Name;
  78. }
  79. else if (item is Audio)
  80. {
  81. //sort tracks by production year and index no so they will sort in order if in a multi-album list
  82. item.SortName = (item.ProductionYear != null ? item.ProductionYear.Value.ToString("000-") : "")
  83. + (item.IndexNumber != null ? item.IndexNumber.Value.ToString("0000 - ") : "") + item.Name;
  84. }
  85. else if (item is MusicAlbum)
  86. {
  87. //sort albums by year
  88. item.SortName = item.ProductionYear != null ? item.ProductionYear.Value.ToString("0000") : item.Name;
  89. }
  90. else
  91. {
  92. if (item.Name == null) return false; //some items may not have name filled in properly
  93. var sortable = item.Name.Trim().ToLower();
  94. sortable = ConfigurationManager.Configuration.SortRemoveCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), string.Empty));
  95. sortable = ConfigurationManager.Configuration.SortReplaceCharacters.Aggregate(sortable, (current, search) => current.Replace(search.ToLower(), " "));
  96. foreach (var search in ConfigurationManager.Configuration.SortRemoveWords)
  97. {
  98. cancellationToken.ThrowIfCancellationRequested();
  99. var searchLower = search.ToLower();
  100. // Remove from beginning if a space follows
  101. if (sortable.StartsWith(searchLower + " "))
  102. {
  103. sortable = sortable.Remove(0, searchLower.Length + 1);
  104. }
  105. // Remove from middle if surrounded by spaces
  106. sortable = sortable.Replace(" " + searchLower + " ", " ");
  107. // Remove from end if followed by a space
  108. if (sortable.EndsWith(" " + searchLower))
  109. {
  110. sortable = sortable.Remove(sortable.Length - (searchLower.Length + 1));
  111. }
  112. }
  113. item.SortName = sortable;
  114. }
  115. return true;
  116. }
  117. }
  118. }