jellyfin-server/MediaBrowser.Api/SearchService.cs

210 lines
7.4 KiB
C#
Raw Normal View History

2013-05-07 13:06:01 +00:00
using MediaBrowser.Controller;
2013-09-18 18:49:06 +00:00
using MediaBrowser.Controller.Drawing;
2013-04-26 19:20:53 +00: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 15:52:38 +00:00
using ServiceStack;
2013-04-26 19:20:53 +00:00
using System;
2013-05-07 13:06:01 +00:00
using System.Collections;
2013-04-26 19:20:53 +00:00
using System.Collections.Generic;
using System.Linq;
2013-05-07 13:06:01 +00:00
using System.Threading.Tasks;
2013-04-26 19:20:53 +00:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetSearchHints
/// </summary>
[Route("/Search/Hints", "GET")]
[Api(Description = "Gets search hints based on a search term")]
2013-04-27 13:05:33 +00:00
public class GetSearchHints : IReturn<SearchHintResult>
2013-04-26 19:20:53 +00: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")]
public Guid? UserId { get; set; }
/// <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; }
}
/// <summary>
/// Class SearchService
/// </summary>
public class SearchService : BaseApiService
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager;
/// <summary>
/// The _search engine
/// </summary>
private readonly ISearchEngine _searchEngine;
2013-04-26 19:20:53 +00:00
private readonly ILibraryManager _libraryManager;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService;
2013-09-18 18:49:06 +00:00
private readonly IImageProcessor _imageProcessor;
2013-04-26 19:20:53 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="SearchService" /> class.
/// </summary>
/// <param name="userManager">The user manager.</param>
/// <param name="searchEngine">The search engine.</param>
/// <param name="libraryManager">The library manager.</param>
public SearchService(IUserManager userManager, ISearchEngine searchEngine, ILibraryManager libraryManager, IDtoService dtoService, IImageProcessor imageProcessor)
2013-04-26 19:20:53 +00:00
{
_userManager = userManager;
_searchEngine = searchEngine;
_libraryManager = libraryManager;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService;
2013-09-18 18:49:06 +00:00
_imageProcessor = imageProcessor;
2013-04-26 19:20:53 +00: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;
return ToOptimizedResult(result);
}
/// <summary>
/// Gets the search hints async.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
2013-04-27 13:05:33 +00:00
private async Task<SearchHintResult> GetSearchHintsAsync(GetSearchHints request)
2013-04-26 19:20:53 +00:00
{
2013-09-27 12:24:28 +00:00
var inputItems = GetAllLibraryItems(request.UserId, _userManager, _libraryManager);
2013-04-26 19:20:53 +00:00
var results = await _searchEngine.GetSearchHints(inputItems, request.SearchTerm).ConfigureAwait(false);
var searchResultArray = results.ToList();
2013-04-27 13:05:33 +00:00
IEnumerable<SearchHintInfo> returnResults = searchResultArray;
2013-04-26 19:20:53 +00:00
if (request.StartIndex.HasValue)
{
2013-04-27 13:05:33 +00:00
returnResults = returnResults.Skip(request.StartIndex.Value);
2013-04-26 19:20:53 +00:00
}
if (request.Limit.HasValue)
{
2013-04-27 13:05:33 +00:00
returnResults = returnResults.Take(request.Limit.Value);
2013-04-26 19:20:53 +00:00
}
2013-04-27 13:05:33 +00:00
return new SearchHintResult
{
TotalRecordCount = searchResultArray.Count,
2013-04-27 13:05:33 +00:00
SearchHints = returnResults.Select(GetSearchHintResult).ToArray()
};
2013-04-26 19:20:53 +00:00
}
/// <summary>
/// Gets the search hint result.
/// </summary>
2013-04-27 13:05:33 +00:00
/// <param name="hintInfo">The hint info.</param>
2013-04-26 19:20:53 +00:00
/// <returns>SearchHintResult.</returns>
2013-04-27 13:05:33 +00:00
private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
2013-04-26 19:20:53 +00:00
{
2013-04-27 13:05:33 +00:00
var item = hintInfo.Item;
var result = new SearchHint
2013-04-26 19:20:53 +00:00
{
Name = item.Name,
IndexNumber = item.IndexNumber,
ParentIndexNumber = item.ParentIndexNumber,
2013-09-04 17:02:19 +00:00
ItemId = _dtoService.GetDtoId(item),
2013-11-21 20:48:26 +00:00
Type = item.GetClientTypeName(),
2013-04-27 13:05:33 +00:00
MediaType = item.MediaType,
MatchedTerm = hintInfo.MatchedTerm,
DisplayMediaType = item.DisplayMediaType,
2013-12-27 17:12:23 +00:00
RunTimeTicks = item.RunTimeTicks,
ProductionYear = item.ProductionYear
2013-04-26 19:20:53 +00:00
};
if (item.HasImage(ImageType.Primary))
{
2013-12-19 21:51:32 +00:00
result.PrimaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary, item.GetImagePath(ImageType.Primary));
2013-04-26 19:20:53 +00: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 13:05:33 +00:00
2013-09-27 12:24:28 +00:00
result.EpisodeCount = season.GetRecursiveChildren(i => i is Episode).Count;
2013-04-27 13:05:33 +00:00
}
var series = item as Series;
if (series != null)
{
2013-09-27 12:24:28 +00:00
result.EpisodeCount = series.GetRecursiveChildren(i => i is Episode).Count;
2013-04-26 19:20:53 +00:00
}
var album = item as MusicAlbum;
if (album != null)
{
2013-09-27 12:24:28 +00:00
var songs = album.GetRecursiveChildren().OfType<Audio>().ToList();
2013-04-26 19:20:53 +00:00
2013-04-27 13:05:33 +00:00
result.SongCount = songs.Count;
2013-11-21 20:48:26 +00:00
result.Artists = _libraryManager.GetAllArtists(songs)
2013-04-26 19:20:53 +00: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;
result.Artists = song.Artists.ToArray();
2013-04-26 19:20:53 +00:00
}
return result;
}
}
}