2
0

SearchService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. var program = item as LiveTvProgram;
  201. if (program != null)
  202. {
  203. result.StartDate = program.StartDate;
  204. }
  205. var hasSeries = item as IHasSeries;
  206. if (hasSeries != null)
  207. {
  208. result.Series = hasSeries.SeriesName;
  209. }
  210. var series = item as Series;
  211. if (series != null)
  212. {
  213. if (series.Status.HasValue)
  214. {
  215. result.Status = series.Status.Value.ToString();
  216. }
  217. }
  218. var album = item as MusicAlbum;
  219. if (album != null)
  220. {
  221. result.Artists = album.Artists;
  222. result.AlbumArtist = album.AlbumArtist;
  223. }
  224. var song = item as Audio;
  225. if (song != null)
  226. {
  227. result.AlbumArtist = song.AlbumArtists.FirstOrDefault();
  228. result.Artists = song.Artists;
  229. album = song.AlbumEntity;
  230. if (album != null)
  231. {
  232. result.Album = album.Name;
  233. result.AlbumId = album.Id;
  234. }
  235. else
  236. {
  237. result.Album = song.Album;
  238. }
  239. }
  240. if (!item.ChannelId.Equals(Guid.Empty))
  241. {
  242. var channel = _libraryManager.GetItemById(item.ChannelId);
  243. result.ChannelName = channel == null ? null : channel.Name;
  244. }
  245. return result;
  246. }
  247. private void SetThumbImageInfo(SearchHint hint, BaseItem item)
  248. {
  249. var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
  250. if (itemWithImage == null)
  251. {
  252. if (item is Episode)
  253. {
  254. itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
  255. }
  256. }
  257. if (itemWithImage == null)
  258. {
  259. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Thumb);
  260. }
  261. if (itemWithImage != null)
  262. {
  263. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Thumb);
  264. if (tag != null)
  265. {
  266. hint.ThumbImageTag = tag;
  267. hint.ThumbImageItemId = itemWithImage.Id.ToString("N", CultureInfo.InvariantCulture);
  268. }
  269. }
  270. }
  271. private void SetBackdropImageInfo(SearchHint hint, BaseItem item)
  272. {
  273. var itemWithImage = item.HasImage(ImageType.Backdrop) ? item : null;
  274. if (itemWithImage == null)
  275. {
  276. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Backdrop);
  277. }
  278. if (itemWithImage != null)
  279. {
  280. var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Backdrop);
  281. if (tag != null)
  282. {
  283. hint.BackdropImageTag = tag;
  284. hint.BackdropImageItemId = itemWithImage.Id.ToString("N", CultureInfo.InvariantCulture);
  285. }
  286. }
  287. }
  288. private T GetParentWithImage<T>(BaseItem item, ImageType type)
  289. where T : BaseItem
  290. {
  291. return item.GetParents().OfType<T>().FirstOrDefault(i => i.HasImage(type));
  292. }
  293. }
  294. }