SearchService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System;
  2. using System.Linq;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Dto;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Entities.Audio;
  7. using MediaBrowser.Controller.Entities.TV;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.LiveTv;
  10. using MediaBrowser.Controller.Net;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Search;
  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 Guid 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. [ApiMember(Name = "ExcludeItemTypes", 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)]
  59. public string ExcludeItemTypes { get; set; }
  60. [ApiMember(Name = "MediaTypes", 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)]
  61. public string MediaTypes { get; set; }
  62. public string ParentId { get; set; }
  63. [ApiMember(Name = "IsMovie", Description = "Optional filter for movies.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  64. public bool? IsMovie { get; set; }
  65. [ApiMember(Name = "IsSeries", Description = "Optional filter for movies.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  66. public bool? IsSeries { get; set; }
  67. [ApiMember(Name = "IsNews", Description = "Optional filter for news.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  68. public bool? IsNews { get; set; }
  69. [ApiMember(Name = "IsKids", Description = "Optional filter for kids.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  70. public bool? IsKids { get; set; }
  71. [ApiMember(Name = "IsSports", Description = "Optional filter for sports.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  72. public bool? IsSports { get; set; }
  73. public GetSearchHints()
  74. {
  75. IncludeArtists = true;
  76. IncludeGenres = true;
  77. IncludeMedia = true;
  78. IncludePeople = true;
  79. IncludeStudios = true;
  80. }
  81. }
  82. /// <summary>
  83. /// Class SearchService
  84. /// </summary>
  85. [Authenticated]
  86. public class SearchService : BaseApiService
  87. {
  88. /// <summary>
  89. /// The _search engine
  90. /// </summary>
  91. private readonly ISearchEngine _searchEngine;
  92. private readonly ILibraryManager _libraryManager;
  93. private readonly IDtoService _dtoService;
  94. private readonly IImageProcessor _imageProcessor;
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="SearchService" /> class.
  97. /// </summary>
  98. /// <param name="searchEngine">The search engine.</param>
  99. /// <param name="libraryManager">The library manager.</param>
  100. /// <param name="dtoService">The dto service.</param>
  101. /// <param name="imageProcessor">The image processor.</param>
  102. public SearchService(ISearchEngine searchEngine, ILibraryManager libraryManager, IDtoService dtoService, IImageProcessor imageProcessor)
  103. {
  104. _searchEngine = searchEngine;
  105. _libraryManager = libraryManager;
  106. _dtoService = dtoService;
  107. _imageProcessor = imageProcessor;
  108. }
  109. /// <summary>
  110. /// Gets the specified request.
  111. /// </summary>
  112. /// <param name="request">The request.</param>
  113. /// <returns>System.Object.</returns>
  114. public object Get(GetSearchHints request)
  115. {
  116. var result = GetSearchHintsAsync(request);
  117. return ToOptimizedResult(result);
  118. }
  119. /// <summary>
  120. /// Gets the search hints async.
  121. /// </summary>
  122. /// <param name="request">The request.</param>
  123. /// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
  124. private SearchHintResult GetSearchHintsAsync(GetSearchHints request)
  125. {
  126. var result = _searchEngine.GetSearchHints(new SearchQuery
  127. {
  128. Limit = request.Limit,
  129. SearchTerm = request.SearchTerm,
  130. IncludeArtists = request.IncludeArtists,
  131. IncludeGenres = request.IncludeGenres,
  132. IncludeMedia = request.IncludeMedia,
  133. IncludePeople = request.IncludePeople,
  134. IncludeStudios = request.IncludeStudios,
  135. StartIndex = request.StartIndex,
  136. UserId = request.UserId,
  137. IncludeItemTypes = ApiEntryPoint.Split(request.IncludeItemTypes, ',', true),
  138. ExcludeItemTypes = ApiEntryPoint.Split(request.ExcludeItemTypes, ',', true),
  139. MediaTypes = ApiEntryPoint.Split(request.MediaTypes, ',', true),
  140. ParentId = request.ParentId,
  141. IsKids = request.IsKids,
  142. IsMovie = request.IsMovie,
  143. IsNews = request.IsNews,
  144. IsSeries = request.IsSeries,
  145. IsSports = request.IsSports
  146. });
  147. return new SearchHintResult
  148. {
  149. TotalRecordCount = result.TotalRecordCount,
  150. SearchHints = result.Items.Select(GetSearchHintResult).ToArray()
  151. };
  152. }
  153. /// <summary>
  154. /// Gets the search hint result.
  155. /// </summary>
  156. /// <param name="hintInfo">The hint info.</param>
  157. /// <returns>SearchHintResult.</returns>
  158. private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
  159. {
  160. var item = hintInfo.Item;
  161. var result = new SearchHint
  162. {
  163. Name = item.Name,
  164. IndexNumber = item.IndexNumber,
  165. ParentIndexNumber = item.ParentIndexNumber,
  166. Id = item.Id,
  167. Type = item.GetClientTypeName(),
  168. MediaType = item.MediaType,
  169. MatchedTerm = hintInfo.MatchedTerm,
  170. RunTimeTicks = item.RunTimeTicks,
  171. ProductionYear = item.ProductionYear,
  172. ChannelId = item.ChannelId,
  173. EndDate = item.EndDate
  174. };
  175. // legacy
  176. result.ItemId = result.Id;
  177. if (item.IsFolder)
  178. {
  179. result.IsFolder = true;
  180. }
  181. var primaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary);
  182. if (primaryImageTag != null)
  183. {
  184. result.PrimaryImageTag = primaryImageTag;
  185. result.PrimaryImageAspectRatio = _dtoService.GetPrimaryImageAspectRatio(item);
  186. }
  187. SetThumbImageInfo(result, item);
  188. SetBackdropImageInfo(result, item);
  189. var program = item as LiveTvProgram;
  190. if (program != null)
  191. {
  192. result.StartDate = program.StartDate;
  193. }
  194. var hasSeries = item as IHasSeries;
  195. if (hasSeries != null)
  196. {
  197. result.Series = hasSeries.SeriesName;
  198. }
  199. var series = item as Series;
  200. if (series != null)
  201. {
  202. if (series.Status.HasValue)
  203. {
  204. result.Status = series.Status.Value.ToString();
  205. }
  206. }
  207. var album = item as MusicAlbum;
  208. if (album != null)
  209. {
  210. result.Artists = album.Artists;
  211. result.AlbumArtist = album.AlbumArtist;
  212. }
  213. var song = item as Audio;
  214. if (song != null)
  215. {
  216. result.AlbumArtist = song.AlbumArtists.FirstOrDefault();
  217. result.Artists = song.Artists;
  218. album = song.AlbumEntity;
  219. if (album != null)
  220. {
  221. result.Album = album.Name;
  222. result.AlbumId = album.Id;
  223. }
  224. else
  225. {
  226. result.Album = song.Album;
  227. }
  228. }
  229. if (!item.ChannelId.Equals(Guid.Empty))
  230. {
  231. var channel = _libraryManager.GetItemById(item.ChannelId);
  232. result.ChannelName = channel == null ? null : channel.Name;
  233. }
  234. return result;
  235. }
  236. private void SetThumbImageInfo(SearchHint hint, BaseItem item)
  237. {
  238. var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
  239. if (itemWithImage == null)
  240. {
  241. if (item is Episode)
  242. {
  243. itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
  244. }
  245. }
  246. if (itemWithImage == null)
  247. {
  248. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Thumb);
  249. }
  250. if (itemWithImage != null)
  251. {
  252. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Thumb);
  253. if (tag != null)
  254. {
  255. hint.ThumbImageTag = tag;
  256. hint.ThumbImageItemId = itemWithImage.Id.ToString("N");
  257. }
  258. }
  259. }
  260. private void SetBackdropImageInfo(SearchHint hint, BaseItem item)
  261. {
  262. var itemWithImage = item.HasImage(ImageType.Backdrop) ? item : null;
  263. if (itemWithImage == null)
  264. {
  265. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Backdrop);
  266. }
  267. if (itemWithImage != null)
  268. {
  269. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Backdrop);
  270. if (tag != null)
  271. {
  272. hint.BackdropImageTag = tag;
  273. hint.BackdropImageItemId = itemWithImage.Id.ToString("N");
  274. }
  275. }
  276. }
  277. private T GetParentWithImage<T>(BaseItem item, ImageType type)
  278. where T : BaseItem
  279. {
  280. return item.GetParents().OfType<T>().FirstOrDefault(i => i.HasImage(type));
  281. }
  282. }
  283. }