SearchService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.Controller.Net;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Search;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Model.Services;
  13. namespace MediaBrowser.Api
  14. {
  15. /// <summary>
  16. /// Class GetSearchHints
  17. /// </summary>
  18. [Route("/Search/Hints", "GET", Summary = "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. [ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  56. public string IncludeItemTypes { get; set; }
  57. public string ParentId { get; set; }
  58. public GetSearchHints()
  59. {
  60. IncludeArtists = true;
  61. IncludeGenres = true;
  62. IncludeMedia = true;
  63. IncludePeople = true;
  64. IncludeStudios = true;
  65. }
  66. }
  67. /// <summary>
  68. /// Class SearchService
  69. /// </summary>
  70. [Authenticated]
  71. public class SearchService : BaseApiService
  72. {
  73. /// <summary>
  74. /// The _search engine
  75. /// </summary>
  76. private readonly ISearchEngine _searchEngine;
  77. private readonly ILibraryManager _libraryManager;
  78. private readonly IDtoService _dtoService;
  79. private readonly IImageProcessor _imageProcessor;
  80. /// <summary>
  81. /// Initializes a new instance of the <see cref="SearchService" /> class.
  82. /// </summary>
  83. /// <param name="searchEngine">The search engine.</param>
  84. /// <param name="libraryManager">The library manager.</param>
  85. /// <param name="dtoService">The dto service.</param>
  86. /// <param name="imageProcessor">The image processor.</param>
  87. public SearchService(ISearchEngine searchEngine, ILibraryManager libraryManager, IDtoService dtoService, IImageProcessor imageProcessor)
  88. {
  89. _searchEngine = searchEngine;
  90. _libraryManager = libraryManager;
  91. _dtoService = dtoService;
  92. _imageProcessor = imageProcessor;
  93. }
  94. /// <summary>
  95. /// Gets the specified request.
  96. /// </summary>
  97. /// <param name="request">The request.</param>
  98. /// <returns>System.Object.</returns>
  99. public async Task<object> Get(GetSearchHints request)
  100. {
  101. var result = await GetSearchHintsAsync(request).ConfigureAwait(false);
  102. return ToOptimizedSerializedResultUsingCache(result);
  103. }
  104. /// <summary>
  105. /// Gets the search hints async.
  106. /// </summary>
  107. /// <param name="request">The request.</param>
  108. /// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
  109. private async Task<SearchHintResult> GetSearchHintsAsync(GetSearchHints request)
  110. {
  111. var result = await _searchEngine.GetSearchHints(new SearchQuery
  112. {
  113. Limit = request.Limit,
  114. SearchTerm = request.SearchTerm,
  115. IncludeArtists = request.IncludeArtists,
  116. IncludeGenres = request.IncludeGenres,
  117. IncludeMedia = request.IncludeMedia,
  118. IncludePeople = request.IncludePeople,
  119. IncludeStudios = request.IncludeStudios,
  120. StartIndex = request.StartIndex,
  121. UserId = request.UserId,
  122. IncludeItemTypes = (request.IncludeItemTypes ?? string.Empty).Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToArray(),
  123. ParentId = request.ParentId
  124. }).ConfigureAwait(false);
  125. return new SearchHintResult
  126. {
  127. TotalRecordCount = result.TotalRecordCount,
  128. SearchHints = result.Items.Select(GetSearchHintResult).ToArray()
  129. };
  130. }
  131. /// <summary>
  132. /// Gets the search hint result.
  133. /// </summary>
  134. /// <param name="hintInfo">The hint info.</param>
  135. /// <returns>SearchHintResult.</returns>
  136. private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
  137. {
  138. var item = hintInfo.Item;
  139. var result = new SearchHint
  140. {
  141. Name = item.Name,
  142. IndexNumber = item.IndexNumber,
  143. ParentIndexNumber = item.ParentIndexNumber,
  144. ItemId = _dtoService.GetDtoId(item),
  145. Type = item.GetClientTypeName(),
  146. MediaType = item.MediaType,
  147. MatchedTerm = hintInfo.MatchedTerm,
  148. DisplayMediaType = item.DisplayMediaType,
  149. RunTimeTicks = item.RunTimeTicks,
  150. ProductionYear = item.ProductionYear
  151. };
  152. result.ChannelId = item.ChannelId;
  153. var primaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary);
  154. if (primaryImageTag != null)
  155. {
  156. result.PrimaryImageTag = primaryImageTag;
  157. result.PrimaryImageAspectRatio = _dtoService.GetPrimaryImageAspectRatio(item);
  158. }
  159. SetThumbImageInfo(result, item);
  160. SetBackdropImageInfo(result, item);
  161. var hasSeries = item as IHasSeries;
  162. if (hasSeries != null)
  163. {
  164. result.Series = hasSeries.SeriesName;
  165. }
  166. var album = item as MusicAlbum;
  167. if (album != null)
  168. {
  169. result.Artists = album.Artists.ToArray();
  170. result.AlbumArtist = album.AlbumArtist;
  171. }
  172. var song = item as Audio;
  173. if (song != null)
  174. {
  175. result.Album = song.Album;
  176. result.AlbumArtist = song.AlbumArtists.FirstOrDefault();
  177. result.Artists = song.Artists.ToArray();
  178. }
  179. if (!string.IsNullOrWhiteSpace(item.ChannelId))
  180. {
  181. var channel = _libraryManager.GetItemById(item.ChannelId);
  182. result.ChannelName = channel == null ? null : channel.Name;
  183. }
  184. return result;
  185. }
  186. private void SetThumbImageInfo(SearchHint hint, BaseItem item)
  187. {
  188. var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
  189. if (itemWithImage == null)
  190. {
  191. if (item is Episode)
  192. {
  193. itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
  194. }
  195. }
  196. if (itemWithImage == null)
  197. {
  198. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Thumb);
  199. }
  200. if (itemWithImage != null)
  201. {
  202. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Thumb);
  203. if (tag != null)
  204. {
  205. hint.ThumbImageTag = tag;
  206. hint.ThumbImageItemId = itemWithImage.Id.ToString("N");
  207. }
  208. }
  209. }
  210. private void SetBackdropImageInfo(SearchHint hint, BaseItem item)
  211. {
  212. var itemWithImage = item.HasImage(ImageType.Backdrop) ? item : null;
  213. if (itemWithImage == null)
  214. {
  215. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Backdrop);
  216. }
  217. if (itemWithImage != null)
  218. {
  219. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Backdrop);
  220. if (tag != null)
  221. {
  222. hint.BackdropImageTag = tag;
  223. hint.BackdropImageItemId = itemWithImage.Id.ToString("N");
  224. }
  225. }
  226. }
  227. private T GetParentWithImage<T>(BaseItem item, ImageType type)
  228. where T : BaseItem
  229. {
  230. return item.GetParents().OfType<T>().FirstOrDefault(i => i.HasImage(type));
  231. }
  232. }
  233. }