SchedulesDirect.cs 43 KB

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