SearchService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.Linq;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Api
  13. {
  14. /// <summary>
  15. /// Class GetSearchHints
  16. /// </summary>
  17. [Route("/Search/Hints", "GET")]
  18. [Api(Description = "Gets search hints based on a search term")]
  19. public class GetSearchHints : IReturn<SearchHintResult>
  20. {
  21. /// <summary>
  22. /// Skips over a given number of items within the results. Use for paging.
  23. /// </summary>
  24. /// <value>The start index.</value>
  25. [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")]
  26. public int? StartIndex { get; set; }
  27. /// <summary>
  28. /// The maximum number of items to return
  29. /// </summary>
  30. /// <value>The limit.</value>
  31. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  32. public int? Limit { get; set; }
  33. /// <summary>
  34. /// Gets or sets the user id.
  35. /// </summary>
  36. /// <value>The user id.</value>
  37. [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")]
  38. public string UserId { get; set; }
  39. /// <summary>
  40. /// Search characters used to find items
  41. /// </summary>
  42. /// <value>The index by.</value>
  43. [ApiMember(Name = "SearchTerm", Description = "The search term to filter on", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  44. public string SearchTerm { get; set; }
  45. [ApiMember(Name = "IncludePeople", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  46. public bool IncludePeople { get; set; }
  47. [ApiMember(Name = "IncludeMedia", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  48. public bool IncludeMedia { get; set; }
  49. [ApiMember(Name = "IncludeGenres", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  50. public bool IncludeGenres { get; set; }
  51. [ApiMember(Name = "IncludeStudios", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  52. public bool IncludeStudios { get; set; }
  53. [ApiMember(Name = "IncludeArtists", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  54. public bool IncludeArtists { get; set; }
  55. public GetSearchHints()
  56. {
  57. IncludeArtists = true;
  58. IncludeGenres = true;
  59. IncludeMedia = true;
  60. IncludePeople = true;
  61. IncludeStudios = true;
  62. }
  63. }
  64. /// <summary>
  65. /// Class SearchService
  66. /// </summary>
  67. public class SearchService : BaseApiService
  68. {
  69. /// <summary>
  70. /// The _search engine
  71. /// </summary>
  72. private readonly ISearchEngine _searchEngine;
  73. private readonly ILibraryManager _libraryManager;
  74. private readonly IDtoService _dtoService;
  75. private readonly IImageProcessor _imageProcessor;
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="SearchService" /> class.
  78. /// </summary>
  79. /// <param name="searchEngine">The search engine.</param>
  80. /// <param name="libraryManager">The library manager.</param>
  81. /// <param name="dtoService">The dto service.</param>
  82. /// <param name="imageProcessor">The image processor.</param>
  83. public SearchService(ISearchEngine searchEngine, ILibraryManager libraryManager, IDtoService dtoService, IImageProcessor imageProcessor)
  84. {
  85. _searchEngine = searchEngine;
  86. _libraryManager = libraryManager;
  87. _dtoService = dtoService;
  88. _imageProcessor = imageProcessor;
  89. }
  90. /// <summary>
  91. /// Gets the specified request.
  92. /// </summary>
  93. /// <param name="request">The request.</param>
  94. /// <returns>System.Object.</returns>
  95. public object Get(GetSearchHints request)
  96. {
  97. var result = GetSearchHintsAsync(request).Result;
  98. return ToOptimizedSerializedResultUsingCache(result);
  99. }
  100. /// <summary>
  101. /// Gets the search hints async.
  102. /// </summary>
  103. /// <param name="request">The request.</param>
  104. /// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
  105. private async Task<SearchHintResult> GetSearchHintsAsync(GetSearchHints request)
  106. {
  107. var result = await _searchEngine.GetSearchHints(new SearchQuery
  108. {
  109. Limit = request.Limit,
  110. SearchTerm = request.SearchTerm,
  111. IncludeArtists = request.IncludeArtists,
  112. IncludeGenres = request.IncludeGenres,
  113. IncludeMedia = request.IncludeMedia,
  114. IncludePeople = request.IncludePeople,
  115. IncludeStudios = request.IncludeStudios,
  116. StartIndex = request.StartIndex,
  117. UserId = request.UserId
  118. }).ConfigureAwait(false);
  119. return new SearchHintResult
  120. {
  121. TotalRecordCount = result.TotalRecordCount,
  122. SearchHints = result.Items.Select(GetSearchHintResult).ToArray()
  123. };
  124. }
  125. /// <summary>
  126. /// Gets the search hint result.
  127. /// </summary>
  128. /// <param name="hintInfo">The hint info.</param>
  129. /// <returns>SearchHintResult.</returns>
  130. private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
  131. {
  132. var item = hintInfo.Item;
  133. var result = new SearchHint
  134. {
  135. Name = item.Name,
  136. IndexNumber = item.IndexNumber,
  137. ParentIndexNumber = item.ParentIndexNumber,
  138. ItemId = _dtoService.GetDtoId(item),
  139. Type = item.GetClientTypeName(),
  140. MediaType = item.MediaType,
  141. MatchedTerm = hintInfo.MatchedTerm,
  142. DisplayMediaType = item.DisplayMediaType,
  143. RunTimeTicks = item.RunTimeTicks,
  144. ProductionYear = item.ProductionYear
  145. };
  146. var primaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary);
  147. if (primaryImageTag.HasValue)
  148. {
  149. result.PrimaryImageTag = primaryImageTag.Value;
  150. }
  151. SetThumbImageInfo(result, item);
  152. SetBackdropImageInfo(result, item);
  153. var episode = item as Episode;
  154. if (episode != null)
  155. {
  156. result.Series = episode.Series.Name;
  157. }
  158. var season = item as Season;
  159. if (season != null)
  160. {
  161. result.Series = season.Series.Name;
  162. result.EpisodeCount = season.GetRecursiveChildren(i => i is Episode).Count;
  163. }
  164. var series = item as Series;
  165. if (series != null)
  166. {
  167. result.EpisodeCount = series.GetRecursiveChildren(i => i is Episode).Count;
  168. }
  169. var album = item as MusicAlbum;
  170. if (album != null)
  171. {
  172. var songs = album.GetRecursiveChildren().OfType<Audio>().ToList();
  173. result.SongCount = songs.Count;
  174. result.Artists = _libraryManager.GetAllArtists(songs)
  175. .ToArray();
  176. result.AlbumArtist = songs.Select(i => i.AlbumArtist).FirstOrDefault(i => !string.IsNullOrEmpty(i));
  177. }
  178. var song = item as Audio;
  179. if (song != null)
  180. {
  181. result.Album = song.Album;
  182. result.AlbumArtist = song.AlbumArtist;
  183. result.Artists = song.Artists.ToArray();
  184. }
  185. return result;
  186. }
  187. private void SetThumbImageInfo(SearchHint hint, BaseItem item)
  188. {
  189. var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
  190. if (itemWithImage == null)
  191. {
  192. if (item is Episode)
  193. {
  194. itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
  195. }
  196. }
  197. if (itemWithImage == null)
  198. {
  199. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Thumb);
  200. }
  201. if (itemWithImage != null)
  202. {
  203. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Thumb);
  204. if (tag.HasValue)
  205. {
  206. hint.ThumbImageTag = tag.Value;
  207. hint.ThumbImageItemId = itemWithImage.Id.ToString("N");
  208. }
  209. }
  210. }
  211. private void SetBackdropImageInfo(SearchHint hint, BaseItem item)
  212. {
  213. var itemWithImage = item.HasImage(ImageType.Backdrop) ? item : null;
  214. if (itemWithImage == null)
  215. {
  216. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Backdrop);
  217. }
  218. if (itemWithImage != null)
  219. {
  220. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Backdrop);
  221. if (tag.HasValue)
  222. {
  223. hint.BackdropImageTag = tag.Value;
  224. hint.BackdropImageItemId = itemWithImage.Id.ToString("N");
  225. }
  226. }
  227. }
  228. private T GetParentWithImage<T>(BaseItem item, ImageType type)
  229. where T : BaseItem
  230. {
  231. return item.Parents.OfType<T>().FirstOrDefault(i => i.HasImage(type));
  232. }
  233. }
  234. }