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