LiveTvDtoService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. };
  163. if (!string.IsNullOrEmpty(info.ChannelId))
  164. {
  165. dto.ChannelId = GetInternalChannelId(serviceName, info.ChannelId).ToString("N");
  166. }
  167. if (!string.IsNullOrEmpty(info.RecordingId))
  168. {
  169. dto.RecordingId = GetInternalRecordingId(serviceName, info.RecordingId).ToString("N");
  170. }
  171. return dto;
  172. }
  173. /// <summary>
  174. /// Gets the channel info dto.
  175. /// </summary>
  176. /// <param name="info">The info.</param>
  177. /// <param name="currentProgram">The current program.</param>
  178. /// <param name="user">The user.</param>
  179. /// <returns>ChannelInfoDto.</returns>
  180. public ChannelInfoDto GetChannelInfoDto(LiveTvChannel info, LiveTvProgram currentProgram, User user = null)
  181. {
  182. var dto = new ChannelInfoDto
  183. {
  184. Name = info.Name,
  185. ServiceName = info.ServiceName,
  186. ChannelType = info.ChannelType,
  187. Number = info.Number,
  188. Type = info.GetClientTypeName(),
  189. Id = info.Id.ToString("N"),
  190. MediaType = info.MediaType,
  191. ExternalId = info.ExternalId,
  192. MediaSources = info.GetMediaSources(true).ToList(),
  193. ServerId = _appHost.SystemId
  194. };
  195. if (user != null)
  196. {
  197. dto.UserData = _userDataManager.GetUserDataDto(info, user);
  198. dto.PlayAccess = info.GetPlayAccess(user);
  199. }
  200. var imageTag = GetImageTag(info);
  201. if (imageTag != null)
  202. {
  203. dto.ImageTags[ImageType.Primary] = imageTag;
  204. _dtoService.AttachPrimaryImageAspectRatio(dto, info, new List<ItemFields>
  205. {
  206. ItemFields.PrimaryImageAspectRatio
  207. });
  208. }
  209. if (currentProgram != null)
  210. {
  211. dto.CurrentProgram = _dtoService.GetBaseItemDto(currentProgram, new DtoOptions(), user);
  212. }
  213. return dto;
  214. }
  215. internal string GetImageTag(IHasImages info)
  216. {
  217. try
  218. {
  219. return _imageProcessor.GetImageCacheTag(info, ImageType.Primary);
  220. }
  221. catch (Exception ex)
  222. {
  223. _logger.ErrorException("Error getting image info for {0}", ex, info.Name);
  224. }
  225. return null;
  226. }
  227. private const string InternalVersionNumber = "4";
  228. public Guid GetInternalChannelId(string serviceName, string externalId)
  229. {
  230. var name = serviceName + externalId + InternalVersionNumber;
  231. return name.ToLower().GetMBId(typeof(LiveTvChannel));
  232. }
  233. public Guid GetInternalTimerId(string serviceName, string externalId)
  234. {
  235. var name = serviceName + externalId + InternalVersionNumber;
  236. return name.ToLower().GetMD5();
  237. }
  238. public Guid GetInternalSeriesTimerId(string serviceName, string externalId)
  239. {
  240. var name = serviceName + externalId + InternalVersionNumber;
  241. return name.ToLower().GetMD5();
  242. }
  243. public Guid GetInternalProgramId(string serviceName, string externalId)
  244. {
  245. var name = serviceName + externalId + InternalVersionNumber;
  246. return name.ToLower().GetMBId(typeof(LiveTvProgram));
  247. }
  248. public Guid GetInternalRecordingId(string serviceName, string externalId)
  249. {
  250. var name = serviceName + externalId + InternalVersionNumber + "0";
  251. return name.ToLower().GetMBId(typeof(ILiveTvRecording));
  252. }
  253. public async Task<TimerInfo> GetTimerInfo(TimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  254. {
  255. var info = new TimerInfo
  256. {
  257. Overview = dto.Overview,
  258. EndDate = dto.EndDate,
  259. Name = dto.Name,
  260. StartDate = dto.StartDate,
  261. Status = dto.Status,
  262. PrePaddingSeconds = dto.PrePaddingSeconds,
  263. PostPaddingSeconds = dto.PostPaddingSeconds,
  264. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  265. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  266. Priority = dto.Priority,
  267. SeriesTimerId = dto.ExternalSeriesTimerId,
  268. ProgramId = dto.ExternalProgramId,
  269. ChannelId = dto.ExternalChannelId,
  270. Id = dto.ExternalId
  271. };
  272. // Convert internal server id's to external tv provider id's
  273. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  274. {
  275. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  276. info.Id = timer.ExternalId;
  277. }
  278. if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId))
  279. {
  280. var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false);
  281. if (channel != null)
  282. {
  283. info.ChannelId = channel.ExternalId;
  284. }
  285. }
  286. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  287. {
  288. var program = liveTv.GetInternalProgram(dto.ProgramId);
  289. if (program != null)
  290. {
  291. info.ProgramId = program.ExternalId;
  292. }
  293. }
  294. if (!string.IsNullOrEmpty(dto.SeriesTimerId) && string.IsNullOrEmpty(info.SeriesTimerId))
  295. {
  296. var timer = await liveTv.GetSeriesTimer(dto.SeriesTimerId, cancellationToken).ConfigureAwait(false);
  297. if (timer != null)
  298. {
  299. info.SeriesTimerId = timer.ExternalId;
  300. }
  301. }
  302. return info;
  303. }
  304. public async Task<SeriesTimerInfo> GetSeriesTimerInfo(SeriesTimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  305. {
  306. var info = new SeriesTimerInfo
  307. {
  308. Overview = dto.Overview,
  309. EndDate = dto.EndDate,
  310. Name = dto.Name,
  311. StartDate = dto.StartDate,
  312. PrePaddingSeconds = dto.PrePaddingSeconds,
  313. PostPaddingSeconds = dto.PostPaddingSeconds,
  314. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  315. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  316. Days = dto.Days,
  317. Priority = dto.Priority,
  318. RecordAnyChannel = dto.RecordAnyChannel,
  319. RecordAnyTime = dto.RecordAnyTime,
  320. RecordNewOnly = dto.RecordNewOnly,
  321. ProgramId = dto.ExternalProgramId,
  322. ChannelId = dto.ExternalChannelId,
  323. Id = dto.ExternalId
  324. };
  325. // Convert internal server id's to external tv provider id's
  326. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  327. {
  328. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  329. info.Id = timer.ExternalId;
  330. }
  331. if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId))
  332. {
  333. var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false);
  334. if (channel != null)
  335. {
  336. info.ChannelId = channel.ExternalId;
  337. }
  338. }
  339. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  340. {
  341. var program = liveTv.GetInternalProgram(dto.ProgramId);
  342. if (program != null)
  343. {
  344. info.ProgramId = program.ExternalId;
  345. }
  346. }
  347. return info;
  348. }
  349. }
  350. }