SchedulesDirect.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. {
  386. Content = new StringContent(imageIdString, Encoding.UTF8, MediaTypeNames.Application.Json)
  387. };
  388. try
  389. {
  390. using var innerResponse2 = await Send(message, true, info, cancellationToken).ConfigureAwait(false);
  391. await using var response = await innerResponse2.Content.ReadAsStreamAsync().ConfigureAwait(false);
  392. return await _jsonSerializer.DeserializeFromStreamAsync<List<ScheduleDirect.ShowImages>>(
  393. response).ConfigureAwait(false);
  394. }
  395. catch (Exception ex)
  396. {
  397. _logger.LogError(ex, "Error getting image info from schedules direct");
  398. return new List<ScheduleDirect.ShowImages>();
  399. }
  400. }
  401. public async Task<List<NameIdPair>> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
  402. {
  403. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  404. var lineups = new List<NameIdPair>();
  405. if (string.IsNullOrWhiteSpace(token))
  406. {
  407. return lineups;
  408. }
  409. using var options = new HttpRequestMessage(HttpMethod.Get, ApiUrl + "/headends?country=" + country + "&postalcode=" + location);
  410. options.Headers.TryAddWithoutValidation("token", token);
  411. try
  412. {
  413. using var httpResponse = await Send(options, false, info, cancellationToken).ConfigureAwait(false);
  414. await using var response = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
  415. var root = await _jsonSerializer.DeserializeFromStreamAsync<List<ScheduleDirect.Headends>>(response).ConfigureAwait(false);
  416. if (root != null)
  417. {
  418. foreach (ScheduleDirect.Headends headend in root)
  419. {
  420. foreach (ScheduleDirect.Lineup lineup in headend.lineups)
  421. {
  422. lineups.Add(new NameIdPair
  423. {
  424. Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
  425. Id = lineup.uri.Substring(18)
  426. });
  427. }
  428. }
  429. }
  430. else
  431. {
  432. _logger.LogInformation("No lineups available");
  433. }
  434. }
  435. catch (Exception ex)
  436. {
  437. _logger.LogError(ex, "Error getting headends");
  438. }
  439. return lineups;
  440. }
  441. private readonly ConcurrentDictionary<string, NameValuePair> _tokens = new ConcurrentDictionary<string, NameValuePair>();
  442. private DateTime _lastErrorResponse;
  443. private async Task<string> GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
  444. {
  445. var username = info.Username;
  446. // Reset the token if there's no username
  447. if (string.IsNullOrWhiteSpace(username))
  448. {
  449. return null;
  450. }
  451. var password = info.Password;
  452. if (string.IsNullOrEmpty(password))
  453. {
  454. return null;
  455. }
  456. // Avoid hammering SD
  457. if ((DateTime.UtcNow - _lastErrorResponse).TotalMinutes < 1)
  458. {
  459. return null;
  460. }
  461. NameValuePair savedToken;
  462. if (!_tokens.TryGetValue(username, out savedToken))
  463. {
  464. savedToken = new NameValuePair();
  465. _tokens.TryAdd(username, savedToken);
  466. }
  467. if (!string.IsNullOrEmpty(savedToken.Name) && !string.IsNullOrEmpty(savedToken.Value))
  468. {
  469. if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out long ticks))
  470. {
  471. // If it's under 24 hours old we can still use it
  472. if (DateTime.UtcNow.Ticks - ticks < TimeSpan.FromHours(20).Ticks)
  473. {
  474. return savedToken.Name;
  475. }
  476. }
  477. }
  478. await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  479. try
  480. {
  481. var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
  482. savedToken.Name = result;
  483. savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
  484. return result;
  485. }
  486. catch (HttpException ex)
  487. {
  488. if (ex.StatusCode.HasValue)
  489. {
  490. if ((int)ex.StatusCode.Value == 400)
  491. {
  492. _tokens.Clear();
  493. _lastErrorResponse = DateTime.UtcNow;
  494. }
  495. }
  496. throw;
  497. }
  498. finally
  499. {
  500. _tokenSemaphore.Release();
  501. }
  502. }
  503. private async Task<HttpResponseMessage> Send(
  504. HttpRequestMessage options,
  505. bool enableRetry,
  506. ListingsProviderInfo providerInfo,
  507. CancellationToken cancellationToken,
  508. HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
  509. {
  510. try
  511. {
  512. return await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, completionOption, cancellationToken).ConfigureAwait(false);
  513. }
  514. catch (HttpException ex)
  515. {
  516. _tokens.Clear();
  517. if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
  518. {
  519. enableRetry = false;
  520. }
  521. if (!enableRetry)
  522. {
  523. throw;
  524. }
  525. }
  526. options.Headers.TryAddWithoutValidation("token", await GetToken(providerInfo, cancellationToken).ConfigureAwait(false));
  527. return await Send(options, false, providerInfo, cancellationToken).ConfigureAwait(false);
  528. }
  529. private async Task<string> GetTokenInternal(
  530. string username,
  531. string password,
  532. CancellationToken cancellationToken)
  533. {
  534. using var options = new HttpRequestMessage(HttpMethod.Post, ApiUrl + "/token");
  535. options.Content = new StringContent("{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}", Encoding.UTF8, MediaTypeNames.Application.Json);
  536. using var response = await Send(options, false, null, cancellationToken).ConfigureAwait(false);
  537. await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
  538. var root = await _jsonSerializer.DeserializeFromStreamAsync<ScheduleDirect.Token>(stream).ConfigureAwait(false);
  539. if (root.message == "OK")
  540. {
  541. _logger.LogInformation("Authenticated with Schedules Direct token: " + root.token);
  542. return root.token;
  543. }
  544. throw new Exception("Could not authenticate with Schedules Direct Error: " + root.message);
  545. }
  546. private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
  547. {
  548. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  549. if (string.IsNullOrEmpty(token))
  550. {
  551. throw new ArgumentException("Authentication required.");
  552. }
  553. if (string.IsNullOrEmpty(info.ListingsId))
  554. {
  555. throw new ArgumentException("Listings Id required");
  556. }
  557. _logger.LogInformation("Adding new LineUp ");
  558. using var options = new HttpRequestMessage(HttpMethod.Put, ApiUrl + "/lineups/" + info.ListingsId);
  559. options.Headers.TryAddWithoutValidation("token", token);
  560. using var response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  561. }
  562. private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
  563. {
  564. if (string.IsNullOrEmpty(info.ListingsId))
  565. {
  566. throw new ArgumentException("Listings Id required");
  567. }
  568. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  569. if (string.IsNullOrEmpty(token))
  570. {
  571. throw new Exception("token required");
  572. }
  573. _logger.LogInformation("Headends on account ");
  574. using var options = new HttpRequestMessage(HttpMethod.Get, ApiUrl + "/lineups");
  575. options.Headers.TryAddWithoutValidation("token", token);
  576. try
  577. {
  578. using var httpResponse = await Send(options, false, null, cancellationToken).ConfigureAwait(false);
  579. await using var stream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
  580. using var response = httpResponse.Content;
  581. var root = await _jsonSerializer.DeserializeFromStreamAsync<ScheduleDirect.Lineups>(stream).ConfigureAwait(false);
  582. return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
  583. }
  584. catch (HttpException ex)
  585. {
  586. // Apparently we're supposed to swallow this
  587. if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.BadRequest)
  588. {
  589. return false;
  590. }
  591. throw;
  592. }
  593. }
  594. public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  595. {
  596. if (validateLogin)
  597. {
  598. if (string.IsNullOrEmpty(info.Username))
  599. {
  600. throw new ArgumentException("Username is required");
  601. }
  602. if (string.IsNullOrEmpty(info.Password))
  603. {
  604. throw new ArgumentException("Password is required");
  605. }
  606. }
  607. if (validateListings)
  608. {
  609. if (string.IsNullOrEmpty(info.ListingsId))
  610. {
  611. throw new ArgumentException("Listings Id required");
  612. }
  613. var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
  614. if (!hasLineup)
  615. {
  616. await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
  617. }
  618. }
  619. }
  620. public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
  621. {
  622. return GetHeadends(info, country, location, CancellationToken.None);
  623. }
  624. public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
  625. {
  626. var listingsId = info.ListingsId;
  627. if (string.IsNullOrEmpty(listingsId))
  628. {
  629. throw new Exception("ListingsId required");
  630. }
  631. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  632. if (string.IsNullOrEmpty(token))
  633. {
  634. throw new Exception("token required");
  635. }
  636. using var options = new HttpRequestMessage(HttpMethod.Get, ApiUrl + "/lineups/" + listingsId);
  637. options.Headers.TryAddWithoutValidation("token", token);
  638. var list = new List<ChannelInfo>();
  639. using var httpResponse = await Send(options, true, info, cancellationToken).ConfigureAwait(false);
  640. await using var stream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
  641. var root = await _jsonSerializer.DeserializeFromStreamAsync<ScheduleDirect.Channel>(stream).ConfigureAwait(false);
  642. _logger.LogInformation("Found {ChannelCount} channels on the lineup on ScheduleDirect", root.map.Count);
  643. _logger.LogInformation("Mapping Stations to Channel");
  644. var allStations = root.stations ?? Enumerable.Empty<ScheduleDirect.Station>();
  645. foreach (ScheduleDirect.Map map in root.map)
  646. {
  647. var channelNumber = GetChannelNumber(map);
  648. var station = allStations.FirstOrDefault(item => string.Equals(item.stationID, map.stationID, StringComparison.OrdinalIgnoreCase));
  649. if (station == null)
  650. {
  651. station = new ScheduleDirect.Station { stationID = map.stationID };
  652. }
  653. var channelInfo = new ChannelInfo
  654. {
  655. Id = station.stationID,
  656. CallSign = station.callsign,
  657. Number = channelNumber,
  658. Name = string.IsNullOrWhiteSpace(station.name) ? channelNumber : station.name
  659. };
  660. if (station.logo != null)
  661. {
  662. channelInfo.ImageUrl = station.logo.URL;
  663. }
  664. list.Add(channelInfo);
  665. }
  666. return list;
  667. }
  668. private ScheduleDirect.Station GetStation(List<ScheduleDirect.Station> allStations, string channelNumber, string channelName)
  669. {
  670. if (!string.IsNullOrWhiteSpace(channelName))
  671. {
  672. channelName = NormalizeName(channelName);
  673. var result = allStations.FirstOrDefault(i => string.Equals(NormalizeName(i.callsign ?? string.Empty), channelName, StringComparison.OrdinalIgnoreCase));
  674. if (result != null)
  675. {
  676. return result;
  677. }
  678. }
  679. if (!string.IsNullOrWhiteSpace(channelNumber))
  680. {
  681. return allStations.FirstOrDefault(i => string.Equals(NormalizeName(i.stationID ?? string.Empty), channelNumber, StringComparison.OrdinalIgnoreCase));
  682. }
  683. return null;
  684. }
  685. private static string NormalizeName(string value)
  686. {
  687. return value.Replace(" ", string.Empty, StringComparison.Ordinal).Replace("-", string.Empty, StringComparison.Ordinal);
  688. }
  689. public class ScheduleDirect
  690. {
  691. public class Token
  692. {
  693. public int code { get; set; }
  694. public string message { get; set; }
  695. public string serverID { get; set; }
  696. public string token { get; set; }
  697. }
  698. public class Lineup
  699. {
  700. public string lineup { get; set; }
  701. public string name { get; set; }
  702. public string transport { get; set; }
  703. public string location { get; set; }
  704. public string uri { get; set; }
  705. }
  706. public class Lineups
  707. {
  708. public int code { get; set; }
  709. public string serverID { get; set; }
  710. public string datetime { get; set; }
  711. public List<Lineup> lineups { get; set; }
  712. }
  713. public class Headends
  714. {
  715. public string headend { get; set; }
  716. public string transport { get; set; }
  717. public string location { get; set; }
  718. public List<Lineup> lineups { get; set; }
  719. }
  720. public class Map
  721. {
  722. public string stationID { get; set; }
  723. public string channel { get; set; }
  724. public string logicalChannelNumber { get; set; }
  725. public int uhfVhf { get; set; }
  726. public int atscMajor { get; set; }
  727. public int atscMinor { get; set; }
  728. }
  729. public class Broadcaster
  730. {
  731. public string city { get; set; }
  732. public string state { get; set; }
  733. public string postalcode { get; set; }
  734. public string country { get; set; }
  735. }
  736. public class Logo
  737. {
  738. public string URL { get; set; }
  739. public int height { get; set; }
  740. public int width { get; set; }
  741. public string md5 { get; set; }
  742. }
  743. public class Station
  744. {
  745. public string stationID { get; set; }
  746. public string name { get; set; }
  747. public string callsign { get; set; }
  748. public List<string> broadcastLanguage { get; set; }
  749. public List<string> descriptionLanguage { get; set; }
  750. public Broadcaster broadcaster { get; set; }
  751. public string affiliate { get; set; }
  752. public Logo logo { get; set; }
  753. public bool? isCommercialFree { get; set; }
  754. }
  755. public class Metadata
  756. {
  757. public string lineup { get; set; }
  758. public string modified { get; set; }
  759. public string transport { get; set; }
  760. }
  761. public class Channel
  762. {
  763. public List<Map> map { get; set; }
  764. public List<Station> stations { get; set; }
  765. public Metadata metadata { get; set; }
  766. }
  767. public class RequestScheduleForChannel
  768. {
  769. public string stationID { get; set; }
  770. public List<string> date { get; set; }
  771. }
  772. public class Rating
  773. {
  774. public string body { get; set; }
  775. public string code { get; set; }
  776. }
  777. public class Multipart
  778. {
  779. public int partNumber { get; set; }
  780. public int totalParts { get; set; }
  781. }
  782. public class Program
  783. {
  784. public string programID { get; set; }
  785. public string airDateTime { get; set; }
  786. public int duration { get; set; }
  787. public string md5 { get; set; }
  788. public List<string> audioProperties { get; set; }
  789. public List<string> videoProperties { get; set; }
  790. public List<Rating> ratings { get; set; }
  791. public bool? @new { get; set; }
  792. public Multipart multipart { get; set; }
  793. public string liveTapeDelay { get; set; }
  794. public bool premiere { get; set; }
  795. public bool repeat { get; set; }
  796. public string isPremiereOrFinale { get; set; }
  797. }
  798. public class MetadataSchedule
  799. {
  800. public string modified { get; set; }
  801. public string md5 { get; set; }
  802. public string startDate { get; set; }
  803. public string endDate { get; set; }
  804. public int days { get; set; }
  805. }
  806. public class Day
  807. {
  808. public string stationID { get; set; }
  809. public List<Program> programs { get; set; }
  810. public MetadataSchedule metadata { get; set; }
  811. public Day()
  812. {
  813. programs = new List<Program>();
  814. }
  815. }
  816. //
  817. public class Title
  818. {
  819. public string title120 { get; set; }
  820. }
  821. public class EventDetails
  822. {
  823. public string subType { get; set; }
  824. }
  825. public class Description100
  826. {
  827. public string descriptionLanguage { get; set; }
  828. public string description { get; set; }
  829. }
  830. public class Description1000
  831. {
  832. public string descriptionLanguage { get; set; }
  833. public string description { get; set; }
  834. }
  835. public class DescriptionsProgram
  836. {
  837. public List<Description100> description100 { get; set; }
  838. public List<Description1000> description1000 { get; set; }
  839. }
  840. public class Gracenote
  841. {
  842. public int season { get; set; }
  843. public int episode { get; set; }
  844. }
  845. public class MetadataPrograms
  846. {
  847. public Gracenote Gracenote { get; set; }
  848. }
  849. public class ContentRating
  850. {
  851. public string body { get; set; }
  852. public string code { get; set; }
  853. }
  854. public class Cast
  855. {
  856. public string billingOrder { get; set; }
  857. public string role { get; set; }
  858. public string nameId { get; set; }
  859. public string personId { get; set; }
  860. public string name { get; set; }
  861. public string characterName { get; set; }
  862. }
  863. public class Crew
  864. {
  865. public string billingOrder { get; set; }
  866. public string role { get; set; }
  867. public string nameId { get; set; }
  868. public string personId { get; set; }
  869. public string name { get; set; }
  870. }
  871. public class QualityRating
  872. {
  873. public string ratingsBody { get; set; }
  874. public string rating { get; set; }
  875. public string minRating { get; set; }
  876. public string maxRating { get; set; }
  877. public string increment { get; set; }
  878. }
  879. public class Movie
  880. {
  881. public string year { get; set; }
  882. public int duration { get; set; }
  883. public List<QualityRating> qualityRating { get; set; }
  884. }
  885. public class Recommendation
  886. {
  887. public string programID { get; set; }
  888. public string title120 { get; set; }
  889. }
  890. public class ProgramDetails
  891. {
  892. public string audience { get; set; }
  893. public string programID { get; set; }
  894. public List<Title> titles { get; set; }
  895. public EventDetails eventDetails { get; set; }
  896. public DescriptionsProgram descriptions { get; set; }
  897. public string originalAirDate { get; set; }
  898. public List<string> genres { get; set; }
  899. public string episodeTitle150 { get; set; }
  900. public List<MetadataPrograms> metadata { get; set; }
  901. public List<ContentRating> contentRating { get; set; }
  902. public List<Cast> cast { get; set; }
  903. public List<Crew> crew { get; set; }
  904. public string entityType { get; set; }
  905. public string showType { get; set; }
  906. public bool hasImageArtwork { get; set; }
  907. public string primaryImage { get; set; }
  908. public string thumbImage { get; set; }
  909. public string backdropImage { get; set; }
  910. public string bannerImage { get; set; }
  911. public string imageID { get; set; }
  912. public string md5 { get; set; }
  913. public List<string> contentAdvisory { get; set; }
  914. public Movie movie { get; set; }
  915. public List<Recommendation> recommendations { get; set; }
  916. }
  917. public class Caption
  918. {
  919. public string content { get; set; }
  920. public string lang { get; set; }
  921. }
  922. public class ImageData
  923. {
  924. public string width { get; set; }
  925. public string height { get; set; }
  926. public string uri { get; set; }
  927. public string size { get; set; }
  928. public string aspect { get; set; }
  929. public string category { get; set; }
  930. public string text { get; set; }
  931. public string primary { get; set; }
  932. public string tier { get; set; }
  933. public Caption caption { get; set; }
  934. }
  935. public class ShowImages
  936. {
  937. public string programID { get; set; }
  938. public List<ImageData> data { get; set; }
  939. }
  940. }
  941. }
  942. }