SchedulesDirect.cs 43 KB

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