jellyfin/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs

483 lines
17 KiB
C#
Raw Normal View History

2014-10-29 18:17:31 -07:00
using MediaBrowser.Common;
using MediaBrowser.Common.Extensions;
2013-12-14 18:17:57 -07:00
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Logging;
2015-04-12 11:58:21 -07:00
using MediaBrowser.Model.Querying;
2013-12-14 18:17:57 -07:00
using System;
2013-12-17 22:44:46 -07:00
using System.Collections.Generic;
using System.Linq;
2013-12-17 22:44:46 -07:00
using System.Threading;
using System.Threading.Tasks;
2013-12-14 18:17:57 -07:00
namespace MediaBrowser.Server.Implementations.LiveTv
{
public class LiveTvDtoService
{
private readonly ILogger _logger;
private readonly IImageProcessor _imageProcessor;
private readonly IUserDataManager _userDataManager;
private readonly IDtoService _dtoService;
2014-10-29 18:17:31 -07:00
private readonly IApplicationHost _appHost;
2013-12-14 18:17:57 -07:00
2014-10-29 18:17:31 -07:00
public LiveTvDtoService(IDtoService dtoService, IUserDataManager userDataManager, IImageProcessor imageProcessor, ILogger logger, IApplicationHost appHost)
2013-12-14 18:17:57 -07:00
{
_dtoService = dtoService;
_userDataManager = userDataManager;
_imageProcessor = imageProcessor;
_logger = logger;
2014-10-29 18:17:31 -07:00
_appHost = appHost;
2013-12-14 18:17:57 -07:00
}
2013-12-22 20:46:03 -07:00
public TimerInfoDto GetTimerInfoDto(TimerInfo info, ILiveTvService service, LiveTvProgram program, LiveTvChannel channel)
2013-12-14 18:17:57 -07:00
{
var dto = new TimerInfoDto
{
Id = GetInternalTimerId(service.Name, info.Id).ToString("N"),
Overview = info.Overview,
EndDate = info.EndDate,
Name = info.Name,
StartDate = info.StartDate,
ExternalId = info.Id,
2013-12-22 20:46:03 -07:00
ChannelId = GetInternalChannelId(service.Name, info.ChannelId).ToString("N"),
2013-12-14 18:17:57 -07:00
Status = info.Status,
SeriesTimerId = string.IsNullOrEmpty(info.SeriesTimerId) ? null : GetInternalSeriesTimerId(service.Name, info.SeriesTimerId).ToString("N"),
2013-12-17 22:44:46 -07:00
PrePaddingSeconds = info.PrePaddingSeconds,
PostPaddingSeconds = info.PostPaddingSeconds,
IsPostPaddingRequired = info.IsPostPaddingRequired,
IsPrePaddingRequired = info.IsPrePaddingRequired,
2013-12-14 18:17:57 -07:00
ExternalChannelId = info.ChannelId,
2013-12-15 07:19:24 -07:00
ExternalSeriesTimerId = info.SeriesTimerId,
2013-12-16 11:44:03 -07:00
ServiceName = service.Name,
2013-12-16 23:08:06 -07:00
ExternalProgramId = info.ProgramId,
2013-12-20 13:09:49 -07:00
Priority = info.Priority,
2014-10-29 18:17:31 -07:00
RunTimeTicks = (info.EndDate - info.StartDate).Ticks,
ServerId = _appHost.SystemId
2013-12-14 18:17:57 -07:00
};
if (!string.IsNullOrEmpty(info.ProgramId))
{
dto.ProgramId = GetInternalProgramId(service.Name, info.ProgramId).ToString("N");
}
2013-12-22 10:16:24 -07:00
if (program != null)
{
2014-01-17 13:51:29 -07:00
dto.ProgramInfo = GetProgramInfoDto(program, channel);
2013-12-22 10:16:24 -07:00
dto.ProgramInfo.TimerId = dto.Id;
dto.ProgramInfo.SeriesTimerId = dto.SeriesTimerId;
}
2013-12-22 20:46:03 -07:00
if (channel != null)
{
2014-01-23 11:05:41 -07:00
dto.ChannelName = channel.Name;
2013-12-22 20:46:03 -07:00
}
2013-12-14 18:17:57 -07:00
return dto;
}
2013-12-22 20:46:03 -07:00
public SeriesTimerInfoDto GetSeriesTimerInfoDto(SeriesTimerInfo info, ILiveTvService service, string channelName)
2013-12-14 18:17:57 -07:00
{
var dto = new SeriesTimerInfoDto
{
Id = GetInternalSeriesTimerId(service.Name, info.Id).ToString("N"),
Overview = info.Overview,
EndDate = info.EndDate,
Name = info.Name,
StartDate = info.StartDate,
ExternalId = info.Id,
2013-12-17 22:44:46 -07:00
PrePaddingSeconds = info.PrePaddingSeconds,
PostPaddingSeconds = info.PostPaddingSeconds,
IsPostPaddingRequired = info.IsPostPaddingRequired,
IsPrePaddingRequired = info.IsPrePaddingRequired,
2013-12-14 18:17:57 -07:00
Days = info.Days,
Priority = info.Priority,
2013-12-16 11:44:03 -07:00
RecordAnyChannel = info.RecordAnyChannel,
RecordAnyTime = info.RecordAnyTime,
RecordNewOnly = info.RecordNewOnly,
2013-12-14 18:17:57 -07:00
ExternalChannelId = info.ChannelId,
2013-12-15 07:19:24 -07:00
ExternalProgramId = info.ProgramId,
2013-12-22 20:46:03 -07:00
ServiceName = service.Name,
2014-10-29 18:17:31 -07:00
ChannelName = channelName,
ServerId = _appHost.SystemId
2013-12-14 18:17:57 -07:00
};
2013-12-15 07:19:24 -07:00
if (!string.IsNullOrEmpty(info.ChannelId))
{
2013-12-22 20:46:03 -07:00
dto.ChannelId = GetInternalChannelId(service.Name, info.ChannelId).ToString("N");
2013-12-15 07:19:24 -07:00
}
2013-12-14 18:17:57 -07:00
if (!string.IsNullOrEmpty(info.ProgramId))
{
dto.ProgramId = GetInternalProgramId(service.Name, info.ProgramId).ToString("N");
}
2013-12-17 22:44:46 -07:00
dto.DayPattern = info.Days == null ? null : GetDayPattern(info.Days);
return dto;
}
public DayPattern? GetDayPattern(List<DayOfWeek> days)
{
2013-12-14 18:17:57 -07:00
DayPattern? pattern = null;
2013-12-17 22:44:46 -07:00
if (days.Count > 0)
2013-12-14 18:17:57 -07:00
{
2013-12-17 22:44:46 -07:00
if (days.Count == 7)
2013-12-14 18:17:57 -07:00
{
pattern = DayPattern.Daily;
}
2013-12-17 22:44:46 -07:00
else if (days.Count == 2)
2013-12-14 18:17:57 -07:00
{
2013-12-17 22:44:46 -07:00
if (days.Contains(DayOfWeek.Saturday) && days.Contains(DayOfWeek.Sunday))
2013-12-14 18:17:57 -07:00
{
pattern = DayPattern.Weekends;
}
}
2013-12-17 22:44:46 -07:00
else if (days.Count == 5)
2013-12-14 18:17:57 -07:00
{
2013-12-17 22:44:46 -07:00
if (days.Contains(DayOfWeek.Monday) && days.Contains(DayOfWeek.Tuesday) && days.Contains(DayOfWeek.Wednesday) && days.Contains(DayOfWeek.Thursday) && days.Contains(DayOfWeek.Friday))
2013-12-14 18:17:57 -07:00
{
pattern = DayPattern.Weekdays;
}
}
}
2013-12-17 22:44:46 -07:00
return pattern;
2013-12-14 18:17:57 -07:00
}
2013-12-19 14:51:32 -07:00
/// <summary>
/// Convert the provider 0-5 scale to our 0-10 scale
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
private float? GetClientCommunityRating(float? val)
{
if (!val.HasValue)
{
return null;
}
2014-01-12 22:41:00 -07:00
return val.Value;
2013-12-19 14:51:32 -07:00
}
2014-01-24 11:09:50 -07:00
public LiveTvTunerInfoDto GetTunerInfoDto(string serviceName, LiveTvTunerInfo info, string channelName)
2014-01-23 15:15:15 -07:00
{
var dto = new LiveTvTunerInfoDto
{
Name = info.Name,
Id = info.Id,
Clients = info.Clients,
ProgramName = info.ProgramName,
SourceType = info.SourceType,
2014-01-24 11:09:50 -07:00
Status = info.Status,
2015-04-17 23:15:31 -07:00
ChannelName = channelName,
Url = info.Url
2014-01-23 15:15:15 -07:00
};
if (!string.IsNullOrEmpty(info.ChannelId))
{
dto.ChannelId = GetInternalChannelId(serviceName, info.ChannelId).ToString("N");
}
if (!string.IsNullOrEmpty(info.RecordingId))
{
dto.RecordingId = GetInternalRecordingId(serviceName, info.RecordingId).ToString("N");
}
return dto;
}
2013-12-14 18:17:57 -07:00
/// <summary>
/// Gets the channel info dto.
/// </summary>
/// <param name="info">The info.</param>
2014-01-02 14:21:06 -07:00
/// <param name="currentProgram">The current program.</param>
2013-12-14 18:17:57 -07:00
/// <param name="user">The user.</param>
/// <returns>ChannelInfoDto.</returns>
2014-01-02 14:21:06 -07:00
public ChannelInfoDto GetChannelInfoDto(LiveTvChannel info, LiveTvProgram currentProgram, User user = null)
2013-12-14 18:17:57 -07:00
{
var dto = new ChannelInfoDto
{
Name = info.Name,
ServiceName = info.ServiceName,
2014-01-23 11:05:41 -07:00
ChannelType = info.ChannelType,
Number = info.Number,
2013-12-19 14:51:32 -07:00
Type = info.GetClientTypeName(),
2013-12-14 18:17:57 -07:00
Id = info.Id.ToString("N"),
2013-12-17 13:02:12 -07:00
MediaType = info.MediaType,
ExternalId = info.ExternalId,
2014-10-29 18:17:31 -07:00
MediaSources = info.GetMediaSources(true).ToList(),
ServerId = _appHost.SystemId
2013-12-14 18:17:57 -07:00
};
if (user != null)
{
dto.UserData = _userDataManager.GetUserDataDto(info, user);
2014-02-22 22:52:30 -07:00
dto.PlayAccess = info.GetPlayAccess(user);
2013-12-14 18:17:57 -07:00
}
2013-12-19 14:51:32 -07:00
var imageTag = GetImageTag(info);
2013-12-14 18:17:57 -07:00
2014-05-08 13:09:53 -07:00
if (imageTag != null)
2013-12-14 18:17:57 -07:00
{
2014-05-08 13:09:53 -07:00
dto.ImageTags[ImageType.Primary] = imageTag;
2014-05-29 07:19:12 -07:00
2014-11-30 12:01:33 -07:00
_dtoService.AttachPrimaryImageAspectRatio(dto, info, new List<ItemFields>
{
ItemFields.PrimaryImageAspectRatio
});
2013-12-14 18:17:57 -07:00
}
2014-01-02 14:21:06 -07:00
if (currentProgram != null)
{
2014-01-17 13:51:29 -07:00
dto.CurrentProgram = GetProgramInfoDto(currentProgram, info, user);
2014-01-02 14:21:06 -07:00
}
2013-12-14 18:17:57 -07:00
return dto;
}
2014-01-17 13:51:29 -07:00
public ProgramInfoDto GetProgramInfoDto(LiveTvProgram item, LiveTvChannel channel, User user = null)
2013-12-14 18:17:57 -07:00
{
var dto = new ProgramInfoDto
{
2014-01-23 11:05:41 -07:00
Id = GetInternalProgramId(item.ServiceName, item.ExternalId).ToString("N"),
ChannelId = GetInternalChannelId(item.ServiceName, item.ExternalChannelId).ToString("N"),
Overview = item.Overview,
Genres = item.Genres,
ExternalId = item.ExternalId,
Name = item.Name,
2013-12-21 11:37:34 -07:00
ServiceName = item.ServiceName,
2014-01-23 11:05:41 -07:00
StartDate = item.StartDate,
OfficialRating = item.OfficialRating,
IsHD = item.IsHD,
2015-03-31 11:50:08 -07:00
OriginalAirDate = item.OriginalAirDate,
2014-01-23 11:05:41 -07:00
Audio = item.Audio,
CommunityRating = GetClientCommunityRating(item.CommunityRating),
IsRepeat = item.IsRepeat,
EpisodeTitle = item.EpisodeTitle,
IsMovie = item.IsMovie,
IsSeries = item.IsSeries,
IsSports = item.IsSports,
IsLive = item.IsLive,
IsNews = item.IsNews,
IsKids = item.IsKids,
IsPremiere = item.IsPremiere,
2014-06-07 12:46:24 -07:00
Type = "Program",
2014-10-29 18:17:31 -07:00
MediaType = item.MediaType,
2015-03-14 13:56:50 -07:00
ServerId = _appHost.SystemId,
ProductionYear = item.ProductionYear
2013-12-14 18:17:57 -07:00
};
2014-01-23 11:05:41 -07:00
if (item.EndDate.HasValue)
{
dto.EndDate = item.EndDate.Value;
dto.RunTimeTicks = (item.EndDate.Value - item.StartDate).Ticks;
}
2014-01-17 13:51:29 -07:00
if (channel != null)
{
dto.ChannelName = channel.Name;
if (!string.IsNullOrEmpty(channel.PrimaryImagePath))
{
dto.ChannelPrimaryImageTag = GetImageTag(channel);
}
}
2013-12-28 14:37:01 -07:00
var imageTag = GetImageTag(item);
2014-05-08 13:09:53 -07:00
if (imageTag != null)
2013-12-28 14:37:01 -07:00
{
2014-05-08 13:09:53 -07:00
dto.ImageTags[ImageType.Primary] = imageTag;
2014-11-30 12:01:33 -07:00
_dtoService.AttachPrimaryImageAspectRatio(dto, item, new List<ItemFields>
{
ItemFields.PrimaryImageAspectRatio
});
2013-12-28 14:37:01 -07:00
}
2013-12-14 18:17:57 -07:00
if (user != null)
{
dto.UserData = _userDataManager.GetUserDataDto(item, user);
2014-02-22 22:52:30 -07:00
dto.PlayAccess = item.GetPlayAccess(user);
2013-12-14 18:17:57 -07:00
}
return dto;
}
2015-05-31 11:22:51 -07:00
internal string GetImageTag(IHasImages info)
2013-12-14 18:17:57 -07:00
{
try
{
2014-02-07 13:30:41 -07:00
return _imageProcessor.GetImageCacheTag(info, ImageType.Primary);
2013-12-14 18:17:57 -07:00
}
catch (Exception ex)
{
2013-12-19 14:51:32 -07:00
_logger.ErrorException("Error getting image info for {0}", ex, info.Name);
2013-12-14 18:17:57 -07:00
}
return null;
}
2015-02-25 11:11:49 -07:00
private const string InternalVersionNumber = "4";
2013-12-22 20:46:03 -07:00
public Guid GetInternalChannelId(string serviceName, string externalId)
2013-12-14 18:17:57 -07:00
{
var name = serviceName + externalId + InternalVersionNumber;
2013-12-14 18:17:57 -07:00
2013-12-19 14:51:32 -07:00
return name.ToLower().GetMBId(typeof(LiveTvChannel));
2013-12-14 18:17:57 -07:00
}
public Guid GetInternalTimerId(string serviceName, string externalId)
{
var name = serviceName + externalId + InternalVersionNumber;
2013-12-14 18:17:57 -07:00
return name.ToLower().GetMD5();
}
public Guid GetInternalSeriesTimerId(string serviceName, string externalId)
{
var name = serviceName + externalId + InternalVersionNumber;
2013-12-14 18:17:57 -07:00
return name.ToLower().GetMD5();
}
public Guid GetInternalProgramId(string serviceName, string externalId)
{
var name = serviceName + externalId + InternalVersionNumber;
2013-12-14 18:17:57 -07:00
return name.ToLower().GetMBId(typeof(LiveTvProgram));
2013-12-14 18:17:57 -07:00
}
public Guid GetInternalRecordingId(string serviceName, string externalId)
{
var name = serviceName + externalId + InternalVersionNumber;
2013-12-14 18:17:57 -07:00
return name.ToLower().GetMBId(typeof(ILiveTvRecording));
2013-12-14 18:17:57 -07:00
}
2013-12-17 13:02:12 -07:00
public async Task<TimerInfo> GetTimerInfo(TimerInfoDto dto, bool isNew, ILiveTvManager liveTv, CancellationToken cancellationToken)
2013-12-14 18:17:57 -07:00
{
2013-12-17 13:02:12 -07:00
var info = new TimerInfo
2013-12-14 18:17:57 -07:00
{
Overview = dto.Overview,
EndDate = dto.EndDate,
Name = dto.Name,
StartDate = dto.StartDate,
Status = dto.Status,
2013-12-17 22:44:46 -07:00
PrePaddingSeconds = dto.PrePaddingSeconds,
PostPaddingSeconds = dto.PostPaddingSeconds,
IsPostPaddingRequired = dto.IsPostPaddingRequired,
IsPrePaddingRequired = dto.IsPrePaddingRequired,
2013-12-19 14:51:32 -07:00
Priority = dto.Priority,
SeriesTimerId = dto.ExternalSeriesTimerId,
ProgramId = dto.ExternalProgramId,
ChannelId = dto.ExternalChannelId,
Id = dto.ExternalId
2013-12-14 18:17:57 -07:00
};
2013-12-17 13:02:12 -07:00
// Convert internal server id's to external tv provider id's
2013-12-19 14:51:32 -07:00
if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
2013-12-17 13:02:12 -07:00
{
2013-12-19 14:51:32 -07:00
var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
2013-12-17 13:02:12 -07:00
info.Id = timer.ExternalId;
}
2013-12-19 14:51:32 -07:00
if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId))
2013-12-17 13:02:12 -07:00
{
2013-12-19 14:51:32 -07:00
var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false);
2013-12-17 13:02:12 -07:00
2013-12-19 14:51:32 -07:00
if (channel != null)
{
info.ChannelId = channel.ExternalId;
}
2013-12-17 13:02:12 -07:00
}
2013-12-19 14:51:32 -07:00
if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
2013-12-17 13:02:12 -07:00
{
2013-12-19 14:51:32 -07:00
var program = await liveTv.GetProgram(dto.ProgramId, cancellationToken).ConfigureAwait(false);
2013-12-17 13:02:12 -07:00
2013-12-19 14:51:32 -07:00
if (program != null)
{
info.ProgramId = program.ExternalId;
}
2013-12-17 13:02:12 -07:00
}
2013-12-19 14:51:32 -07:00
if (!string.IsNullOrEmpty(dto.SeriesTimerId) && string.IsNullOrEmpty(info.SeriesTimerId))
2013-12-17 13:02:12 -07:00
{
2013-12-19 14:51:32 -07:00
var timer = await liveTv.GetSeriesTimer(dto.SeriesTimerId, cancellationToken).ConfigureAwait(false);
2013-12-17 13:02:12 -07:00
2013-12-19 14:51:32 -07:00
if (timer != null)
{
info.SeriesTimerId = timer.ExternalId;
}
2013-12-17 13:02:12 -07:00
}
return info;
2013-12-14 18:17:57 -07:00
}
2013-12-17 13:02:12 -07:00
public async Task<SeriesTimerInfo> GetSeriesTimerInfo(SeriesTimerInfoDto dto, bool isNew, ILiveTvManager liveTv, CancellationToken cancellationToken)
2013-12-14 18:17:57 -07:00
{
2013-12-17 13:02:12 -07:00
var info = new SeriesTimerInfo
2013-12-14 18:17:57 -07:00
{
Overview = dto.Overview,
EndDate = dto.EndDate,
Name = dto.Name,
StartDate = dto.StartDate,
2013-12-17 22:44:46 -07:00
PrePaddingSeconds = dto.PrePaddingSeconds,
PostPaddingSeconds = dto.PostPaddingSeconds,
IsPostPaddingRequired = dto.IsPostPaddingRequired,
IsPrePaddingRequired = dto.IsPrePaddingRequired,
2013-12-14 18:17:57 -07:00
Days = dto.Days,
Priority = dto.Priority,
2013-12-16 11:44:03 -07:00
RecordAnyChannel = dto.RecordAnyChannel,
RecordAnyTime = dto.RecordAnyTime,
2013-12-19 14:51:32 -07:00
RecordNewOnly = dto.RecordNewOnly,
ProgramId = dto.ExternalProgramId,
ChannelId = dto.ExternalChannelId,
Id = dto.ExternalId
2013-12-14 18:17:57 -07:00
};
2013-12-17 13:02:12 -07:00
// Convert internal server id's to external tv provider id's
2013-12-19 14:51:32 -07:00
if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
2013-12-17 13:02:12 -07:00
{
var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
info.Id = timer.ExternalId;
}
2013-12-19 14:51:32 -07:00
if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId))
2013-12-17 13:02:12 -07:00
{
var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false);
2013-12-19 14:51:32 -07:00
if (channel != null)
{
info.ChannelId = channel.ExternalId;
}
2013-12-17 13:02:12 -07:00
}
2013-12-19 14:51:32 -07:00
if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
2013-12-17 13:02:12 -07:00
{
var program = await liveTv.GetProgram(dto.ProgramId, cancellationToken).ConfigureAwait(false);
2013-12-19 14:51:32 -07:00
if (program != null)
{
info.ProgramId = program.ExternalId;
}
2013-12-17 13:02:12 -07:00
}
return info;
2013-12-14 18:17:57 -07:00
}
}
}