jellyfin-server/MediaBrowser.Api/LiveTv/LiveTvService.cs

163 lines
5.7 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.LiveTv;
using ServiceStack.ServiceHost;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api.LiveTv
{
[Route("/LiveTv/Services", "GET")]
[Api(Description = "Gets available live tv services.")]
public class GetServices : IReturn<List<LiveTvServiceInfo>>
{
2013-11-11 19:36:48 +00:00
[ApiMember(Name = "ServiceName", Description = "Optional filter by service.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ServiceName { get; set; }
}
[Route("/LiveTv/Channels", "GET")]
[Api(Description = "Gets available live tv channels.")]
public class GetChannels : IReturn<List<ChannelInfoDto>>
{
2013-11-11 19:36:48 +00:00
[ApiMember(Name = "ServiceName", Description = "Optional filter by service.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ServiceName { get; set; }
2013-11-24 20:51:45 +00:00
[ApiMember(Name = "Type", Description = "Optional filter by channel type.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public ChannelType? Type { get; set; }
[ApiMember(Name = "UserId", Description = "Optional filter by channel user id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
}
2013-10-31 20:45:58 +00:00
2013-11-24 21:30:38 +00:00
[Route("/LiveTv/Channels/{Id}", "GET")]
[Api(Description = "Gets a live tv channel")]
public class GetChannel : IReturn<ChannelInfoDto>
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Channel Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
2013-10-31 20:45:58 +00:00
[Route("/LiveTv/Recordings", "GET")]
[Api(Description = "Gets available live tv recordings.")]
public class GetRecordings : IReturn<List<RecordingInfo>>
{
2013-11-11 19:36:48 +00:00
[ApiMember(Name = "ServiceName", Description = "Optional filter by service.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ServiceName { get; set; }
2013-10-31 20:45:58 +00:00
}
2013-11-15 21:31:44 +00:00
[Route("/LiveTv/Guide", "GET")]
[Api(Description = "Gets available live tv epgs..")]
2013-11-15 21:31:44 +00:00
public class GetGuide : IReturn<List<ChannelGuide>>
{
2013-11-15 21:31:44 +00:00
[ApiMember(Name = "ServiceName", Description = "Live tv service name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2013-11-11 19:36:48 +00:00
public string ServiceName { get; set; }
2013-11-15 21:31:44 +00:00
[ApiMember(Name = "ChannelIds", Description = "The channels to return guide information for.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ChannelIds { get; set; }
}
2013-11-15 21:31:44 +00:00
public class LiveTvService : BaseApiService
{
private readonly ILiveTvManager _liveTvManager;
public LiveTvService(ILiveTvManager liveTvManager)
{
_liveTvManager = liveTvManager;
}
2013-11-11 19:36:48 +00:00
private IEnumerable<ILiveTvService> GetServices(string serviceName)
{
2013-11-11 19:36:48 +00:00
IEnumerable<ILiveTvService> services = _liveTvManager.Services;
if (!string.IsNullOrEmpty(serviceName))
{
services = services.Where(i => string.Equals(i.Name, serviceName, System.StringComparison.OrdinalIgnoreCase));
}
return services;
}
2013-11-11 19:36:48 +00:00
public object Get(GetServices request)
{
var services = GetServices(request.ServiceName)
.Select(GetServiceInfo)
.ToList();
2013-11-11 19:36:48 +00:00
return ToOptimizedResult(services);
}
2013-10-31 20:45:58 +00:00
private LiveTvServiceInfo GetServiceInfo(ILiveTvService service)
{
return new LiveTvServiceInfo
{
Name = service.Name
};
}
public object Get(GetChannels request)
{
2013-11-24 20:51:45 +00:00
var result = _liveTvManager.GetChannels(new ChannelQuery
{
ChannelType = request.Type,
ServiceName = request.ServiceName,
UserId = request.UserId
2013-11-24 20:51:45 +00:00
})
.Select(_liveTvManager.GetChannelInfoDto);
2013-11-24 20:51:45 +00:00
return ToOptimizedResult(result.ToList());
}
2013-10-31 20:45:58 +00:00
2013-11-24 21:30:38 +00:00
public object Get(GetChannel request)
{
var result = _liveTvManager.GetChannel(request.Id);
return ToOptimizedResult(_liveTvManager.GetChannelInfoDto(result));
}
2013-10-31 20:45:58 +00:00
public object Get(GetRecordings request)
{
2013-10-31 20:45:58 +00:00
var result = GetRecordingsAsync(request).Result;
2013-11-12 15:36:25 +00:00
return ToOptimizedResult(result.ToList());
2013-10-31 20:45:58 +00:00
}
private async Task<IEnumerable<RecordingInfo>> GetRecordingsAsync(GetRecordings request)
{
2013-11-11 19:36:48 +00:00
var services = GetServices(request.ServiceName);
2013-10-31 20:45:58 +00:00
2013-11-15 21:31:44 +00:00
var query = new RecordingQuery
{
};
var tasks = services.Select(i => i.GetRecordingsAsync(query, CancellationToken.None));
2013-10-31 20:45:58 +00:00
var recordings = await Task.WhenAll(tasks).ConfigureAwait(false);
2013-11-15 21:31:44 +00:00
return recordings.SelectMany(i => i);
}
2013-11-15 21:31:44 +00:00
public object Get(GetGuide request)
{
2013-11-15 21:31:44 +00:00
var result = GetGuideAsync(request).Result;
return ToOptimizedResult(result);
}
2013-11-15 21:31:44 +00:00
private async Task<IEnumerable<ChannelGuide>> GetGuideAsync(GetGuide request)
{
2013-11-11 19:36:48 +00:00
var service = GetServices(request.ServiceName)
.First();
2013-11-15 21:31:44 +00:00
var channels = request.ChannelIds.Split(',');
2013-11-15 21:31:44 +00:00
return await service.GetChannelGuidesAsync(channels, CancellationToken.None).ConfigureAwait(false);
}
}
}