LiveTvDtoService.cs 20 KB

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