SearchService.cs 13 KB

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