SearchService.cs 12 KB

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