LiveTvDtoService.cs 20 KB

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