SchedulesDirect.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Net.Http.Headers;
  11. using System.Net.Mime;
  12. using System.Text;
  13. using System.Text.Json;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using Emby.Server.Implementations.LiveTv.Listings.SchedulesDirectDtos;
  17. using Jellyfin.Extensions;
  18. using Jellyfin.Extensions.Json;
  19. using MediaBrowser.Common.Net;
  20. using MediaBrowser.Controller.LiveTv;
  21. using MediaBrowser.Model.Cryptography;
  22. using MediaBrowser.Model.Dto;
  23. using MediaBrowser.Model.Entities;
  24. using MediaBrowser.Model.LiveTv;
  25. using Microsoft.Extensions.Logging;
  26. namespace Emby.Server.Implementations.LiveTv.Listings
  27. {
  28. public class SchedulesDirect : IListingsProvider
  29. {
  30. private const string ApiUrl = "https://json.schedulesdirect.org/20141201";
  31. private readonly ILogger<SchedulesDirect> _logger;
  32. private readonly IHttpClientFactory _httpClientFactory;
  33. private readonly SemaphoreSlim _tokenSemaphore = new SemaphoreSlim(1, 1);
  34. private readonly ICryptoProvider _cryptoProvider;
  35. private readonly ConcurrentDictionary<string, NameValuePair> _tokens = new ConcurrentDictionary<string, NameValuePair>();
  36. private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
  37. private DateTime _lastErrorResponse;
  38. public SchedulesDirect(
  39. ILogger<SchedulesDirect> logger,
  40. IHttpClientFactory httpClientFactory,
  41. ICryptoProvider cryptoProvider)
  42. {
  43. _logger = logger;
  44. _httpClientFactory = httpClientFactory;
  45. _cryptoProvider = cryptoProvider;
  46. }
  47. /// <inheritdoc />
  48. public string Name => "Schedules Direct";
  49. /// <inheritdoc />
  50. public string Type => nameof(SchedulesDirect);
  51. private static List<string> GetScheduleRequestDates(DateTime startDateUtc, DateTime endDateUtc)
  52. {
  53. var dates = new List<string>();
  54. var start = new List<DateTime> { startDateUtc, startDateUtc.ToLocalTime() }.Min().Date;
  55. var end = new List<DateTime> { endDateUtc, endDateUtc.ToLocalTime() }.Max().Date;
  56. while (start <= end)
  57. {
  58. dates.Add(start.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
  59. start = start.AddDays(1);
  60. }
  61. return dates;
  62. }
  63. public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
  64. {
  65. if (string.IsNullOrEmpty(channelId))
  66. {
  67. throw new ArgumentNullException(nameof(channelId));
  68. }
  69. // Normalize incoming input
  70. channelId = channelId.Replace(".json.schedulesdirect.org", string.Empty, StringComparison.OrdinalIgnoreCase).TrimStart('I');
  71. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  72. if (string.IsNullOrEmpty(token))
  73. {
  74. _logger.LogWarning("SchedulesDirect token is empty, returning empty program list");
  75. return Enumerable.Empty<ProgramInfo>();
  76. }
  77. var dates = GetScheduleRequestDates(startDateUtc, endDateUtc);
  78. _logger.LogInformation("Channel Station ID is: {ChannelID}", channelId);
  79. var requestList = new List<RequestScheduleForChannelDto>()
  80. {
  81. new RequestScheduleForChannelDto()
  82. {
  83. StationId = channelId,
  84. Date = dates
  85. }
  86. };
  87. var requestString = JsonSerializer.Serialize(requestList, _jsonOptions);
  88. _logger.LogDebug("Request string for schedules is: {RequestString}", requestString);
  89. using var options = new HttpRequestMessage(HttpMethod.Post, ApiUrl + "/schedules");
  90. options.Content = new StringContent(requestString, Encoding.UTF8, MediaTypeNames.Application.Json);
  91. options.Headers.TryAddWithoutValidation("token", token);
  92. using var response = await Send(options, true, info, cancellationToken).ConfigureAwait(false);
  93. await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  94. var dailySchedules = await JsonSerializer.DeserializeAsync<IReadOnlyList<DayDto>>(responseStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  95. if (dailySchedules == null)
  96. {
  97. return Array.Empty<ProgramInfo>();
  98. }
  99. _logger.LogDebug("Found {ScheduleCount} programs on {ChannelID} ScheduleDirect", dailySchedules.Count, channelId);
  100. using var programRequestOptions = new HttpRequestMessage(HttpMethod.Post, ApiUrl + "/programs");
  101. programRequestOptions.Headers.TryAddWithoutValidation("token", token);
  102. var programIds = dailySchedules.SelectMany(d => d.Programs.Select(s => s.ProgramId)).Distinct();
  103. programRequestOptions.Content = new ByteArrayContent(JsonSerializer.SerializeToUtf8Bytes(programIds, _jsonOptions));
  104. programRequestOptions.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(MediaTypeNames.Application.Json);
  105. using var innerResponse = await Send(programRequestOptions, true, info, cancellationToken).ConfigureAwait(false);
  106. await using var innerResponseStream = await innerResponse.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  107. var programDetails = await JsonSerializer.DeserializeAsync<IReadOnlyList<ProgramDetailsDto>>(innerResponseStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  108. if (programDetails == null)
  109. {
  110. return Array.Empty<ProgramInfo>();
  111. }
  112. var programDict = programDetails.ToDictionary(p => p.ProgramId, y => y);
  113. var programIdsWithImages = programDetails
  114. .Where(p => p.HasImageArtwork).Select(p => p.ProgramId)
  115. .ToList();
  116. var images = await GetImageForPrograms(info, programIdsWithImages, cancellationToken).ConfigureAwait(false);
  117. var programsInfo = new List<ProgramInfo>();
  118. foreach (ProgramDto schedule in dailySchedules.SelectMany(d => d.Programs))
  119. {
  120. // _logger.LogDebug("Proccesing Schedule for statio ID " + stationID +
  121. // " which corresponds to channel " + channelNumber + " and program id " +
  122. // schedule.ProgramId + " which says it has images? " +
  123. // programDict[schedule.ProgramId].hasImageArtwork);
  124. if (string.IsNullOrEmpty(schedule.ProgramId))
  125. {
  126. continue;
  127. }
  128. if (images != null)
  129. {
  130. var imageIndex = images.FindIndex(i => i.ProgramId == schedule.ProgramId[..10]);
  131. if (imageIndex > -1)
  132. {
  133. var programEntry = programDict[schedule.ProgramId];
  134. var allImages = images[imageIndex].Data;
  135. var imagesWithText = allImages.Where(i => string.Equals(i.Text, "yes", StringComparison.OrdinalIgnoreCase));
  136. var imagesWithoutText = allImages.Where(i => string.Equals(i.Text, "no", StringComparison.OrdinalIgnoreCase));
  137. const double DesiredAspect = 2.0 / 3;
  138. programEntry.PrimaryImage = GetProgramImage(ApiUrl, imagesWithText, true, DesiredAspect) ??
  139. GetProgramImage(ApiUrl, allImages, true, DesiredAspect);
  140. const double WideAspect = 16.0 / 9;
  141. programEntry.ThumbImage = GetProgramImage(ApiUrl, imagesWithText, true, WideAspect);
  142. // Don't supply the same image twice
  143. if (string.Equals(programEntry.PrimaryImage, programEntry.ThumbImage, StringComparison.Ordinal))
  144. {
  145. programEntry.ThumbImage = null;
  146. }
  147. programEntry.BackdropImage = GetProgramImage(ApiUrl, imagesWithoutText, true, WideAspect);
  148. // programEntry.bannerImage = GetProgramImage(ApiUrl, data, "Banner", false) ??
  149. // GetProgramImage(ApiUrl, data, "Banner-L1", false) ??
  150. // GetProgramImage(ApiUrl, data, "Banner-LO", false) ??
  151. // GetProgramImage(ApiUrl, data, "Banner-LOT", false);
  152. }
  153. }
  154. programsInfo.Add(GetProgram(channelId, schedule, programDict[schedule.ProgramId]));
  155. }
  156. return programsInfo;
  157. }
  158. private static int GetSizeOrder(ImageDataDto image)
  159. {
  160. if (int.TryParse(image.Height, out int value))
  161. {
  162. return value;
  163. }
  164. return 0;
  165. }
  166. private static string GetChannelNumber(MapDto map)
  167. {
  168. var channelNumber = map.LogicalChannelNumber;
  169. if (string.IsNullOrWhiteSpace(channelNumber))
  170. {
  171. channelNumber = map.Channel;
  172. }
  173. if (string.IsNullOrWhiteSpace(channelNumber))
  174. {
  175. channelNumber = map.AtscMajor + "." + map.AtscMinor;
  176. }
  177. return channelNumber.TrimStart('0');
  178. }
  179. private static bool IsMovie(ProgramDetailsDto programInfo)
  180. {
  181. return string.Equals(programInfo.EntityType, "movie", StringComparison.OrdinalIgnoreCase);
  182. }
  183. private ProgramInfo GetProgram(string channelId, ProgramDto programInfo, ProgramDetailsDto details)
  184. {
  185. if (programInfo.AirDateTime == null)
  186. {
  187. return null;
  188. }
  189. var startAt = programInfo.AirDateTime.Value;
  190. var endAt = startAt.AddSeconds(programInfo.Duration);
  191. var audioType = ProgramAudio.Stereo;
  192. var programId = programInfo.ProgramId ?? string.Empty;
  193. string newID = programId + "T" + startAt.Ticks + "C" + channelId;
  194. if (programInfo.AudioProperties.Count != 0)
  195. {
  196. if (programInfo.AudioProperties.Contains("atmos", StringComparer.OrdinalIgnoreCase))
  197. {
  198. audioType = ProgramAudio.Atmos;
  199. }
  200. else if (programInfo.AudioProperties.Contains("dd 5.1", StringComparer.OrdinalIgnoreCase))
  201. {
  202. audioType = ProgramAudio.DolbyDigital;
  203. }
  204. else if (programInfo.AudioProperties.Contains("dd", StringComparer.OrdinalIgnoreCase))
  205. {
  206. audioType = ProgramAudio.DolbyDigital;
  207. }
  208. else if (programInfo.AudioProperties.Contains("stereo", StringComparer.OrdinalIgnoreCase))
  209. {
  210. audioType = ProgramAudio.Stereo;
  211. }
  212. else
  213. {
  214. audioType = ProgramAudio.Mono;
  215. }
  216. }
  217. string episodeTitle = null;
  218. if (details.EpisodeTitle150 != null)
  219. {
  220. episodeTitle = details.EpisodeTitle150;
  221. }
  222. var info = new ProgramInfo
  223. {
  224. ChannelId = channelId,
  225. Id = newID,
  226. StartDate = startAt,
  227. EndDate = endAt,
  228. Name = details.Titles[0].Title120 ?? "Unknown",
  229. OfficialRating = null,
  230. CommunityRating = null,
  231. EpisodeTitle = episodeTitle,
  232. Audio = audioType,
  233. // IsNew = programInfo.@new ?? false,
  234. IsRepeat = programInfo.New == null,
  235. IsSeries = string.Equals(details.EntityType, "episode", StringComparison.OrdinalIgnoreCase),
  236. ImageUrl = details.PrimaryImage,
  237. ThumbImageUrl = details.ThumbImage,
  238. IsKids = string.Equals(details.Audience, "children", StringComparison.OrdinalIgnoreCase),
  239. IsSports = string.Equals(details.EntityType, "sports", StringComparison.OrdinalIgnoreCase),
  240. IsMovie = IsMovie(details),
  241. Etag = programInfo.Md5,
  242. IsLive = string.Equals(programInfo.LiveTapeDelay, "live", StringComparison.OrdinalIgnoreCase),
  243. IsPremiere = programInfo.Premiere || (programInfo.IsPremiereOrFinale ?? string.Empty).IndexOf("premiere", StringComparison.OrdinalIgnoreCase) != -1
  244. };
  245. var showId = programId;
  246. if (!info.IsSeries)
  247. {
  248. // It's also a series if it starts with SH
  249. info.IsSeries = showId.StartsWith("SH", StringComparison.OrdinalIgnoreCase) && showId.Length >= 14;
  250. }
  251. // According to SchedulesDirect, these are generic, unidentified episodes
  252. // SH005316560000
  253. var hasUniqueShowId = !showId.StartsWith("SH", StringComparison.OrdinalIgnoreCase) ||
  254. !showId.EndsWith("0000", StringComparison.OrdinalIgnoreCase);
  255. if (!hasUniqueShowId)
  256. {
  257. showId = newID;
  258. }
  259. info.ShowId = showId;
  260. if (programInfo.VideoProperties != null)
  261. {
  262. info.IsHD = programInfo.VideoProperties.Contains("hdtv", StringComparer.OrdinalIgnoreCase);
  263. info.Is3D = programInfo.VideoProperties.Contains("3d", StringComparer.OrdinalIgnoreCase);
  264. }
  265. if (details.ContentRating != null && details.ContentRating.Count > 0)
  266. {
  267. info.OfficialRating = details.ContentRating[0].Code.Replace("TV", "TV-", StringComparison.Ordinal)
  268. .Replace("--", "-", StringComparison.Ordinal);
  269. var invalid = new[] { "N/A", "Approved", "Not Rated", "Passed" };
  270. if (invalid.Contains(info.OfficialRating, StringComparer.OrdinalIgnoreCase))
  271. {
  272. info.OfficialRating = null;
  273. }
  274. }
  275. if (details.Descriptions != null)
  276. {
  277. if (details.Descriptions.Description1000 != null && details.Descriptions.Description1000.Count > 0)
  278. {
  279. info.Overview = details.Descriptions.Description1000[0].Description;
  280. }
  281. else if (details.Descriptions.Description100 != null && details.Descriptions.Description100.Count > 0)
  282. {
  283. info.Overview = details.Descriptions.Description100[0].Description;
  284. }
  285. }
  286. if (info.IsSeries)
  287. {
  288. info.SeriesId = programId.Substring(0, 10);
  289. info.SeriesProviderIds[MetadataProvider.Zap2It.ToString()] = info.SeriesId;
  290. if (details.Metadata != null)
  291. {
  292. foreach (var metadataProgram in details.Metadata)
  293. {
  294. var gracenote = metadataProgram.Gracenote;
  295. if (gracenote != null)
  296. {
  297. info.SeasonNumber = gracenote.Season;
  298. if (gracenote.Episode > 0)
  299. {
  300. info.EpisodeNumber = gracenote.Episode;
  301. }
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. if (details.OriginalAirDate != null)
  308. {
  309. info.OriginalAirDate = details.OriginalAirDate;
  310. info.ProductionYear = info.OriginalAirDate.Value.Year;
  311. }
  312. if (details.Movie != null)
  313. {
  314. if (!string.IsNullOrEmpty(details.Movie.Year)
  315. && int.TryParse(details.Movie.Year, out int year))
  316. {
  317. info.ProductionYear = year;
  318. }
  319. }
  320. if (details.Genres != null)
  321. {
  322. info.Genres = details.Genres.Where(g => !string.IsNullOrWhiteSpace(g)).ToList();
  323. info.IsNews = details.Genres.Contains("news", StringComparer.OrdinalIgnoreCase);
  324. if (info.Genres.Contains("children", StringComparer.OrdinalIgnoreCase))
  325. {
  326. info.IsKids = true;
  327. }
  328. }
  329. return info;
  330. }
  331. private string GetProgramImage(string apiUrl, IEnumerable<ImageDataDto> images, bool returnDefaultImage, double desiredAspect)
  332. {
  333. var match = images
  334. .OrderBy(i => Math.Abs(desiredAspect - GetAspectRatio(i)))
  335. .ThenByDescending(i => GetSizeOrder(i))
  336. .FirstOrDefault();
  337. if (match == null)
  338. {
  339. return null;
  340. }
  341. var uri = match.Uri;
  342. if (string.IsNullOrWhiteSpace(uri))
  343. {
  344. return null;
  345. }
  346. else if (uri.IndexOf("http", StringComparison.OrdinalIgnoreCase) != -1)
  347. {
  348. return uri;
  349. }
  350. else
  351. {
  352. return apiUrl + "/image/" + uri;
  353. }
  354. }
  355. private static double GetAspectRatio(ImageDataDto i)
  356. {
  357. int width = 0;
  358. int height = 0;
  359. if (!string.IsNullOrWhiteSpace(i.Width))
  360. {
  361. _ = int.TryParse(i.Width, out width);
  362. }
  363. if (!string.IsNullOrWhiteSpace(i.Height))
  364. {
  365. _ = int.TryParse(i.Height, out height);
  366. }
  367. if (height == 0 || width == 0)
  368. {
  369. return 0;
  370. }
  371. double result = width;
  372. result /= height;
  373. return result;
  374. }
  375. private async Task<IReadOnlyList<ShowImagesDto>> GetImageForPrograms(
  376. ListingsProviderInfo info,
  377. IReadOnlyList<string> programIds,
  378. CancellationToken cancellationToken)
  379. {
  380. if (programIds.Count == 0)
  381. {
  382. return Array.Empty<ShowImagesDto>();
  383. }
  384. StringBuilder str = new StringBuilder("[", 1 + (programIds.Count * 13));
  385. foreach (ReadOnlySpan<char> i in programIds)
  386. {
  387. str.Append('"')
  388. .Append(i.Slice(0, 10))
  389. .Append("\",");
  390. }
  391. // Remove last ,
  392. str.Length--;
  393. str.Append(']');
  394. using var message = new HttpRequestMessage(HttpMethod.Post, ApiUrl + "/metadata/programs")
  395. {
  396. Content = new StringContent(str.ToString(), Encoding.UTF8, MediaTypeNames.Application.Json)
  397. };
  398. try
  399. {
  400. using var innerResponse2 = await Send(message, true, info, cancellationToken).ConfigureAwait(false);
  401. await using var response = await innerResponse2.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  402. return await JsonSerializer.DeserializeAsync<IReadOnlyList<ShowImagesDto>>(response, _jsonOptions, cancellationToken).ConfigureAwait(false);
  403. }
  404. catch (Exception ex)
  405. {
  406. _logger.LogError(ex, "Error getting image info from schedules direct");
  407. return Array.Empty<ShowImagesDto>();
  408. }
  409. }
  410. public async Task<List<NameIdPair>> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
  411. {
  412. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  413. var lineups = new List<NameIdPair>();
  414. if (string.IsNullOrWhiteSpace(token))
  415. {
  416. return lineups;
  417. }
  418. using var options = new HttpRequestMessage(HttpMethod.Get, ApiUrl + "/headends?country=" + country + "&postalcode=" + location);
  419. options.Headers.TryAddWithoutValidation("token", token);
  420. try
  421. {
  422. using var httpResponse = await Send(options, false, info, cancellationToken).ConfigureAwait(false);
  423. await using var response = await httpResponse.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  424. var root = await JsonSerializer.DeserializeAsync<IReadOnlyList<HeadendsDto>>(response, _jsonOptions, cancellationToken).ConfigureAwait(false);
  425. if (root != null)
  426. {
  427. foreach (HeadendsDto headend in root)
  428. {
  429. foreach (LineupDto lineup in headend.Lineups)
  430. {
  431. lineups.Add(new NameIdPair
  432. {
  433. Name = string.IsNullOrWhiteSpace(lineup.Name) ? lineup.Lineup : lineup.Name,
  434. Id = lineup.Uri?[18..]
  435. });
  436. }
  437. }
  438. }
  439. else
  440. {
  441. _logger.LogInformation("No lineups available");
  442. }
  443. }
  444. catch (Exception ex)
  445. {
  446. _logger.LogError(ex, "Error getting headends");
  447. }
  448. return lineups;
  449. }
  450. private async Task<string> GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
  451. {
  452. var username = info.Username;
  453. // Reset the token if there's no username
  454. if (string.IsNullOrWhiteSpace(username))
  455. {
  456. return null;
  457. }
  458. var password = info.Password;
  459. if (string.IsNullOrEmpty(password))
  460. {
  461. return null;
  462. }
  463. // Avoid hammering SD
  464. if ((DateTime.UtcNow - _lastErrorResponse).TotalMinutes < 1)
  465. {
  466. return null;
  467. }
  468. if (!_tokens.TryGetValue(username, out NameValuePair savedToken))
  469. {
  470. savedToken = new NameValuePair();
  471. _tokens.TryAdd(username, savedToken);
  472. }
  473. if (!string.IsNullOrEmpty(savedToken.Name) && !string.IsNullOrEmpty(savedToken.Value))
  474. {
  475. if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out long ticks))
  476. {
  477. // If it's under 24 hours old we can still use it
  478. if (DateTime.UtcNow.Ticks - ticks < TimeSpan.FromHours(20).Ticks)
  479. {
  480. return savedToken.Name;
  481. }
  482. }
  483. }
  484. await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  485. try
  486. {
  487. var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
  488. savedToken.Name = result;
  489. savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
  490. return result;
  491. }
  492. catch (HttpRequestException ex)
  493. {
  494. if (ex.StatusCode.HasValue)
  495. {
  496. if ((int)ex.StatusCode.Value == 400)
  497. {
  498. _tokens.Clear();
  499. _lastErrorResponse = DateTime.UtcNow;
  500. }
  501. }
  502. throw;
  503. }
  504. finally
  505. {
  506. _tokenSemaphore.Release();
  507. }
  508. }
  509. private async Task<HttpResponseMessage> Send(
  510. HttpRequestMessage options,
  511. bool enableRetry,
  512. ListingsProviderInfo providerInfo,
  513. CancellationToken cancellationToken,
  514. HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
  515. {
  516. var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  517. .SendAsync(options, completionOption, cancellationToken).ConfigureAwait(false);
  518. if (response.IsSuccessStatusCode)
  519. {
  520. return response;
  521. }
  522. // Response is automatically disposed in the calling function,
  523. // so dispose manually if not returning.
  524. response.Dispose();
  525. if (!enableRetry || (int)response.StatusCode >= 500)
  526. {
  527. throw new HttpRequestException(
  528. string.Format(CultureInfo.InvariantCulture, "Request failed: {0}", response.ReasonPhrase),
  529. null,
  530. response.StatusCode);
  531. }
  532. _tokens.Clear();
  533. options.Headers.TryAddWithoutValidation("token", await GetToken(providerInfo, cancellationToken).ConfigureAwait(false));
  534. return await Send(options, false, providerInfo, cancellationToken).ConfigureAwait(false);
  535. }
  536. private async Task<string> GetTokenInternal(
  537. string username,
  538. string password,
  539. CancellationToken cancellationToken)
  540. {
  541. using var options = new HttpRequestMessage(HttpMethod.Post, ApiUrl + "/token");
  542. var hashedPasswordBytes = _cryptoProvider.ComputeHash("SHA1", Encoding.ASCII.GetBytes(password), Array.Empty<byte>());
  543. // TODO: remove ToLower when Convert.ToHexString supports lowercase
  544. // Schedules Direct requires the hex to be lowercase
  545. string hashedPassword = Convert.ToHexString(hashedPasswordBytes).ToLowerInvariant();
  546. options.Content = new StringContent("{\"username\":\"" + username + "\",\"password\":\"" + hashedPassword + "\"}", Encoding.UTF8, MediaTypeNames.Application.Json);
  547. using var response = await Send(options, false, null, cancellationToken).ConfigureAwait(false);
  548. response.EnsureSuccessStatusCode();
  549. await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  550. var root = await JsonSerializer.DeserializeAsync<TokenDto>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  551. if (string.Equals(root?.Message, "OK", StringComparison.Ordinal))
  552. {
  553. _logger.LogInformation("Authenticated with Schedules Direct token: {Token}", root.Token);
  554. return root.Token;
  555. }
  556. throw new Exception("Could not authenticate with Schedules Direct Error: " + root.Message);
  557. }
  558. private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
  559. {
  560. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  561. if (string.IsNullOrEmpty(token))
  562. {
  563. throw new ArgumentException("Authentication required.");
  564. }
  565. if (string.IsNullOrEmpty(info.ListingsId))
  566. {
  567. throw new ArgumentException("Listings Id required");
  568. }
  569. _logger.LogInformation("Adding new LineUp ");
  570. using var options = new HttpRequestMessage(HttpMethod.Put, ApiUrl + "/lineups/" + info.ListingsId);
  571. options.Headers.TryAddWithoutValidation("token", token);
  572. using var response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  573. }
  574. private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
  575. {
  576. if (string.IsNullOrEmpty(info.ListingsId))
  577. {
  578. throw new ArgumentException("Listings Id required");
  579. }
  580. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  581. if (string.IsNullOrEmpty(token))
  582. {
  583. throw new Exception("token required");
  584. }
  585. _logger.LogInformation("Headends on account ");
  586. using var options = new HttpRequestMessage(HttpMethod.Get, ApiUrl + "/lineups");
  587. options.Headers.TryAddWithoutValidation("token", token);
  588. try
  589. {
  590. using var httpResponse = await Send(options, false, null, cancellationToken).ConfigureAwait(false);
  591. httpResponse.EnsureSuccessStatusCode();
  592. await using var stream = await httpResponse.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  593. using var response = httpResponse.Content;
  594. var root = await JsonSerializer.DeserializeAsync<LineupsDto>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  595. return root?.Lineups.Any(i => string.Equals(info.ListingsId, i.Lineup, StringComparison.OrdinalIgnoreCase)) ?? false;
  596. }
  597. catch (HttpRequestException ex)
  598. {
  599. // SchedulesDirect returns 400 if no lineups are configured.
  600. if (ex.StatusCode is HttpStatusCode.BadRequest)
  601. {
  602. return false;
  603. }
  604. throw;
  605. }
  606. }
  607. public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  608. {
  609. if (validateLogin)
  610. {
  611. if (string.IsNullOrEmpty(info.Username))
  612. {
  613. throw new ArgumentException("Username is required");
  614. }
  615. if (string.IsNullOrEmpty(info.Password))
  616. {
  617. throw new ArgumentException("Password is required");
  618. }
  619. }
  620. if (validateListings)
  621. {
  622. if (string.IsNullOrEmpty(info.ListingsId))
  623. {
  624. throw new ArgumentException("Listings Id required");
  625. }
  626. var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
  627. if (!hasLineup)
  628. {
  629. await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
  630. }
  631. }
  632. }
  633. public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
  634. {
  635. return GetHeadends(info, country, location, CancellationToken.None);
  636. }
  637. public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
  638. {
  639. var listingsId = info.ListingsId;
  640. if (string.IsNullOrEmpty(listingsId))
  641. {
  642. throw new Exception("ListingsId required");
  643. }
  644. var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
  645. if (string.IsNullOrEmpty(token))
  646. {
  647. throw new Exception("token required");
  648. }
  649. using var options = new HttpRequestMessage(HttpMethod.Get, ApiUrl + "/lineups/" + listingsId);
  650. options.Headers.TryAddWithoutValidation("token", token);
  651. using var httpResponse = await Send(options, true, info, cancellationToken).ConfigureAwait(false);
  652. await using var stream = await httpResponse.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  653. var root = await JsonSerializer.DeserializeAsync<ChannelDto>(stream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  654. if (root == null)
  655. {
  656. return new List<ChannelInfo>();
  657. }
  658. _logger.LogInformation("Found {ChannelCount} channels on the lineup on ScheduleDirect", root.Map.Count);
  659. _logger.LogInformation("Mapping Stations to Channel");
  660. var allStations = root.Stations;
  661. var map = root.Map;
  662. var list = new List<ChannelInfo>(map.Count);
  663. foreach (var channel in map)
  664. {
  665. var channelNumber = GetChannelNumber(channel);
  666. var stationIndex = allStations.FindIndex(item => string.Equals(item.StationId, channel.StationId, StringComparison.OrdinalIgnoreCase));
  667. var station = stationIndex == -1
  668. ? new StationDto { StationId = channel.StationId }
  669. : allStations[stationIndex];
  670. var channelInfo = new ChannelInfo
  671. {
  672. Id = station.StationId,
  673. CallSign = station.Callsign,
  674. Number = channelNumber,
  675. Name = string.IsNullOrWhiteSpace(station.Name) ? channelNumber : station.Name
  676. };
  677. if (station.Logo != null)
  678. {
  679. channelInfo.ImageUrl = station.Logo.Url;
  680. }
  681. list.Add(channelInfo);
  682. }
  683. return list;
  684. }
  685. private static string NormalizeName(string value)
  686. {
  687. return value.Replace(" ", string.Empty, StringComparison.Ordinal).Replace("-", string.Empty, StringComparison.Ordinal);
  688. }
  689. }
  690. }