SearchService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System.Collections;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Dto;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Entities.TV;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Search;
  10. using ServiceStack.ServiceHost;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Threading.Tasks;
  14. using System.Linq;
  15. namespace MediaBrowser.Api
  16. {
  17. /// <summary>
  18. /// Class GetSearchHints
  19. /// </summary>
  20. [Route("/Search/Hints", "GET")]
  21. [Api(Description = "Gets search hints based on a search term")]
  22. public class GetSearchHints : IReturn<List<SearchHintResult>>
  23. {
  24. /// <summary>
  25. /// Skips over a given number of items within the results. Use for paging.
  26. /// </summary>
  27. /// <value>The start index.</value>
  28. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  29. public int? StartIndex { get; set; }
  30. /// <summary>
  31. /// The maximum number of items to return
  32. /// </summary>
  33. /// <value>The limit.</value>
  34. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  35. public int? Limit { get; set; }
  36. /// <summary>
  37. /// Gets or sets the user id.
  38. /// </summary>
  39. /// <value>The user id.</value>
  40. [ApiMember(Name = "UserId", Description = "Optional. Supply a user id to search within a user's library or omit to search all.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  41. public Guid? UserId { get; set; }
  42. /// <summary>
  43. /// Search characters used to find items
  44. /// </summary>
  45. /// <value>The index by.</value>
  46. [ApiMember(Name = "SearchTerm", Description = "The search term to filter on", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  47. public string SearchTerm { get; set; }
  48. }
  49. /// <summary>
  50. /// Class SearchService
  51. /// </summary>
  52. public class SearchService : BaseApiService
  53. {
  54. /// <summary>
  55. /// The _user manager
  56. /// </summary>
  57. private readonly IUserManager _userManager;
  58. /// <summary>
  59. /// The _search engine
  60. /// </summary>
  61. private readonly ILibrarySearchEngine _searchEngine;
  62. private readonly ILibraryManager _libraryManager;
  63. /// <summary>
  64. /// Initializes a new instance of the <see cref="SearchService" /> class.
  65. /// </summary>
  66. /// <param name="userManager">The user manager.</param>
  67. /// <param name="searchEngine">The search engine.</param>
  68. /// <param name="libraryManager">The library manager.</param>
  69. public SearchService(IUserManager userManager, ILibrarySearchEngine searchEngine, ILibraryManager libraryManager)
  70. {
  71. _userManager = userManager;
  72. _searchEngine = searchEngine;
  73. _libraryManager = libraryManager;
  74. }
  75. /// <summary>
  76. /// Gets the specified request.
  77. /// </summary>
  78. /// <param name="request">The request.</param>
  79. /// <returns>System.Object.</returns>
  80. public object Get(GetSearchHints request)
  81. {
  82. var result = GetSearchHintsAsync(request).Result;
  83. return ToOptimizedResult(result);
  84. }
  85. /// <summary>
  86. /// Gets the search hints async.
  87. /// </summary>
  88. /// <param name="request">The request.</param>
  89. /// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
  90. private async Task<IEnumerable<SearchHintResult>> GetSearchHintsAsync(GetSearchHints request)
  91. {
  92. IEnumerable<BaseItem> inputItems;
  93. if (request.UserId.HasValue)
  94. {
  95. var user = _userManager.GetUserById(request.UserId.Value);
  96. inputItems = user.RootFolder.GetRecursiveChildren(user);
  97. }
  98. else
  99. {
  100. inputItems = _libraryManager.RootFolder.RecursiveChildren;
  101. }
  102. var results = await _searchEngine.GetSearchHints(inputItems, request.SearchTerm).ConfigureAwait(false);
  103. if (request.StartIndex.HasValue)
  104. {
  105. results = results.Skip(request.StartIndex.Value);
  106. }
  107. if (request.Limit.HasValue)
  108. {
  109. results = results.Take(request.Limit.Value);
  110. }
  111. return results.Select(GetSearchHintResult);
  112. }
  113. /// <summary>
  114. /// Gets the search hint result.
  115. /// </summary>
  116. /// <param name="item">The item.</param>
  117. /// <returns>SearchHintResult.</returns>
  118. private SearchHintResult GetSearchHintResult(BaseItem item)
  119. {
  120. var result = new SearchHintResult
  121. {
  122. Name = item.Name,
  123. IndexNumber = item.IndexNumber,
  124. ParentIndexNumber = item.ParentIndexNumber,
  125. ItemId = DtoBuilder.GetClientItemId(item),
  126. Type = item.GetType().Name,
  127. MediaType = item.MediaType
  128. };
  129. if (item.HasImage(ImageType.Primary))
  130. {
  131. result.PrimaryImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Primary, item.GetImage(ImageType.Primary));
  132. }
  133. var episode = item as Episode;
  134. if (episode != null)
  135. {
  136. result.Series = episode.Series.Name;
  137. }
  138. var season = item as Season;
  139. if (season != null)
  140. {
  141. result.Series = season.Series.Name;
  142. }
  143. var album = item as MusicAlbum;
  144. if (album != null)
  145. {
  146. var songs = album.Children.OfType<Audio>().ToList();
  147. result.Artists = songs
  148. .Select(i => i.Artist)
  149. .Where(i => !string.IsNullOrEmpty(i))
  150. .Distinct(StringComparer.OrdinalIgnoreCase)
  151. .ToArray();
  152. result.AlbumArtist = songs.Select(i => i.AlbumArtist).FirstOrDefault(i => !string.IsNullOrEmpty(i));
  153. }
  154. var song = item as Audio;
  155. if (song != null)
  156. {
  157. result.Album = song.Album;
  158. result.AlbumArtist = song.AlbumArtist;
  159. result.Artists = !string.IsNullOrEmpty(song.Artist) ? new[] { song.Artist } : new string[] { };
  160. }
  161. return result;
  162. }
  163. }
  164. }