2
0

SearchService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.Model.Entities;
  8. using MediaBrowser.Model.Search;
  9. using ServiceStack;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Api
  13. {
  14. /// <summary>
  15. /// Class GetSearchHints
  16. /// </summary>
  17. [Route("/Search/Hints", "GET")]
  18. [Api(Description = "Gets search hints based on a search term")]
  19. public class GetSearchHints : IReturn<SearchHintResult>
  20. {
  21. /// <summary>
  22. /// Skips over a given number of items within the results. Use for paging.
  23. /// </summary>
  24. /// <value>The start index.</value>
  25. [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")]
  26. public int? StartIndex { get; set; }
  27. /// <summary>
  28. /// The maximum number of items to return
  29. /// </summary>
  30. /// <value>The limit.</value>
  31. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  32. public int? Limit { get; set; }
  33. /// <summary>
  34. /// Gets or sets the user id.
  35. /// </summary>
  36. /// <value>The user id.</value>
  37. [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")]
  38. public string UserId { get; set; }
  39. /// <summary>
  40. /// Search characters used to find items
  41. /// </summary>
  42. /// <value>The index by.</value>
  43. [ApiMember(Name = "SearchTerm", Description = "The search term to filter on", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  44. public string SearchTerm { get; set; }
  45. [ApiMember(Name = "IncludePeople", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  46. public bool IncludePeople { get; set; }
  47. [ApiMember(Name = "IncludeMedia", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  48. public bool IncludeMedia { get; set; }
  49. [ApiMember(Name = "IncludeGenres", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  50. public bool IncludeGenres { get; set; }
  51. [ApiMember(Name = "IncludeStudios", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  52. public bool IncludeStudios { get; set; }
  53. [ApiMember(Name = "IncludeArtists", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  54. public bool IncludeArtists { get; set; }
  55. public GetSearchHints()
  56. {
  57. IncludeArtists = true;
  58. IncludeGenres = true;
  59. IncludeMedia = true;
  60. IncludePeople = true;
  61. IncludeStudios = true;
  62. }
  63. }
  64. /// <summary>
  65. /// Class SearchService
  66. /// </summary>
  67. public class SearchService : BaseApiService
  68. {
  69. /// <summary>
  70. /// The _search engine
  71. /// </summary>
  72. private readonly ISearchEngine _searchEngine;
  73. private readonly ILibraryManager _libraryManager;
  74. private readonly IDtoService _dtoService;
  75. private readonly IImageProcessor _imageProcessor;
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="SearchService" /> class.
  78. /// </summary>
  79. /// <param name="searchEngine">The search engine.</param>
  80. /// <param name="libraryManager">The library manager.</param>
  81. /// <param name="dtoService">The dto service.</param>
  82. /// <param name="imageProcessor">The image processor.</param>
  83. public SearchService(ISearchEngine searchEngine, ILibraryManager libraryManager, IDtoService dtoService, IImageProcessor imageProcessor)
  84. {
  85. _searchEngine = searchEngine;
  86. _libraryManager = libraryManager;
  87. _dtoService = dtoService;
  88. _imageProcessor = imageProcessor;
  89. }
  90. /// <summary>
  91. /// Gets the specified request.
  92. /// </summary>
  93. /// <param name="request">The request.</param>
  94. /// <returns>System.Object.</returns>
  95. public object Get(GetSearchHints request)
  96. {
  97. var result = GetSearchHintsAsync(request).Result;
  98. return ToOptimizedResult(result);
  99. }
  100. /// <summary>
  101. /// Gets the search hints async.
  102. /// </summary>
  103. /// <param name="request">The request.</param>
  104. /// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
  105. private async Task<SearchHintResult> GetSearchHintsAsync(GetSearchHints request)
  106. {
  107. var result = await _searchEngine.GetSearchHints(new SearchQuery
  108. {
  109. Limit = request.Limit,
  110. SearchTerm = request.SearchTerm,
  111. IncludeArtists = request.IncludeArtists,
  112. IncludeGenres = request.IncludeGenres,
  113. IncludeMedia = request.IncludeMedia,
  114. IncludePeople = request.IncludePeople,
  115. IncludeStudios = request.IncludeStudios,
  116. StartIndex = request.StartIndex,
  117. UserId = request.UserId
  118. }).ConfigureAwait(false);
  119. return new SearchHintResult
  120. {
  121. TotalRecordCount = result.TotalRecordCount,
  122. SearchHints = result.Items.Select(GetSearchHintResult).ToArray()
  123. };
  124. }
  125. /// <summary>
  126. /// Gets the search hint result.
  127. /// </summary>
  128. /// <param name="hintInfo">The hint info.</param>
  129. /// <returns>SearchHintResult.</returns>
  130. private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
  131. {
  132. var item = hintInfo.Item;
  133. var result = new SearchHint
  134. {
  135. Name = item.Name,
  136. IndexNumber = item.IndexNumber,
  137. ParentIndexNumber = item.ParentIndexNumber,
  138. ItemId = _dtoService.GetDtoId(item),
  139. Type = item.GetClientTypeName(),
  140. MediaType = item.MediaType,
  141. MatchedTerm = hintInfo.MatchedTerm,
  142. DisplayMediaType = item.DisplayMediaType,
  143. RunTimeTicks = item.RunTimeTicks,
  144. ProductionYear = item.ProductionYear
  145. };
  146. if (item.HasImage(ImageType.Primary))
  147. {
  148. result.PrimaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary, item.GetImagePath(ImageType.Primary));
  149. }
  150. SetThumbImageInfo(result, item);
  151. SetBackdropImageInfo(result, item);
  152. var episode = item as Episode;
  153. if (episode != null)
  154. {
  155. result.Series = episode.Series.Name;
  156. }
  157. var season = item as Season;
  158. if (season != null)
  159. {
  160. result.Series = season.Series.Name;
  161. result.EpisodeCount = season.GetRecursiveChildren(i => i is Episode).Count;
  162. }
  163. var series = item as Series;
  164. if (series != null)
  165. {
  166. result.EpisodeCount = series.GetRecursiveChildren(i => i is Episode).Count;
  167. }
  168. var album = item as MusicAlbum;
  169. if (album != null)
  170. {
  171. var songs = album.GetRecursiveChildren().OfType<Audio>().ToList();
  172. result.SongCount = songs.Count;
  173. result.Artists = _libraryManager.GetAllArtists(songs)
  174. .ToArray();
  175. result.AlbumArtist = songs.Select(i => i.AlbumArtist).FirstOrDefault(i => !string.IsNullOrEmpty(i));
  176. }
  177. var song = item as Audio;
  178. if (song != null)
  179. {
  180. result.Album = song.Album;
  181. result.AlbumArtist = song.AlbumArtist;
  182. result.Artists = song.Artists.ToArray();
  183. }
  184. return result;
  185. }
  186. private void SetThumbImageInfo(SearchHint hint, BaseItem item)
  187. {
  188. var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
  189. if (itemWithImage == null)
  190. {
  191. if (item is Episode)
  192. {
  193. itemWithImage = GetParentWithImage<Series>(item, ImageType.Thumb);
  194. }
  195. }
  196. if (itemWithImage == null)
  197. {
  198. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Thumb);
  199. }
  200. if (itemWithImage != null)
  201. {
  202. hint.ThumbImageTag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Thumb, itemWithImage.GetImagePath(ImageType.Thumb));
  203. hint.ThumbImageItemId = itemWithImage.Id.ToString("N");
  204. }
  205. }
  206. private void SetBackdropImageInfo(SearchHint hint, BaseItem item)
  207. {
  208. var itemWithImage = item.HasImage(ImageType.Backdrop) ? item : null;
  209. if (itemWithImage == null)
  210. {
  211. itemWithImage = GetParentWithImage<BaseItem>(item, ImageType.Backdrop);
  212. }
  213. if (itemWithImage != null)
  214. {
  215. hint.BackdropImageTag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Backdrop, itemWithImage.GetImagePath(ImageType.Backdrop, 0));
  216. hint.BackdropImageItemId = itemWithImage.Id.ToString("N");
  217. }
  218. }
  219. private T GetParentWithImage<T>(BaseItem item, ImageType type)
  220. where T : BaseItem
  221. {
  222. return item.Parents.OfType<T>().FirstOrDefault(i => i.HasImage(type));
  223. }
  224. }
  225. }