LiveTvDtoService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 Microsoft.Extensions.Logging;
  11. using System;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using MediaBrowser.Controller.Entities.TV;
  16. using MediaBrowser.Model.Dto;
  17. using MediaBrowser.Model.Extensions;
  18. using System.Collections.Generic;
  19. namespace Emby.Server.Implementations.LiveTv
  20. {
  21. public class LiveTvDtoService
  22. {
  23. private readonly ILogger _logger;
  24. private readonly IImageProcessor _imageProcessor;
  25. private readonly IDtoService _dtoService;
  26. private readonly IApplicationHost _appHost;
  27. private readonly ILibraryManager _libraryManager;
  28. public LiveTvDtoService(IDtoService dtoService, IImageProcessor imageProcessor, ILogger logger, IApplicationHost appHost, ILibraryManager libraryManager)
  29. {
  30. _dtoService = dtoService;
  31. _imageProcessor = imageProcessor;
  32. _logger = logger;
  33. _appHost = appHost;
  34. _libraryManager = libraryManager;
  35. }
  36. public TimerInfoDto GetTimerInfoDto(TimerInfo info, ILiveTvService service, LiveTvProgram program, BaseItem channel)
  37. {
  38. var dto = new TimerInfoDto
  39. {
  40. Id = GetInternalTimerId(info.Id),
  41. Overview = info.Overview,
  42. EndDate = info.EndDate,
  43. Name = info.Name,
  44. StartDate = info.StartDate,
  45. ExternalId = info.Id,
  46. ChannelId = GetInternalChannelId(service.Name, info.ChannelId),
  47. Status = info.Status,
  48. SeriesTimerId = string.IsNullOrEmpty(info.SeriesTimerId) ? null : GetInternalSeriesTimerId(info.SeriesTimerId).ToString("N"),
  49. PrePaddingSeconds = info.PrePaddingSeconds,
  50. PostPaddingSeconds = info.PostPaddingSeconds,
  51. IsPostPaddingRequired = info.IsPostPaddingRequired,
  52. IsPrePaddingRequired = info.IsPrePaddingRequired,
  53. KeepUntil = info.KeepUntil,
  54. ExternalChannelId = info.ChannelId,
  55. ExternalSeriesTimerId = info.SeriesTimerId,
  56. ServiceName = service.Name,
  57. ExternalProgramId = info.ProgramId,
  58. Priority = info.Priority,
  59. RunTimeTicks = (info.EndDate - info.StartDate).Ticks,
  60. ServerId = _appHost.SystemId
  61. };
  62. if (!string.IsNullOrEmpty(info.ProgramId))
  63. {
  64. dto.ProgramId = GetInternalProgramId(info.ProgramId).ToString("N");
  65. }
  66. if (program != null)
  67. {
  68. dto.ProgramInfo = _dtoService.GetBaseItemDto(program, new DtoOptions());
  69. if (info.Status != RecordingStatus.Cancelled && info.Status != RecordingStatus.Error)
  70. {
  71. dto.ProgramInfo.TimerId = dto.Id;
  72. dto.ProgramInfo.Status = info.Status.ToString();
  73. }
  74. dto.ProgramInfo.SeriesTimerId = dto.SeriesTimerId;
  75. if (!string.IsNullOrEmpty(info.SeriesTimerId))
  76. {
  77. FillImages(dto.ProgramInfo, info.Name, info.SeriesId);
  78. }
  79. }
  80. if (channel != null)
  81. {
  82. dto.ChannelName = channel.Name;
  83. if (channel.HasImage(ImageType.Primary))
  84. {
  85. dto.ChannelPrimaryImageTag = GetImageTag(channel);
  86. }
  87. }
  88. return dto;
  89. }
  90. public SeriesTimerInfoDto GetSeriesTimerInfoDto(SeriesTimerInfo info, ILiveTvService service, string channelName)
  91. {
  92. var dto = new SeriesTimerInfoDto
  93. {
  94. Id = GetInternalSeriesTimerId(info.Id).ToString("N"),
  95. Overview = info.Overview,
  96. EndDate = info.EndDate,
  97. Name = info.Name,
  98. StartDate = info.StartDate,
  99. ExternalId = info.Id,
  100. PrePaddingSeconds = info.PrePaddingSeconds,
  101. PostPaddingSeconds = info.PostPaddingSeconds,
  102. IsPostPaddingRequired = info.IsPostPaddingRequired,
  103. IsPrePaddingRequired = info.IsPrePaddingRequired,
  104. Days = info.Days.ToArray(),
  105. Priority = info.Priority,
  106. RecordAnyChannel = info.RecordAnyChannel,
  107. RecordAnyTime = info.RecordAnyTime,
  108. SkipEpisodesInLibrary = info.SkipEpisodesInLibrary,
  109. KeepUpTo = info.KeepUpTo,
  110. KeepUntil = info.KeepUntil,
  111. RecordNewOnly = info.RecordNewOnly,
  112. ExternalChannelId = info.ChannelId,
  113. ExternalProgramId = info.ProgramId,
  114. ServiceName = service.Name,
  115. ChannelName = channelName,
  116. ServerId = _appHost.SystemId
  117. };
  118. if (!string.IsNullOrEmpty(info.ChannelId))
  119. {
  120. dto.ChannelId = GetInternalChannelId(service.Name, info.ChannelId);
  121. }
  122. if (!string.IsNullOrEmpty(info.ProgramId))
  123. {
  124. dto.ProgramId = GetInternalProgramId(info.ProgramId).ToString("N");
  125. }
  126. dto.DayPattern = info.Days == null ? null : GetDayPattern(info.Days.ToArray());
  127. FillImages(dto, info.Name, info.SeriesId);
  128. return dto;
  129. }
  130. private void FillImages(BaseItemDto dto, string seriesName, string programSeriesId)
  131. {
  132. var librarySeries = _libraryManager.GetItemList(new InternalItemsQuery
  133. {
  134. IncludeItemTypes = new string[] { typeof(Series).Name },
  135. Name = seriesName,
  136. Limit = 1,
  137. ImageTypes = new ImageType[] { ImageType.Thumb },
  138. DtoOptions = new DtoOptions(false)
  139. }).FirstOrDefault();
  140. if (librarySeries != null)
  141. {
  142. var image = librarySeries.GetImageInfo(ImageType.Thumb, 0);
  143. if (image != null)
  144. {
  145. try
  146. {
  147. dto.ParentThumbImageTag = _imageProcessor.GetImageCacheTag(librarySeries, image);
  148. dto.ParentThumbItemId = librarySeries.Id.ToString("N");
  149. }
  150. catch (Exception ex)
  151. {
  152. _logger.LogError(ex, "Error");
  153. }
  154. }
  155. image = librarySeries.GetImageInfo(ImageType.Backdrop, 0);
  156. if (image != null)
  157. {
  158. try
  159. {
  160. dto.ParentBackdropImageTags = new string[]
  161. {
  162. _imageProcessor.GetImageCacheTag(librarySeries, image)
  163. };
  164. dto.ParentBackdropItemId = librarySeries.Id.ToString("N");
  165. }
  166. catch (Exception ex)
  167. {
  168. _logger.LogError(ex, "Error");
  169. }
  170. }
  171. }
  172. var program = _libraryManager.GetItemList(new InternalItemsQuery
  173. {
  174. IncludeItemTypes = new string[] { typeof(LiveTvProgram).Name },
  175. ExternalSeriesId = programSeriesId,
  176. Limit = 1,
  177. ImageTypes = new ImageType[] { ImageType.Primary },
  178. DtoOptions = new DtoOptions(false),
  179. Name = string.IsNullOrEmpty(programSeriesId) ? seriesName : null
  180. }).FirstOrDefault();
  181. if (program != null)
  182. {
  183. var image = program.GetImageInfo(ImageType.Primary, 0);
  184. if (image != null)
  185. {
  186. try
  187. {
  188. dto.ParentPrimaryImageTag = _imageProcessor.GetImageCacheTag(program, image);
  189. dto.ParentPrimaryImageItemId = program.Id.ToString("N");
  190. }
  191. catch (Exception ex)
  192. {
  193. _logger.LogError(ex, "Error");
  194. }
  195. }
  196. if (dto.ParentBackdropImageTags == null || dto.ParentBackdropImageTags.Length == 0)
  197. {
  198. image = program.GetImageInfo(ImageType.Backdrop, 0);
  199. if (image != null)
  200. {
  201. try
  202. {
  203. dto.ParentBackdropImageTags = new string[]
  204. {
  205. _imageProcessor.GetImageCacheTag(program, image)
  206. };
  207. dto.ParentBackdropItemId = program.Id.ToString("N");
  208. }
  209. catch (Exception ex)
  210. {
  211. _logger.LogError(ex, "Error");
  212. }
  213. }
  214. }
  215. }
  216. }
  217. private void FillImages(SeriesTimerInfoDto dto, string seriesName, string programSeriesId)
  218. {
  219. var librarySeries = _libraryManager.GetItemList(new InternalItemsQuery
  220. {
  221. IncludeItemTypes = new string[] { typeof(Series).Name },
  222. Name = seriesName,
  223. Limit = 1,
  224. ImageTypes = new ImageType[] { ImageType.Thumb },
  225. DtoOptions = new DtoOptions(false)
  226. }).FirstOrDefault();
  227. if (librarySeries != null)
  228. {
  229. var image = librarySeries.GetImageInfo(ImageType.Thumb, 0);
  230. if (image != null)
  231. {
  232. try
  233. {
  234. dto.ParentThumbImageTag = _imageProcessor.GetImageCacheTag(librarySeries, image);
  235. dto.ParentThumbItemId = librarySeries.Id.ToString("N");
  236. }
  237. catch (Exception ex)
  238. {
  239. _logger.LogError(ex, "Error");
  240. }
  241. }
  242. image = librarySeries.GetImageInfo(ImageType.Backdrop, 0);
  243. if (image != null)
  244. {
  245. try
  246. {
  247. dto.ParentBackdropImageTags = new string[]
  248. {
  249. _imageProcessor.GetImageCacheTag(librarySeries, image)
  250. };
  251. dto.ParentBackdropItemId = librarySeries.Id.ToString("N");
  252. }
  253. catch (Exception ex)
  254. {
  255. _logger.LogError(ex, "Error");
  256. }
  257. }
  258. }
  259. var program = _libraryManager.GetItemList(new InternalItemsQuery
  260. {
  261. IncludeItemTypes = new string[] { typeof(Series).Name },
  262. Name = seriesName,
  263. Limit = 1,
  264. ImageTypes = new ImageType[] { ImageType.Primary },
  265. DtoOptions = new DtoOptions(false)
  266. }).FirstOrDefault();
  267. if (program == null)
  268. {
  269. program = _libraryManager.GetItemList(new InternalItemsQuery
  270. {
  271. IncludeItemTypes = new string[] { typeof(LiveTvProgram).Name },
  272. ExternalSeriesId = programSeriesId,
  273. Limit = 1,
  274. ImageTypes = new ImageType[] { ImageType.Primary },
  275. DtoOptions = new DtoOptions(false),
  276. Name = string.IsNullOrEmpty(programSeriesId) ? seriesName : null
  277. }).FirstOrDefault();
  278. }
  279. if (program != null)
  280. {
  281. var image = program.GetImageInfo(ImageType.Primary, 0);
  282. if (image != null)
  283. {
  284. try
  285. {
  286. dto.ParentPrimaryImageTag = _imageProcessor.GetImageCacheTag(program, image);
  287. dto.ParentPrimaryImageItemId = program.Id.ToString("N");
  288. }
  289. catch (Exception ex)
  290. {
  291. }
  292. }
  293. if (dto.ParentBackdropImageTags == null || dto.ParentBackdropImageTags.Length == 0)
  294. {
  295. image = program.GetImageInfo(ImageType.Backdrop, 0);
  296. if (image != null)
  297. {
  298. try
  299. {
  300. dto.ParentBackdropImageTags = new string[]
  301. {
  302. _imageProcessor.GetImageCacheTag(program, image)
  303. };
  304. dto.ParentBackdropItemId = program.Id.ToString("N");
  305. }
  306. catch (Exception ex)
  307. {
  308. _logger.LogError(ex, "Error");
  309. }
  310. }
  311. }
  312. }
  313. }
  314. public DayPattern? GetDayPattern(DayOfWeek[] days)
  315. {
  316. DayPattern? pattern = null;
  317. if (days.Length > 0)
  318. {
  319. if (days.Length == 7)
  320. {
  321. pattern = DayPattern.Daily;
  322. }
  323. else if (days.Length == 2)
  324. {
  325. if (days.Contains(DayOfWeek.Saturday) && days.Contains(DayOfWeek.Sunday))
  326. {
  327. pattern = DayPattern.Weekends;
  328. }
  329. }
  330. else if (days.Length == 5)
  331. {
  332. if (days.Contains(DayOfWeek.Monday) && days.Contains(DayOfWeek.Tuesday) && days.Contains(DayOfWeek.Wednesday) && days.Contains(DayOfWeek.Thursday) && days.Contains(DayOfWeek.Friday))
  333. {
  334. pattern = DayPattern.Weekdays;
  335. }
  336. }
  337. }
  338. return pattern;
  339. }
  340. internal string GetImageTag(BaseItem info)
  341. {
  342. try
  343. {
  344. return _imageProcessor.GetImageCacheTag(info, ImageType.Primary);
  345. }
  346. catch (Exception ex)
  347. {
  348. _logger.LogError(ex, "Error getting image info for {name}", info.Name);
  349. }
  350. return null;
  351. }
  352. private const string InternalVersionNumber = "4";
  353. public Guid GetInternalChannelId(string serviceName, string externalId)
  354. {
  355. var name = serviceName + externalId + InternalVersionNumber;
  356. return _libraryManager.GetNewItemId(name.ToLower(), typeof(LiveTvChannel));
  357. }
  358. private const string ServiceName = "Emby";
  359. public string GetInternalTimerId(string externalId)
  360. {
  361. var name = ServiceName + externalId + InternalVersionNumber;
  362. return name.ToLower().GetMD5().ToString("N");
  363. }
  364. public Guid GetInternalSeriesTimerId(string externalId)
  365. {
  366. var name = ServiceName + externalId + InternalVersionNumber;
  367. return name.ToLower().GetMD5();
  368. }
  369. public Guid GetInternalProgramId(string externalId)
  370. {
  371. var name = ServiceName + externalId + InternalVersionNumber;
  372. return _libraryManager.GetNewItemId(name.ToLower(), typeof(LiveTvProgram));
  373. }
  374. public async Task<TimerInfo> GetTimerInfo(TimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  375. {
  376. var info = new TimerInfo
  377. {
  378. Overview = dto.Overview,
  379. EndDate = dto.EndDate,
  380. Name = dto.Name,
  381. StartDate = dto.StartDate,
  382. Status = dto.Status,
  383. PrePaddingSeconds = dto.PrePaddingSeconds,
  384. PostPaddingSeconds = dto.PostPaddingSeconds,
  385. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  386. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  387. KeepUntil = dto.KeepUntil,
  388. Priority = dto.Priority,
  389. SeriesTimerId = dto.ExternalSeriesTimerId,
  390. ProgramId = dto.ExternalProgramId,
  391. ChannelId = dto.ExternalChannelId,
  392. Id = dto.ExternalId
  393. };
  394. // Convert internal server id's to external tv provider id's
  395. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  396. {
  397. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  398. info.Id = timer.ExternalId;
  399. }
  400. if (!dto.ChannelId.Equals(Guid.Empty) && string.IsNullOrEmpty(info.ChannelId))
  401. {
  402. var channel = _libraryManager.GetItemById(dto.ChannelId);
  403. if (channel != null)
  404. {
  405. info.ChannelId = channel.ExternalId;
  406. }
  407. }
  408. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  409. {
  410. var program = _libraryManager.GetItemById(dto.ProgramId);
  411. if (program != null)
  412. {
  413. info.ProgramId = program.ExternalId;
  414. }
  415. }
  416. if (!string.IsNullOrEmpty(dto.SeriesTimerId) && string.IsNullOrEmpty(info.SeriesTimerId))
  417. {
  418. var timer = await liveTv.GetSeriesTimer(dto.SeriesTimerId, cancellationToken).ConfigureAwait(false);
  419. if (timer != null)
  420. {
  421. info.SeriesTimerId = timer.ExternalId;
  422. }
  423. }
  424. return info;
  425. }
  426. public async Task<SeriesTimerInfo> GetSeriesTimerInfo(SeriesTimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  427. {
  428. var info = new SeriesTimerInfo
  429. {
  430. Overview = dto.Overview,
  431. EndDate = dto.EndDate,
  432. Name = dto.Name,
  433. StartDate = dto.StartDate,
  434. PrePaddingSeconds = dto.PrePaddingSeconds,
  435. PostPaddingSeconds = dto.PostPaddingSeconds,
  436. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  437. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  438. Days = dto.Days.ToList(),
  439. Priority = dto.Priority,
  440. RecordAnyChannel = dto.RecordAnyChannel,
  441. RecordAnyTime = dto.RecordAnyTime,
  442. SkipEpisodesInLibrary = dto.SkipEpisodesInLibrary,
  443. KeepUpTo = dto.KeepUpTo,
  444. KeepUntil = dto.KeepUntil,
  445. RecordNewOnly = dto.RecordNewOnly,
  446. ProgramId = dto.ExternalProgramId,
  447. ChannelId = dto.ExternalChannelId,
  448. Id = dto.ExternalId
  449. };
  450. // Convert internal server id's to external tv provider id's
  451. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  452. {
  453. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  454. info.Id = timer.ExternalId;
  455. }
  456. if (!dto.ChannelId.Equals(Guid.Empty) && string.IsNullOrEmpty(info.ChannelId))
  457. {
  458. var channel = _libraryManager.GetItemById(dto.ChannelId);
  459. if (channel != null)
  460. {
  461. info.ChannelId = channel.ExternalId;
  462. }
  463. }
  464. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  465. {
  466. var program = _libraryManager.GetItemById(dto.ProgramId);
  467. if (program != null)
  468. {
  469. info.ProgramId = program.ExternalId;
  470. }
  471. }
  472. return info;
  473. }
  474. }
  475. }