SchedulesDirect.cs 35 KB

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