LiveTvDtoService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Dto;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.LiveTv;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Querying;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace MediaBrowser.Server.Implementations.LiveTv
  18. {
  19. public class LiveTvDtoService
  20. {
  21. private readonly ILogger _logger;
  22. private readonly IImageProcessor _imageProcessor;
  23. private readonly IUserDataManager _userDataManager;
  24. private readonly IDtoService _dtoService;
  25. private readonly IApplicationHost _appHost;
  26. public LiveTvDtoService(IDtoService dtoService, IUserDataManager userDataManager, IImageProcessor imageProcessor, ILogger logger, IApplicationHost appHost)
  27. {
  28. _dtoService = dtoService;
  29. _userDataManager = userDataManager;
  30. _imageProcessor = imageProcessor;
  31. _logger = logger;
  32. _appHost = appHost;
  33. }
  34. public TimerInfoDto GetTimerInfoDto(TimerInfo info, ILiveTvService service, LiveTvProgram program, LiveTvChannel channel)
  35. {
  36. var dto = new TimerInfoDto
  37. {
  38. Id = GetInternalTimerId(service.Name, info.Id).ToString("N"),
  39. Overview = info.Overview,
  40. EndDate = info.EndDate,
  41. Name = info.Name,
  42. StartDate = info.StartDate,
  43. ExternalId = info.Id,
  44. ChannelId = GetInternalChannelId(service.Name, info.ChannelId).ToString("N"),
  45. Status = info.Status,
  46. SeriesTimerId = string.IsNullOrEmpty(info.SeriesTimerId) ? null : GetInternalSeriesTimerId(service.Name, info.SeriesTimerId).ToString("N"),
  47. PrePaddingSeconds = info.PrePaddingSeconds,
  48. PostPaddingSeconds = info.PostPaddingSeconds,
  49. IsPostPaddingRequired = info.IsPostPaddingRequired,
  50. IsPrePaddingRequired = info.IsPrePaddingRequired,
  51. ExternalChannelId = info.ChannelId,
  52. ExternalSeriesTimerId = info.SeriesTimerId,
  53. ServiceName = service.Name,
  54. ExternalProgramId = info.ProgramId,
  55. Priority = info.Priority,
  56. RunTimeTicks = (info.EndDate - info.StartDate).Ticks,
  57. ServerId = _appHost.SystemId
  58. };
  59. if (!string.IsNullOrEmpty(info.ProgramId))
  60. {
  61. dto.ProgramId = GetInternalProgramId(service.Name, info.ProgramId).ToString("N");
  62. }
  63. if (program != null)
  64. {
  65. dto.ProgramInfo = _dtoService.GetBaseItemDto(program, new DtoOptions());
  66. dto.ProgramInfo.TimerId = dto.Id;
  67. dto.ProgramInfo.SeriesTimerId = dto.SeriesTimerId;
  68. }
  69. if (channel != null)
  70. {
  71. dto.ChannelName = channel.Name;
  72. }
  73. return dto;
  74. }
  75. public SeriesTimerInfoDto GetSeriesTimerInfoDto(SeriesTimerInfo info, ILiveTvService service, string channelName)
  76. {
  77. var dto = new SeriesTimerInfoDto
  78. {
  79. Id = GetInternalSeriesTimerId(service.Name, info.Id).ToString("N"),
  80. Overview = info.Overview,
  81. EndDate = info.EndDate,
  82. Name = info.Name,
  83. StartDate = info.StartDate,
  84. ExternalId = info.Id,
  85. PrePaddingSeconds = info.PrePaddingSeconds,
  86. PostPaddingSeconds = info.PostPaddingSeconds,
  87. IsPostPaddingRequired = info.IsPostPaddingRequired,
  88. IsPrePaddingRequired = info.IsPrePaddingRequired,
  89. Days = info.Days,
  90. Priority = info.Priority,
  91. RecordAnyChannel = info.RecordAnyChannel,
  92. RecordAnyTime = info.RecordAnyTime,
  93. RecordNewOnly = info.RecordNewOnly,
  94. ExternalChannelId = info.ChannelId,
  95. ExternalProgramId = info.ProgramId,
  96. ServiceName = service.Name,
  97. ChannelName = channelName,
  98. ServerId = _appHost.SystemId
  99. };
  100. if (!string.IsNullOrEmpty(info.ChannelId))
  101. {
  102. dto.ChannelId = GetInternalChannelId(service.Name, info.ChannelId).ToString("N");
  103. }
  104. if (!string.IsNullOrEmpty(info.ProgramId))
  105. {
  106. dto.ProgramId = GetInternalProgramId(service.Name, info.ProgramId).ToString("N");
  107. }
  108. dto.DayPattern = info.Days == null ? null : GetDayPattern(info.Days);
  109. return dto;
  110. }
  111. public DayPattern? GetDayPattern(List<DayOfWeek> days)
  112. {
  113. DayPattern? pattern = null;
  114. if (days.Count > 0)
  115. {
  116. if (days.Count == 7)
  117. {
  118. pattern = DayPattern.Daily;
  119. }
  120. else if (days.Count == 2)
  121. {
  122. if (days.Contains(DayOfWeek.Saturday) && days.Contains(DayOfWeek.Sunday))
  123. {
  124. pattern = DayPattern.Weekends;
  125. }
  126. }
  127. else if (days.Count == 5)
  128. {
  129. if (days.Contains(DayOfWeek.Monday) && days.Contains(DayOfWeek.Tuesday) && days.Contains(DayOfWeek.Wednesday) && days.Contains(DayOfWeek.Thursday) && days.Contains(DayOfWeek.Friday))
  130. {
  131. pattern = DayPattern.Weekdays;
  132. }
  133. }
  134. }
  135. return pattern;
  136. }
  137. /// <summary>
  138. /// Convert the provider 0-5 scale to our 0-10 scale
  139. /// </summary>
  140. /// <param name="val"></param>
  141. /// <returns></returns>
  142. private float? GetClientCommunityRating(float? val)
  143. {
  144. if (!val.HasValue)
  145. {
  146. return null;
  147. }
  148. return val.Value;
  149. }
  150. public LiveTvTunerInfoDto GetTunerInfoDto(string serviceName, LiveTvTunerInfo info, string channelName)
  151. {
  152. var dto = new LiveTvTunerInfoDto
  153. {
  154. Name = info.Name,
  155. Id = info.Id,
  156. Clients = info.Clients,
  157. ProgramName = info.ProgramName,
  158. SourceType = info.SourceType,
  159. Status = info.Status,
  160. ChannelName = channelName,
  161. Url = info.Url,
  162. CanReset = info.CanReset
  163. };
  164. if (!string.IsNullOrEmpty(info.ChannelId))
  165. {
  166. dto.ChannelId = GetInternalChannelId(serviceName, info.ChannelId).ToString("N");
  167. }
  168. if (!string.IsNullOrEmpty(info.RecordingId))
  169. {
  170. dto.RecordingId = GetInternalRecordingId(serviceName, info.RecordingId).ToString("N");
  171. }
  172. return dto;
  173. }
  174. /// <summary>
  175. /// Gets the channel info dto.
  176. /// </summary>
  177. /// <param name="info">The info.</param>
  178. /// <param name="options">The options.</param>
  179. /// <param name="currentProgram">The current program.</param>
  180. /// <param name="user">The user.</param>
  181. /// <returns>ChannelInfoDto.</returns>
  182. public ChannelInfoDto GetChannelInfoDto(LiveTvChannel info, DtoOptions options, LiveTvProgram currentProgram, User user = null)
  183. {
  184. var dto = new ChannelInfoDto
  185. {
  186. Name = info.Name,
  187. ServiceName = info.ServiceName,
  188. ChannelType = info.ChannelType,
  189. Number = info.Number,
  190. Type = info.GetClientTypeName(),
  191. Id = info.Id.ToString("N"),
  192. MediaType = info.MediaType,
  193. ExternalId = info.ExternalId,
  194. MediaSources = info.GetMediaSources(true).ToList(),
  195. ServerId = _appHost.SystemId
  196. };
  197. if (user != null)
  198. {
  199. dto.UserData = _userDataManager.GetUserDataDto(info, user);
  200. dto.PlayAccess = info.GetPlayAccess(user);
  201. }
  202. var imageTag = GetImageTag(info);
  203. if (imageTag != null)
  204. {
  205. dto.ImageTags[ImageType.Primary] = imageTag;
  206. _dtoService.AttachPrimaryImageAspectRatio(dto, info);
  207. }
  208. if (currentProgram != null)
  209. {
  210. dto.CurrentProgram = _dtoService.GetBaseItemDto(currentProgram, options, user);
  211. }
  212. return dto;
  213. }
  214. internal string GetImageTag(IHasImages info)
  215. {
  216. try
  217. {
  218. return _imageProcessor.GetImageCacheTag(info, ImageType.Primary);
  219. }
  220. catch (Exception ex)
  221. {
  222. _logger.ErrorException("Error getting image info for {0}", ex, info.Name);
  223. }
  224. return null;
  225. }
  226. private const string InternalVersionNumber = "4";
  227. public Guid GetInternalChannelId(string serviceName, string externalId)
  228. {
  229. var name = serviceName + externalId + InternalVersionNumber;
  230. return name.ToLower().GetMBId(typeof(LiveTvChannel));
  231. }
  232. public Guid GetInternalTimerId(string serviceName, string externalId)
  233. {
  234. var name = serviceName + externalId + InternalVersionNumber;
  235. return name.ToLower().GetMD5();
  236. }
  237. public Guid GetInternalSeriesTimerId(string serviceName, string externalId)
  238. {
  239. var name = serviceName + externalId + InternalVersionNumber;
  240. return name.ToLower().GetMD5();
  241. }
  242. public Guid GetInternalProgramId(string serviceName, string externalId)
  243. {
  244. var name = serviceName + externalId + InternalVersionNumber;
  245. return name.ToLower().GetMBId(typeof(LiveTvProgram));
  246. }
  247. public Guid GetInternalRecordingId(string serviceName, string externalId)
  248. {
  249. var name = serviceName + externalId + InternalVersionNumber + "0";
  250. return name.ToLower().GetMBId(typeof(ILiveTvRecording));
  251. }
  252. public async Task<TimerInfo> GetTimerInfo(TimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  253. {
  254. var info = new TimerInfo
  255. {
  256. Overview = dto.Overview,
  257. EndDate = dto.EndDate,
  258. Name = dto.Name,
  259. StartDate = dto.StartDate,
  260. Status = dto.Status,
  261. PrePaddingSeconds = dto.PrePaddingSeconds,
  262. PostPaddingSeconds = dto.PostPaddingSeconds,
  263. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  264. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  265. Priority = dto.Priority,
  266. SeriesTimerId = dto.ExternalSeriesTimerId,
  267. ProgramId = dto.ExternalProgramId,
  268. ChannelId = dto.ExternalChannelId,
  269. Id = dto.ExternalId
  270. };
  271. // Convert internal server id's to external tv provider id's
  272. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  273. {
  274. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  275. info.Id = timer.ExternalId;
  276. }
  277. if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId))
  278. {
  279. var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false);
  280. if (channel != null)
  281. {
  282. info.ChannelId = channel.ExternalId;
  283. }
  284. }
  285. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  286. {
  287. var program = liveTv.GetInternalProgram(dto.ProgramId);
  288. if (program != null)
  289. {
  290. info.ProgramId = program.ExternalId;
  291. }
  292. }
  293. if (!string.IsNullOrEmpty(dto.SeriesTimerId) && string.IsNullOrEmpty(info.SeriesTimerId))
  294. {
  295. var timer = await liveTv.GetSeriesTimer(dto.SeriesTimerId, cancellationToken).ConfigureAwait(false);
  296. if (timer != null)
  297. {
  298. info.SeriesTimerId = timer.ExternalId;
  299. }
  300. }
  301. return info;
  302. }
  303. public async Task<SeriesTimerInfo> GetSeriesTimerInfo(SeriesTimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  304. {
  305. var info = new SeriesTimerInfo
  306. {
  307. Overview = dto.Overview,
  308. EndDate = dto.EndDate,
  309. Name = dto.Name,
  310. StartDate = dto.StartDate,
  311. PrePaddingSeconds = dto.PrePaddingSeconds,
  312. PostPaddingSeconds = dto.PostPaddingSeconds,
  313. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  314. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  315. Days = dto.Days,
  316. Priority = dto.Priority,
  317. RecordAnyChannel = dto.RecordAnyChannel,
  318. RecordAnyTime = dto.RecordAnyTime,
  319. RecordNewOnly = dto.RecordNewOnly,
  320. ProgramId = dto.ExternalProgramId,
  321. ChannelId = dto.ExternalChannelId,
  322. Id = dto.ExternalId
  323. };
  324. // Convert internal server id's to external tv provider id's
  325. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  326. {
  327. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  328. info.Id = timer.ExternalId;
  329. }
  330. if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId))
  331. {
  332. var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false);
  333. if (channel != null)
  334. {
  335. info.ChannelId = channel.ExternalId;
  336. }
  337. }
  338. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  339. {
  340. var program = liveTv.GetInternalProgram(dto.ProgramId);
  341. if (program != null)
  342. {
  343. info.ProgramId = program.ExternalId;
  344. }
  345. }
  346. return info;
  347. }
  348. }
  349. }