SearchService.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using MediaBrowser.Controller.Drawing;
  2. using MediaBrowser.Controller.Dto;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Entities.TV;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Search;
  9. using ServiceStack;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  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<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 ISearchEngine _searchEngine;
  62. private readonly ILibraryManager _libraryManager;
  63. private readonly IDtoService _dtoService;
  64. private readonly IImageProcessor _imageProcessor;
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="SearchService" /> class.
  67. /// </summary>
  68. /// <param name="userManager">The user manager.</param>
  69. /// <param name="searchEngine">The search engine.</param>
  70. /// <param name="libraryManager">The library manager.</param>
  71. public SearchService(IUserManager userManager, ISearchEngine searchEngine, ILibraryManager libraryManager, IDtoService dtoService, IImageProcessor imageProcessor)
  72. {
  73. _userManager = userManager;
  74. _searchEngine = searchEngine;
  75. _libraryManager = libraryManager;
  76. _dtoService = dtoService;
  77. _imageProcessor = imageProcessor;
  78. }
  79. /// <summary>
  80. /// Gets the specified request.
  81. /// </summary>
  82. /// <param name="request">The request.</param>
  83. /// <returns>System.Object.</returns>
  84. public object Get(GetSearchHints request)
  85. {
  86. var result = GetSearchHintsAsync(request).Result;
  87. return ToOptimizedResult(result);
  88. }
  89. /// <summary>
  90. /// Gets the search hints async.
  91. /// </summary>
  92. /// <param name="request">The request.</param>
  93. /// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
  94. private async Task<SearchHintResult> GetSearchHintsAsync(GetSearchHints request)
  95. {
  96. var inputItems = GetAllLibraryItems(request.UserId, _userManager, _libraryManager);
  97. var results = await _searchEngine.GetSearchHints(inputItems, request.SearchTerm).ConfigureAwait(false);
  98. var searchResultArray = results.ToList();
  99. IEnumerable<SearchHintInfo> returnResults = searchResultArray;
  100. if (request.StartIndex.HasValue)
  101. {
  102. returnResults = returnResults.Skip(request.StartIndex.Value);
  103. }
  104. if (request.Limit.HasValue)
  105. {
  106. returnResults = returnResults.Take(request.Limit.Value);
  107. }
  108. return new SearchHintResult
  109. {
  110. TotalRecordCount = searchResultArray.Count,
  111. SearchHints = returnResults.Select(GetSearchHintResult).ToArray()
  112. };
  113. }
  114. /// <summary>
  115. /// Gets the search hint result.
  116. /// </summary>
  117. /// <param name="hintInfo">The hint info.</param>
  118. /// <returns>SearchHintResult.</returns>
  119. private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
  120. {
  121. var item = hintInfo.Item;
  122. var result = new SearchHint
  123. {
  124. Name = item.Name,
  125. IndexNumber = item.IndexNumber,
  126. ParentIndexNumber = item.ParentIndexNumber,
  127. ItemId = _dtoService.GetDtoId(item),
  128. Type = item.GetClientTypeName(),
  129. MediaType = item.MediaType,
  130. MatchedTerm = hintInfo.MatchedTerm,
  131. DisplayMediaType = item.DisplayMediaType,
  132. RunTimeTicks = item.RunTimeTicks,
  133. ProductionYear = item.ProductionYear
  134. };
  135. if (item.HasImage(ImageType.Primary))
  136. {
  137. result.PrimaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary, item.GetImagePath(ImageType.Primary));
  138. }
  139. SetThumbImageInfo(result, item);
  140. SetBackdropImageInfo(result, item);
  141. var episode = item as Episode;
  142. if (episode != null)
  143. {
  144. result.Series = episode.Series.Name;
  145. }
  146. var season = item as Season;
  147. if (season != null)
  148. {
  149. result.Series = season.Series.Name;
  150. result.EpisodeCount = season.GetRecursiveChildren(i => i is Episode).Count;
  151. }
  152. var series = item as Series;
  153. if (series != null)
  154. {
  155. result.EpisodeCount = series.GetRecursiveChildren(i => i is Episode).Count;
  156. }
  157. var album = item as MusicAlbum;
  158. if (album != null)
  159. {
  160. var songs = album.GetRecursiveChildren().OfType<Audio>().ToList();
  161. result.SongCount = songs.Count;
  162. result.Artists = _libraryManager.GetAllArtists(songs)
  163. .ToArray();
  164. result.AlbumArtist = songs.Select(i => i.AlbumArtist).FirstOrDefault(i => !string.IsNullOrEmpty(i));
  165. }
  166. var song = item as Audio;
  167. if (song != null)
  168. {
  169. result.Album = song.Album;
  170. result.AlbumArtist = song.AlbumArtist;
  171. result.Artists = song.Artists.ToArray();
  172. }
  173. return result;
  174. }
  175. private void SetThumbImageInfo(SearchHint hint, BaseItem item)
  176. {
  177. var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
  178. if (itemWithImage == null)
  179. {
  180. if (item is Episode)
  181. {
  182. itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
  183. }
  184. }
  185. if (itemWithImage == null)
  186. {
  187. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Thumb);
  188. }
  189. if (itemWithImage != null)
  190. {
  191. hint.ThumbImageTag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Thumb, itemWithImage.GetImagePath(ImageType.Thumb));
  192. hint.ThumbImageItemId = itemWithImage.Id.ToString("N");
  193. }
  194. }
  195. private void SetBackdropImageInfo(SearchHint hint, BaseItem item)
  196. {
  197. var itemWithImage = item.HasImage(ImageType.Backdrop) ? item : null;
  198. if (itemWithImage == null)
  199. {
  200. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Backdrop);
  201. }
  202. if (itemWithImage != null)
  203. {
  204. hint.BackdropImageTag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Backdrop, itemWithImage.GetImagePath(ImageType.Backdrop, 0));
  205. hint.BackdropImageItemId = itemWithImage.Id.ToString("N");
  206. }
  207. }
  208. private T GetParentWithImage<T>(BaseItem item, ImageType type)
  209. where T : BaseItem
  210. {
  211. return item.Parents.OfType<T>().FirstOrDefault(i => i.HasImage(type));
  212. }
  213. }
  214. }