SearchService.cs 13 KB

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