LiveTvDtoService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 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. }
  153. }
  154. image = librarySeries.GetImageInfo(ImageType.Backdrop, 0);
  155. if (image != null)
  156. {
  157. try
  158. {
  159. dto.ParentBackdropImageTags = new string[]
  160. {
  161. _imageProcessor.GetImageCacheTag(librarySeries, image)
  162. };
  163. dto.ParentBackdropItemId = librarySeries.Id.ToString("N");
  164. }
  165. catch (Exception ex)
  166. {
  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. }
  192. }
  193. if (dto.ParentBackdropImageTags == null || dto.ParentBackdropImageTags.Length == 0)
  194. {
  195. image = program.GetImageInfo(ImageType.Backdrop, 0);
  196. if (image != null)
  197. {
  198. try
  199. {
  200. dto.ParentBackdropImageTags = new string[]
  201. {
  202. _imageProcessor.GetImageCacheTag(program, image)
  203. };
  204. dto.ParentBackdropItemId = program.Id.ToString("N");
  205. }
  206. catch (Exception ex)
  207. {
  208. }
  209. }
  210. }
  211. }
  212. }
  213. private void FillImages(SeriesTimerInfoDto dto, string seriesName, string programSeriesId)
  214. {
  215. var librarySeries = _libraryManager.GetItemList(new InternalItemsQuery
  216. {
  217. IncludeItemTypes = new string[] { typeof(Series).Name },
  218. Name = seriesName,
  219. Limit = 1,
  220. ImageTypes = new ImageType[] { ImageType.Thumb },
  221. DtoOptions = new DtoOptions(false)
  222. }).FirstOrDefault();
  223. if (librarySeries != null)
  224. {
  225. var image = librarySeries.GetImageInfo(ImageType.Thumb, 0);
  226. if (image != null)
  227. {
  228. try
  229. {
  230. dto.ParentThumbImageTag = _imageProcessor.GetImageCacheTag(librarySeries, image);
  231. dto.ParentThumbItemId = librarySeries.Id.ToString("N");
  232. }
  233. catch (Exception ex)
  234. {
  235. }
  236. }
  237. image = librarySeries.GetImageInfo(ImageType.Backdrop, 0);
  238. if (image != null)
  239. {
  240. try
  241. {
  242. dto.ParentBackdropImageTags = new string[]
  243. {
  244. _imageProcessor.GetImageCacheTag(librarySeries, image)
  245. };
  246. dto.ParentBackdropItemId = librarySeries.Id.ToString("N");
  247. }
  248. catch (Exception ex)
  249. {
  250. }
  251. }
  252. }
  253. var program = _libraryManager.GetItemList(new InternalItemsQuery
  254. {
  255. IncludeItemTypes = new string[] { typeof(Series).Name },
  256. Name = seriesName,
  257. Limit = 1,
  258. ImageTypes = new ImageType[] { ImageType.Primary },
  259. DtoOptions = new DtoOptions(false)
  260. }).FirstOrDefault();
  261. if (program == null)
  262. {
  263. program = _libraryManager.GetItemList(new InternalItemsQuery
  264. {
  265. IncludeItemTypes = new string[] { typeof(LiveTvProgram).Name },
  266. ExternalSeriesId = programSeriesId,
  267. Limit = 1,
  268. ImageTypes = new ImageType[] { ImageType.Primary },
  269. DtoOptions = new DtoOptions(false),
  270. Name = string.IsNullOrEmpty(programSeriesId) ? seriesName : null
  271. }).FirstOrDefault();
  272. }
  273. if (program != null)
  274. {
  275. var image = program.GetImageInfo(ImageType.Primary, 0);
  276. if (image != null)
  277. {
  278. try
  279. {
  280. dto.ParentPrimaryImageTag = _imageProcessor.GetImageCacheTag(program, image);
  281. dto.ParentPrimaryImageItemId = program.Id.ToString("N");
  282. }
  283. catch (Exception ex)
  284. {
  285. }
  286. }
  287. if (dto.ParentBackdropImageTags == null || dto.ParentBackdropImageTags.Length == 0)
  288. {
  289. image = program.GetImageInfo(ImageType.Backdrop, 0);
  290. if (image != null)
  291. {
  292. try
  293. {
  294. dto.ParentBackdropImageTags = new string[]
  295. {
  296. _imageProcessor.GetImageCacheTag(program, image)
  297. };
  298. dto.ParentBackdropItemId = program.Id.ToString("N");
  299. }
  300. catch (Exception ex)
  301. {
  302. }
  303. }
  304. }
  305. }
  306. }
  307. public DayPattern? GetDayPattern(DayOfWeek[] days)
  308. {
  309. DayPattern? pattern = null;
  310. if (days.Length > 0)
  311. {
  312. if (days.Length == 7)
  313. {
  314. pattern = DayPattern.Daily;
  315. }
  316. else if (days.Length == 2)
  317. {
  318. if (days.Contains(DayOfWeek.Saturday) && days.Contains(DayOfWeek.Sunday))
  319. {
  320. pattern = DayPattern.Weekends;
  321. }
  322. }
  323. else if (days.Length == 5)
  324. {
  325. if (days.Contains(DayOfWeek.Monday) && days.Contains(DayOfWeek.Tuesday) && days.Contains(DayOfWeek.Wednesday) && days.Contains(DayOfWeek.Thursday) && days.Contains(DayOfWeek.Friday))
  326. {
  327. pattern = DayPattern.Weekdays;
  328. }
  329. }
  330. }
  331. return pattern;
  332. }
  333. internal string GetImageTag(BaseItem info)
  334. {
  335. try
  336. {
  337. return _imageProcessor.GetImageCacheTag(info, ImageType.Primary);
  338. }
  339. catch (Exception ex)
  340. {
  341. _logger.ErrorException("Error getting image info for {0}", ex, info.Name);
  342. }
  343. return null;
  344. }
  345. private const string InternalVersionNumber = "4";
  346. public Guid GetInternalChannelId(string serviceName, string externalId)
  347. {
  348. var name = serviceName + externalId + InternalVersionNumber;
  349. return _libraryManager.GetNewItemId(name.ToLower(), typeof(LiveTvChannel));
  350. }
  351. private const string ServiceName = "Emby";
  352. public string GetInternalTimerId(string externalId)
  353. {
  354. var name = ServiceName + externalId + InternalVersionNumber;
  355. return name.ToLower().GetMD5().ToString("N");
  356. }
  357. public Guid GetInternalSeriesTimerId(string externalId)
  358. {
  359. var name = ServiceName + externalId + InternalVersionNumber;
  360. return name.ToLower().GetMD5();
  361. }
  362. public Guid GetInternalProgramId(string externalId)
  363. {
  364. var name = ServiceName + externalId + InternalVersionNumber;
  365. return _libraryManager.GetNewItemId(name.ToLower(), typeof(LiveTvProgram));
  366. }
  367. public async Task<TimerInfo> GetTimerInfo(TimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  368. {
  369. var info = new TimerInfo
  370. {
  371. Overview = dto.Overview,
  372. EndDate = dto.EndDate,
  373. Name = dto.Name,
  374. StartDate = dto.StartDate,
  375. Status = dto.Status,
  376. PrePaddingSeconds = dto.PrePaddingSeconds,
  377. PostPaddingSeconds = dto.PostPaddingSeconds,
  378. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  379. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  380. KeepUntil = dto.KeepUntil,
  381. Priority = dto.Priority,
  382. SeriesTimerId = dto.ExternalSeriesTimerId,
  383. ProgramId = dto.ExternalProgramId,
  384. ChannelId = dto.ExternalChannelId,
  385. Id = dto.ExternalId
  386. };
  387. // Convert internal server id's to external tv provider id's
  388. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  389. {
  390. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  391. info.Id = timer.ExternalId;
  392. }
  393. if (!dto.ChannelId.Equals(Guid.Empty) && string.IsNullOrEmpty(info.ChannelId))
  394. {
  395. var channel = _libraryManager.GetItemById(dto.ChannelId);
  396. if (channel != null)
  397. {
  398. info.ChannelId = channel.ExternalId;
  399. }
  400. }
  401. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  402. {
  403. var program = _libraryManager.GetItemById(dto.ProgramId);
  404. if (program != null)
  405. {
  406. info.ProgramId = program.ExternalId;
  407. }
  408. }
  409. if (!string.IsNullOrEmpty(dto.SeriesTimerId) && string.IsNullOrEmpty(info.SeriesTimerId))
  410. {
  411. var timer = await liveTv.GetSeriesTimer(dto.SeriesTimerId, cancellationToken).ConfigureAwait(false);
  412. if (timer != null)
  413. {
  414. info.SeriesTimerId = timer.ExternalId;
  415. }
  416. }
  417. return info;
  418. }
  419. public async Task<SeriesTimerInfo> GetSeriesTimerInfo(SeriesTimerInfoDto dto, bool isNew, LiveTvManager liveTv, CancellationToken cancellationToken)
  420. {
  421. var info = new SeriesTimerInfo
  422. {
  423. Overview = dto.Overview,
  424. EndDate = dto.EndDate,
  425. Name = dto.Name,
  426. StartDate = dto.StartDate,
  427. PrePaddingSeconds = dto.PrePaddingSeconds,
  428. PostPaddingSeconds = dto.PostPaddingSeconds,
  429. IsPostPaddingRequired = dto.IsPostPaddingRequired,
  430. IsPrePaddingRequired = dto.IsPrePaddingRequired,
  431. Days = dto.Days.ToList(),
  432. Priority = dto.Priority,
  433. RecordAnyChannel = dto.RecordAnyChannel,
  434. RecordAnyTime = dto.RecordAnyTime,
  435. SkipEpisodesInLibrary = dto.SkipEpisodesInLibrary,
  436. KeepUpTo = dto.KeepUpTo,
  437. KeepUntil = dto.KeepUntil,
  438. RecordNewOnly = dto.RecordNewOnly,
  439. ProgramId = dto.ExternalProgramId,
  440. ChannelId = dto.ExternalChannelId,
  441. Id = dto.ExternalId
  442. };
  443. // Convert internal server id's to external tv provider id's
  444. if (!isNew && !string.IsNullOrEmpty(dto.Id) && string.IsNullOrEmpty(info.Id))
  445. {
  446. var timer = await liveTv.GetSeriesTimer(dto.Id, cancellationToken).ConfigureAwait(false);
  447. info.Id = timer.ExternalId;
  448. }
  449. if (!dto.ChannelId.Equals(Guid.Empty) && string.IsNullOrEmpty(info.ChannelId))
  450. {
  451. var channel = _libraryManager.GetItemById(dto.ChannelId);
  452. if (channel != null)
  453. {
  454. info.ChannelId = channel.ExternalId;
  455. }
  456. }
  457. if (!string.IsNullOrEmpty(dto.ProgramId) && string.IsNullOrEmpty(info.ProgramId))
  458. {
  459. var program = _libraryManager.GetItemById(dto.ProgramId);
  460. if (program != null)
  461. {
  462. info.ProgramId = program.ExternalId;
  463. }
  464. }
  465. return info;
  466. }
  467. }
  468. }