SchedulesDirect.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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
  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 = null;
  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 ?? string.Empty) + " " + details.episodeTitle150).Trim();
  259. }
  260. var imageLink = "";
  261. if (details.hasImageArtwork)
  262. {
  263. imageLink = details.images;
  264. }
  265. var info = new ProgramInfo
  266. {
  267. ChannelId = channel,
  268. Id = newID,
  269. Overview = desc,
  270. StartDate = startAt,
  271. EndDate = endAt,
  272. Name = details.titles[0].title120 ?? "Unkown",
  273. OfficialRating = "0",
  274. CommunityRating = null,
  275. EpisodeTitle = episodeTitle,
  276. Audio = audioType,
  277. IsHD = hdtv,
  278. IsRepeat = repeat,
  279. IsSeries =
  280. ((details.showType ?? "No ShowType") == "Series") ||
  281. (details.showType ?? "No ShowType") == "Miniseries",
  282. ImageUrl = imageLink,
  283. HasImage = details.hasImageArtwork,
  284. IsNews = false,
  285. IsKids = false,
  286. IsSports =
  287. ((details.showType ?? "No ShowType") == "Sports non-event") ||
  288. (details.showType ?? "No ShowType") == "Sports event",
  289. IsLive = false,
  290. IsMovie =
  291. (details.showType ?? "No ShowType") == "Feature Film" ||
  292. (details.showType ?? "No ShowType") == "TV Movie" ||
  293. (details.showType ?? "No ShowType") == "Short Film",
  294. IsPremiere = false,
  295. ShowId = programInfo.programID
  296. };
  297. if (!string.IsNullOrWhiteSpace(details.originalAirDate))
  298. {
  299. info.OriginalAirDate = DateTime.Parse(details.originalAirDate);
  300. }
  301. if (details.genres != null)
  302. {
  303. info.Genres = details.genres.Where(g => !string.IsNullOrWhiteSpace(g)).ToList();
  304. info.IsNews = details.genres.Contains("news", StringComparer.OrdinalIgnoreCase);
  305. info.IsKids = false;
  306. }
  307. return info;
  308. }
  309. private string GetProgramLogo(string apiUrl, ScheduleDirect.ShowImages images)
  310. {
  311. string url = "";
  312. if (images.data != null)
  313. {
  314. var smallImages = images.data.Where(i => i.size == "Sm").ToList();
  315. if (smallImages.Any())
  316. {
  317. images.data = smallImages;
  318. }
  319. var logoIndex = images.data.FindIndex(i => i.category == "Logo");
  320. if (logoIndex == -1)
  321. {
  322. logoIndex = 0;
  323. }
  324. if (images.data[logoIndex].uri.Contains("http"))
  325. {
  326. url = images.data[logoIndex].uri;
  327. }
  328. else
  329. {
  330. url = apiUrl + "/image/" + images.data[logoIndex].uri;
  331. }
  332. //_logger.Debug("URL for image is : " + url);
  333. }
  334. return url;
  335. }
  336. private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(List<string> programIds,
  337. CancellationToken cancellationToken)
  338. {
  339. var imageIdString = "[";
  340. programIds.ForEach(i =>
  341. {
  342. if (!imageIdString.Contains(i.Substring(0, 10)))
  343. {
  344. imageIdString += "\"" + i.Substring(0, 10) + "\",";
  345. }
  346. ;
  347. });
  348. imageIdString = imageIdString.TrimEnd(',') + "]";
  349. _logger.Debug("Json for show images = " + imageIdString);
  350. var httpOptions = new HttpRequestOptions()
  351. {
  352. Url = ApiUrl + "/metadata/programs",
  353. UserAgent = UserAgent,
  354. CancellationToken = cancellationToken,
  355. RequestContent = imageIdString
  356. };
  357. List<ScheduleDirect.ShowImages> images;
  358. using (var innerResponse2 = await _httpClient.Post(httpOptions))
  359. {
  360. images = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.ShowImages>>(
  361. innerResponse2.Content);
  362. }
  363. return images;
  364. }
  365. public async Task<List<NameIdPair>> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
  366. {
  367. var token = await GetToken(info, cancellationToken);
  368. var lineups = new List<NameIdPair>();
  369. if (string.IsNullOrWhiteSpace(token))
  370. {
  371. return lineups;
  372. }
  373. _logger.Info("Headends on account ");
  374. var options = new HttpRequestOptions()
  375. {
  376. Url = ApiUrl + "/headends?country=" + country + "&postalcode=" + location,
  377. UserAgent = UserAgent,
  378. CancellationToken = cancellationToken
  379. };
  380. options.RequestHeaders["token"] = token;
  381. try
  382. {
  383. using (Stream responce = await _httpClient.Get(options).ConfigureAwait(false))
  384. {
  385. var root = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.Headends>>(responce);
  386. _logger.Info("Lineups on account ");
  387. if (root != null)
  388. {
  389. foreach (ScheduleDirect.Headends headend in root)
  390. {
  391. _logger.Info("Headend: " + headend.headend);
  392. foreach (ScheduleDirect.Lineup lineup in headend.lineups)
  393. {
  394. _logger.Info("Headend: " + lineup.uri);
  395. lineups.Add(new NameIdPair
  396. {
  397. Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
  398. Id = lineup.uri.Substring(18)
  399. });
  400. }
  401. }
  402. }
  403. else
  404. {
  405. _logger.Info("No lineups on account");
  406. }
  407. }
  408. }
  409. catch (Exception ex)
  410. {
  411. _logger.Error("Error getting headends", ex);
  412. }
  413. return lineups;
  414. }
  415. private readonly ConcurrentDictionary<string, NameValuePair> _tokens = new ConcurrentDictionary<string, NameValuePair>();
  416. private async Task<string> GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
  417. {
  418. var username = info.Username;
  419. // Reset the token if there's no username
  420. if (string.IsNullOrWhiteSpace(username))
  421. {
  422. return null;
  423. }
  424. var password = info.Password;
  425. if (string.IsNullOrWhiteSpace(password))
  426. {
  427. return null;
  428. }
  429. NameValuePair savedToken = null;
  430. if (!_tokens.TryGetValue(username, out savedToken))
  431. {
  432. savedToken = new NameValuePair();
  433. _tokens.TryAdd(username, savedToken);
  434. }
  435. if (!string.IsNullOrWhiteSpace(savedToken.Name) && !string.IsNullOrWhiteSpace(savedToken.Value))
  436. {
  437. long ticks;
  438. if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out ticks))
  439. {
  440. // If it's under 24 hours old we can still use it
  441. if ((DateTime.UtcNow.Ticks - ticks) < TimeSpan.FromHours(24).Ticks)
  442. {
  443. return savedToken.Name;
  444. }
  445. }
  446. }
  447. await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  448. try
  449. {
  450. var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
  451. savedToken.Name = result;
  452. savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
  453. return result;
  454. }
  455. finally
  456. {
  457. _tokenSemaphore.Release();
  458. }
  459. }
  460. private async Task<string> GetTokenInternal(string username, string password,
  461. CancellationToken cancellationToken)
  462. {
  463. var httpOptions = new HttpRequestOptions()
  464. {
  465. Url = ApiUrl + "/token",
  466. UserAgent = UserAgent,
  467. RequestContent = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}",
  468. CancellationToken = cancellationToken
  469. };
  470. //_logger.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
  471. // httpOptions.RequestContent);
  472. using (var responce = await _httpClient.Post(httpOptions))
  473. {
  474. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Token>(responce.Content);
  475. if (root.message == "OK")
  476. {
  477. _logger.Info("Authenticated with Schedules Direct token: " + root.token);
  478. return root.token;
  479. }
  480. throw new ApplicationException("Could not authenticate with Schedules Direct Error: " + root.message);
  481. }
  482. }
  483. private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
  484. {
  485. var token = await GetToken(info, cancellationToken);
  486. if (string.IsNullOrWhiteSpace(token))
  487. {
  488. throw new ArgumentException("Authentication required.");
  489. }
  490. if (string.IsNullOrWhiteSpace(info.ListingsId))
  491. {
  492. throw new ArgumentException("Listings Id required");
  493. }
  494. _logger.Info("Adding new LineUp ");
  495. var httpOptions = new HttpRequestOptions()
  496. {
  497. Url = ApiUrl + "/lineups/" + info.ListingsId,
  498. UserAgent = UserAgent,
  499. CancellationToken = cancellationToken
  500. };
  501. httpOptions.RequestHeaders["token"] = token;
  502. using (var response = await _httpClient.SendAsync(httpOptions, "PUT"))
  503. {
  504. }
  505. }
  506. public string Name
  507. {
  508. get { return "Schedules Direct"; }
  509. }
  510. public string Type
  511. {
  512. get { return "SchedulesDirect"; }
  513. }
  514. private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
  515. {
  516. if (string.IsNullOrWhiteSpace(info.ListingsId))
  517. {
  518. throw new ArgumentException("Listings Id required");
  519. }
  520. var token = await GetToken(info, cancellationToken);
  521. if (string.IsNullOrWhiteSpace(token))
  522. {
  523. throw new Exception("token required");
  524. }
  525. _logger.Info("Headends on account ");
  526. var options = new HttpRequestOptions()
  527. {
  528. Url = ApiUrl + "/lineups",
  529. UserAgent = UserAgent,
  530. CancellationToken = cancellationToken
  531. };
  532. options.RequestHeaders["token"] = token;
  533. using (var response = await _httpClient.Get(options).ConfigureAwait(false))
  534. {
  535. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Lineups>(response);
  536. return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
  537. }
  538. }
  539. public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  540. {
  541. if (validateLogin)
  542. {
  543. if (string.IsNullOrWhiteSpace(info.Username))
  544. {
  545. throw new ArgumentException("Username is required");
  546. }
  547. if (string.IsNullOrWhiteSpace(info.Password))
  548. {
  549. throw new ArgumentException("Password is required");
  550. }
  551. }
  552. if (validateListings)
  553. {
  554. if (string.IsNullOrWhiteSpace(info.ListingsId))
  555. {
  556. throw new ArgumentException("Listings Id required");
  557. }
  558. var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
  559. if (!hasLineup)
  560. {
  561. await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
  562. }
  563. }
  564. }
  565. public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
  566. {
  567. return GetHeadends(info, country, location, CancellationToken.None);
  568. }
  569. public class ScheduleDirect
  570. {
  571. public class Token
  572. {
  573. public int code { get; set; }
  574. public string message { get; set; }
  575. public string serverID { get; set; }
  576. public string token { get; set; }
  577. }
  578. public class Lineup
  579. {
  580. public string lineup { get; set; }
  581. public string name { get; set; }
  582. public string transport { get; set; }
  583. public string location { get; set; }
  584. public string uri { get; set; }
  585. }
  586. public class Lineups
  587. {
  588. public int code { get; set; }
  589. public string serverID { get; set; }
  590. public string datetime { get; set; }
  591. public List<Lineup> lineups { get; set; }
  592. }
  593. public class Headends
  594. {
  595. public string headend { get; set; }
  596. public string transport { get; set; }
  597. public string location { get; set; }
  598. public List<Lineup> lineups { get; set; }
  599. }
  600. public class Map
  601. {
  602. public string stationID { get; set; }
  603. public string channel { get; set; }
  604. public int uhfVhf { get; set; }
  605. public int atscMajor { get; set; }
  606. public int atscMinor { get; set; }
  607. }
  608. public class Broadcaster
  609. {
  610. public string city { get; set; }
  611. public string state { get; set; }
  612. public string postalcode { get; set; }
  613. public string country { get; set; }
  614. }
  615. public class Logo
  616. {
  617. public string URL { get; set; }
  618. public int height { get; set; }
  619. public int width { get; set; }
  620. public string md5 { get; set; }
  621. }
  622. public class Station
  623. {
  624. public string stationID { get; set; }
  625. public string name { get; set; }
  626. public string callsign { get; set; }
  627. public List<string> broadcastLanguage { get; set; }
  628. public List<string> descriptionLanguage { get; set; }
  629. public Broadcaster broadcaster { get; set; }
  630. public string affiliate { get; set; }
  631. public Logo logo { get; set; }
  632. public bool? isCommercialFree { get; set; }
  633. }
  634. public class Metadata
  635. {
  636. public string lineup { get; set; }
  637. public string modified { get; set; }
  638. public string transport { get; set; }
  639. }
  640. public class Channel
  641. {
  642. public List<Map> map { get; set; }
  643. public List<Station> stations { get; set; }
  644. public Metadata metadata { get; set; }
  645. }
  646. public class RequestScheduleForChannel
  647. {
  648. public string stationID { get; set; }
  649. public List<string> date { get; set; }
  650. }
  651. public class Rating
  652. {
  653. public string body { get; set; }
  654. public string code { get; set; }
  655. }
  656. public class Multipart
  657. {
  658. public int partNumber { get; set; }
  659. public int totalParts { get; set; }
  660. }
  661. public class Program
  662. {
  663. public string programID { get; set; }
  664. public string airDateTime { get; set; }
  665. public int duration { get; set; }
  666. public string md5 { get; set; }
  667. public List<string> audioProperties { get; set; }
  668. public List<string> videoProperties { get; set; }
  669. public List<Rating> ratings { get; set; }
  670. public bool? @new { get; set; }
  671. public Multipart multipart { get; set; }
  672. }
  673. public class MetadataSchedule
  674. {
  675. public string modified { get; set; }
  676. public string md5 { get; set; }
  677. public string startDate { get; set; }
  678. public string endDate { get; set; }
  679. public int days { get; set; }
  680. }
  681. public class Day
  682. {
  683. public string stationID { get; set; }
  684. public List<Program> programs { get; set; }
  685. public MetadataSchedule metadata { get; set; }
  686. }
  687. //
  688. public class Title
  689. {
  690. public string title120 { get; set; }
  691. }
  692. public class EventDetails
  693. {
  694. public string subType { get; set; }
  695. }
  696. public class Description100
  697. {
  698. public string descriptionLanguage { get; set; }
  699. public string description { get; set; }
  700. }
  701. public class Description1000
  702. {
  703. public string descriptionLanguage { get; set; }
  704. public string description { get; set; }
  705. }
  706. public class DescriptionsProgram
  707. {
  708. public List<Description100> description100 { get; set; }
  709. public List<Description1000> description1000 { get; set; }
  710. }
  711. public class Gracenote
  712. {
  713. public int season { get; set; }
  714. public int episode { get; set; }
  715. }
  716. public class MetadataPrograms
  717. {
  718. public Gracenote Gracenote { get; set; }
  719. }
  720. public class ContentRating
  721. {
  722. public string body { get; set; }
  723. public string code { get; set; }
  724. }
  725. public class Cast
  726. {
  727. public string billingOrder { get; set; }
  728. public string role { get; set; }
  729. public string nameId { get; set; }
  730. public string personId { get; set; }
  731. public string name { get; set; }
  732. public string characterName { get; set; }
  733. }
  734. public class Crew
  735. {
  736. public string billingOrder { get; set; }
  737. public string role { get; set; }
  738. public string nameId { get; set; }
  739. public string personId { get; set; }
  740. public string name { get; set; }
  741. }
  742. public class QualityRating
  743. {
  744. public string ratingsBody { get; set; }
  745. public string rating { get; set; }
  746. public string minRating { get; set; }
  747. public string maxRating { get; set; }
  748. public string increment { get; set; }
  749. }
  750. public class Movie
  751. {
  752. public string year { get; set; }
  753. public int duration { get; set; }
  754. public List<QualityRating> qualityRating { get; set; }
  755. }
  756. public class Recommendation
  757. {
  758. public string programID { get; set; }
  759. public string title120 { get; set; }
  760. }
  761. public class ProgramDetails
  762. {
  763. public string programID { get; set; }
  764. public List<Title> titles { get; set; }
  765. public EventDetails eventDetails { get; set; }
  766. public DescriptionsProgram descriptions { get; set; }
  767. public string originalAirDate { get; set; }
  768. public List<string> genres { get; set; }
  769. public string episodeTitle150 { get; set; }
  770. public List<MetadataPrograms> metadata { get; set; }
  771. public List<ContentRating> contentRating { get; set; }
  772. public List<Cast> cast { get; set; }
  773. public List<Crew> crew { get; set; }
  774. public string showType { get; set; }
  775. public bool hasImageArtwork { get; set; }
  776. public string images { get; set; }
  777. public string imageID { get; set; }
  778. public string md5 { get; set; }
  779. public List<string> contentAdvisory { get; set; }
  780. public Movie movie { get; set; }
  781. public List<Recommendation> recommendations { get; set; }
  782. }
  783. public class Caption
  784. {
  785. public string content { get; set; }
  786. public string lang { get; set; }
  787. }
  788. public class ImageData
  789. {
  790. public string width { get; set; }
  791. public string height { get; set; }
  792. public string uri { get; set; }
  793. public string size { get; set; }
  794. public string aspect { get; set; }
  795. public string category { get; set; }
  796. public string text { get; set; }
  797. public string primary { get; set; }
  798. public string tier { get; set; }
  799. public Caption caption { get; set; }
  800. }
  801. public class ShowImages
  802. {
  803. public string programID { get; set; }
  804. public List<ImageData> data { get; set; }
  805. }
  806. }
  807. }
  808. }