SchedulesDirect.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. using System.Net;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.LiveTv;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.LiveTv;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Net;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using MediaBrowser.Model.Extensions;
  19. namespace Emby.Server.Implementations.LiveTv.Listings
  20. {
  21. public class SchedulesDirect : IListingsProvider
  22. {
  23. private readonly ILogger _logger;
  24. private readonly IJsonSerializer _jsonSerializer;
  25. private readonly IHttpClient _httpClient;
  26. private readonly SemaphoreSlim _tokenSemaphore = new SemaphoreSlim(1, 1);
  27. private readonly IApplicationHost _appHost;
  28. private const string ApiUrl = "https://json.schedulesdirect.org/20141201";
  29. private readonly Dictionary<string, Dictionary<string, ScheduleDirect.Station>> _channelPairingCache =
  30. new Dictionary<string, Dictionary<string, ScheduleDirect.Station>>(StringComparer.OrdinalIgnoreCase);
  31. public SchedulesDirect(ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient, IApplicationHost appHost)
  32. {
  33. _logger = logger;
  34. _jsonSerializer = jsonSerializer;
  35. _httpClient = httpClient;
  36. _appHost = appHost;
  37. }
  38. private string UserAgent
  39. {
  40. get { return "Emby/" + _appHost.ApplicationVersion; }
  41. }
  42. private List<string> GetScheduleRequestDates(DateTime startDateUtc, DateTime endDateUtc)
  43. {
  44. List<string> dates = new List<string>();
  45. var start = new List<DateTime> { startDateUtc, startDateUtc.ToLocalTime() }.Min().Date;
  46. var end = new List<DateTime> { endDateUtc, endDateUtc.ToLocalTime() }.Max().Date;
  47. while (start <= end)
  48. {
  49. dates.Add(start.ToString("yyyy-MM-dd"));
  50. start = start.AddDays(1);
  51. }
  52. return dates;
  53. }
  54. public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
  55. {
  56. if (string.IsNullOrWhiteSpace(channelId))
  57. {
  58. throw new ArgumentNullException("channelId");
  59. }
  60. // Normalize incoming input
  61. channelId = channelId.Replace(".json.schedulesdirect.org", string.Empty, StringComparison.OrdinalIgnoreCase).TrimStart('I');
  62. List<ProgramInfo> programsInfo = new List<ProgramInfo>();
  63. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  64. if (string.IsNullOrWhiteSpace(token))
  65. {
  66. _logger.Warn("SchedulesDirect token is empty, returning empty program list");
  67. return programsInfo;
  68. }
  69. var dates = GetScheduleRequestDates(startDateUtc, endDateUtc);
  70. string stationID = channelId;
  71. _logger.Info("Channel Station ID is: " + stationID);
  72. List<ScheduleDirect.RequestScheduleForChannel> requestList =
  73. new List<ScheduleDirect.RequestScheduleForChannel>()
  74. {
  75. new ScheduleDirect.RequestScheduleForChannel()
  76. {
  77. stationID = stationID,
  78. date = dates
  79. }
  80. };
  81. var requestString = _jsonSerializer.SerializeToString(requestList);
  82. _logger.Debug("Request string for schedules is: " + requestString);
  83. var httpOptions = new HttpRequestOptions()
  84. {
  85. Url = ApiUrl + "/schedules",
  86. UserAgent = UserAgent,
  87. CancellationToken = cancellationToken,
  88. // The data can be large so give it some extra time
  89. TimeoutMs = 60000,
  90. LogErrorResponseBody = true
  91. };
  92. httpOptions.RequestHeaders["token"] = token;
  93. httpOptions.RequestContent = requestString;
  94. using (var response = await Post(httpOptions, true, info).ConfigureAwait(false))
  95. {
  96. StreamReader reader = new StreamReader(response.Content);
  97. string responseString = reader.ReadToEnd();
  98. var dailySchedules = _jsonSerializer.DeserializeFromString<List<ScheduleDirect.Day>>(responseString);
  99. _logger.Debug("Found " + dailySchedules.Count + " programs on " + stationID + " ScheduleDirect");
  100. httpOptions = new HttpRequestOptions()
  101. {
  102. Url = ApiUrl + "/programs",
  103. UserAgent = UserAgent,
  104. CancellationToken = cancellationToken,
  105. LogErrorResponseBody = true,
  106. // The data can be large so give it some extra time
  107. TimeoutMs = 60000
  108. };
  109. httpOptions.RequestHeaders["token"] = token;
  110. List<string> programsID = new List<string>();
  111. programsID = dailySchedules.SelectMany(d => d.programs.Select(s => s.programID)).Distinct().ToList();
  112. var requestBody = "[\"" + string.Join("\", \"", programsID) + "\"]";
  113. httpOptions.RequestContent = requestBody;
  114. double wideAspect = 1.77777778;
  115. var primaryImageCategory = "Logo";
  116. using (var innerResponse = await Post(httpOptions, true, info).ConfigureAwait(false))
  117. {
  118. StreamReader innerReader = new StreamReader(innerResponse.Content);
  119. responseString = innerReader.ReadToEnd();
  120. var programDetails =
  121. _jsonSerializer.DeserializeFromString<List<ScheduleDirect.ProgramDetails>>(
  122. responseString);
  123. var programDict = programDetails.ToDictionary(p => p.programID, y => y);
  124. var programIdsWithImages =
  125. programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID)
  126. .ToList();
  127. var images = await GetImageForPrograms(info, programIdsWithImages, cancellationToken).ConfigureAwait(false);
  128. var schedules = dailySchedules.SelectMany(d => d.programs);
  129. foreach (ScheduleDirect.Program schedule in schedules)
  130. {
  131. //_logger.Debug("Proccesing Schedule for statio ID " + stationID +
  132. // " which corresponds to channel " + channelNumber + " and program id " +
  133. // schedule.programID + " which says it has images? " +
  134. // programDict[schedule.programID].hasImageArtwork);
  135. if (images != null)
  136. {
  137. var imageIndex = images.FindIndex(i => i.programID == schedule.programID.Substring(0, 10));
  138. if (imageIndex > -1)
  139. {
  140. var programEntry = programDict[schedule.programID];
  141. var allImages = (images[imageIndex].data ?? new List<ScheduleDirect.ImageData>()).ToList();
  142. var imagesWithText = allImages.Where(i => string.Equals(i.text, "yes", StringComparison.OrdinalIgnoreCase)).ToList();
  143. var imagesWithoutText = allImages.Where(i => string.Equals(i.text, "no", StringComparison.OrdinalIgnoreCase)).ToList();
  144. double desiredAspect = 0.666666667;
  145. programEntry.primaryImage = GetProgramImage(ApiUrl, imagesWithText, true, desiredAspect) ??
  146. GetProgramImage(ApiUrl, allImages, true, desiredAspect);
  147. programEntry.thumbImage = GetProgramImage(ApiUrl, imagesWithText, true, wideAspect);
  148. // Don't supply the same image twice
  149. if (string.Equals(programEntry.primaryImage, programEntry.thumbImage, StringComparison.Ordinal))
  150. {
  151. programEntry.thumbImage = null;
  152. }
  153. programEntry.backdropImage = GetProgramImage(ApiUrl, imagesWithoutText, true, wideAspect);
  154. //programEntry.bannerImage = GetProgramImage(ApiUrl, data, "Banner", false) ??
  155. // GetProgramImage(ApiUrl, data, "Banner-L1", false) ??
  156. // GetProgramImage(ApiUrl, data, "Banner-LO", false) ??
  157. // GetProgramImage(ApiUrl, data, "Banner-LOT", false);
  158. }
  159. }
  160. programsInfo.Add(GetProgram(channelId, schedule, programDict[schedule.programID]));
  161. }
  162. }
  163. }
  164. return programsInfo;
  165. }
  166. private int GetSizeOrder(ScheduleDirect.ImageData image)
  167. {
  168. if (!string.IsNullOrWhiteSpace(image.height))
  169. {
  170. int value;
  171. if (int.TryParse(image.height, out value))
  172. {
  173. return value;
  174. }
  175. }
  176. return 0;
  177. }
  178. private string GetChannelNumber(ScheduleDirect.Map map)
  179. {
  180. var channelNumber = map.logicalChannelNumber;
  181. if (string.IsNullOrWhiteSpace(channelNumber))
  182. {
  183. channelNumber = map.channel;
  184. }
  185. if (string.IsNullOrWhiteSpace(channelNumber))
  186. {
  187. channelNumber = map.atscMajor + "." + map.atscMinor;
  188. }
  189. channelNumber = channelNumber.TrimStart('0');
  190. return channelNumber;
  191. }
  192. private bool IsMovie(ScheduleDirect.ProgramDetails programInfo)
  193. {
  194. return string.Equals(programInfo.entityType, "movie", StringComparison.OrdinalIgnoreCase);
  195. }
  196. private ProgramInfo GetProgram(string channelId, ScheduleDirect.Program programInfo, ScheduleDirect.ProgramDetails details)
  197. {
  198. DateTime startAt = GetDate(programInfo.airDateTime);
  199. DateTime endAt = startAt.AddSeconds(programInfo.duration);
  200. ProgramAudio audioType = ProgramAudio.Stereo;
  201. bool repeat = programInfo.@new == null;
  202. var programId = programInfo.programID ?? string.Empty;
  203. string newID = programId + "T" + startAt.Ticks + "C" + channelId;
  204. if (programInfo.audioProperties != null)
  205. {
  206. if (programInfo.audioProperties.Exists(item => string.Equals(item, "atmos", StringComparison.OrdinalIgnoreCase)))
  207. {
  208. audioType = ProgramAudio.Atmos;
  209. }
  210. else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd 5.1", StringComparison.OrdinalIgnoreCase)))
  211. {
  212. audioType = ProgramAudio.DolbyDigital;
  213. }
  214. else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd", StringComparison.OrdinalIgnoreCase)))
  215. {
  216. audioType = ProgramAudio.DolbyDigital;
  217. }
  218. else if (programInfo.audioProperties.Exists(item => string.Equals(item, "stereo", StringComparison.OrdinalIgnoreCase)))
  219. {
  220. audioType = ProgramAudio.Stereo;
  221. }
  222. else
  223. {
  224. audioType = ProgramAudio.Mono;
  225. }
  226. }
  227. string episodeTitle = null;
  228. if (details.episodeTitle150 != null)
  229. {
  230. episodeTitle = details.episodeTitle150;
  231. }
  232. var info = new ProgramInfo
  233. {
  234. ChannelId = channelId,
  235. Id = newID,
  236. StartDate = startAt,
  237. EndDate = endAt,
  238. Name = details.titles[0].title120 ?? "Unkown",
  239. OfficialRating = null,
  240. CommunityRating = null,
  241. EpisodeTitle = episodeTitle,
  242. Audio = audioType,
  243. IsRepeat = repeat,
  244. IsSeries = string.Equals(details.entityType, "episode", StringComparison.OrdinalIgnoreCase),
  245. ImageUrl = details.primaryImage,
  246. ThumbImageUrl = details.thumbImage,
  247. IsKids = string.Equals(details.audience, "children", StringComparison.OrdinalIgnoreCase),
  248. IsSports = string.Equals(details.entityType, "sports", StringComparison.OrdinalIgnoreCase),
  249. IsMovie = IsMovie(details),
  250. Etag = programInfo.md5
  251. };
  252. var showId = programId;
  253. if (!info.IsSeries)
  254. {
  255. // It's also a series if it starts with SH
  256. info.IsSeries = showId.StartsWith("SH", StringComparison.OrdinalIgnoreCase) && showId.Length >= 14;
  257. }
  258. // According to SchedulesDirect, these are generic, unidentified episodes
  259. // SH005316560000
  260. var hasUniqueShowId = !showId.StartsWith("SH", StringComparison.OrdinalIgnoreCase) ||
  261. !showId.EndsWith("0000", StringComparison.OrdinalIgnoreCase);
  262. if (!hasUniqueShowId)
  263. {
  264. showId = newID;
  265. }
  266. info.ShowId = showId;
  267. if (programInfo.videoProperties != null)
  268. {
  269. info.IsHD = programInfo.videoProperties.Contains("hdtv", StringComparer.OrdinalIgnoreCase);
  270. info.Is3D = programInfo.videoProperties.Contains("3d", StringComparer.OrdinalIgnoreCase);
  271. }
  272. if (details.contentRating != null && details.contentRating.Count > 0)
  273. {
  274. info.OfficialRating = details.contentRating[0].code.Replace("TV", "TV-").Replace("--", "-");
  275. var invalid = new[] { "N/A", "Approved", "Not Rated", "Passed" };
  276. if (invalid.Contains(info.OfficialRating, StringComparer.OrdinalIgnoreCase))
  277. {
  278. info.OfficialRating = null;
  279. }
  280. }
  281. if (details.descriptions != null)
  282. {
  283. if (details.descriptions.description1000 != null && details.descriptions.description1000.Count > 0)
  284. {
  285. info.Overview = details.descriptions.description1000[0].description;
  286. }
  287. else if (details.descriptions.description100 != null && details.descriptions.description100.Count > 0)
  288. {
  289. info.Overview = details.descriptions.description100[0].description;
  290. }
  291. }
  292. if (info.IsSeries)
  293. {
  294. info.SeriesId = programId.Substring(0, 10);
  295. if (details.metadata != null)
  296. {
  297. foreach (var metadataProgram in details.metadata)
  298. {
  299. var gracenote = metadataProgram.Gracenote;
  300. if (gracenote != null)
  301. {
  302. info.SeasonNumber = gracenote.season;
  303. if (gracenote.episode > 0)
  304. {
  305. info.EpisodeNumber = gracenote.episode;
  306. }
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. if (!string.IsNullOrWhiteSpace(details.originalAirDate) && (!info.IsSeries || info.IsRepeat))
  313. {
  314. info.OriginalAirDate = DateTime.Parse(details.originalAirDate);
  315. info.ProductionYear = info.OriginalAirDate.Value.Year;
  316. }
  317. if (details.genres != null)
  318. {
  319. info.Genres = details.genres.Where(g => !string.IsNullOrWhiteSpace(g)).ToList();
  320. info.IsNews = details.genres.Contains("news", StringComparer.OrdinalIgnoreCase);
  321. if (info.Genres.Contains("children", StringComparer.OrdinalIgnoreCase))
  322. {
  323. info.IsKids = true;
  324. }
  325. }
  326. return info;
  327. }
  328. private DateTime GetDate(string value)
  329. {
  330. var date = DateTime.ParseExact(value, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture);
  331. if (date.Kind != DateTimeKind.Utc)
  332. {
  333. date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
  334. }
  335. return date;
  336. }
  337. private string GetProgramImage(string apiUrl, List<ScheduleDirect.ImageData> images, bool returnDefaultImage, double desiredAspect)
  338. {
  339. string url = null;
  340. var matches = images;
  341. matches = matches
  342. .OrderBy(i => Math.Abs(desiredAspect - GetApsectRatio(i)))
  343. .ThenByDescending(GetSizeOrder)
  344. .ToList();
  345. var match = matches.FirstOrDefault();
  346. if (match == null)
  347. {
  348. return null;
  349. }
  350. var uri = match.uri;
  351. if (!string.IsNullOrWhiteSpace(uri))
  352. {
  353. if (uri.IndexOf("http", StringComparison.OrdinalIgnoreCase) != -1)
  354. {
  355. url = uri;
  356. }
  357. else
  358. {
  359. url = apiUrl + "/image/" + uri;
  360. }
  361. }
  362. //_logger.Debug("URL for image is : " + url);
  363. return url;
  364. }
  365. private double GetApsectRatio(ScheduleDirect.ImageData i)
  366. {
  367. int width = 0;
  368. int height = 0;
  369. if (!string.IsNullOrWhiteSpace(i.width))
  370. {
  371. int.TryParse(i.width, out width);
  372. }
  373. if (!string.IsNullOrWhiteSpace(i.height))
  374. {
  375. int.TryParse(i.height, out height);
  376. }
  377. if (height == 0 || width == 0)
  378. {
  379. return 0;
  380. }
  381. double result = width;
  382. result /= height;
  383. return result;
  384. }
  385. private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(
  386. ListingsProviderInfo info,
  387. List<string> programIds,
  388. CancellationToken cancellationToken)
  389. {
  390. if (programIds.Count == 0)
  391. {
  392. return new List<ScheduleDirect.ShowImages>();
  393. }
  394. var imageIdString = "[";
  395. foreach (var i in programIds)
  396. {
  397. var imageId = i.Substring(0, 10);
  398. if (!imageIdString.Contains(imageId))
  399. {
  400. imageIdString += "\"" + imageId + "\",";
  401. }
  402. }
  403. imageIdString = imageIdString.TrimEnd(',') + "]";
  404. var httpOptions = new HttpRequestOptions()
  405. {
  406. Url = ApiUrl + "/metadata/programs",
  407. UserAgent = UserAgent,
  408. CancellationToken = cancellationToken,
  409. RequestContent = imageIdString,
  410. LogErrorResponseBody = true,
  411. // The data can be large so give it some extra time
  412. TimeoutMs = 60000
  413. };
  414. try
  415. {
  416. using (var innerResponse2 = await Post(httpOptions, true, info).ConfigureAwait(false))
  417. {
  418. return _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.ShowImages>>(
  419. innerResponse2.Content);
  420. }
  421. }
  422. catch (Exception ex)
  423. {
  424. _logger.ErrorException("Error getting image info from schedules direct", ex);
  425. return new List<ScheduleDirect.ShowImages>();
  426. }
  427. }
  428. public async Task<List<NameIdPair>> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
  429. {
  430. var token = await GetToken(info, cancellationToken);
  431. var lineups = new List<NameIdPair>();
  432. if (string.IsNullOrWhiteSpace(token))
  433. {
  434. return lineups;
  435. }
  436. var options = new HttpRequestOptions()
  437. {
  438. Url = ApiUrl + "/headends?country=" + country + "&postalcode=" + location,
  439. UserAgent = UserAgent,
  440. CancellationToken = cancellationToken,
  441. LogErrorResponseBody = true
  442. };
  443. options.RequestHeaders["token"] = token;
  444. try
  445. {
  446. using (var httpResponse = await Get(options, false, info).ConfigureAwait(false))
  447. {
  448. using (Stream responce = httpResponse.Content)
  449. {
  450. var root = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.Headends>>(responce);
  451. if (root != null)
  452. {
  453. foreach (ScheduleDirect.Headends headend in root)
  454. {
  455. foreach (ScheduleDirect.Lineup lineup in headend.lineups)
  456. {
  457. lineups.Add(new NameIdPair
  458. {
  459. Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
  460. Id = lineup.uri.Substring(18)
  461. });
  462. }
  463. }
  464. }
  465. else
  466. {
  467. _logger.Info("No lineups available");
  468. }
  469. }
  470. }
  471. }
  472. catch (Exception ex)
  473. {
  474. _logger.Error("Error getting headends", ex);
  475. }
  476. return lineups;
  477. }
  478. private readonly ConcurrentDictionary<string, NameValuePair> _tokens = new ConcurrentDictionary<string, NameValuePair>();
  479. private DateTime _lastErrorResponse;
  480. private async Task<string> GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
  481. {
  482. var username = info.Username;
  483. // Reset the token if there's no username
  484. if (string.IsNullOrWhiteSpace(username))
  485. {
  486. return null;
  487. }
  488. var password = info.Password;
  489. if (string.IsNullOrWhiteSpace(password))
  490. {
  491. return null;
  492. }
  493. // Avoid hammering SD
  494. if ((DateTime.UtcNow - _lastErrorResponse).TotalMinutes < 1)
  495. {
  496. return null;
  497. }
  498. NameValuePair savedToken = null;
  499. if (!_tokens.TryGetValue(username, out savedToken))
  500. {
  501. savedToken = new NameValuePair();
  502. _tokens.TryAdd(username, savedToken);
  503. }
  504. if (!string.IsNullOrWhiteSpace(savedToken.Name) && !string.IsNullOrWhiteSpace(savedToken.Value))
  505. {
  506. long ticks;
  507. if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out ticks))
  508. {
  509. // If it's under 24 hours old we can still use it
  510. if (DateTime.UtcNow.Ticks - ticks < TimeSpan.FromHours(20).Ticks)
  511. {
  512. return savedToken.Name;
  513. }
  514. }
  515. }
  516. await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  517. try
  518. {
  519. var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
  520. savedToken.Name = result;
  521. savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
  522. return result;
  523. }
  524. catch (HttpException ex)
  525. {
  526. if (ex.StatusCode.HasValue)
  527. {
  528. if ((int)ex.StatusCode.Value == 400)
  529. {
  530. _tokens.Clear();
  531. _lastErrorResponse = DateTime.UtcNow;
  532. }
  533. }
  534. throw;
  535. }
  536. finally
  537. {
  538. _tokenSemaphore.Release();
  539. }
  540. }
  541. private async Task<HttpResponseInfo> Post(HttpRequestOptions options,
  542. bool enableRetry,
  543. ListingsProviderInfo providerInfo)
  544. {
  545. // Schedules direct requires that the client support compression and will return a 400 response without it
  546. options.EnableHttpCompression = true;
  547. // On windows 7 under .net core, this header is not getting added
  548. #if NETSTANDARD2_0
  549. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  550. {
  551. options.RequestHeaders["Accept-Encoding"] = "deflate";
  552. }
  553. #endif
  554. try
  555. {
  556. return await _httpClient.Post(options).ConfigureAwait(false);
  557. }
  558. catch (HttpException ex)
  559. {
  560. _tokens.Clear();
  561. if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
  562. {
  563. enableRetry = false;
  564. }
  565. if (!enableRetry)
  566. {
  567. throw;
  568. }
  569. }
  570. var newToken = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
  571. options.RequestHeaders["token"] = newToken;
  572. return await Post(options, false, providerInfo).ConfigureAwait(false);
  573. }
  574. private async Task<HttpResponseInfo> Get(HttpRequestOptions options,
  575. bool enableRetry,
  576. ListingsProviderInfo providerInfo)
  577. {
  578. // Schedules direct requires that the client support compression and will return a 400 response without it
  579. options.EnableHttpCompression = true;
  580. // On windows 7 under .net core, this header is not getting added
  581. #if NETSTANDARD2_0
  582. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  583. {
  584. options.RequestHeaders["Accept-Encoding"] = "deflate";
  585. }
  586. #endif
  587. try
  588. {
  589. return await _httpClient.SendAsync(options, "GET").ConfigureAwait(false);
  590. }
  591. catch (HttpException ex)
  592. {
  593. _tokens.Clear();
  594. if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
  595. {
  596. enableRetry = false;
  597. }
  598. if (!enableRetry)
  599. {
  600. throw;
  601. }
  602. }
  603. var newToken = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
  604. options.RequestHeaders["token"] = newToken;
  605. return await Get(options, false, providerInfo).ConfigureAwait(false);
  606. }
  607. private async Task<string> GetTokenInternal(string username, string password,
  608. CancellationToken cancellationToken)
  609. {
  610. var httpOptions = new HttpRequestOptions()
  611. {
  612. Url = ApiUrl + "/token",
  613. UserAgent = UserAgent,
  614. RequestContent = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}",
  615. CancellationToken = cancellationToken,
  616. LogErrorResponseBody = true
  617. };
  618. //_logger.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
  619. // httpOptions.RequestContent);
  620. using (var responce = await Post(httpOptions, false, null).ConfigureAwait(false))
  621. {
  622. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Token>(responce.Content);
  623. if (root.message == "OK")
  624. {
  625. _logger.Info("Authenticated with Schedules Direct token: " + root.token);
  626. return root.token;
  627. }
  628. throw new Exception("Could not authenticate with Schedules Direct Error: " + root.message);
  629. }
  630. }
  631. private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
  632. {
  633. var token = await GetToken(info, cancellationToken);
  634. if (string.IsNullOrWhiteSpace(token))
  635. {
  636. throw new ArgumentException("Authentication required.");
  637. }
  638. if (string.IsNullOrWhiteSpace(info.ListingsId))
  639. {
  640. throw new ArgumentException("Listings Id required");
  641. }
  642. _logger.Info("Adding new LineUp ");
  643. var httpOptions = new HttpRequestOptions()
  644. {
  645. Url = ApiUrl + "/lineups/" + info.ListingsId,
  646. UserAgent = UserAgent,
  647. CancellationToken = cancellationToken,
  648. LogErrorResponseBody = true,
  649. BufferContent = false
  650. };
  651. httpOptions.RequestHeaders["token"] = token;
  652. using (var response = await _httpClient.SendAsync(httpOptions, "PUT"))
  653. {
  654. }
  655. }
  656. public string Name
  657. {
  658. get { return "Schedules Direct"; }
  659. }
  660. public static string TypeName = "SchedulesDirect";
  661. public string Type
  662. {
  663. get { return TypeName; }
  664. }
  665. private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
  666. {
  667. if (string.IsNullOrWhiteSpace(info.ListingsId))
  668. {
  669. throw new ArgumentException("Listings Id required");
  670. }
  671. var token = await GetToken(info, cancellationToken);
  672. if (string.IsNullOrWhiteSpace(token))
  673. {
  674. throw new Exception("token required");
  675. }
  676. _logger.Info("Headends on account ");
  677. var options = new HttpRequestOptions()
  678. {
  679. Url = ApiUrl + "/lineups",
  680. UserAgent = UserAgent,
  681. CancellationToken = cancellationToken,
  682. LogErrorResponseBody = true
  683. };
  684. options.RequestHeaders["token"] = token;
  685. try
  686. {
  687. using (var httpResponse = await Get(options, false, null).ConfigureAwait(false))
  688. {
  689. using (var response = httpResponse.Content)
  690. {
  691. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Lineups>(response);
  692. return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
  693. }
  694. }
  695. }
  696. catch (HttpException ex)
  697. {
  698. // Apparently we're supposed to swallow this
  699. if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.BadRequest)
  700. {
  701. return false;
  702. }
  703. throw;
  704. }
  705. }
  706. public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  707. {
  708. if (validateLogin)
  709. {
  710. if (string.IsNullOrWhiteSpace(info.Username))
  711. {
  712. throw new ArgumentException("Username is required");
  713. }
  714. if (string.IsNullOrWhiteSpace(info.Password))
  715. {
  716. throw new ArgumentException("Password is required");
  717. }
  718. }
  719. if (validateListings)
  720. {
  721. if (string.IsNullOrWhiteSpace(info.ListingsId))
  722. {
  723. throw new ArgumentException("Listings Id required");
  724. }
  725. var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
  726. if (!hasLineup)
  727. {
  728. await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
  729. }
  730. }
  731. }
  732. public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
  733. {
  734. return GetHeadends(info, country, location, CancellationToken.None);
  735. }
  736. public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
  737. {
  738. var listingsId = info.ListingsId;
  739. if (string.IsNullOrWhiteSpace(listingsId))
  740. {
  741. throw new Exception("ListingsId required");
  742. }
  743. var token = await GetToken(info, cancellationToken);
  744. if (string.IsNullOrWhiteSpace(token))
  745. {
  746. throw new Exception("token required");
  747. }
  748. var httpOptions = new HttpRequestOptions()
  749. {
  750. Url = ApiUrl + "/lineups/" + listingsId,
  751. UserAgent = UserAgent,
  752. CancellationToken = cancellationToken,
  753. LogErrorResponseBody = true,
  754. // The data can be large so give it some extra time
  755. TimeoutMs = 60000
  756. };
  757. httpOptions.RequestHeaders["token"] = token;
  758. var list = new List<ChannelInfo>();
  759. using (var httpResponse = await Get(httpOptions, true, info).ConfigureAwait(false))
  760. {
  761. using (var response = httpResponse.Content)
  762. {
  763. var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Channel>(response);
  764. _logger.Info("Found " + root.map.Count + " channels on the lineup on ScheduleDirect");
  765. _logger.Info("Mapping Stations to Channel");
  766. var allStations = root.stations ?? new List<ScheduleDirect.Station>();
  767. foreach (ScheduleDirect.Map map in root.map)
  768. {
  769. var channelNumber = GetChannelNumber(map);
  770. var station = allStations.FirstOrDefault(item => string.Equals(item.stationID, map.stationID, StringComparison.OrdinalIgnoreCase));
  771. if (station == null)
  772. {
  773. station = new ScheduleDirect.Station
  774. {
  775. stationID = map.stationID
  776. };
  777. }
  778. var name = channelNumber;
  779. var channelInfo = new ChannelInfo
  780. {
  781. Number = channelNumber,
  782. Name = name
  783. };
  784. if (station != null)
  785. {
  786. if (!string.IsNullOrWhiteSpace(station.name))
  787. {
  788. channelInfo.Name = station.name;
  789. }
  790. channelInfo.Id = station.stationID;
  791. channelInfo.CallSign = station.callsign;
  792. if (station.logo != null)
  793. {
  794. channelInfo.ImageUrl = station.logo.URL;
  795. channelInfo.HasImage = true;
  796. }
  797. }
  798. list.Add(channelInfo);
  799. }
  800. }
  801. }
  802. return list;
  803. }
  804. private ScheduleDirect.Station GetStation(List<ScheduleDirect.Station> allStations, string channelNumber, string channelName)
  805. {
  806. if (!string.IsNullOrWhiteSpace(channelName))
  807. {
  808. channelName = NormalizeName(channelName);
  809. var result = allStations.FirstOrDefault(i => string.Equals(NormalizeName(i.callsign ?? string.Empty), channelName, StringComparison.OrdinalIgnoreCase));
  810. if (result != null)
  811. {
  812. return result;
  813. }
  814. }
  815. if (!string.IsNullOrWhiteSpace(channelNumber))
  816. {
  817. return allStations.FirstOrDefault(i => string.Equals(NormalizeName(i.stationID ?? string.Empty), channelNumber, StringComparison.OrdinalIgnoreCase));
  818. }
  819. return null;
  820. }
  821. private string NormalizeName(string value)
  822. {
  823. return value.Replace(" ", string.Empty).Replace("-", string.Empty);
  824. }
  825. public class ScheduleDirect
  826. {
  827. public class Token
  828. {
  829. public int code { get; set; }
  830. public string message { get; set; }
  831. public string serverID { get; set; }
  832. public string token { get; set; }
  833. }
  834. public class Lineup
  835. {
  836. public string lineup { get; set; }
  837. public string name { get; set; }
  838. public string transport { get; set; }
  839. public string location { get; set; }
  840. public string uri { get; set; }
  841. }
  842. public class Lineups
  843. {
  844. public int code { get; set; }
  845. public string serverID { get; set; }
  846. public string datetime { get; set; }
  847. public List<Lineup> lineups { get; set; }
  848. }
  849. public class Headends
  850. {
  851. public string headend { get; set; }
  852. public string transport { get; set; }
  853. public string location { get; set; }
  854. public List<Lineup> lineups { get; set; }
  855. }
  856. public class Map
  857. {
  858. public string stationID { get; set; }
  859. public string channel { get; set; }
  860. public string logicalChannelNumber { get; set; }
  861. public int uhfVhf { get; set; }
  862. public int atscMajor { get; set; }
  863. public int atscMinor { get; set; }
  864. }
  865. public class Broadcaster
  866. {
  867. public string city { get; set; }
  868. public string state { get; set; }
  869. public string postalcode { get; set; }
  870. public string country { get; set; }
  871. }
  872. public class Logo
  873. {
  874. public string URL { get; set; }
  875. public int height { get; set; }
  876. public int width { get; set; }
  877. public string md5 { get; set; }
  878. }
  879. public class Station
  880. {
  881. public string stationID { get; set; }
  882. public string name { get; set; }
  883. public string callsign { get; set; }
  884. public List<string> broadcastLanguage { get; set; }
  885. public List<string> descriptionLanguage { get; set; }
  886. public Broadcaster broadcaster { get; set; }
  887. public string affiliate { get; set; }
  888. public Logo logo { get; set; }
  889. public bool? isCommercialFree { get; set; }
  890. }
  891. public class Metadata
  892. {
  893. public string lineup { get; set; }
  894. public string modified { get; set; }
  895. public string transport { get; set; }
  896. }
  897. public class Channel
  898. {
  899. public List<Map> map { get; set; }
  900. public List<Station> stations { get; set; }
  901. public Metadata metadata { get; set; }
  902. }
  903. public class RequestScheduleForChannel
  904. {
  905. public string stationID { get; set; }
  906. public List<string> date { get; set; }
  907. }
  908. public class Rating
  909. {
  910. public string body { get; set; }
  911. public string code { get; set; }
  912. }
  913. public class Multipart
  914. {
  915. public int partNumber { get; set; }
  916. public int totalParts { get; set; }
  917. }
  918. public class Program
  919. {
  920. public string programID { get; set; }
  921. public string airDateTime { get; set; }
  922. public int duration { get; set; }
  923. public string md5 { get; set; }
  924. public List<string> audioProperties { get; set; }
  925. public List<string> videoProperties { get; set; }
  926. public List<Rating> ratings { get; set; }
  927. public bool? @new { get; set; }
  928. public Multipart multipart { get; set; }
  929. }
  930. public class MetadataSchedule
  931. {
  932. public string modified { get; set; }
  933. public string md5 { get; set; }
  934. public string startDate { get; set; }
  935. public string endDate { get; set; }
  936. public int days { get; set; }
  937. }
  938. public class Day
  939. {
  940. public string stationID { get; set; }
  941. public List<Program> programs { get; set; }
  942. public MetadataSchedule metadata { get; set; }
  943. public Day()
  944. {
  945. programs = new List<Program>();
  946. }
  947. }
  948. //
  949. public class Title
  950. {
  951. public string title120 { get; set; }
  952. }
  953. public class EventDetails
  954. {
  955. public string subType { get; set; }
  956. }
  957. public class Description100
  958. {
  959. public string descriptionLanguage { get; set; }
  960. public string description { get; set; }
  961. }
  962. public class Description1000
  963. {
  964. public string descriptionLanguage { get; set; }
  965. public string description { get; set; }
  966. }
  967. public class DescriptionsProgram
  968. {
  969. public List<Description100> description100 { get; set; }
  970. public List<Description1000> description1000 { get; set; }
  971. }
  972. public class Gracenote
  973. {
  974. public int season { get; set; }
  975. public int episode { get; set; }
  976. }
  977. public class MetadataPrograms
  978. {
  979. public Gracenote Gracenote { get; set; }
  980. }
  981. public class ContentRating
  982. {
  983. public string body { get; set; }
  984. public string code { get; set; }
  985. }
  986. public class Cast
  987. {
  988. public string billingOrder { get; set; }
  989. public string role { get; set; }
  990. public string nameId { get; set; }
  991. public string personId { get; set; }
  992. public string name { get; set; }
  993. public string characterName { get; set; }
  994. }
  995. public class Crew
  996. {
  997. public string billingOrder { get; set; }
  998. public string role { get; set; }
  999. public string nameId { get; set; }
  1000. public string personId { get; set; }
  1001. public string name { get; set; }
  1002. }
  1003. public class QualityRating
  1004. {
  1005. public string ratingsBody { get; set; }
  1006. public string rating { get; set; }
  1007. public string minRating { get; set; }
  1008. public string maxRating { get; set; }
  1009. public string increment { get; set; }
  1010. }
  1011. public class Movie
  1012. {
  1013. public string year { get; set; }
  1014. public int duration { get; set; }
  1015. public List<QualityRating> qualityRating { get; set; }
  1016. }
  1017. public class Recommendation
  1018. {
  1019. public string programID { get; set; }
  1020. public string title120 { get; set; }
  1021. }
  1022. public class ProgramDetails
  1023. {
  1024. public string audience { get; set; }
  1025. public string programID { get; set; }
  1026. public List<Title> titles { get; set; }
  1027. public EventDetails eventDetails { get; set; }
  1028. public DescriptionsProgram descriptions { get; set; }
  1029. public string originalAirDate { get; set; }
  1030. public List<string> genres { get; set; }
  1031. public string episodeTitle150 { get; set; }
  1032. public List<MetadataPrograms> metadata { get; set; }
  1033. public List<ContentRating> contentRating { get; set; }
  1034. public List<Cast> cast { get; set; }
  1035. public List<Crew> crew { get; set; }
  1036. public string entityType { get; set; }
  1037. public string showType { get; set; }
  1038. public bool hasImageArtwork { get; set; }
  1039. public string primaryImage { get; set; }
  1040. public string thumbImage { get; set; }
  1041. public string backdropImage { get; set; }
  1042. public string bannerImage { get; set; }
  1043. public string imageID { get; set; }
  1044. public string md5 { get; set; }
  1045. public List<string> contentAdvisory { get; set; }
  1046. public Movie movie { get; set; }
  1047. public List<Recommendation> recommendations { get; set; }
  1048. }
  1049. public class Caption
  1050. {
  1051. public string content { get; set; }
  1052. public string lang { get; set; }
  1053. }
  1054. public class ImageData
  1055. {
  1056. public string width { get; set; }
  1057. public string height { get; set; }
  1058. public string uri { get; set; }
  1059. public string size { get; set; }
  1060. public string aspect { get; set; }
  1061. public string category { get; set; }
  1062. public string text { get; set; }
  1063. public string primary { get; set; }
  1064. public string tier { get; set; }
  1065. public Caption caption { get; set; }
  1066. }
  1067. public class ShowImages
  1068. {
  1069. public string programID { get; set; }
  1070. public List<ImageData> data { get; set; }
  1071. }
  1072. }
  1073. }
  1074. }