2
0

SortNameProvider.cs 5.3 KB

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