SortNameProvider.cs 5.4 KB

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