LiveTvDtoService.cs 21 KB

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