LiveTvService.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using MediaBrowser.Controller.Dto;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.LiveTv;
  4. using MediaBrowser.Controller.Net;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.LiveTv;
  8. using MediaBrowser.Model.Querying;
  9. using ServiceStack;
  10. using System;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Api.LiveTv
  16. {
  17. /// <summary>
  18. /// This is insecure right now to avoid windows phone refactoring
  19. /// </summary>
  20. [Route("/LiveTv/Info", "GET", Summary = "Gets available live tv services.")]
  21. [Authenticated]
  22. public class GetLiveTvInfo : IReturn<LiveTvInfo>
  23. {
  24. }
  25. [Route("/LiveTv/Channels", "GET", Summary = "Gets available live tv channels.")]
  26. [Authenticated]
  27. public class GetChannels : IReturn<QueryResult<ChannelInfoDto>>
  28. {
  29. [ApiMember(Name = "Type", Description = "Optional filter by channel type.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  30. public ChannelType? Type { get; set; }
  31. [ApiMember(Name = "UserId", Description = "Optional filter by user and attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  32. public string UserId { get; set; }
  33. /// <summary>
  34. /// Skips over a given number of items within the results. Use for paging.
  35. /// </summary>
  36. /// <value>The start index.</value>
  37. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  38. public int? StartIndex { get; set; }
  39. /// <summary>
  40. /// The maximum number of items to return
  41. /// </summary>
  42. /// <value>The limit.</value>
  43. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  44. public int? Limit { get; set; }
  45. [ApiMember(Name = "IsFavorite", Description = "Filter by channels that are favorites, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  46. public bool? IsFavorite { get; set; }
  47. [ApiMember(Name = "IsLiked", Description = "Filter by channels that are liked, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  48. public bool? IsLiked { get; set; }
  49. [ApiMember(Name = "IsDisliked", Description = "Filter by channels that are disliked, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  50. public bool? IsDisliked { get; set; }
  51. [ApiMember(Name = "EnableFavoriteSorting", Description = "Incorporate favorite and like status into channel sorting.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  52. public bool EnableFavoriteSorting { get; set; }
  53. }
  54. [Route("/LiveTv/Channels/{Id}", "GET", Summary = "Gets a live tv channel")]
  55. [Authenticated]
  56. public class GetChannel : IReturn<ChannelInfoDto>
  57. {
  58. /// <summary>
  59. /// Gets or sets the id.
  60. /// </summary>
  61. /// <value>The id.</value>
  62. [ApiMember(Name = "Id", Description = "Channel Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  63. public string Id { get; set; }
  64. [ApiMember(Name = "UserId", Description = "Optional attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  65. public string UserId { get; set; }
  66. }
  67. [Route("/LiveTv/Recordings", "GET", Summary = "Gets live tv recordings")]
  68. [Authenticated]
  69. public class GetRecordings : IReturn<QueryResult<RecordingInfoDto>>
  70. {
  71. [ApiMember(Name = "ChannelId", Description = "Optional filter by channel id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  72. public string ChannelId { get; set; }
  73. [ApiMember(Name = "UserId", Description = "Optional filter by user and attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  74. public string UserId { get; set; }
  75. [ApiMember(Name = "GroupId", Description = "Optional filter by recording group.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  76. public string GroupId { get; set; }
  77. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  78. public int? StartIndex { get; set; }
  79. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  80. public int? Limit { get; set; }
  81. [ApiMember(Name = "Status", Description = "Optional filter by recording status.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  82. public RecordingStatus? Status { get; set; }
  83. [ApiMember(Name = "Status", Description = "Optional filter by recordings that are in progress, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  84. public bool? IsInProgress { get; set; }
  85. [ApiMember(Name = "SeriesTimerId", Description = "Optional filter by recordings belonging to a series timer", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  86. public string SeriesTimerId { get; set; }
  87. }
  88. [Route("/LiveTv/Recordings/Groups", "GET", Summary = "Gets live tv recording groups")]
  89. [Authenticated]
  90. public class GetRecordingGroups : IReturn<QueryResult<RecordingGroupDto>>
  91. {
  92. [ApiMember(Name = "UserId", Description = "Optional filter by user and attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  93. public string UserId { get; set; }
  94. }
  95. [Route("/LiveTv/Recordings/{Id}", "GET", Summary = "Gets a live tv recording")]
  96. [Authenticated]
  97. public class GetRecording : IReturn<RecordingInfoDto>
  98. {
  99. [ApiMember(Name = "Id", Description = "Recording Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  100. public string Id { get; set; }
  101. [ApiMember(Name = "UserId", Description = "Optional attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  102. public string UserId { get; set; }
  103. }
  104. [Route("/LiveTv/Tuners/{Id}/Reset", "POST", Summary = "Resets a tv tuner")]
  105. [Authenticated]
  106. public class ResetTuner : IReturnVoid
  107. {
  108. [ApiMember(Name = "Id", Description = "Tuner Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  109. public string Id { get; set; }
  110. }
  111. [Route("/LiveTv/Timers/{Id}", "GET", Summary = "Gets a live tv timer")]
  112. [Authenticated]
  113. public class GetTimer : IReturn<TimerInfoDto>
  114. {
  115. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  116. public string Id { get; set; }
  117. }
  118. [Route("/LiveTv/Timers/Defaults", "GET", Summary = "Gets default values for a new timer")]
  119. [Authenticated]
  120. public class GetDefaultTimer : IReturn<SeriesTimerInfoDto>
  121. {
  122. [ApiMember(Name = "ProgramId", Description = "Optional, to attach default values based on a program.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  123. public string ProgramId { get; set; }
  124. }
  125. [Route("/LiveTv/Timers", "GET", Summary = "Gets live tv timers")]
  126. [Authenticated]
  127. public class GetTimers : IReturn<QueryResult<TimerInfoDto>>
  128. {
  129. [ApiMember(Name = "ChannelId", Description = "Optional filter by channel id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  130. public string ChannelId { get; set; }
  131. [ApiMember(Name = "SeriesTimerId", Description = "Optional filter by timers belonging to a series timer", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  132. public string SeriesTimerId { get; set; }
  133. }
  134. [Route("/LiveTv/Programs", "GET,POST", Summary = "Gets available live tv epgs..")]
  135. [Authenticated]
  136. public class GetPrograms : IReturn<QueryResult<ProgramInfoDto>>
  137. {
  138. [ApiMember(Name = "ChannelIds", Description = "The channels to return guide information for.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  139. public string ChannelIds { get; set; }
  140. [ApiMember(Name = "UserId", Description = "Optional filter by user id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  141. public string UserId { get; set; }
  142. [ApiMember(Name = "MinStartDate", Description = "Optional. The minimum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  143. public string MinStartDate { get; set; }
  144. [ApiMember(Name = "HasAired", Description = "Optional. Filter by programs that have completed airing, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  145. public bool? HasAired { get; set; }
  146. [ApiMember(Name = "MaxStartDate", Description = "Optional. The maximum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  147. public string MaxStartDate { get; set; }
  148. [ApiMember(Name = "MinEndDate", Description = "Optional. The minimum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  149. public string MinEndDate { get; set; }
  150. [ApiMember(Name = "MaxEndDate", Description = "Optional. The maximum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  151. public string MaxEndDate { get; set; }
  152. [ApiMember(Name = "IsMovie", Description = "Optional filter for movies.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  153. public bool? IsMovie { get; set; }
  154. [ApiMember(Name = "IsSports", Description = "Optional filter for sports.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  155. public bool? IsSports { get; set; }
  156. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  157. public int? StartIndex { get; set; }
  158. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  159. public int? Limit { get; set; }
  160. [ApiMember(Name = "SortBy", Description = "Optional. Specify one or more sort orders, comma delimeted. Options: Name, StartDate", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  161. public string SortBy { get; set; }
  162. [ApiMember(Name = "SortOrder", Description = "Sort Order - Ascending,Descending", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  163. public SortOrder? SortOrder { get; set; }
  164. [ApiMember(Name = "Genres", Description = "The genres to return guide information for.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  165. public string Genres { get; set; }
  166. }
  167. [Route("/LiveTv/Programs/Recommended", "GET", Summary = "Gets available live tv epgs..")]
  168. [Authenticated]
  169. public class GetRecommendedPrograms : IReturn<QueryResult<ProgramInfoDto>>
  170. {
  171. [ApiMember(Name = "UserId", Description = "Optional filter by user id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  172. public string UserId { get; set; }
  173. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  174. public int? Limit { get; set; }
  175. [ApiMember(Name = "IsAiring", Description = "Optional. Filter by programs that are currently airing, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  176. public bool? IsAiring { get; set; }
  177. [ApiMember(Name = "HasAired", Description = "Optional. Filter by programs that have completed airing, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  178. public bool? HasAired { get; set; }
  179. [ApiMember(Name = "IsSports", Description = "Optional filter for sports.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET,POST")]
  180. public bool? IsSports { get; set; }
  181. [ApiMember(Name = "IsMovie", Description = "Optional filter for movies.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  182. public bool? IsMovie { get; set; }
  183. }
  184. [Route("/LiveTv/Programs/{Id}", "GET", Summary = "Gets a live tv program")]
  185. [Authenticated]
  186. public class GetProgram : IReturn<ProgramInfoDto>
  187. {
  188. [ApiMember(Name = "Id", Description = "Program Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  189. public string Id { get; set; }
  190. [ApiMember(Name = "UserId", Description = "Optional attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  191. public string UserId { get; set; }
  192. }
  193. [Route("/LiveTv/Recordings/{Id}", "DELETE", Summary = "Deletes a live tv recording")]
  194. [Authenticated]
  195. public class DeleteRecording : IReturnVoid
  196. {
  197. [ApiMember(Name = "Id", Description = "Recording Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  198. public string Id { get; set; }
  199. }
  200. [Route("/LiveTv/Timers/{Id}", "DELETE", Summary = "Cancels a live tv timer")]
  201. [Authenticated]
  202. public class CancelTimer : IReturnVoid
  203. {
  204. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  205. public string Id { get; set; }
  206. }
  207. [Route("/LiveTv/Timers/{Id}", "POST", Summary = "Updates a live tv timer")]
  208. [Authenticated]
  209. public class UpdateTimer : TimerInfoDto, IReturnVoid
  210. {
  211. }
  212. [Route("/LiveTv/Timers", "POST", Summary = "Creates a live tv timer")]
  213. [Authenticated]
  214. public class CreateTimer : TimerInfoDto, IReturnVoid
  215. {
  216. }
  217. [Route("/LiveTv/SeriesTimers/{Id}", "GET", Summary = "Gets a live tv series timer")]
  218. [Authenticated]
  219. public class GetSeriesTimer : IReturn<TimerInfoDto>
  220. {
  221. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  222. public string Id { get; set; }
  223. }
  224. [Route("/LiveTv/SeriesTimers", "GET", Summary = "Gets live tv series timers")]
  225. [Authenticated]
  226. public class GetSeriesTimers : IReturn<QueryResult<SeriesTimerInfoDto>>
  227. {
  228. [ApiMember(Name = "SortBy", Description = "Optional. Sort by SortName or Priority", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  229. public string SortBy { get; set; }
  230. [ApiMember(Name = "SortOrder", Description = "Optional. Sort in Ascending or Descending order", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  231. public SortOrder SortOrder { get; set; }
  232. }
  233. [Route("/LiveTv/SeriesTimers/{Id}", "DELETE", Summary = "Cancels a live tv series timer")]
  234. [Authenticated]
  235. public class CancelSeriesTimer : IReturnVoid
  236. {
  237. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  238. public string Id { get; set; }
  239. }
  240. [Route("/LiveTv/SeriesTimers/{Id}", "POST", Summary = "Updates a live tv series timer")]
  241. [Authenticated]
  242. public class UpdateSeriesTimer : SeriesTimerInfoDto, IReturnVoid
  243. {
  244. }
  245. [Route("/LiveTv/SeriesTimers", "POST", Summary = "Creates a live tv series timer")]
  246. [Authenticated]
  247. public class CreateSeriesTimer : SeriesTimerInfoDto, IReturnVoid
  248. {
  249. }
  250. [Route("/LiveTv/Recordings/Groups/{Id}", "GET", Summary = "Gets a recording group")]
  251. [Authenticated]
  252. public class GetRecordingGroup : IReturn<RecordingGroupDto>
  253. {
  254. [ApiMember(Name = "Id", Description = "Recording group Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  255. public string Id { get; set; }
  256. }
  257. [Route("/LiveTv/GuideInfo", "GET", Summary = "Gets guide info")]
  258. [Authenticated]
  259. public class GetGuideInfo : IReturn<GuideInfo>
  260. {
  261. }
  262. [Route("/LiveTv/Folder", "GET", Summary = "Gets the users live tv folder, along with configured images")]
  263. [Authenticated]
  264. public class GetLiveTvFolder : IReturn<BaseItemDto>
  265. {
  266. [ApiMember(Name = "UserId", Description = "Optional attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  267. public string UserId { get; set; }
  268. }
  269. public class LiveTvService : BaseApiService
  270. {
  271. private readonly ILiveTvManager _liveTvManager;
  272. private readonly IUserManager _userManager;
  273. public LiveTvService(ILiveTvManager liveTvManager, IUserManager userManager)
  274. {
  275. _liveTvManager = liveTvManager;
  276. _userManager = userManager;
  277. }
  278. private void AssertUserCanManageLiveTv()
  279. {
  280. var user = SessionContext.GetUser(Request).Result;
  281. if (user == null)
  282. {
  283. throw new UnauthorizedAccessException("Anonymous live tv management is not allowed.");
  284. }
  285. if (!user.Policy.EnableLiveTvManagement)
  286. {
  287. throw new UnauthorizedAccessException("The current user does not have permission to manage live tv.");
  288. }
  289. }
  290. public async Task<object> Get(GetLiveTvInfo request)
  291. {
  292. var info = await _liveTvManager.GetLiveTvInfo(CancellationToken.None).ConfigureAwait(false);
  293. return ToOptimizedSerializedResultUsingCache(info);
  294. }
  295. public async Task<object> Get(GetChannels request)
  296. {
  297. var result = await _liveTvManager.GetChannels(new LiveTvChannelQuery
  298. {
  299. ChannelType = request.Type,
  300. UserId = request.UserId,
  301. StartIndex = request.StartIndex,
  302. Limit = request.Limit,
  303. IsFavorite = request.IsFavorite,
  304. IsLiked = request.IsLiked,
  305. IsDisliked = request.IsDisliked,
  306. EnableFavoriteSorting = request.EnableFavoriteSorting
  307. }, CancellationToken.None).ConfigureAwait(false);
  308. return ToOptimizedSerializedResultUsingCache(result);
  309. }
  310. public async Task<object> Get(GetChannel request)
  311. {
  312. var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(request.UserId);
  313. var result = await _liveTvManager.GetChannel(request.Id, CancellationToken.None, user).ConfigureAwait(false);
  314. return ToOptimizedSerializedResultUsingCache(result);
  315. }
  316. public async Task<object> Get(GetLiveTvFolder request)
  317. {
  318. return ToOptimizedResult(await _liveTvManager.GetLiveTvFolder(request.UserId, CancellationToken.None).ConfigureAwait(false));
  319. }
  320. public async Task<object> Get(GetPrograms request)
  321. {
  322. var query = new ProgramQuery
  323. {
  324. ChannelIds = (request.ChannelIds ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray(),
  325. UserId = request.UserId,
  326. HasAired = request.HasAired
  327. };
  328. if (!string.IsNullOrEmpty(request.MinStartDate))
  329. {
  330. query.MinStartDate = DateTime.Parse(request.MinStartDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  331. }
  332. if (!string.IsNullOrEmpty(request.MinEndDate))
  333. {
  334. query.MinEndDate = DateTime.Parse(request.MinEndDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  335. }
  336. if (!string.IsNullOrEmpty(request.MaxStartDate))
  337. {
  338. query.MaxStartDate = DateTime.Parse(request.MaxStartDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  339. }
  340. if (!string.IsNullOrEmpty(request.MaxEndDate))
  341. {
  342. query.MaxEndDate = DateTime.Parse(request.MaxEndDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  343. }
  344. query.StartIndex = request.StartIndex;
  345. query.Limit = request.Limit;
  346. query.SortBy = (request.SortBy ?? String.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  347. query.SortOrder = request.SortOrder;
  348. query.IsMovie = request.IsMovie;
  349. query.IsSports = request.IsSports;
  350. query.Genres = (request.Genres ?? String.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  351. var result = await _liveTvManager.GetPrograms(query, CancellationToken.None).ConfigureAwait(false);
  352. return ToOptimizedResult(result);
  353. }
  354. public async Task<object> Get(GetRecommendedPrograms request)
  355. {
  356. var query = new RecommendedProgramQuery
  357. {
  358. UserId = request.UserId,
  359. IsAiring = request.IsAiring,
  360. Limit = request.Limit,
  361. HasAired = request.HasAired,
  362. IsMovie = request.IsMovie,
  363. IsSports = request.IsSports
  364. };
  365. var result = await _liveTvManager.GetRecommendedPrograms(query, CancellationToken.None).ConfigureAwait(false);
  366. return ToOptimizedResult(result);
  367. }
  368. public object Post(GetPrograms request)
  369. {
  370. return Get(request);
  371. }
  372. public async Task<object> Get(GetRecordings request)
  373. {
  374. var options = new DtoOptions();
  375. options.DeviceId = AuthorizationContext.GetAuthorizationInfo(Request).DeviceId;
  376. var result = await _liveTvManager.GetRecordings(new RecordingQuery
  377. {
  378. ChannelId = request.ChannelId,
  379. UserId = request.UserId,
  380. GroupId = request.GroupId,
  381. StartIndex = request.StartIndex,
  382. Limit = request.Limit,
  383. Status = request.Status,
  384. SeriesTimerId = request.SeriesTimerId,
  385. IsInProgress = request.IsInProgress
  386. }, options, CancellationToken.None).ConfigureAwait(false);
  387. return ToOptimizedResult(result);
  388. }
  389. public async Task<object> Get(GetRecording request)
  390. {
  391. var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(request.UserId);
  392. var options = new DtoOptions();
  393. options.DeviceId = AuthorizationContext.GetAuthorizationInfo(Request).DeviceId;
  394. var result = await _liveTvManager.GetRecording(request.Id, options, CancellationToken.None, user).ConfigureAwait(false);
  395. return ToOptimizedSerializedResultUsingCache(result);
  396. }
  397. public async Task<object> Get(GetTimer request)
  398. {
  399. var result = await _liveTvManager.GetTimer(request.Id, CancellationToken.None).ConfigureAwait(false);
  400. return ToOptimizedSerializedResultUsingCache(result);
  401. }
  402. public async Task<object> Get(GetTimers request)
  403. {
  404. var result = await _liveTvManager.GetTimers(new TimerQuery
  405. {
  406. ChannelId = request.ChannelId,
  407. SeriesTimerId = request.SeriesTimerId
  408. }, CancellationToken.None).ConfigureAwait(false);
  409. return ToOptimizedSerializedResultUsingCache(result);
  410. }
  411. public void Delete(DeleteRecording request)
  412. {
  413. AssertUserCanManageLiveTv();
  414. var task = _liveTvManager.DeleteRecording(request.Id);
  415. Task.WaitAll(task);
  416. }
  417. public void Delete(CancelTimer request)
  418. {
  419. AssertUserCanManageLiveTv();
  420. var task = _liveTvManager.CancelTimer(request.Id);
  421. Task.WaitAll(task);
  422. }
  423. public void Post(UpdateTimer request)
  424. {
  425. AssertUserCanManageLiveTv();
  426. var task = _liveTvManager.UpdateTimer(request, CancellationToken.None);
  427. Task.WaitAll(task);
  428. }
  429. public async Task<object> Get(GetSeriesTimers request)
  430. {
  431. var result = await _liveTvManager.GetSeriesTimers(new SeriesTimerQuery
  432. {
  433. SortOrder = request.SortOrder,
  434. SortBy = request.SortBy
  435. }, CancellationToken.None).ConfigureAwait(false);
  436. return ToOptimizedSerializedResultUsingCache(result);
  437. }
  438. public async Task<object> Get(GetSeriesTimer request)
  439. {
  440. var result = await _liveTvManager.GetSeriesTimer(request.Id, CancellationToken.None).ConfigureAwait(false);
  441. return ToOptimizedSerializedResultUsingCache(result);
  442. }
  443. public void Delete(CancelSeriesTimer request)
  444. {
  445. AssertUserCanManageLiveTv();
  446. var task = _liveTvManager.CancelSeriesTimer(request.Id);
  447. Task.WaitAll(task);
  448. }
  449. public void Post(UpdateSeriesTimer request)
  450. {
  451. AssertUserCanManageLiveTv();
  452. var task = _liveTvManager.UpdateSeriesTimer(request, CancellationToken.None);
  453. Task.WaitAll(task);
  454. }
  455. public async Task<object> Get(GetDefaultTimer request)
  456. {
  457. if (string.IsNullOrEmpty(request.ProgramId))
  458. {
  459. var result = await _liveTvManager.GetNewTimerDefaults(CancellationToken.None).ConfigureAwait(false);
  460. return ToOptimizedSerializedResultUsingCache(result);
  461. }
  462. else
  463. {
  464. var result = await _liveTvManager.GetNewTimerDefaults(request.ProgramId, CancellationToken.None).ConfigureAwait(false);
  465. return ToOptimizedSerializedResultUsingCache(result);
  466. }
  467. }
  468. public async Task<object> Get(GetProgram request)
  469. {
  470. var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(request.UserId);
  471. var result = await _liveTvManager.GetProgram(request.Id, CancellationToken.None, user).ConfigureAwait(false);
  472. return ToOptimizedSerializedResultUsingCache(result);
  473. }
  474. public void Post(CreateSeriesTimer request)
  475. {
  476. AssertUserCanManageLiveTv();
  477. var task = _liveTvManager.CreateSeriesTimer(request, CancellationToken.None);
  478. Task.WaitAll(task);
  479. }
  480. public void Post(CreateTimer request)
  481. {
  482. AssertUserCanManageLiveTv();
  483. var task = _liveTvManager.CreateTimer(request, CancellationToken.None);
  484. Task.WaitAll(task);
  485. }
  486. public async Task<object> Get(GetRecordingGroups request)
  487. {
  488. var result = await _liveTvManager.GetRecordingGroups(new RecordingGroupQuery
  489. {
  490. UserId = request.UserId
  491. }, CancellationToken.None).ConfigureAwait(false);
  492. return ToOptimizedSerializedResultUsingCache(result);
  493. }
  494. public async Task<object> Get(GetRecordingGroup request)
  495. {
  496. var result = await _liveTvManager.GetRecordingGroups(new RecordingGroupQuery
  497. {
  498. }, CancellationToken.None).ConfigureAwait(false);
  499. var group = result.Items.FirstOrDefault(i => string.Equals(i.Id, request.Id, StringComparison.OrdinalIgnoreCase));
  500. return ToOptimizedSerializedResultUsingCache(group);
  501. }
  502. public object Get(GetGuideInfo request)
  503. {
  504. return ToOptimizedResult(_liveTvManager.GetGuideInfo());
  505. }
  506. public void Post(ResetTuner request)
  507. {
  508. AssertUserCanManageLiveTv();
  509. var task = _liveTvManager.ResetTuner(request.Id, CancellationToken.None);
  510. Task.WaitAll(task);
  511. }
  512. }
  513. }