SchedulesDirect.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.LiveTv;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.LiveTv;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Serialization;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Server.Implementations.LiveTv.Listings
  17. {
  18. public class SchedulesDirect : IListingsProvider
  19. {
  20. private readonly ILogger _logger;
  21. private readonly IJsonSerializer _jsonSerializer;
  22. private readonly IHttpClient _httpClient;
  23. private readonly SemaphoreSlim _tokenSemaphore = new SemaphoreSlim(1, 1);
  24. private readonly IApplicationHost _appHost;
  25. private const string ApiUrl = "https://json.schedulesdirect.org/20141201";
  26. private readonly ConcurrentDictionary<string, ScheduleDirect.Station> _channelPair =
  27. new ConcurrentDictionary<string, ScheduleDirect.Station>();
  28. public SchedulesDirect(ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient, IApplicationHost appHost)
  29. {
  30. _logger = logger;
  31. _jsonSerializer = jsonSerializer;
  32. _httpClient = httpClient;
  33. _appHost = appHost;
  34. }
  35. private string UserAgent
  36. {
  37. get { return "Emby/" + _appHost.ApplicationVersion; }
  38. }
  39. private List<string> GetScheduleRequestDates(DateTime startDateUtc, DateTime endDateUtc)
  40. {
  41. List<string> dates = new List<string>();
  42. var start = new List<DateTime> { startDateUtc, startDateUtc.ToLocalTime() }.Min();
  43. var end = new List<DateTime> { endDateUtc, endDateUtc.ToLocalTime() }.Max();
  44. while (start.DayOfYear <= end.Day)
  45. {
  46. dates.Add(start.ToString("yyyy-MM-dd"));
  47. start = start.AddDays(1);
  48. }
  49. return dates;
  50. }
  51. public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
  52. {
  53. List<ProgramInfo> programsInfo = new List<ProgramInfo>();
  54. var token = await GetToken(info, cancellationToken);
  55. if (string.IsNullOrWhiteSpace(token))
  56. {
  57. return programsInfo;
  58. }
  59. if (string.IsNullOrWhiteSpace(info.ListingsId))
  60. {
  61. return programsInfo;
  62. }
  63. var httpOptions = new HttpRequestOptions()
  64. {
  65. Url = ApiUrl + "/schedules",
  66. UserAgent = UserAgent,
  67. CancellationToken = cancellationToken
  68. };
  69. httpOptions.RequestHeaders["token"] = token;
  70. var dates = GetScheduleRequestDates(startDateUtc, endDateUtc);
  71. ScheduleDirect.Station station = null;
  72. if (!_channelPair.TryGetValue(channelNumber, out station))
  73. {
  74. return programsInfo;
  75. }
  76. string stationID = station.stationID;
  77. _logger.Info("Channel Station ID is: " + stationID);
  78. List<ScheduleDirect.RequestScheduleForChannel> requestList =
  79. new List<ScheduleDirect.RequestScheduleForChannel>()
  80. {
  81. new ScheduleDirect.RequestScheduleForChannel()
  82. {
  83. stationID = stationID,
  84. date = dates
  85. }
  86. };
  87. var requestString = _jsonSerializer.SerializeToString(requestList);
  88. _logger.Debug("Request string for schedules is: " + requestString);
  89. httpOptions.RequestContent = requestString;
  90. using (var response = await _httpClient.Post(httpOptions))
  91. {
  92. StreamReader reader = new StreamReader(response.Content);
  93. string responseString = reader.ReadToEnd();
  94. var dailySchedules = _jsonSerializer.DeserializeFromString<List<ScheduleDirect.Day>>(responseString);
  95. _logger.Debug("Found " + dailySchedules.Count() + " programs on " + channelNumber + " ScheduleDirect");
  96. httpOptions = new HttpRequestOptions()
  97. {
  98. Url = ApiUrl + "/programs",
  99. UserAgent = UserAgent,
  100. CancellationToken = cancellationToken
  101. };
  102. httpOptions.RequestHeaders["token"] = token;
  103. List<string> programsID = new List<string>();
  104. programsID = dailySchedules.SelectMany(d => d.programs.Select(s => s.programID)).Distinct().ToList();
  105. var requestBody = "[\"" + string.Join("\", \"", programsID) + "\"]";
  106. httpOptions.RequestContent = requestBody;
  107. using (var innerResponse = await _httpClient.Post(httpOptions))
  108. {
  109. StreamReader innerReader = new StreamReader(innerResponse.Content);
  110. responseString = innerReader.ReadToEnd();
  111. var programDetails =
  112. _jsonSerializer.DeserializeFromString<List<ScheduleDirect.ProgramDetails>>(
  113. responseString);
  114. var programDict = programDetails.ToDictionary(p => p.programID, y => y);
  115. var images = await GetImageForPrograms(programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID).ToList(), cancellationToken);
  116. var schedules = dailySchedules.SelectMany(d => d.programs);
  117. foreach (ScheduleDirect.Program schedule in schedules)
  118. {
  119. _logger.Debug("Proccesing Schedule for statio ID " + stationID +
  120. " which corresponds to channel " + channelNumber + " and program id " +
  121. schedule.programID + " which says it has images? " +
  122. programDict[schedule.programID].hasImageArtwork);
  123. var imageIndex = images.FindIndex(i => i.programID == schedule.programID.Substring(0, 10));
  124. if (imageIndex > -1)
  125. {
  126. programDict[schedule.programID].images = GetProgramLogo(ApiUrl, images[imageIndex]);
  127. }
  128. programsInfo.Add(GetProgram(channelNumber, schedule, programDict[schedule.programID]));
  129. }
  130. _logger.Info("Finished with EPGData");
  131. }
  132. }
  133. return programsInfo;
  134. }
  135. public async Task AddMetadata(ListingsProviderInfo info, List<ChannelInfo> channels,
  136. CancellationToken cancellationToken)
  137. {
  138. if (string.IsNullOrWhiteSpace(info.ListingsId))
  139. {
  140. throw new Exception("ListingsId required");
  141. }
  142. var token = await GetToken(info, cancellationToken);
  143. if (string.IsNullOrWhiteSpace(token))
  144. {
  145. throw new Exception("token required");
  146. }
  147. _channelPair.Clear();
  148. var httpOptions = new HttpRequestOptions()
  149. {
  150. Url = ApiUrl + "/lineups/" + info.ListingsId,
  151. UserAgent = UserAgent,
  152. CancellationToken = cancellationToken
  153. };
  154. httpOptions.RequestHeaders["token"] = token;
  155. using (var response = await _httpClient.Get(httpOptions))
  156. {
  157. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Channel>(response);
  158. _logger.Info("Found " + root.map.Count() + " channels on the lineup on ScheduleDirect");
  159. _logger.Info("Mapping Stations to Channel");
  160. foreach (ScheduleDirect.Map map in root.map)
  161. {
  162. var channel = (map.channel ?? (map.atscMajor + "." + map.atscMinor)).TrimStart('0');
  163. _logger.Debug("Found channel: " + channel + " in Schedules Direct");
  164. var schChannel = root.stations.FirstOrDefault(item => item.stationID == map.stationID);
  165. if (!_channelPair.ContainsKey(channel) && channel != "0.0" && schChannel != null)
  166. {
  167. _channelPair.TryAdd(channel, schChannel);
  168. }
  169. }
  170. _logger.Info("Added " + _channelPair.Count() + " channels to the dictionary");
  171. foreach (ChannelInfo channel in channels)
  172. {
  173. // Helper.logger.Info("Modifyin channel " + channel.Number);
  174. if (_channelPair.ContainsKey(channel.Number))
  175. {
  176. string channelName;
  177. if (_channelPair[channel.Number].logo != null)
  178. {
  179. channel.ImageUrl = _channelPair[channel.Number].logo.URL;
  180. channel.HasImage = true;
  181. }
  182. if (_channelPair[channel.Number].affiliate != null)
  183. {
  184. channelName = _channelPair[channel.Number].affiliate;
  185. }
  186. else
  187. {
  188. channelName = _channelPair[channel.Number].name;
  189. }
  190. channel.Name = channelName;
  191. }
  192. else
  193. {
  194. _logger.Info("Schedules Direct doesnt have data for channel: " + channel.Number + " " +
  195. channel.Name);
  196. }
  197. }
  198. }
  199. }
  200. private ProgramInfo GetProgram(string channel, ScheduleDirect.Program programInfo,
  201. ScheduleDirect.ProgramDetails details)
  202. {
  203. //_logger.Debug("Show type is: " + (details.showType ?? "No ShowType"));
  204. DateTime startAt = DateTime.ParseExact(programInfo.airDateTime, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'",
  205. CultureInfo.InvariantCulture);
  206. DateTime endAt = startAt.AddSeconds(programInfo.duration);
  207. ProgramAudio audioType = ProgramAudio.Stereo;
  208. bool hdtv = false;
  209. bool repeat = (programInfo.@new == null);
  210. string newID = programInfo.programID + "T" + startAt.Ticks + "C" + channel;
  211. if (programInfo.audioProperties != null)
  212. {
  213. if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd 5.1", StringComparison.OrdinalIgnoreCase)))
  214. {
  215. audioType = ProgramAudio.DolbyDigital;
  216. }
  217. else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd", StringComparison.OrdinalIgnoreCase)))
  218. {
  219. audioType = ProgramAudio.DolbyDigital;
  220. }
  221. else if (programInfo.audioProperties.Exists(item => string.Equals(item, "stereo", StringComparison.OrdinalIgnoreCase)))
  222. {
  223. audioType = ProgramAudio.Stereo;
  224. }
  225. else
  226. {
  227. audioType = ProgramAudio.Mono;
  228. }
  229. }
  230. if ((programInfo.videoProperties != null))
  231. {
  232. hdtv = programInfo.videoProperties.Exists(item => item == "hdtv");
  233. }
  234. string desc = "";
  235. if (details.descriptions != null)
  236. {
  237. if (details.descriptions.description1000 != null)
  238. {
  239. desc = details.descriptions.description1000[0].description;
  240. }
  241. else if (details.descriptions.description100 != null)
  242. {
  243. desc = details.descriptions.description100[0].description;
  244. }
  245. }
  246. ScheduleDirect.Gracenote gracenote;
  247. string EpisodeTitle = "";
  248. if (details.metadata != null)
  249. {
  250. gracenote = details.metadata.Find(x => x.Gracenote != null).Gracenote;
  251. if ((details.showType ?? "No ShowType") == "Series")
  252. {
  253. EpisodeTitle = "Season: " + gracenote.season + " Episode: " + gracenote.episode;
  254. }
  255. }
  256. if (details.episodeTitle150 != null)
  257. {
  258. EpisodeTitle = EpisodeTitle + " " + details.episodeTitle150;
  259. }
  260. bool hasImage = false;
  261. var imageLink = "";
  262. if (details.hasImageArtwork)
  263. {
  264. imageLink = details.images;
  265. }
  266. var info = new ProgramInfo
  267. {
  268. ChannelId = channel,
  269. Id = newID,
  270. Overview = desc,
  271. StartDate = startAt,
  272. EndDate = endAt,
  273. Genres = new List<string>() { "N/A" },
  274. Name = details.titles[0].title120 ?? "Unkown",
  275. OfficialRating = "0",
  276. CommunityRating = null,
  277. EpisodeTitle = EpisodeTitle,
  278. Audio = audioType,
  279. IsHD = hdtv,
  280. IsRepeat = repeat,
  281. IsSeries =
  282. ((details.showType ?? "No ShowType") == "Series") ||
  283. (details.showType ?? "No ShowType") == "Miniseries",
  284. ImageUrl = imageLink,
  285. HasImage = details.hasImageArtwork,
  286. IsNews = false,
  287. IsKids = false,
  288. IsSports =
  289. ((details.showType ?? "No ShowType") == "Sports non-event") ||
  290. (details.showType ?? "No ShowType") == "Sports event",
  291. IsLive = false,
  292. IsMovie =
  293. (details.showType ?? "No ShowType") == "Feature Film" ||
  294. (details.showType ?? "No ShowType") == "TV Movie" ||
  295. (details.showType ?? "No ShowType") == "Short Film",
  296. IsPremiere = false,
  297. };
  298. //logger.Info("Done init");
  299. if (null != details.originalAirDate)
  300. {
  301. info.OriginalAirDate = DateTime.Parse(details.originalAirDate);
  302. }
  303. if (details.genres != null)
  304. {
  305. info.Genres = details.genres.Where(g => !string.IsNullOrWhiteSpace(g)).ToList();
  306. info.IsNews = details.genres.Contains("news", StringComparer.OrdinalIgnoreCase);
  307. info.IsKids = false;
  308. }
  309. return info;
  310. }
  311. private string GetProgramLogo(string apiUrl, ScheduleDirect.ShowImages images)
  312. {
  313. string url = "";
  314. if (images.data != null)
  315. {
  316. var smallImages = images.data.Where(i => i.size == "Sm").ToList();
  317. if (smallImages.Any())
  318. {
  319. images.data = smallImages;
  320. }
  321. var logoIndex = images.data.FindIndex(i => i.category == "Logo");
  322. if (logoIndex == -1)
  323. {
  324. logoIndex = 0;
  325. }
  326. if (images.data[logoIndex].uri.Contains("http"))
  327. {
  328. url = images.data[logoIndex].uri;
  329. }
  330. else
  331. {
  332. url = apiUrl + "/image/" + images.data[logoIndex].uri;
  333. }
  334. //_logger.Debug("URL for image is : " + url);
  335. }
  336. return url;
  337. }
  338. private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(List<string> programIds,
  339. CancellationToken cancellationToken)
  340. {
  341. var imageIdString = "[";
  342. programIds.ForEach(i =>
  343. {
  344. if (!imageIdString.Contains(i.Substring(0, 10)))
  345. {
  346. imageIdString += "\"" + i.Substring(0, 10) + "\",";
  347. }
  348. ;
  349. });
  350. imageIdString = imageIdString.TrimEnd(',') + "]";
  351. _logger.Debug("Json for show images = " + imageIdString);
  352. var httpOptions = new HttpRequestOptions()
  353. {
  354. Url = ApiUrl + "/metadata/programs",
  355. UserAgent = UserAgent,
  356. CancellationToken = cancellationToken,
  357. RequestContent = imageIdString
  358. };
  359. List<ScheduleDirect.ShowImages> images;
  360. using (var innerResponse2 = await _httpClient.Post(httpOptions))
  361. {
  362. images = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.ShowImages>>(
  363. innerResponse2.Content);
  364. }
  365. return images;
  366. }
  367. public async Task<List<NameIdPair>> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
  368. {
  369. var token = await GetToken(info, cancellationToken);
  370. var lineups = new List<NameIdPair>();
  371. if (string.IsNullOrWhiteSpace(token))
  372. {
  373. return lineups;
  374. }
  375. _logger.Info("Headends on account ");
  376. var options = new HttpRequestOptions()
  377. {
  378. Url = ApiUrl + "/headends?country=" + country + "&postalcode=" + location,
  379. UserAgent = UserAgent,
  380. CancellationToken = cancellationToken
  381. };
  382. options.RequestHeaders["token"] = token;
  383. try
  384. {
  385. using (Stream responce = await _httpClient.Get(options).ConfigureAwait(false))
  386. {
  387. var root = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.Headends>>(responce);
  388. _logger.Info("Lineups on account ");
  389. if (root != null)
  390. {
  391. foreach (ScheduleDirect.Headends headend in root)
  392. {
  393. _logger.Info("Headend: " + headend.headend);
  394. foreach (ScheduleDirect.Lineup lineup in headend.lineups)
  395. {
  396. _logger.Info("Headend: " + lineup.uri.Substring(18));
  397. lineups.Add(new NameIdPair()
  398. {
  399. Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
  400. Id = lineup.uri.Substring(18)
  401. });
  402. }
  403. }
  404. }
  405. else
  406. {
  407. _logger.Info("No lineups on account");
  408. }
  409. }
  410. }
  411. catch (Exception ex)
  412. {
  413. _logger.Error("Error getting headends", ex);
  414. }
  415. return lineups;
  416. }
  417. private readonly ConcurrentDictionary<string, NameValuePair> _tokens = new ConcurrentDictionary<string, NameValuePair>();
  418. private async Task<string> GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
  419. {
  420. var username = info.Username;
  421. // Reset the token if there's no username
  422. if (string.IsNullOrWhiteSpace(username))
  423. {
  424. return null;
  425. }
  426. var password = info.Password;
  427. if (string.IsNullOrWhiteSpace(password))
  428. {
  429. return null;
  430. }
  431. NameValuePair savedToken = null;
  432. if (!_tokens.TryGetValue(username, out savedToken))
  433. {
  434. savedToken = new NameValuePair();
  435. _tokens.TryAdd(username, savedToken);
  436. }
  437. if (!string.IsNullOrWhiteSpace(savedToken.Name) && !string.IsNullOrWhiteSpace(savedToken.Value))
  438. {
  439. long ticks;
  440. if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out ticks))
  441. {
  442. // If it's under 24 hours old we can still use it
  443. if ((DateTime.UtcNow.Ticks - ticks) < TimeSpan.FromHours(24).Ticks)
  444. {
  445. return savedToken.Name;
  446. }
  447. }
  448. }
  449. await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  450. try
  451. {
  452. var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
  453. savedToken.Name = result;
  454. savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
  455. return result;
  456. }
  457. finally
  458. {
  459. _tokenSemaphore.Release();
  460. }
  461. }
  462. private async Task<string> GetTokenInternal(string username, string password,
  463. CancellationToken cancellationToken)
  464. {
  465. var httpOptions = new HttpRequestOptions()
  466. {
  467. Url = ApiUrl + "/token",
  468. UserAgent = UserAgent,
  469. RequestContent = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}",
  470. CancellationToken = cancellationToken
  471. };
  472. //_logger.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
  473. // httpOptions.RequestContent);
  474. using (var responce = await _httpClient.Post(httpOptions))
  475. {
  476. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Token>(responce.Content);
  477. if (root.message == "OK")
  478. {
  479. _logger.Info("Authenticated with Schedules Direct token: " + root.token);
  480. return root.token;
  481. }
  482. throw new ApplicationException("Could not authenticate with Schedules Direct Error: " + root.message);
  483. }
  484. }
  485. private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
  486. {
  487. var token = await GetToken(info, cancellationToken);
  488. if (string.IsNullOrWhiteSpace(token))
  489. {
  490. throw new ArgumentException("Authentication required.");
  491. }
  492. if (string.IsNullOrWhiteSpace(info.ListingsId))
  493. {
  494. throw new ArgumentException("Listings Id required");
  495. }
  496. _logger.Info("Adding new LineUp ");
  497. var httpOptions = new HttpRequestOptions()
  498. {
  499. Url = ApiUrl + "/lineups/" + info.ListingsId,
  500. UserAgent = UserAgent,
  501. CancellationToken = cancellationToken
  502. };
  503. httpOptions.RequestHeaders["token"] = token;
  504. using (var response = await _httpClient.SendAsync(httpOptions, "PUT"))
  505. {
  506. }
  507. }
  508. public string Name
  509. {
  510. get { return "Schedules Direct"; }
  511. }
  512. public string Type
  513. {
  514. get { return "SchedulesDirect"; }
  515. }
  516. private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
  517. {
  518. if (string.IsNullOrWhiteSpace(info.ListingsId))
  519. {
  520. throw new ArgumentException("Listings Id required");
  521. }
  522. var token = await GetToken(info, cancellationToken);
  523. if (string.IsNullOrWhiteSpace(token))
  524. {
  525. throw new Exception("token required");
  526. }
  527. _logger.Info("Headends on account ");
  528. var options = new HttpRequestOptions()
  529. {
  530. Url = ApiUrl + "/lineups",
  531. UserAgent = UserAgent,
  532. CancellationToken = cancellationToken
  533. };
  534. options.RequestHeaders["token"] = token;
  535. using (var response = await _httpClient.Get(options).ConfigureAwait(false))
  536. {
  537. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Lineups>(response);
  538. return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
  539. }
  540. }
  541. public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  542. {
  543. if (validateLogin)
  544. {
  545. if (string.IsNullOrWhiteSpace(info.Username))
  546. {
  547. throw new ArgumentException("Username is required");
  548. }
  549. if (string.IsNullOrWhiteSpace(info.Password))
  550. {
  551. throw new ArgumentException("Password is required");
  552. }
  553. }
  554. if (validateListings)
  555. {
  556. if (string.IsNullOrWhiteSpace(info.ListingsId))
  557. {
  558. throw new ArgumentException("Listings Id required");
  559. }
  560. var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
  561. if (!hasLineup)
  562. {
  563. await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
  564. }
  565. }
  566. }
  567. public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
  568. {
  569. return GetHeadends(info, country, location, CancellationToken.None);
  570. }
  571. public class ScheduleDirect
  572. {
  573. public class Token
  574. {
  575. public int code { get; set; }
  576. public string message { get; set; }
  577. public string serverID { get; set; }
  578. public string token { get; set; }
  579. }
  580. public class Lineup
  581. {
  582. public string lineup { get; set; }
  583. public string name { get; set; }
  584. public string transport { get; set; }
  585. public string location { get; set; }
  586. public string uri { get; set; }
  587. }
  588. public class Lineups
  589. {
  590. public int code { get; set; }
  591. public string serverID { get; set; }
  592. public string datetime { get; set; }
  593. public List<Lineup> lineups { get; set; }
  594. }
  595. public class Headends
  596. {
  597. public string headend { get; set; }
  598. public string transport { get; set; }
  599. public string location { get; set; }
  600. public List<Lineup> lineups { get; set; }
  601. }
  602. public class Map
  603. {
  604. public string stationID { get; set; }
  605. public string channel { get; set; }
  606. public int uhfVhf { get; set; }
  607. public int atscMajor { get; set; }
  608. public int atscMinor { get; set; }
  609. }
  610. public class Broadcaster
  611. {
  612. public string city { get; set; }
  613. public string state { get; set; }
  614. public string postalcode { get; set; }
  615. public string country { get; set; }
  616. }
  617. public class Logo
  618. {
  619. public string URL { get; set; }
  620. public int height { get; set; }
  621. public int width { get; set; }
  622. public string md5 { get; set; }
  623. }
  624. public class Station
  625. {
  626. public string stationID { get; set; }
  627. public string name { get; set; }
  628. public string callsign { get; set; }
  629. public List<string> broadcastLanguage { get; set; }
  630. public List<string> descriptionLanguage { get; set; }
  631. public Broadcaster broadcaster { get; set; }
  632. public string affiliate { get; set; }
  633. public Logo logo { get; set; }
  634. public bool? isCommercialFree { get; set; }
  635. }
  636. public class Metadata
  637. {
  638. public string lineup { get; set; }
  639. public string modified { get; set; }
  640. public string transport { get; set; }
  641. }
  642. public class Channel
  643. {
  644. public List<Map> map { get; set; }
  645. public List<Station> stations { get; set; }
  646. public Metadata metadata { get; set; }
  647. }
  648. public class RequestScheduleForChannel
  649. {
  650. public string stationID { get; set; }
  651. public List<string> date { get; set; }
  652. }
  653. public class Rating
  654. {
  655. public string body { get; set; }
  656. public string code { get; set; }
  657. }
  658. public class Multipart
  659. {
  660. public int partNumber { get; set; }
  661. public int totalParts { get; set; }
  662. }
  663. public class Program
  664. {
  665. public string programID { get; set; }
  666. public string airDateTime { get; set; }
  667. public int duration { get; set; }
  668. public string md5 { get; set; }
  669. public List<string> audioProperties { get; set; }
  670. public List<string> videoProperties { get; set; }
  671. public List<Rating> ratings { get; set; }
  672. public bool? @new { get; set; }
  673. public Multipart multipart { get; set; }
  674. }
  675. public class MetadataSchedule
  676. {
  677. public string modified { get; set; }
  678. public string md5 { get; set; }
  679. public string startDate { get; set; }
  680. public string endDate { get; set; }
  681. public int days { get; set; }
  682. }
  683. public class Day
  684. {
  685. public string stationID { get; set; }
  686. public List<Program> programs { get; set; }
  687. public MetadataSchedule metadata { get; set; }
  688. }
  689. //
  690. public class Title
  691. {
  692. public string title120 { get; set; }
  693. }
  694. public class EventDetails
  695. {
  696. public string subType { get; set; }
  697. }
  698. public class Description100
  699. {
  700. public string descriptionLanguage { get; set; }
  701. public string description { get; set; }
  702. }
  703. public class Description1000
  704. {
  705. public string descriptionLanguage { get; set; }
  706. public string description { get; set; }
  707. }
  708. public class DescriptionsProgram
  709. {
  710. public List<Description100> description100 { get; set; }
  711. public List<Description1000> description1000 { get; set; }
  712. }
  713. public class Gracenote
  714. {
  715. public int season { get; set; }
  716. public int episode { get; set; }
  717. }
  718. public class MetadataPrograms
  719. {
  720. public Gracenote Gracenote { get; set; }
  721. }
  722. public class ContentRating
  723. {
  724. public string body { get; set; }
  725. public string code { get; set; }
  726. }
  727. public class Cast
  728. {
  729. public string billingOrder { get; set; }
  730. public string role { get; set; }
  731. public string nameId { get; set; }
  732. public string personId { get; set; }
  733. public string name { get; set; }
  734. public string characterName { get; set; }
  735. }
  736. public class Crew
  737. {
  738. public string billingOrder { get; set; }
  739. public string role { get; set; }
  740. public string nameId { get; set; }
  741. public string personId { get; set; }
  742. public string name { get; set; }
  743. }
  744. public class QualityRating
  745. {
  746. public string ratingsBody { get; set; }
  747. public string rating { get; set; }
  748. public string minRating { get; set; }
  749. public string maxRating { get; set; }
  750. public string increment { get; set; }
  751. }
  752. public class Movie
  753. {
  754. public string year { get; set; }
  755. public int duration { get; set; }
  756. public List<QualityRating> qualityRating { get; set; }
  757. }
  758. public class Recommendation
  759. {
  760. public string programID { get; set; }
  761. public string title120 { get; set; }
  762. }
  763. public class ProgramDetails
  764. {
  765. public string programID { get; set; }
  766. public List<Title> titles { get; set; }
  767. public EventDetails eventDetails { get; set; }
  768. public DescriptionsProgram descriptions { get; set; }
  769. public string originalAirDate { get; set; }
  770. public List<string> genres { get; set; }
  771. public string episodeTitle150 { get; set; }
  772. public List<MetadataPrograms> metadata { get; set; }
  773. public List<ContentRating> contentRating { get; set; }
  774. public List<Cast> cast { get; set; }
  775. public List<Crew> crew { get; set; }
  776. public string showType { get; set; }
  777. public bool hasImageArtwork { get; set; }
  778. public string images { get; set; }
  779. public string imageID { get; set; }
  780. public string md5 { get; set; }
  781. public List<string> contentAdvisory { get; set; }
  782. public Movie movie { get; set; }
  783. public List<Recommendation> recommendations { get; set; }
  784. }
  785. public class Caption
  786. {
  787. public string content { get; set; }
  788. public string lang { get; set; }
  789. }
  790. public class ImageData
  791. {
  792. public string width { get; set; }
  793. public string height { get; set; }
  794. public string uri { get; set; }
  795. public string size { get; set; }
  796. public string aspect { get; set; }
  797. public string category { get; set; }
  798. public string text { get; set; }
  799. public string primary { get; set; }
  800. public string tier { get; set; }
  801. public Caption caption { get; set; }
  802. }
  803. public class ShowImages
  804. {
  805. public string programID { get; set; }
  806. public List<ImageData> data { get; set; }
  807. }
  808. }
  809. }
  810. }