jellyfin-server/Emby.Server.Implementations/LiveTv/Listings/SchedulesDirect.cs

1225 lines
45 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2015-07-21 04:22:46 +00:00
using System;
2015-07-23 05:25:55 +00:00
using System.Collections.Concurrent;
2015-07-21 04:22:46 +00:00
using System.Collections.Generic;
2015-07-23 05:25:55 +00:00
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
2015-07-21 04:22:46 +00:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
2015-07-21 04:22:46 +00:00
2016-11-03 23:35:19 +00:00
namespace Emby.Server.Implementations.LiveTv.Listings
2015-07-21 04:22:46 +00:00
{
2015-08-15 18:46:57 +00:00
public class SchedulesDirect : IListingsProvider
2015-07-21 04:22:46 +00:00
{
2015-07-23 05:25:55 +00:00
private readonly ILogger _logger;
private readonly IJsonSerializer _jsonSerializer;
private readonly IHttpClient _httpClient;
private readonly SemaphoreSlim _tokenSemaphore = new SemaphoreSlim(1, 1);
2015-07-29 20:31:15 +00:00
private readonly IApplicationHost _appHost;
2015-07-23 05:25:55 +00:00
private const string ApiUrl = "https://json.schedulesdirect.org/20141201";
public SchedulesDirect(
ILogger<SchedulesDirect> logger,
IJsonSerializer jsonSerializer,
IHttpClient httpClient,
IApplicationHost appHost)
2015-07-23 05:25:55 +00:00
{
_logger = logger;
_jsonSerializer = jsonSerializer;
_httpClient = httpClient;
2015-07-29 20:31:15 +00:00
_appHost = appHost;
}
private string UserAgent => _appHost.ApplicationUserAgent;
2015-07-23 05:25:55 +00:00
2019-09-20 10:42:08 +00:00
/// <inheritdoc />
public string Name => "Schedules Direct";
/// <inheritdoc />
public string Type => nameof(SchedulesDirect);
private static List<string> GetScheduleRequestDates(DateTime startDateUtc, DateTime endDateUtc)
2015-07-29 17:16:00 +00:00
{
2019-01-13 20:37:13 +00:00
var dates = new List<string>();
2015-07-29 17:16:00 +00:00
2015-11-12 02:25:12 +00:00
var start = new List<DateTime> { startDateUtc, startDateUtc.ToLocalTime() }.Min().Date;
var end = new List<DateTime> { endDateUtc, endDateUtc.ToLocalTime() }.Max().Date;
2015-07-29 17:16:00 +00:00
2015-11-12 02:25:12 +00:00
while (start <= end)
2015-07-29 17:16:00 +00:00
{
dates.Add(start.ToString("yyyy-MM-dd"));
start = start.AddDays(1);
}
return dates;
}
2017-02-04 23:32:16 +00:00
public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelId, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
2015-07-23 05:25:55 +00:00
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(channelId))
2017-02-04 23:32:16 +00:00
{
throw new ArgumentNullException(nameof(channelId));
2017-02-04 23:32:16 +00:00
}
// Normalize incoming input
channelId = channelId.Replace(".json.schedulesdirect.org", string.Empty, StringComparison.OrdinalIgnoreCase).TrimStart('I');
2015-09-01 19:18:25 +00:00
var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
2015-07-23 05:25:55 +00:00
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(token))
2015-07-23 05:25:55 +00:00
{
_logger.LogWarning("SchedulesDirect token is empty, returning empty program list");
return Enumerable.Empty<ProgramInfo>();
2015-07-23 05:25:55 +00:00
}
2015-07-29 17:16:00 +00:00
var dates = GetScheduleRequestDates(startDateUtc, endDateUtc);
2015-07-23 05:25:55 +00:00
_logger.LogInformation("Channel Station ID is: {ChannelID}", channelId);
var requestList = new List<ScheduleDirect.RequestScheduleForChannel>()
{
new ScheduleDirect.RequestScheduleForChannel()
2015-07-23 05:25:55 +00:00
{
stationID = channelId,
date = dates
}
};
2015-07-23 05:25:55 +00:00
var requestString = _jsonSerializer.SerializeToString(requestList);
_logger.LogDebug("Request string for schedules is: {RequestString}", requestString);
2016-03-17 19:29:53 +00:00
var httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/schedules",
UserAgent = UserAgent,
CancellationToken = cancellationToken,
LogErrorResponseBody = true,
RequestContent = requestString
2016-03-17 19:29:53 +00:00
};
httpOptions.RequestHeaders["token"] = token;
using (var response = await Post(httpOptions, true, info).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
2018-12-30 23:51:15 +00:00
var dailySchedules = await _jsonSerializer.DeserializeFromStreamAsync<List<ScheduleDirect.Day>>(response.Content).ConfigureAwait(false);
_logger.LogDebug("Found {ScheduleCount} programs on {ChannelID} ScheduleDirect", dailySchedules.Count, channelId);
2015-07-23 05:25:55 +00:00
httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/programs",
UserAgent = UserAgent,
2015-10-11 16:12:53 +00:00
CancellationToken = cancellationToken,
2019-03-08 19:32:14 +00:00
LogErrorResponseBody = true
2015-07-23 05:25:55 +00:00
};
httpOptions.RequestHeaders["token"] = token;
var programsID = dailySchedules.SelectMany(d => d.programs.Select(s => s.programID)).Distinct();
httpOptions.RequestContent = "[\"" + string.Join("\", \"", programsID) + "\"]";
2017-06-11 20:40:25 +00:00
using (var innerResponse = await Post(httpOptions, true, info).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
2018-12-30 23:51:15 +00:00
var programDetails = await _jsonSerializer.DeserializeFromStreamAsync<List<ScheduleDirect.ProgramDetails>>(innerResponse.Content).ConfigureAwait(false);
2015-07-23 05:25:55 +00:00
var programDict = programDetails.ToDictionary(p => p.programID, y => y);
2017-02-09 03:58:04 +00:00
var programIdsWithImages =
programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID)
.ToList();
var images = await GetImageForPrograms(info, programIdsWithImages, cancellationToken).ConfigureAwait(false);
2015-07-23 05:25:55 +00:00
2019-01-13 20:37:13 +00:00
var programsInfo = new List<ProgramInfo>();
foreach (ScheduleDirect.Program schedule in dailySchedules.SelectMany(d => d.programs))
2015-07-23 05:25:55 +00:00
{
//_logger.LogDebug("Proccesing Schedule for statio ID " + stationID +
2015-08-19 06:12:58 +00:00
// " which corresponds to channel " + channelNumber + " and program id " +
// schedule.programID + " which says it has images? " +
// programDict[schedule.programID].hasImageArtwork);
2015-07-23 05:25:55 +00:00
2015-10-25 18:34:31 +00:00
if (images != null)
2015-07-23 05:25:55 +00:00
{
2015-10-25 18:34:31 +00:00
var imageIndex = images.FindIndex(i => i.programID == schedule.programID.Substring(0, 10));
if (imageIndex > -1)
{
2016-09-11 07:33:53 +00:00
var programEntry = programDict[schedule.programID];
var allImages = images[imageIndex].data ?? new List<ScheduleDirect.ImageData>();
var imagesWithText = allImages.Where(i => string.Equals(i.text, "yes", StringComparison.OrdinalIgnoreCase));
var imagesWithoutText = allImages.Where(i => string.Equals(i.text, "no", StringComparison.OrdinalIgnoreCase));
2017-05-17 18:18:18 +00:00
2019-09-20 10:42:08 +00:00
const double DesiredAspect = 2.0 / 3;
2017-06-11 20:40:25 +00:00
2019-09-20 10:42:08 +00:00
programEntry.primaryImage = GetProgramImage(ApiUrl, imagesWithText, true, DesiredAspect) ??
GetProgramImage(ApiUrl, allImages, true, DesiredAspect);
2017-06-11 20:40:25 +00:00
2019-09-20 10:42:08 +00:00
const double WideAspect = 16.0 / 9;
2019-09-20 10:42:08 +00:00
programEntry.thumbImage = GetProgramImage(ApiUrl, imagesWithText, true, WideAspect);
2017-06-11 20:40:25 +00:00
// Don't supply the same image twice
if (string.Equals(programEntry.primaryImage, programEntry.thumbImage, StringComparison.Ordinal))
{
programEntry.thumbImage = null;
}
2016-09-11 07:33:53 +00:00
2019-09-20 10:42:08 +00:00
programEntry.backdropImage = GetProgramImage(ApiUrl, imagesWithoutText, true, WideAspect);
2017-06-11 21:58:49 +00:00
2016-09-11 07:33:53 +00:00
//programEntry.bannerImage = GetProgramImage(ApiUrl, data, "Banner", false) ??
// GetProgramImage(ApiUrl, data, "Banner-L1", false) ??
// GetProgramImage(ApiUrl, data, "Banner-LO", false) ??
// GetProgramImage(ApiUrl, data, "Banner-LOT", false);
2015-10-25 18:34:31 +00:00
}
2015-07-23 05:25:55 +00:00
}
2017-02-04 23:32:16 +00:00
programsInfo.Add(GetProgram(channelId, schedule, programDict[schedule.programID]));
2015-07-23 05:25:55 +00:00
}
2019-09-20 10:42:08 +00:00
return programsInfo;
2015-07-23 05:25:55 +00:00
}
}
}
private static int GetSizeOrder(ScheduleDirect.ImageData image)
2016-09-11 07:33:53 +00:00
{
2019-09-20 10:42:08 +00:00
if (!string.IsNullOrWhiteSpace(image.height)
&& int.TryParse(image.height, out int value))
2016-09-11 07:33:53 +00:00
{
2019-09-20 10:42:08 +00:00
return value;
2016-09-11 07:33:53 +00:00
}
return 0;
}
private static string GetChannelNumber(ScheduleDirect.Map map)
2015-10-11 00:39:30 +00:00
{
2017-02-04 23:32:16 +00:00
var channelNumber = map.logicalChannelNumber;
2016-09-09 06:59:23 +00:00
2017-02-04 23:32:16 +00:00
if (string.IsNullOrWhiteSpace(channelNumber))
2015-10-11 00:39:30 +00:00
{
2017-02-04 23:32:16 +00:00
channelNumber = map.channel;
2015-10-11 00:39:30 +00:00
}
2017-02-04 23:32:16 +00:00
if (string.IsNullOrWhiteSpace(channelNumber))
2016-03-17 19:29:53 +00:00
{
2017-02-04 23:32:16 +00:00
channelNumber = map.atscMajor + "." + map.atscMinor;
2016-03-17 19:29:53 +00:00
}
2015-10-11 00:39:30 +00:00
return channelNumber.TrimStart('0');
2015-10-11 00:39:30 +00:00
}
private static bool IsMovie(ScheduleDirect.ProgramDetails programInfo)
2017-06-11 20:40:25 +00:00
{
2017-06-21 14:51:11 +00:00
return string.Equals(programInfo.entityType, "movie", StringComparison.OrdinalIgnoreCase);
2017-06-11 20:40:25 +00:00
}
2017-02-04 23:32:16 +00:00
private ProgramInfo GetProgram(string channelId, ScheduleDirect.Program programInfo, ScheduleDirect.ProgramDetails details)
2015-07-23 05:25:55 +00:00
{
2019-01-13 20:37:13 +00:00
var startAt = GetDate(programInfo.airDateTime);
var endAt = startAt.AddSeconds(programInfo.duration);
var audioType = ProgramAudio.Stereo;
2015-08-19 06:12:58 +00:00
2017-08-23 16:49:42 +00:00
var programId = programInfo.programID ?? string.Empty;
string newID = programId + "T" + startAt.Ticks + "C" + channelId;
2015-07-23 05:25:55 +00:00
if (programInfo.audioProperties != null)
{
2016-09-08 20:32:30 +00:00
if (programInfo.audioProperties.Exists(item => string.Equals(item, "atmos", StringComparison.OrdinalIgnoreCase)))
{
audioType = ProgramAudio.Atmos;
}
else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd 5.1", StringComparison.OrdinalIgnoreCase)))
2015-07-29 17:16:00 +00:00
{
audioType = ProgramAudio.DolbyDigital;
}
else if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd", StringComparison.OrdinalIgnoreCase)))
{
audioType = ProgramAudio.DolbyDigital;
}
else if (programInfo.audioProperties.Exists(item => string.Equals(item, "stereo", StringComparison.OrdinalIgnoreCase)))
2015-07-23 05:25:55 +00:00
{
audioType = ProgramAudio.Stereo;
}
else
{
audioType = ProgramAudio.Mono;
}
}
2015-08-04 14:26:36 +00:00
string episodeTitle = null;
2015-07-23 05:25:55 +00:00
if (details.episodeTitle150 != null)
{
2015-08-19 06:12:58 +00:00
episodeTitle = details.episodeTitle150;
2015-07-23 05:25:55 +00:00
}
2015-08-04 14:26:36 +00:00
2015-07-23 05:25:55 +00:00
var info = new ProgramInfo
{
2017-02-04 23:32:16 +00:00
ChannelId = channelId,
2015-07-23 05:25:55 +00:00
Id = newID,
StartDate = startAt,
EndDate = endAt,
Name = details.titles[0].title120 ?? "Unkown",
2015-08-16 18:37:53 +00:00
OfficialRating = null,
2015-07-23 05:25:55 +00:00
CommunityRating = null,
2015-08-04 14:26:36 +00:00
EpisodeTitle = episodeTitle,
2015-07-23 05:25:55 +00:00
Audio = audioType,
2018-09-12 17:26:21 +00:00
//IsNew = programInfo.@new ?? false,
IsRepeat = programInfo.@new == null,
2017-06-21 14:51:11 +00:00
IsSeries = string.Equals(details.entityType, "episode", StringComparison.OrdinalIgnoreCase),
2016-09-11 07:33:53 +00:00
ImageUrl = details.primaryImage,
2017-06-11 20:40:25 +00:00
ThumbImageUrl = details.thumbImage,
2015-08-19 06:12:58 +00:00
IsKids = string.Equals(details.audience, "children", StringComparison.OrdinalIgnoreCase),
2017-06-21 14:51:11 +00:00
IsSports = string.Equals(details.entityType, "sports", StringComparison.OrdinalIgnoreCase),
2017-06-11 20:40:25 +00:00
IsMovie = IsMovie(details),
2018-09-12 17:26:21 +00:00
Etag = programInfo.md5,
IsLive = string.Equals(programInfo.liveTapeDelay, "live", StringComparison.OrdinalIgnoreCase),
IsPremiere = programInfo.premiere || (programInfo.isPremiereOrFinale ?? string.Empty).IndexOf("premiere", StringComparison.OrdinalIgnoreCase) != -1
2015-07-23 05:25:55 +00:00
};
2015-08-04 14:26:36 +00:00
2017-08-23 16:49:42 +00:00
var showId = programId;
2016-12-22 15:58:09 +00:00
2017-03-01 20:30:05 +00:00
if (!info.IsSeries)
{
// It's also a series if it starts with SH
info.IsSeries = showId.StartsWith("SH", StringComparison.OrdinalIgnoreCase) && showId.Length >= 14;
}
2016-12-22 15:58:09 +00:00
// According to SchedulesDirect, these are generic, unidentified episodes
// SH005316560000
var hasUniqueShowId = !showId.StartsWith("SH", StringComparison.OrdinalIgnoreCase) ||
!showId.EndsWith("0000", StringComparison.OrdinalIgnoreCase);
if (!hasUniqueShowId)
{
showId = newID;
}
info.ShowId = showId;
2015-08-19 06:12:58 +00:00
if (programInfo.videoProperties != null)
{
info.IsHD = programInfo.videoProperties.Contains("hdtv", StringComparer.OrdinalIgnoreCase);
2016-09-08 20:32:30 +00:00
info.Is3D = programInfo.videoProperties.Contains("3d", StringComparer.OrdinalIgnoreCase);
2015-08-19 06:12:58 +00:00
}
if (details.contentRating != null && details.contentRating.Count > 0)
{
info.OfficialRating = details.contentRating[0].code.Replace("TV", "TV-").Replace("--", "-");
2015-08-21 15:34:42 +00:00
2015-08-26 18:17:38 +00:00
var invalid = new[] { "N/A", "Approved", "Not Rated", "Passed" };
2015-08-21 15:34:42 +00:00
if (invalid.Contains(info.OfficialRating, StringComparer.OrdinalIgnoreCase))
{
info.OfficialRating = null;
}
2015-08-19 06:12:58 +00:00
}
if (details.descriptions != null)
{
2017-08-23 16:49:42 +00:00
if (details.descriptions.description1000 != null && details.descriptions.description1000.Count > 0)
2015-08-19 06:12:58 +00:00
{
info.Overview = details.descriptions.description1000[0].description;
}
2017-08-23 16:49:42 +00:00
else if (details.descriptions.description100 != null && details.descriptions.description100.Count > 0)
2015-08-19 06:12:58 +00:00
{
2017-01-26 20:27:12 +00:00
info.Overview = details.descriptions.description100[0].description;
2015-08-19 06:12:58 +00:00
}
}
2015-08-16 18:37:53 +00:00
if (info.IsSeries)
{
2017-08-23 16:49:42 +00:00
info.SeriesId = programId.Substring(0, 10);
2015-08-19 06:12:58 +00:00
2018-09-12 17:26:21 +00:00
info.SeriesProviderIds[MetadataProviders.Zap2It.ToString()] = info.SeriesId;
2015-08-19 06:12:58 +00:00
if (details.metadata != null)
{
2017-08-24 19:52:48 +00:00
foreach (var metadataProgram in details.metadata)
2017-03-01 20:30:05 +00:00
{
2017-08-24 19:52:48 +00:00
var gracenote = metadataProgram.Gracenote;
if (gracenote != null)
2017-08-23 16:49:42 +00:00
{
2017-08-24 19:52:48 +00:00
info.SeasonNumber = gracenote.season;
if (gracenote.episode > 0)
{
info.EpisodeNumber = gracenote.episode;
}
break;
2017-08-23 16:49:42 +00:00
}
2017-03-01 20:30:05 +00:00
}
2015-08-19 06:12:58 +00:00
}
2015-08-16 18:37:53 +00:00
}
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrWhiteSpace(details.originalAirDate))
2015-07-23 05:25:55 +00:00
{
info.OriginalAirDate = DateTime.Parse(details.originalAirDate);
2016-10-22 14:50:21 +00:00
info.ProductionYear = info.OriginalAirDate.Value.Year;
2015-07-23 05:25:55 +00:00
}
2018-09-12 17:26:21 +00:00
if (details.movie != null)
{
if (!string.IsNullOrEmpty(details.movie.year) && int.TryParse(details.movie.year, out int year))
2018-09-12 17:26:21 +00:00
{
info.ProductionYear = year;
}
}
2015-07-23 05:25:55 +00:00
if (details.genres != null)
{
info.Genres = details.genres.Where(g => !string.IsNullOrWhiteSpace(g)).ToList();
info.IsNews = details.genres.Contains("news", StringComparer.OrdinalIgnoreCase);
2015-08-19 17:58:41 +00:00
if (info.Genres.Contains("children", StringComparer.OrdinalIgnoreCase))
{
info.IsKids = true;
}
2015-07-23 05:25:55 +00:00
}
2015-08-19 06:12:58 +00:00
2015-07-23 05:25:55 +00:00
return info;
}
private static DateTime GetDate(string value)
2015-09-18 01:51:22 +00:00
{
2015-09-21 16:26:05 +00:00
var date = DateTime.ParseExact(value, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture);
if (date.Kind != DateTimeKind.Utc)
{
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
}
return date;
2015-09-18 01:51:22 +00:00
}
private string GetProgramImage(string apiUrl, IEnumerable<ScheduleDirect.ImageData> images, bool returnDefaultImage, double desiredAspect)
2015-07-23 05:25:55 +00:00
{
var match = images
.OrderBy(i => Math.Abs(desiredAspect - GetAspectRatio(i)))
2017-06-11 20:40:25 +00:00
.ThenByDescending(GetSizeOrder)
.FirstOrDefault();
2016-09-20 15:21:44 +00:00
2016-09-15 23:19:27 +00:00
if (match == null)
{
return null;
}
var uri = match.uri;
2016-09-11 07:33:53 +00:00
if (string.IsNullOrWhiteSpace(uri))
2016-09-11 07:33:53 +00:00
{
return null;
}
else if (uri.IndexOf("http", StringComparison.OrdinalIgnoreCase) != -1)
{
return uri;
}
else
{
return apiUrl + "/image/" + uri;
2015-07-23 05:25:55 +00:00
}
}
private static double GetAspectRatio(ScheduleDirect.ImageData i)
2017-06-11 20:40:25 +00:00
{
int width = 0;
int height = 0;
if (!string.IsNullOrWhiteSpace(i.width))
{
int.TryParse(i.width, out width);
}
if (!string.IsNullOrWhiteSpace(i.height))
{
int.TryParse(i.height, out height);
}
if (height == 0 || width == 0)
{
return 0;
}
double result = width;
result /= height;
return result;
}
private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(
2016-06-08 21:04:52 +00:00
ListingsProviderInfo info,
List<string> programIds,
2015-07-23 05:25:55 +00:00
CancellationToken cancellationToken)
{
2017-02-09 03:58:04 +00:00
if (programIds.Count == 0)
{
return new List<ScheduleDirect.ShowImages>();
}
2015-07-23 05:25:55 +00:00
var imageIdString = "[";
2016-11-03 23:35:19 +00:00
foreach (var i in programIds)
2015-07-23 05:25:55 +00:00
{
2017-02-09 03:58:04 +00:00
var imageId = i.Substring(0, 10);
if (!imageIdString.Contains(imageId))
2015-07-23 05:25:55 +00:00
{
2017-02-09 03:58:04 +00:00
imageIdString += "\"" + imageId + "\",";
2015-07-23 05:25:55 +00:00
}
2016-11-03 23:35:19 +00:00
}
2015-07-23 05:25:55 +00:00
imageIdString = imageIdString.TrimEnd(',') + "]";
2015-08-22 02:59:10 +00:00
2015-07-23 05:25:55 +00:00
var httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/metadata/programs",
UserAgent = UserAgent,
CancellationToken = cancellationToken,
2015-10-11 16:12:53 +00:00
RequestContent = imageIdString,
2015-11-21 03:07:44 +00:00
LogErrorResponseBody = true,
2015-07-23 05:25:55 +00:00
};
2017-02-09 03:58:04 +00:00
try
2015-07-23 05:25:55 +00:00
{
2017-02-09 03:58:04 +00:00
using (var innerResponse2 = await Post(httpOptions, true, info).ConfigureAwait(false))
{
2018-09-12 17:26:21 +00:00
return await _jsonSerializer.DeserializeFromStreamAsync<List<ScheduleDirect.ShowImages>>(
innerResponse2.Content).ConfigureAwait(false);
2017-02-09 03:58:04 +00:00
}
2015-07-23 05:25:55 +00:00
}
2017-02-09 03:58:04 +00:00
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
_logger.LogError(ex, "Error getting image info from schedules direct");
2015-07-23 05:25:55 +00:00
2017-02-09 03:58:04 +00:00
return new List<ScheduleDirect.ShowImages>();
}
2015-07-23 05:25:55 +00:00
}
2015-07-23 17:58:20 +00:00
public async Task<List<NameIdPair>> GetHeadends(ListingsProviderInfo info, string country, string location, CancellationToken cancellationToken)
2015-07-23 05:25:55 +00:00
{
2019-10-25 10:47:20 +00:00
var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
2015-07-23 05:25:55 +00:00
var lineups = new List<NameIdPair>();
if (string.IsNullOrWhiteSpace(token))
{
return lineups;
}
var options = new HttpRequestOptions()
{
2015-07-25 17:21:10 +00:00
Url = ApiUrl + "/headends?country=" + country + "&postalcode=" + location,
2015-07-23 05:25:55 +00:00
UserAgent = UserAgent,
2015-10-11 16:12:53 +00:00
CancellationToken = cancellationToken,
LogErrorResponseBody = true
2015-07-23 05:25:55 +00:00
};
options.RequestHeaders["token"] = token;
try
{
2017-10-20 16:16:56 +00:00
using (var httpResponse = await Get(options, false, info).ConfigureAwait(false))
using (Stream responce = httpResponse.Content)
2015-07-23 05:25:55 +00:00
{
2018-12-20 12:11:26 +00:00
var root = await _jsonSerializer.DeserializeFromStreamAsync<List<ScheduleDirect.Headends>>(responce).ConfigureAwait(false);
2017-10-20 16:16:56 +00:00
2018-12-20 12:11:26 +00:00
if (root != null)
{
foreach (ScheduleDirect.Headends headend in root)
2015-07-23 05:25:55 +00:00
{
foreach (ScheduleDirect.Lineup lineup in headend.lineups)
2015-07-23 05:25:55 +00:00
{
2018-12-20 12:11:26 +00:00
lineups.Add(new NameIdPair
2015-07-23 05:25:55 +00:00
{
2018-12-20 12:11:26 +00:00
Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
Id = lineup.uri.Substring(18)
});
2015-07-23 05:25:55 +00:00
}
}
2018-12-20 12:11:26 +00:00
}
else
{
_logger.LogInformation("No lineups available");
2015-07-23 05:25:55 +00:00
}
}
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
_logger.LogError(ex, "Error getting headends");
2015-07-23 05:25:55 +00:00
}
return lineups;
}
private readonly ConcurrentDictionary<string, NameValuePair> _tokens = new ConcurrentDictionary<string, NameValuePair>();
2015-09-01 19:18:25 +00:00
private DateTime _lastErrorResponse;
2015-07-23 05:25:55 +00:00
private async Task<string> GetToken(ListingsProviderInfo info, CancellationToken cancellationToken)
{
2015-07-23 13:23:22 +00:00
var username = info.Username;
2015-07-23 05:25:55 +00:00
2015-07-23 13:23:22 +00:00
// Reset the token if there's no username
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
2015-07-23 05:25:55 +00:00
2015-07-23 13:23:22 +00:00
var password = info.Password;
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(password))
2015-07-23 13:23:22 +00:00
{
return null;
}
2015-07-23 05:25:55 +00:00
2015-09-01 19:18:25 +00:00
// Avoid hammering SD
if ((DateTime.UtcNow - _lastErrorResponse).TotalMinutes < 1)
{
return null;
}
2015-07-23 13:23:22 +00:00
NameValuePair savedToken = null;
if (!_tokens.TryGetValue(username, out savedToken))
{
savedToken = new NameValuePair();
_tokens.TryAdd(username, savedToken);
}
2015-07-23 05:25:55 +00:00
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(savedToken.Name) && !string.IsNullOrEmpty(savedToken.Value))
2015-07-23 13:23:22 +00:00
{
if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out long ticks))
2015-07-23 05:25:55 +00:00
{
2015-07-23 13:23:22 +00:00
// If it's under 24 hours old we can still use it
2016-03-27 21:11:27 +00:00
if (DateTime.UtcNow.Ticks - ticks < TimeSpan.FromHours(20).Ticks)
2015-07-23 05:25:55 +00:00
{
2015-07-23 13:23:22 +00:00
return savedToken.Name;
2015-07-23 05:25:55 +00:00
}
}
2015-07-23 13:23:22 +00:00
}
2015-07-23 05:25:55 +00:00
2015-07-23 13:23:22 +00:00
await _tokenSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
2015-07-23 05:25:55 +00:00
var result = await GetTokenInternal(username, password, cancellationToken).ConfigureAwait(false);
savedToken.Name = result;
savedToken.Value = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
return result;
}
2015-09-01 19:18:25 +00:00
catch (HttpException ex)
{
if (ex.StatusCode.HasValue)
{
if ((int)ex.StatusCode.Value == 400)
{
_tokens.Clear();
_lastErrorResponse = DateTime.UtcNow;
}
}
throw;
}
2015-07-23 05:25:55 +00:00
finally
{
_tokenSemaphore.Release();
}
}
2016-06-08 21:04:52 +00:00
private async Task<HttpResponseInfo> Post(HttpRequestOptions options,
bool enableRetry,
ListingsProviderInfo providerInfo)
2015-11-21 03:13:03 +00:00
{
2017-10-22 21:38:03 +00:00
// Schedules direct requires that the client support compression and will return a 400 response without it
options.DecompressionMethod = CompressionMethods.Deflate;
2015-11-21 03:13:03 +00:00
try
{
return await _httpClient.Post(options).ConfigureAwait(false);
2016-06-08 21:04:52 +00:00
}
catch (HttpException ex)
{
_tokens.Clear();
if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
{
enableRetry = false;
}
if (!enableRetry)
{
throw;
}
}
options.RequestHeaders["token"] = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
2016-06-08 21:04:52 +00:00
return await Post(options, false, providerInfo).ConfigureAwait(false);
2015-11-21 03:13:03 +00:00
}
2017-10-20 16:16:56 +00:00
private async Task<HttpResponseInfo> Get(HttpRequestOptions options,
2016-06-08 21:04:52 +00:00
bool enableRetry,
ListingsProviderInfo providerInfo)
2015-11-21 03:13:03 +00:00
{
2017-10-22 21:38:03 +00:00
// Schedules direct requires that the client support compression and will return a 400 response without it
options.DecompressionMethod = CompressionMethods.Deflate;
2015-11-21 03:13:03 +00:00
try
{
return await _httpClient.SendAsync(options, HttpMethod.Get).ConfigureAwait(false);
2016-06-08 21:04:52 +00:00
}
catch (HttpException ex)
{
_tokens.Clear();
if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
{
enableRetry = false;
}
if (!enableRetry)
{
throw;
}
}
options.RequestHeaders["token"] = await GetToken(providerInfo, options.CancellationToken).ConfigureAwait(false);
2016-06-08 21:04:52 +00:00
return await Get(options, false, providerInfo).ConfigureAwait(false);
2015-11-21 03:13:03 +00:00
}
2015-07-23 05:25:55 +00:00
private async Task<string> GetTokenInternal(string username, string password,
CancellationToken cancellationToken)
2015-07-21 04:22:46 +00:00
{
2015-07-23 05:25:55 +00:00
var httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/token",
UserAgent = UserAgent,
RequestContent = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}",
2015-10-11 16:12:53 +00:00
CancellationToken = cancellationToken,
LogErrorResponseBody = true
2015-07-23 05:25:55 +00:00
};
//_logger.LogInformation("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
2015-07-23 05:25:55 +00:00
// httpOptions.RequestContent);
using (var response = await Post(httpOptions, false, null).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
var root = await _jsonSerializer.DeserializeFromStreamAsync<ScheduleDirect.Token>(response.Content).ConfigureAwait(false);
2015-07-23 05:25:55 +00:00
if (root.message == "OK")
{
_logger.LogInformation("Authenticated with Schedules Direct token: " + root.token);
2015-07-23 05:25:55 +00:00
return root.token;
}
2016-11-03 23:35:19 +00:00
throw new Exception("Could not authenticate with Schedules Direct Error: " + root.message);
2015-07-23 05:25:55 +00:00
}
}
2015-07-23 17:04:54 +00:00
private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
{
2019-10-25 10:47:20 +00:00
var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
2015-07-23 17:04:54 +00:00
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(token))
2015-07-23 17:04:54 +00:00
{
throw new ArgumentException("Authentication required.");
}
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(info.ListingsId))
2015-07-24 21:44:25 +00:00
{
throw new ArgumentException("Listings Id required");
}
_logger.LogInformation("Adding new LineUp ");
2015-07-23 17:04:54 +00:00
var httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/lineups/" + info.ListingsId,
UserAgent = UserAgent,
2015-10-11 16:12:53 +00:00
CancellationToken = cancellationToken,
2016-10-06 18:55:01 +00:00
LogErrorResponseBody = true,
BufferContent = false
2015-07-23 17:04:54 +00:00
};
httpOptions.RequestHeaders["token"] = token;
2015-07-29 17:16:00 +00:00
using (await _httpClient.SendAsync(httpOptions, HttpMethod.Put).ConfigureAwait(false))
2015-07-23 17:04:54 +00:00
{
}
}
2015-07-23 23:40:54 +00:00
private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(info.ListingsId))
2015-07-24 21:44:25 +00:00
{
throw new ArgumentException("Listings Id required");
}
2019-10-25 10:47:20 +00:00
var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
2015-07-23 23:40:54 +00:00
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(token))
2015-07-26 21:02:23 +00:00
{
throw new Exception("token required");
}
_logger.LogInformation("Headends on account ");
2015-07-23 23:40:54 +00:00
var options = new HttpRequestOptions()
{
Url = ApiUrl + "/lineups",
UserAgent = UserAgent,
2015-10-11 16:12:53 +00:00
CancellationToken = cancellationToken,
LogErrorResponseBody = true
2015-07-23 23:40:54 +00:00
};
options.RequestHeaders["token"] = token;
2015-08-21 05:00:56 +00:00
try
{
2017-10-20 16:16:56 +00:00
using (var httpResponse = await Get(options, false, null).ConfigureAwait(false))
using (var response = httpResponse.Content)
2015-08-21 05:00:56 +00:00
{
var root = await _jsonSerializer.DeserializeFromStreamAsync<ScheduleDirect.Lineups>(response).ConfigureAwait(false);
2015-08-21 05:00:56 +00:00
return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
2015-08-21 05:00:56 +00:00
}
}
catch (HttpException ex)
2015-07-23 23:40:54 +00:00
{
2015-08-21 05:00:56 +00:00
// Apparently we're supposed to swallow this
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.BadRequest)
{
return false;
}
2015-07-23 23:40:54 +00:00
2015-08-21 05:00:56 +00:00
throw;
2015-07-23 23:40:54 +00:00
}
}
2015-07-25 17:21:10 +00:00
public async Task Validate(ListingsProviderInfo info, bool validateLogin, bool validateListings)
2015-07-23 23:40:54 +00:00
{
2015-07-25 17:21:10 +00:00
if (validateLogin)
2015-07-24 21:44:25 +00:00
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(info.Username))
2015-07-25 17:21:10 +00:00
{
throw new ArgumentException("Username is required");
}
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(info.Password))
2015-07-25 17:21:10 +00:00
{
throw new ArgumentException("Password is required");
}
2015-07-24 21:44:25 +00:00
}
2015-07-25 17:21:10 +00:00
if (validateListings)
{
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(info.ListingsId))
2015-07-25 17:21:10 +00:00
{
throw new ArgumentException("Listings Id required");
}
2015-07-24 21:44:25 +00:00
2015-07-25 17:21:10 +00:00
var hasLineup = await HasLineup(info, CancellationToken.None).ConfigureAwait(false);
2015-07-23 23:40:54 +00:00
2015-07-25 17:21:10 +00:00
if (!hasLineup)
{
await AddLineupToAccount(info, CancellationToken.None).ConfigureAwait(false);
}
2015-07-23 23:40:54 +00:00
}
}
public Task<List<NameIdPair>> GetLineups(ListingsProviderInfo info, string country, string location)
{
return GetHeadends(info, country, location, CancellationToken.None);
}
2016-06-08 05:24:25 +00:00
public async Task<List<ChannelInfo>> GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken)
{
2016-06-08 21:04:52 +00:00
var listingsId = info.ListingsId;
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(listingsId))
2016-06-08 21:04:52 +00:00
{
throw new Exception("ListingsId required");
}
2019-10-25 10:47:20 +00:00
var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
2016-06-08 21:04:52 +00:00
2018-09-12 17:26:21 +00:00
if (string.IsNullOrEmpty(token))
2016-06-08 21:04:52 +00:00
{
throw new Exception("token required");
}
var httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/lineups/" + listingsId,
UserAgent = UserAgent,
CancellationToken = cancellationToken,
LogErrorResponseBody = true,
};
httpOptions.RequestHeaders["token"] = token;
var list = new List<ChannelInfo>();
2017-10-20 16:16:56 +00:00
using (var httpResponse = await Get(httpOptions, true, info).ConfigureAwait(false))
using (var response = httpResponse.Content)
2016-06-08 21:04:52 +00:00
{
var root = await _jsonSerializer.DeserializeFromStreamAsync<ScheduleDirect.Channel>(response).ConfigureAwait(false);
_logger.LogInformation("Found {ChannelCount} channels on the lineup on ScheduleDirect", root.map.Count);
_logger.LogInformation("Mapping Stations to Channel");
2016-06-10 16:45:04 +00:00
var allStations = root.stations ?? Enumerable.Empty<ScheduleDirect.Station>();
2017-02-04 23:32:16 +00:00
foreach (ScheduleDirect.Map map in root.map)
{
var channelNumber = GetChannelNumber(map);
2017-02-04 23:32:16 +00:00
var station = allStations.FirstOrDefault(item => string.Equals(item.stationID, map.stationID, StringComparison.OrdinalIgnoreCase));
if (station == null)
{
station = new ScheduleDirect.Station
2017-02-04 23:32:16 +00:00
{
stationID = map.stationID
2017-10-20 16:16:56 +00:00
};
}
2017-10-20 16:16:56 +00:00
var channelInfo = new ChannelInfo
{
Id = station.stationID,
CallSign = station.callsign,
Number = channelNumber,
Name = string.IsNullOrWhiteSpace(station.name) ? channelNumber : station.name
};
2017-02-04 23:32:16 +00:00
if (station.logo != null)
{
channelInfo.ImageUrl = station.logo.URL;
2017-10-20 16:16:56 +00:00
}
list.Add(channelInfo);
2016-06-08 21:04:52 +00:00
}
}
return list;
2016-06-08 05:24:25 +00:00
}
2017-02-04 23:32:16 +00:00
private ScheduleDirect.Station GetStation(List<ScheduleDirect.Station> allStations, string channelNumber, string channelName)
{
if (!string.IsNullOrWhiteSpace(channelName))
{
channelName = NormalizeName(channelName);
var result = allStations.FirstOrDefault(i => string.Equals(NormalizeName(i.callsign ?? string.Empty), channelName, StringComparison.OrdinalIgnoreCase));
if (result != null)
{
return result;
}
}
if (!string.IsNullOrWhiteSpace(channelNumber))
{
return allStations.FirstOrDefault(i => string.Equals(NormalizeName(i.stationID ?? string.Empty), channelNumber, StringComparison.OrdinalIgnoreCase));
}
return null;
}
private static string NormalizeName(string value)
2017-02-04 23:32:16 +00:00
{
return value.Replace(" ", string.Empty).Replace("-", string.Empty);
}
2015-07-23 05:25:55 +00:00
public class ScheduleDirect
{
public class Token
{
public int code { get; set; }
public string message { get; set; }
public string serverID { get; set; }
public string token { get; set; }
}
public class Lineup
{
public string lineup { get; set; }
public string name { get; set; }
public string transport { get; set; }
public string location { get; set; }
public string uri { get; set; }
}
public class Lineups
{
public int code { get; set; }
public string serverID { get; set; }
public string datetime { get; set; }
public List<Lineup> lineups { get; set; }
}
public class Headends
{
public string headend { get; set; }
public string transport { get; set; }
public string location { get; set; }
public List<Lineup> lineups { get; set; }
}
public class Map
{
public string stationID { get; set; }
public string channel { get; set; }
2015-10-01 16:28:24 +00:00
public string logicalChannelNumber { get; set; }
2015-07-23 05:25:55 +00:00
public int uhfVhf { get; set; }
public int atscMajor { get; set; }
public int atscMinor { get; set; }
}
public class Broadcaster
{
public string city { get; set; }
public string state { get; set; }
public string postalcode { get; set; }
public string country { get; set; }
}
public class Logo
{
public string URL { get; set; }
public int height { get; set; }
public int width { get; set; }
public string md5 { get; set; }
}
public class Station
{
public string stationID { get; set; }
public string name { get; set; }
public string callsign { get; set; }
public List<string> broadcastLanguage { get; set; }
public List<string> descriptionLanguage { get; set; }
public Broadcaster broadcaster { get; set; }
public string affiliate { get; set; }
public Logo logo { get; set; }
public bool? isCommercialFree { get; set; }
}
public class Metadata
{
public string lineup { get; set; }
public string modified { get; set; }
public string transport { get; set; }
}
public class Channel
{
public List<Map> map { get; set; }
public List<Station> stations { get; set; }
public Metadata metadata { get; set; }
}
public class RequestScheduleForChannel
{
public string stationID { get; set; }
public List<string> date { get; set; }
}
public class Rating
{
public string body { get; set; }
public string code { get; set; }
}
public class Multipart
{
public int partNumber { get; set; }
public int totalParts { get; set; }
}
public class Program
{
public string programID { get; set; }
public string airDateTime { get; set; }
public int duration { get; set; }
public string md5 { get; set; }
public List<string> audioProperties { get; set; }
public List<string> videoProperties { get; set; }
public List<Rating> ratings { get; set; }
public bool? @new { get; set; }
public Multipart multipart { get; set; }
2018-09-12 17:26:21 +00:00
public string liveTapeDelay { get; set; }
public bool premiere { get; set; }
public bool repeat { get; set; }
public string isPremiereOrFinale { get; set; }
2015-07-23 05:25:55 +00:00
}
public class MetadataSchedule
{
public string modified { get; set; }
public string md5 { get; set; }
public string startDate { get; set; }
public string endDate { get; set; }
public int days { get; set; }
}
public class Day
{
public string stationID { get; set; }
public List<Program> programs { get; set; }
public MetadataSchedule metadata { get; set; }
2016-03-01 04:23:58 +00:00
public Day()
{
programs = new List<Program>();
}
2015-07-23 05:25:55 +00:00
}
//
public class Title
{
public string title120 { get; set; }
}
public class EventDetails
{
public string subType { get; set; }
}
public class Description100
{
public string descriptionLanguage { get; set; }
public string description { get; set; }
}
public class Description1000
{
public string descriptionLanguage { get; set; }
public string description { get; set; }
}
public class DescriptionsProgram
{
public List<Description100> description100 { get; set; }
public List<Description1000> description1000 { get; set; }
}
public class Gracenote
{
public int season { get; set; }
public int episode { get; set; }
}
public class MetadataPrograms
{
public Gracenote Gracenote { get; set; }
}
public class ContentRating
{
public string body { get; set; }
public string code { get; set; }
}
public class Cast
{
public string billingOrder { get; set; }
public string role { get; set; }
public string nameId { get; set; }
public string personId { get; set; }
public string name { get; set; }
public string characterName { get; set; }
}
public class Crew
{
public string billingOrder { get; set; }
public string role { get; set; }
public string nameId { get; set; }
public string personId { get; set; }
public string name { get; set; }
}
public class QualityRating
{
public string ratingsBody { get; set; }
public string rating { get; set; }
public string minRating { get; set; }
public string maxRating { get; set; }
public string increment { get; set; }
}
public class Movie
{
public string year { get; set; }
public int duration { get; set; }
public List<QualityRating> qualityRating { get; set; }
}
public class Recommendation
{
public string programID { get; set; }
public string title120 { get; set; }
}
public class ProgramDetails
{
2015-08-19 06:12:58 +00:00
public string audience { get; set; }
2015-07-23 05:25:55 +00:00
public string programID { get; set; }
public List<Title> titles { get; set; }
public EventDetails eventDetails { get; set; }
public DescriptionsProgram descriptions { get; set; }
public string originalAirDate { get; set; }
public List<string> genres { get; set; }
public string episodeTitle150 { get; set; }
public List<MetadataPrograms> metadata { get; set; }
public List<ContentRating> contentRating { get; set; }
public List<Cast> cast { get; set; }
public List<Crew> crew { get; set; }
2017-06-21 14:51:11 +00:00
public string entityType { get; set; }
2015-07-23 05:25:55 +00:00
public string showType { get; set; }
public bool hasImageArtwork { get; set; }
2016-09-11 07:33:53 +00:00
public string primaryImage { get; set; }
public string thumbImage { get; set; }
2017-06-11 21:58:49 +00:00
public string backdropImage { get; set; }
2016-09-11 07:33:53 +00:00
public string bannerImage { get; set; }
2015-07-23 05:25:55 +00:00
public string imageID { get; set; }
public string md5 { get; set; }
public List<string> contentAdvisory { get; set; }
public Movie movie { get; set; }
public List<Recommendation> recommendations { get; set; }
}
public class Caption
{
public string content { get; set; }
public string lang { get; set; }
}
public class ImageData
{
public string width { get; set; }
public string height { get; set; }
public string uri { get; set; }
public string size { get; set; }
public string aspect { get; set; }
public string category { get; set; }
public string text { get; set; }
public string primary { get; set; }
public string tier { get; set; }
public Caption caption { get; set; }
}
public class ShowImages
{
public string programID { get; set; }
public List<ImageData> data { get; set; }
}
2015-07-21 04:22:46 +00:00
}
}
2018-12-30 17:30:29 +00:00
}