2014-03-23 20:36:25 +01:00
using MediaBrowser.Controller.Drawing ;
2013-04-26 21:20:53 +02:00
using MediaBrowser.Controller.Dto ;
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Entities.Audio ;
using MediaBrowser.Controller.Entities.TV ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Search ;
2013-12-07 16:52:38 +01:00
using ServiceStack ;
2014-03-23 20:36:25 +01:00
using System ;
2013-04-26 21:20:53 +02:00
using System.Linq ;
2013-05-07 15:06:01 +02:00
using System.Threading.Tasks ;
2013-04-26 21:20:53 +02:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetSearchHints
/// </summary>
2014-03-23 20:36:25 +01:00
[Route("/Search/Hints", "GET", Summary = "Gets search hints based on a search term")]
2013-04-27 15:05:33 +02:00
public class GetSearchHints : IReturn < SearchHintResult >
2013-04-26 21:20:53 +02:00
{
/// <summary>
/// Skips over a given number of items within the results. Use for paging.
/// </summary>
/// <value>The start index.</value>
[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")]
public int? StartIndex { get ; set ; }
/// <summary>
/// The maximum number of items to return
/// </summary>
/// <value>The limit.</value>
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? Limit { get ; set ; }
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[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")]
2014-01-07 21:12:39 +01:00
public string UserId { get ; set ; }
2013-04-26 21:20:53 +02:00
/// <summary>
/// Search characters used to find items
/// </summary>
/// <value>The index by.</value>
[ApiMember(Name = "SearchTerm", Description = "The search term to filter on", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string SearchTerm { get ; set ; }
2014-01-07 21:12:39 +01:00
[ApiMember(Name = "IncludePeople", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool IncludePeople { get ; set ; }
[ApiMember(Name = "IncludeMedia", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool IncludeMedia { get ; set ; }
[ApiMember(Name = "IncludeGenres", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool IncludeGenres { get ; set ; }
[ApiMember(Name = "IncludeStudios", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool IncludeStudios { get ; set ; }
[ApiMember(Name = "IncludeArtists", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool IncludeArtists { get ; set ; }
2014-03-07 16:53:23 +01:00
[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)]
public string IncludeItemTypes { get ; set ; }
2014-03-23 20:36:25 +01:00
2014-01-07 21:12:39 +01:00
public GetSearchHints ( )
{
IncludeArtists = true ;
IncludeGenres = true ;
IncludeMedia = true ;
IncludePeople = true ;
IncludeStudios = true ;
}
2013-04-26 21:20:53 +02:00
}
/// <summary>
/// Class SearchService
/// </summary>
public class SearchService : BaseApiService
{
/// <summary>
/// The _search engine
/// </summary>
2014-01-04 05:53:49 +01:00
private readonly ISearchEngine _searchEngine ;
2013-04-26 21:20:53 +02:00
private readonly ILibraryManager _libraryManager ;
2013-09-04 19:02:19 +02:00
private readonly IDtoService _dtoService ;
2013-09-18 20:49:06 +02:00
private readonly IImageProcessor _imageProcessor ;
2013-04-26 21:20:53 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="SearchService" /> class.
/// </summary>
/// <param name="searchEngine">The search engine.</param>
/// <param name="libraryManager">The library manager.</param>
2014-01-07 21:12:39 +01:00
/// <param name="dtoService">The dto service.</param>
/// <param name="imageProcessor">The image processor.</param>
public SearchService ( ISearchEngine searchEngine , ILibraryManager libraryManager , IDtoService dtoService , IImageProcessor imageProcessor )
2013-04-26 21:20:53 +02:00
{
_searchEngine = searchEngine ;
_libraryManager = libraryManager ;
2013-09-04 19:02:19 +02:00
_dtoService = dtoService ;
2013-09-18 20:49:06 +02:00
_imageProcessor = imageProcessor ;
2013-04-26 21:20:53 +02:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetSearchHints request )
{
var result = GetSearchHintsAsync ( request ) . Result ;
2014-02-04 05:04:19 +01:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-04-26 21:20:53 +02:00
}
/// <summary>
/// Gets the search hints async.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
2013-04-27 15:05:33 +02:00
private async Task < SearchHintResult > GetSearchHintsAsync ( GetSearchHints request )
2013-04-26 21:20:53 +02:00
{
2014-01-07 21:12:39 +01:00
var result = await _searchEngine . GetSearchHints ( new SearchQuery
2013-04-26 21:20:53 +02:00
{
2014-01-07 21:12:39 +01:00
Limit = request . Limit ,
SearchTerm = request . SearchTerm ,
IncludeArtists = request . IncludeArtists ,
IncludeGenres = request . IncludeGenres ,
IncludeMedia = request . IncludeMedia ,
IncludePeople = request . IncludePeople ,
IncludeStudios = request . IncludeStudios ,
StartIndex = request . StartIndex ,
2014-03-07 16:53:23 +01:00
UserId = request . UserId ,
2014-03-07 19:48:55 +01:00
IncludeItemTypes = ( request . IncludeItemTypes ? ? string . Empty ) . Split ( ',' ) . Where ( i = > ! string . IsNullOrWhiteSpace ( i ) ) . ToArray ( )
2014-01-07 21:12:39 +01:00
} ) . ConfigureAwait ( false ) ;
2013-04-26 21:20:53 +02:00
2013-04-27 15:05:33 +02:00
return new SearchHintResult
{
2014-01-07 21:12:39 +01:00
TotalRecordCount = result . TotalRecordCount ,
2013-04-27 15:05:33 +02:00
2014-01-07 21:12:39 +01:00
SearchHints = result . Items . Select ( GetSearchHintResult ) . ToArray ( )
2013-04-27 15:05:33 +02:00
} ;
2013-04-26 21:20:53 +02:00
}
/// <summary>
/// Gets the search hint result.
/// </summary>
2013-04-27 15:05:33 +02:00
/// <param name="hintInfo">The hint info.</param>
2013-04-26 21:20:53 +02:00
/// <returns>SearchHintResult.</returns>
2013-04-27 15:05:33 +02:00
private SearchHint GetSearchHintResult ( SearchHintInfo hintInfo )
2013-04-26 21:20:53 +02:00
{
2013-04-27 15:05:33 +02:00
var item = hintInfo . Item ;
var result = new SearchHint
2013-04-26 21:20:53 +02:00
{
Name = item . Name ,
IndexNumber = item . IndexNumber ,
ParentIndexNumber = item . ParentIndexNumber ,
2013-09-04 19:02:19 +02:00
ItemId = _dtoService . GetDtoId ( item ) ,
2013-11-21 21:48:26 +01:00
Type = item . GetClientTypeName ( ) ,
2013-04-27 15:05:33 +02:00
MediaType = item . MediaType ,
MatchedTerm = hintInfo . MatchedTerm ,
DisplayMediaType = item . DisplayMediaType ,
2013-12-27 18:12:23 +01:00
RunTimeTicks = item . RunTimeTicks ,
ProductionYear = item . ProductionYear
2013-04-26 21:20:53 +02:00
} ;
2014-02-07 21:30:41 +01:00
var primaryImageTag = _imageProcessor . GetImageCacheTag ( item , ImageType . Primary ) ;
2014-05-08 22:09:53 +02:00
if ( primaryImageTag ! = null )
2013-04-26 21:20:53 +02:00
{
2014-05-08 22:09:53 +02:00
result . PrimaryImageTag = primaryImageTag ;
2013-04-26 21:20:53 +02:00
}
2014-01-05 07:08:22 +01:00
SetThumbImageInfo ( result , item ) ;
SetBackdropImageInfo ( result , item ) ;
2013-04-26 21:20:53 +02:00
var episode = item as Episode ;
if ( episode ! = null )
{
result . Series = episode . Series . Name ;
}
var season = item as Season ;
if ( season ! = null )
{
result . Series = season . Series . Name ;
2013-04-27 15:05:33 +02:00
2013-09-27 14:24:28 +02:00
result . EpisodeCount = season . GetRecursiveChildren ( i = > i is Episode ) . Count ;
2013-04-27 15:05:33 +02:00
}
var series = item as Series ;
if ( series ! = null )
{
2013-09-27 14:24:28 +02:00
result . EpisodeCount = series . GetRecursiveChildren ( i = > i is Episode ) . Count ;
2013-04-26 21:20:53 +02:00
}
var album = item as MusicAlbum ;
if ( album ! = null )
{
2013-09-27 14:24:28 +02:00
var songs = album . GetRecursiveChildren ( ) . OfType < Audio > ( ) . ToList ( ) ;
2013-04-26 21:20:53 +02:00
2013-04-27 15:05:33 +02:00
result . SongCount = songs . Count ;
2014-01-07 21:12:39 +01:00
2014-03-09 23:14:44 +01:00
result . Artists = songs . SelectMany ( i = > i . AllArtists )
. Distinct ( StringComparer . OrdinalIgnoreCase )
2013-04-26 21:20:53 +02:00
. ToArray ( ) ;
result . AlbumArtist = songs . Select ( i = > i . AlbumArtist ) . FirstOrDefault ( i = > ! string . IsNullOrEmpty ( i ) ) ;
}
var song = item as Audio ;
if ( song ! = null )
{
result . Album = song . Album ;
result . AlbumArtist = song . AlbumArtist ;
2013-09-05 21:00:50 +02:00
result . Artists = song . Artists . ToArray ( ) ;
2013-04-26 21:20:53 +02:00
}
return result ;
}
2014-01-05 07:08:22 +01:00
private void SetThumbImageInfo ( SearchHint hint , BaseItem item )
{
var itemWithImage = item . HasImage ( ImageType . Thumb ) ? item : null ;
if ( itemWithImage = = null )
{
if ( item is Episode )
{
itemWithImage = GetParentWithImage < Series > ( item , ImageType . Thumb ) ;
}
}
if ( itemWithImage = = null )
{
itemWithImage = GetParentWithImage < BaseItem > ( item , ImageType . Thumb ) ;
}
if ( itemWithImage ! = null )
{
2014-02-07 21:30:41 +01:00
var tag = _imageProcessor . GetImageCacheTag ( itemWithImage , ImageType . Thumb ) ;
2014-05-08 22:09:53 +02:00
if ( tag ! = null )
2014-02-07 21:30:41 +01:00
{
2014-05-08 22:09:53 +02:00
hint . ThumbImageTag = tag ;
2014-02-07 21:30:41 +01:00
hint . ThumbImageItemId = itemWithImage . Id . ToString ( "N" ) ;
}
2014-01-05 07:08:22 +01:00
}
}
private void SetBackdropImageInfo ( SearchHint hint , BaseItem item )
{
var itemWithImage = item . HasImage ( ImageType . Backdrop ) ? item : null ;
if ( itemWithImage = = null )
{
itemWithImage = GetParentWithImage < BaseItem > ( item , ImageType . Backdrop ) ;
}
if ( itemWithImage ! = null )
{
2014-02-07 21:30:41 +01:00
var tag = _imageProcessor . GetImageCacheTag ( itemWithImage , ImageType . Backdrop ) ;
2014-05-08 22:09:53 +02:00
if ( tag ! = null )
2014-02-07 21:30:41 +01:00
{
2014-05-08 22:09:53 +02:00
hint . BackdropImageTag = tag ;
2014-02-07 21:30:41 +01:00
hint . BackdropImageItemId = itemWithImage . Id . ToString ( "N" ) ;
}
2014-01-05 07:08:22 +01:00
}
}
private T GetParentWithImage < T > ( BaseItem item , ImageType type )
where T : BaseItem
{
return item . Parents . OfType < T > ( ) . FirstOrDefault ( i = > i . HasImage ( type ) ) ;
}
2013-04-26 21:20:53 +02:00
}
}