2
0

SortNameProvider.cs 5.4 KB

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