LiveTvDtoService.cs 22 KB

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