LiveTvService.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.LiveTv;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.LiveTv;
  5. using MediaBrowser.Model.Querying;
  6. using ServiceStack;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api.LiveTv
  14. {
  15. [Route("/LiveTv/Services", "GET")]
  16. [Api(Description = "Gets available live tv services.")]
  17. public class GetServices : IReturn<List<LiveTvServiceInfo>>
  18. {
  19. }
  20. [Route("/LiveTv/Channels", "GET")]
  21. [Api(Description = "Gets available live tv channels.")]
  22. public class GetChannels : IReturn<QueryResult<ChannelInfoDto>>
  23. {
  24. [ApiMember(Name = "Type", Description = "Optional filter by channel type.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  25. public ChannelType? Type { get; set; }
  26. [ApiMember(Name = "UserId", Description = "Optional filter by user and attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  27. public string UserId { get; set; }
  28. /// <summary>
  29. /// Skips over a given number of items within the results. Use for paging.
  30. /// </summary>
  31. /// <value>The start index.</value>
  32. [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")]
  33. public int? StartIndex { get; set; }
  34. /// <summary>
  35. /// The maximum number of items to return
  36. /// </summary>
  37. /// <value>The limit.</value>
  38. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  39. public int? Limit { get; set; }
  40. }
  41. [Route("/LiveTv/Channels/{Id}", "GET")]
  42. [Api(Description = "Gets a live tv channel")]
  43. public class GetChannel : IReturn<ChannelInfoDto>
  44. {
  45. /// <summary>
  46. /// Gets or sets the id.
  47. /// </summary>
  48. /// <value>The id.</value>
  49. [ApiMember(Name = "Id", Description = "Channel Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  50. public string Id { get; set; }
  51. [ApiMember(Name = "UserId", Description = "Optional attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  52. public string UserId { get; set; }
  53. }
  54. [Route("/LiveTv/Recordings", "GET")]
  55. [Api(Description = "Gets live tv recordings")]
  56. public class GetRecordings : IReturn<QueryResult<RecordingInfoDto>>
  57. {
  58. [ApiMember(Name = "ChannelId", Description = "Optional filter by channel id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  59. public string ChannelId { get; set; }
  60. [ApiMember(Name = "UserId", Description = "Optional filter by user and attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  61. public string UserId { get; set; }
  62. [ApiMember(Name = "GroupId", Description = "Optional filter by recording group.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  63. public string GroupId { get; set; }
  64. [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")]
  65. public int? StartIndex { get; set; }
  66. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  67. public int? Limit { get; set; }
  68. [ApiMember(Name = "IsRecording", Description = "Optional filter by recordings that are currently active, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  69. public bool? IsRecording { get; set; }
  70. [ApiMember(Name = "SeriesTimerId", Description = "Optional filter by recordings belonging to a series timer", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  71. public string SeriesTimerId { get; set; }
  72. }
  73. [Route("/LiveTv/Recordings/Groups", "GET")]
  74. [Api(Description = "Gets live tv recording groups")]
  75. public class GetRecordingGroups : IReturn<QueryResult<RecordingGroupDto>>
  76. {
  77. [ApiMember(Name = "UserId", Description = "Optional filter by user and attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  78. public string UserId { get; set; }
  79. }
  80. [Route("/LiveTv/Recordings/{Id}", "GET")]
  81. [Api(Description = "Gets a live tv recording")]
  82. public class GetRecording : IReturn<RecordingInfoDto>
  83. {
  84. [ApiMember(Name = "Id", Description = "Recording Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  85. public string Id { get; set; }
  86. [ApiMember(Name = "UserId", Description = "Optional attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  87. public string UserId { get; set; }
  88. }
  89. [Route("/LiveTv/Timers/{Id}", "GET")]
  90. [Api(Description = "Gets a live tv timer")]
  91. public class GetTimer : IReturn<TimerInfoDto>
  92. {
  93. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  94. public string Id { get; set; }
  95. }
  96. [Route("/LiveTv/Timers/Defaults", "GET")]
  97. [Api(Description = "Gets default values for a new timer")]
  98. public class GetDefaultTimer : IReturn<SeriesTimerInfoDto>
  99. {
  100. [ApiMember(Name = "ProgramId", Description = "Optional, to attach default values based on a program.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  101. public string ProgramId { get; set; }
  102. }
  103. [Route("/LiveTv/Timers", "GET")]
  104. [Api(Description = "Gets live tv timers")]
  105. public class GetTimers : IReturn<QueryResult<TimerInfoDto>>
  106. {
  107. [ApiMember(Name = "ChannelId", Description = "Optional filter by channel id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  108. public string ChannelId { get; set; }
  109. [ApiMember(Name = "SeriesTimerId", Description = "Optional filter by timers belonging to a series timer", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  110. public string SeriesTimerId { get; set; }
  111. }
  112. [Route("/LiveTv/Programs", "GET,POST")]
  113. [Api(Description = "Gets available live tv epgs..")]
  114. public class GetPrograms : IReturn<QueryResult<ProgramInfoDto>>
  115. {
  116. [ApiMember(Name = "ChannelIds", Description = "The channels to return guide information for.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  117. public string ChannelIds { get; set; }
  118. [ApiMember(Name = "UserId", Description = "Optional filter by user id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  119. public string UserId { get; set; }
  120. [ApiMember(Name = "MinStartDate", Description = "Optional. The minimum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  121. public string MinStartDate { get; set; }
  122. [ApiMember(Name = "MaxStartDate", Description = "Optional. The maximum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  123. public string MaxStartDate { get; set; }
  124. [ApiMember(Name = "MinEndDate", Description = "Optional. The minimum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  125. public string MinEndDate { get; set; }
  126. [ApiMember(Name = "MaxEndDate", Description = "Optional. The maximum premiere date. Format = ISO", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  127. public string MaxEndDate { get; set; }
  128. }
  129. [Route("/LiveTv/Programs/Recommended", "GET")]
  130. [Api(Description = "Gets available live tv epgs..")]
  131. public class GetRecommendedPrograms : IReturn<QueryResult<ProgramInfoDto>>
  132. {
  133. [ApiMember(Name = "UserId", Description = "Optional filter by user id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  134. public string UserId { get; set; }
  135. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  136. public int? Limit { get; set; }
  137. [ApiMember(Name = "IsAiring", Description = "Optional. Filter by programs that are currently airing, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  138. public bool? IsAiring { get; set; }
  139. [ApiMember(Name = "HasAired", Description = "Optional. Filter by programs that have completed airing, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  140. public bool? HasAired { get; set; }
  141. }
  142. [Route("/LiveTv/Programs/{Id}", "GET")]
  143. [Api(Description = "Gets a live tv program")]
  144. public class GetProgram : IReturn<ProgramInfoDto>
  145. {
  146. [ApiMember(Name = "Id", Description = "Program Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  147. public string Id { get; set; }
  148. [ApiMember(Name = "UserId", Description = "Optional attach user data.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  149. public string UserId { get; set; }
  150. }
  151. [Route("/LiveTv/Recordings/{Id}", "DELETE")]
  152. [Api(Description = "Deletes a live tv recording")]
  153. public class DeleteRecording : IReturnVoid
  154. {
  155. [ApiMember(Name = "Id", Description = "Recording Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  156. public string Id { get; set; }
  157. }
  158. [Route("/LiveTv/Timers/{Id}", "DELETE")]
  159. [Api(Description = "Cancels a live tv timer")]
  160. public class CancelTimer : IReturnVoid
  161. {
  162. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  163. public string Id { get; set; }
  164. }
  165. [Route("/LiveTv/Timers/{Id}", "POST")]
  166. [Api(Description = "Updates a live tv timer")]
  167. public class UpdateTimer : TimerInfoDto, IReturnVoid
  168. {
  169. }
  170. [Route("/LiveTv/Timers", "POST")]
  171. [Api(Description = "Creates a live tv timer")]
  172. public class CreateTimer : TimerInfoDto, IReturnVoid
  173. {
  174. }
  175. [Route("/LiveTv/SeriesTimers/{Id}", "GET")]
  176. [Api(Description = "Gets a live tv series timer")]
  177. public class GetSeriesTimer : IReturn<TimerInfoDto>
  178. {
  179. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  180. public string Id { get; set; }
  181. }
  182. [Route("/LiveTv/SeriesTimers", "GET")]
  183. [Api(Description = "Gets live tv series timers")]
  184. public class GetSeriesTimers : IReturn<QueryResult<SeriesTimerInfoDto>>
  185. {
  186. [ApiMember(Name = "SortBy", Description = "Optional. Sort by SortName or Priority", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  187. public string SortBy { get; set; }
  188. [ApiMember(Name = "SortOrder", Description = "Optional. Sort in Ascending or Descending order", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET,POST")]
  189. public SortOrder SortOrder { get; set; }
  190. }
  191. [Route("/LiveTv/SeriesTimers/{Id}", "DELETE")]
  192. [Api(Description = "Cancels a live tv series timer")]
  193. public class CancelSeriesTimer : IReturnVoid
  194. {
  195. [ApiMember(Name = "Id", Description = "Timer Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  196. public string Id { get; set; }
  197. }
  198. [Route("/LiveTv/SeriesTimers/{Id}", "POST")]
  199. [Api(Description = "Updates a live tv series timer")]
  200. public class UpdateSeriesTimer : SeriesTimerInfoDto, IReturnVoid
  201. {
  202. }
  203. [Route("/LiveTv/SeriesTimers", "POST")]
  204. [Api(Description = "Creates a live tv series timer")]
  205. public class CreateSeriesTimer : SeriesTimerInfoDto, IReturnVoid
  206. {
  207. }
  208. [Route("/LiveTv/Recordings/Groups/{Id}", "GET")]
  209. [Api(Description = "Gets a recording group")]
  210. public class GetRecordingGroup : IReturn<RecordingGroupDto>
  211. {
  212. [ApiMember(Name = "Id", Description = "Recording group Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  213. public string Id { get; set; }
  214. }
  215. [Route("/LiveTv/GuideInfo", "GET")]
  216. [Api(Description = "Gets guide info")]
  217. public class GetGuideInfo : IReturn<GuideInfo>
  218. {
  219. }
  220. public class LiveTvService : BaseApiService
  221. {
  222. private readonly ILiveTvManager _liveTvManager;
  223. private readonly IUserManager _userManager;
  224. public LiveTvService(ILiveTvManager liveTvManager, IUserManager userManager)
  225. {
  226. _liveTvManager = liveTvManager;
  227. _userManager = userManager;
  228. }
  229. public object Get(GetServices request)
  230. {
  231. var services = _liveTvManager.Services
  232. .Select(GetServiceInfo)
  233. .ToList();
  234. return ToOptimizedResult(services);
  235. }
  236. private LiveTvServiceInfo GetServiceInfo(ILiveTvService service)
  237. {
  238. return new LiveTvServiceInfo
  239. {
  240. Name = service.Name
  241. };
  242. }
  243. public object Get(GetChannels request)
  244. {
  245. var result = _liveTvManager.GetChannels(new ChannelQuery
  246. {
  247. ChannelType = request.Type,
  248. UserId = request.UserId,
  249. StartIndex = request.StartIndex,
  250. Limit = request.Limit
  251. }, CancellationToken.None).Result;
  252. return ToOptimizedResult(result);
  253. }
  254. public object Get(GetChannel request)
  255. {
  256. var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(new Guid(request.UserId));
  257. var result = _liveTvManager.GetChannel(request.Id, CancellationToken.None, user).Result;
  258. return ToOptimizedResult(result);
  259. }
  260. public object Get(GetPrograms request)
  261. {
  262. var query = new ProgramQuery
  263. {
  264. ChannelIdList = (request.ChannelIds ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray(),
  265. UserId = request.UserId
  266. };
  267. if (!string.IsNullOrEmpty(request.MinStartDate))
  268. {
  269. query.MinStartDate = DateTime.Parse(request.MinStartDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  270. }
  271. if (!string.IsNullOrEmpty(request.MinEndDate))
  272. {
  273. query.MinEndDate = DateTime.Parse(request.MinEndDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  274. }
  275. if (!string.IsNullOrEmpty(request.MaxStartDate))
  276. {
  277. query.MaxStartDate = DateTime.Parse(request.MaxStartDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  278. }
  279. if (!string.IsNullOrEmpty(request.MaxEndDate))
  280. {
  281. query.MaxEndDate = DateTime.Parse(request.MaxEndDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
  282. }
  283. var result = _liveTvManager.GetPrograms(query, CancellationToken.None).Result;
  284. return ToOptimizedResult(result);
  285. }
  286. public object Get(GetRecommendedPrograms request)
  287. {
  288. var query = new RecommendedProgramQuery
  289. {
  290. UserId = request.UserId,
  291. IsAiring = request.IsAiring,
  292. Limit = request.Limit,
  293. HasAired = request.HasAired
  294. };
  295. var result = _liveTvManager.GetRecommendedPrograms(query, CancellationToken.None).Result;
  296. return ToOptimizedResult(result);
  297. }
  298. public object Post(GetPrograms request)
  299. {
  300. return Get(request);
  301. }
  302. public object Get(GetRecordings request)
  303. {
  304. var result = _liveTvManager.GetRecordings(new RecordingQuery
  305. {
  306. ChannelId = request.ChannelId,
  307. UserId = request.UserId,
  308. GroupId = request.GroupId,
  309. StartIndex = request.StartIndex,
  310. Limit = request.Limit,
  311. IsRecording = request.IsRecording,
  312. SeriesTimerId = request.SeriesTimerId
  313. }, CancellationToken.None).Result;
  314. return ToOptimizedResult(result);
  315. }
  316. public object Get(GetRecording request)
  317. {
  318. var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(new Guid(request.UserId));
  319. var result = _liveTvManager.GetRecording(request.Id, CancellationToken.None, user).Result;
  320. return ToOptimizedResult(result);
  321. }
  322. public object Get(GetTimer request)
  323. {
  324. var result = _liveTvManager.GetTimer(request.Id, CancellationToken.None).Result;
  325. return ToOptimizedResult(result);
  326. }
  327. public object Get(GetTimers request)
  328. {
  329. var result = _liveTvManager.GetTimers(new TimerQuery
  330. {
  331. ChannelId = request.ChannelId,
  332. SeriesTimerId = request.SeriesTimerId
  333. }, CancellationToken.None).Result;
  334. return ToOptimizedResult(result);
  335. }
  336. public void Delete(DeleteRecording request)
  337. {
  338. var task = _liveTvManager.DeleteRecording(request.Id);
  339. Task.WaitAll(task);
  340. }
  341. public void Delete(CancelTimer request)
  342. {
  343. var task = _liveTvManager.CancelTimer(request.Id);
  344. Task.WaitAll(task);
  345. }
  346. public void Post(UpdateTimer request)
  347. {
  348. var task = _liveTvManager.UpdateTimer(request, CancellationToken.None);
  349. Task.WaitAll(task);
  350. }
  351. public object Get(GetSeriesTimers request)
  352. {
  353. var result = _liveTvManager.GetSeriesTimers(new SeriesTimerQuery
  354. {
  355. SortOrder = request.SortOrder,
  356. SortBy = request.SortBy
  357. }, CancellationToken.None).Result;
  358. return ToOptimizedResult(result);
  359. }
  360. public object Get(GetSeriesTimer request)
  361. {
  362. var result = _liveTvManager.GetSeriesTimer(request.Id, CancellationToken.None).Result;
  363. return ToOptimizedResult(result);
  364. }
  365. public void Delete(CancelSeriesTimer request)
  366. {
  367. var task = _liveTvManager.CancelSeriesTimer(request.Id);
  368. Task.WaitAll(task);
  369. }
  370. public void Post(UpdateSeriesTimer request)
  371. {
  372. var task = _liveTvManager.UpdateSeriesTimer(request, CancellationToken.None);
  373. Task.WaitAll(task);
  374. }
  375. public object Get(GetDefaultTimer request)
  376. {
  377. if (string.IsNullOrEmpty(request.ProgramId))
  378. {
  379. var result = _liveTvManager.GetNewTimerDefaults(CancellationToken.None).Result;
  380. return ToOptimizedResult(result);
  381. }
  382. else
  383. {
  384. var result = _liveTvManager.GetNewTimerDefaults(request.ProgramId, CancellationToken.None).Result;
  385. return ToOptimizedResult(result);
  386. }
  387. }
  388. public object Get(GetProgram request)
  389. {
  390. var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(new Guid(request.UserId));
  391. var result = _liveTvManager.GetProgram(request.Id, CancellationToken.None, user).Result;
  392. return ToOptimizedResult(result);
  393. }
  394. public void Post(CreateSeriesTimer request)
  395. {
  396. var task = _liveTvManager.CreateSeriesTimer(request, CancellationToken.None);
  397. Task.WaitAll(task);
  398. }
  399. public void Post(CreateTimer request)
  400. {
  401. var task = _liveTvManager.CreateTimer(request, CancellationToken.None);
  402. Task.WaitAll(task);
  403. }
  404. public object Get(GetRecordingGroups request)
  405. {
  406. var result = _liveTvManager.GetRecordingGroups(new RecordingGroupQuery
  407. {
  408. UserId = request.UserId
  409. }, CancellationToken.None).Result;
  410. return ToOptimizedResult(result);
  411. }
  412. public object Get(GetRecordingGroup request)
  413. {
  414. var result = _liveTvManager.GetRecordingGroups(new RecordingGroupQuery
  415. {
  416. }, CancellationToken.None).Result;
  417. var group = result.Items.FirstOrDefault(i => string.Equals(i.Id, request.Id, StringComparison.OrdinalIgnoreCase));
  418. return ToOptimizedResult(group);
  419. }
  420. public object Get(GetGuideInfo request)
  421. {
  422. return ToOptimizedResult(_liveTvManager.GetGuideInfo());
  423. }
  424. }
  425. }