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

1095 lines
39 KiB
C#
Raw Normal View History

2015-08-21 05:00:56 +00:00
using System.Net;
using MediaBrowser.Common;
2015-07-29 20:31:15 +00:00
using MediaBrowser.Common.Net;
2015-07-23 05:25:55 +00:00
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Logging;
2015-08-21 05:00:56 +00:00
using MediaBrowser.Model.Net;
2015-07-23 05:25:55 +00:00
using MediaBrowser.Model.Serialization;
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;
2015-07-21 04:22:46 +00:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.LiveTv.Listings
{
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";
private readonly ConcurrentDictionary<string, ScheduleDirect.Station> _channelPair =
new ConcurrentDictionary<string, ScheduleDirect.Station>();
2015-07-29 20:31:15 +00:00
public SchedulesDirect(ILogger 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
{
get { return "Emby/" + _appHost.ApplicationVersion; }
2015-07-23 05:25:55 +00:00
}
2015-07-29 17:16:00 +00:00
private List<string> GetScheduleRequestDates(DateTime startDateUtc, DateTime endDateUtc)
{
List<string> dates = new List<string>();
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;
}
2015-10-11 00:39:30 +00:00
public async Task<IEnumerable<ProgramInfo>> GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken)
2015-07-23 05:25:55 +00:00
{
List<ProgramInfo> programsInfo = new List<ProgramInfo>();
2015-09-01 19:18:25 +00:00
var token = await GetToken(info, cancellationToken).ConfigureAwait(false);
2015-07-23 05:25:55 +00:00
if (string.IsNullOrWhiteSpace(token))
{
return programsInfo;
}
if (string.IsNullOrWhiteSpace(info.ListingsId))
{
return programsInfo;
}
var httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/schedules",
UserAgent = UserAgent,
2015-09-02 02:56:19 +00:00
CancellationToken = cancellationToken,
// The data can be large so give it some extra time
2015-11-21 03:13:03 +00:00
TimeoutMs = 60000,
2015-10-11 16:12:53 +00:00
LogErrorResponseBody = true
2015-07-23 05:25:55 +00:00
};
httpOptions.RequestHeaders["token"] = token;
2015-07-29 17:16:00 +00:00
var dates = GetScheduleRequestDates(startDateUtc, endDateUtc);
2015-07-23 05:25:55 +00:00
2015-10-11 00:39:30 +00:00
ScheduleDirect.Station station = GetStation(channelNumber, channelName);
2015-07-23 05:25:55 +00:00
2015-10-11 00:39:30 +00:00
if (station == null)
2015-07-23 05:25:55 +00:00
{
2015-10-24 15:33:22 +00:00
_logger.Info("No Schedules Direct Station found for channel {0} with name {1}", channelNumber, channelName);
2015-07-23 05:25:55 +00:00
return programsInfo;
}
2015-10-11 00:39:30 +00:00
2015-07-23 05:25:55 +00:00
string stationID = station.stationID;
_logger.Info("Channel Station ID is: " + stationID);
List<ScheduleDirect.RequestScheduleForChannel> requestList =
new List<ScheduleDirect.RequestScheduleForChannel>()
{
new ScheduleDirect.RequestScheduleForChannel()
{
stationID = stationID,
date = dates
}
};
var requestString = _jsonSerializer.SerializeToString(requestList);
_logger.Debug("Request string for schedules is: " + requestString);
httpOptions.RequestContent = requestString;
using (var response = await Post(httpOptions, true, info).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
StreamReader reader = new StreamReader(response.Content);
string responseString = reader.ReadToEnd();
var dailySchedules = _jsonSerializer.DeserializeFromString<List<ScheduleDirect.Day>>(responseString);
2015-07-29 17:16:00 +00:00
_logger.Debug("Found " + dailySchedules.Count() + " programs on " + channelNumber + " ScheduleDirect");
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,
2015-11-21 03:07:44 +00:00
LogErrorResponseBody = true,
// The data can be large so give it some extra time
TimeoutMs = 60000
2015-07-23 05:25:55 +00:00
};
httpOptions.RequestHeaders["token"] = token;
List<string> programsID = new List<string>();
programsID = dailySchedules.SelectMany(d => d.programs.Select(s => s.programID)).Distinct().ToList();
var requestBody = "[\"" + string.Join("\", \"", programsID) + "\"]";
httpOptions.RequestContent = requestBody;
using (var innerResponse = await Post(httpOptions, true, info).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
StreamReader innerReader = new StreamReader(innerResponse.Content);
responseString = innerReader.ReadToEnd();
2015-08-19 06:12:58 +00:00
2015-07-23 05:25:55 +00:00
var programDetails =
_jsonSerializer.DeserializeFromString<List<ScheduleDirect.ProgramDetails>>(
responseString);
var programDict = programDetails.ToDictionary(p => p.programID, y => y);
var images = await GetImageForPrograms(info, programDetails.Where(p => p.hasImageArtwork).Select(p => p.programID).ToList(), cancellationToken);
2015-07-23 05:25:55 +00:00
var schedules = dailySchedules.SelectMany(d => d.programs);
foreach (ScheduleDirect.Program schedule in schedules)
{
2015-08-19 06:12:58 +00:00
//_logger.Debug("Proccesing Schedule for statio ID " + stationID +
// " 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)
{
programDict[schedule.programID].images = GetProgramLogo(ApiUrl, images[imageIndex]);
}
2015-07-23 05:25:55 +00:00
}
programsInfo.Add(GetProgram(channelNumber, schedule, programDict[schedule.programID]));
}
_logger.Info("Finished with EPGData");
}
}
return programsInfo;
}
2015-10-11 00:39:30 +00:00
private ScheduleDirect.Station GetStation(string channelNumber, string channelName)
{
ScheduleDirect.Station station;
if (_channelPair.TryGetValue(channelNumber, out station))
{
return station;
}
if (string.IsNullOrWhiteSpace(channelName))
{
return null;
}
channelName = NormalizeName(channelName);
return _channelPair.Values.FirstOrDefault(i => string.Equals(NormalizeName(i.callsign ?? string.Empty), channelName, StringComparison.OrdinalIgnoreCase));
}
private string NormalizeName(string value)
{
return value.Replace(" ", string.Empty).Replace("-", string.Empty);
}
2015-07-23 05:25:55 +00:00
public async Task AddMetadata(ListingsProviderInfo info, List<ChannelInfo> channels,
CancellationToken cancellationToken)
{
2015-07-26 21:02:23 +00:00
if (string.IsNullOrWhiteSpace(info.ListingsId))
{
throw new Exception("ListingsId required");
}
2015-07-23 05:25:55 +00:00
var token = await GetToken(info, cancellationToken);
2015-07-26 21:02:23 +00:00
if (string.IsNullOrWhiteSpace(token))
{
throw new Exception("token required");
}
2015-07-23 05:25:55 +00:00
_channelPair.Clear();
2015-07-26 21:02:23 +00:00
var httpOptions = new HttpRequestOptions()
2015-07-23 05:25:55 +00:00
{
2015-07-26 21:02:23 +00:00
Url = ApiUrl + "/lineups/" + info.ListingsId,
UserAgent = UserAgent,
2015-10-11 16:12:53 +00:00
CancellationToken = cancellationToken,
2015-11-21 03:13:03 +00:00
LogErrorResponseBody = true,
// The data can be large so give it some extra time
TimeoutMs = 60000
2015-07-26 21:02:23 +00:00
};
2015-07-23 05:25:55 +00:00
2015-07-26 21:02:23 +00:00
httpOptions.RequestHeaders["token"] = token;
using (var response = await Get(httpOptions, true, info).ConfigureAwait(false))
2015-07-26 21:02:23 +00:00
{
var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Channel>(response);
_logger.Info("Found " + root.map.Count() + " channels on the lineup on ScheduleDirect");
_logger.Info("Mapping Stations to Channel");
foreach (ScheduleDirect.Map map in root.map)
2015-07-23 05:25:55 +00:00
{
2015-10-11 00:39:30 +00:00
var channelNumber = map.logicalChannelNumber;
2015-10-01 16:28:24 +00:00
2015-10-11 00:39:30 +00:00
if (string.IsNullOrWhiteSpace(channelNumber))
2015-10-01 16:28:24 +00:00
{
2015-10-11 00:39:30 +00:00
channelNumber = map.channel;
2015-10-01 16:28:24 +00:00
}
2015-10-11 00:39:30 +00:00
if (string.IsNullOrWhiteSpace(channelNumber))
2015-10-01 16:28:24 +00:00
{
2015-10-11 00:39:30 +00:00
channelNumber = (map.atscMajor + "." + map.atscMinor);
2015-10-01 16:28:24 +00:00
}
2015-10-11 00:39:30 +00:00
channelNumber = channelNumber.TrimStart('0');
2015-10-01 16:28:24 +00:00
2015-10-11 00:39:30 +00:00
_logger.Debug("Found channel: " + channelNumber + " in Schedules Direct");
2015-07-26 21:02:23 +00:00
var schChannel = root.stations.FirstOrDefault(item => item.stationID == map.stationID);
2015-07-23 05:25:55 +00:00
2015-10-11 00:39:30 +00:00
_channelPair.TryAdd(channelNumber, schChannel);
2015-07-26 21:02:23 +00:00
}
2015-10-11 00:39:30 +00:00
_logger.Info("Added " + _channelPair.Count + " channels to the dictionary");
2015-07-23 05:25:55 +00:00
2015-07-26 21:02:23 +00:00
foreach (ChannelInfo channel in channels)
{
2015-10-24 15:33:22 +00:00
var station = GetStation(channel.Number, channel.Name);
2015-10-11 00:39:30 +00:00
if (station != null)
2015-07-23 05:25:55 +00:00
{
2015-10-11 00:39:30 +00:00
if (station.logo != null)
2015-07-23 05:25:55 +00:00
{
2015-10-11 00:39:30 +00:00
channel.ImageUrl = station.logo.URL;
2015-07-26 21:02:23 +00:00
channel.HasImage = true;
}
2015-10-11 00:39:30 +00:00
string channelName = station.name;
2015-07-26 21:02:23 +00:00
channel.Name = channelName;
}
else
{
2015-10-11 00:39:30 +00:00
_logger.Info("Schedules Direct doesnt have data for channel: " + channel.Number + " " + channel.Name);
2015-07-23 05:25:55 +00:00
}
}
}
}
private ProgramInfo GetProgram(string channel, ScheduleDirect.Program programInfo,
ScheduleDirect.ProgramDetails details)
{
2015-07-23 23:40:54 +00:00
//_logger.Debug("Show type is: " + (details.showType ?? "No ShowType"));
2015-09-18 01:51:22 +00:00
DateTime startAt = GetDate(programInfo.airDateTime);
2015-07-23 05:25:55 +00:00
DateTime endAt = startAt.AddSeconds(programInfo.duration);
ProgramAudio audioType = ProgramAudio.Stereo;
2015-08-19 06:12:58 +00:00
2015-07-23 05:25:55 +00:00
bool repeat = (programInfo.@new == null);
string newID = programInfo.programID + "T" + startAt.Ticks + "C" + channel;
if (programInfo.audioProperties != null)
{
2015-07-29 17:16:00 +00:00
if (programInfo.audioProperties.Exists(item => string.Equals(item, "dd 5.1", StringComparison.OrdinalIgnoreCase)))
{
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-08-19 06:12:58 +00:00
string imageUrl = null;
2015-07-23 05:25:55 +00:00
if (details.hasImageArtwork)
{
2015-08-19 06:12:58 +00:00
imageUrl = details.images;
2015-07-23 05:25:55 +00:00
}
2015-08-16 18:37:53 +00:00
var showType = details.showType ?? string.Empty;
2015-07-23 05:25:55 +00:00
var info = new ProgramInfo
{
ChannelId = channel,
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,
IsRepeat = repeat,
2015-08-16 18:37:53 +00:00
IsSeries = showType.IndexOf("series", StringComparison.OrdinalIgnoreCase) != -1,
2015-08-19 06:12:58 +00:00
ImageUrl = imageUrl,
IsKids = string.Equals(details.audience, "children", StringComparison.OrdinalIgnoreCase),
2015-08-16 18:37:53 +00:00
IsSports = showType.IndexOf("sports", StringComparison.OrdinalIgnoreCase) != -1,
IsMovie = showType.IndexOf("movie", StringComparison.OrdinalIgnoreCase) != -1 || showType.IndexOf("film", StringComparison.OrdinalIgnoreCase) != -1,
2015-08-22 02:59:10 +00:00
ShowId = programInfo.programID,
Etag = programInfo.md5
2015-07-23 05:25:55 +00:00
};
2015-08-04 14:26:36 +00:00
2015-08-19 06:12:58 +00:00
if (programInfo.videoProperties != null)
{
info.IsHD = programInfo.videoProperties.Contains("hdtv", StringComparer.OrdinalIgnoreCase);
}
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)
{
if (details.descriptions.description1000 != null)
{
info.Overview = details.descriptions.description1000[0].description;
}
else if (details.descriptions.description100 != null)
{
info.ShortOverview = details.descriptions.description100[0].description;
}
}
2015-08-16 18:37:53 +00:00
if (info.IsSeries)
{
info.SeriesId = programInfo.programID.Substring(0, 10);
2015-08-19 06:12:58 +00:00
if (details.metadata != null)
{
var gracenote = details.metadata.Find(x => x.Gracenote != null).Gracenote;
info.SeasonNumber = gracenote.season;
info.EpisodeNumber = gracenote.episode;
}
2015-08-16 18:37:53 +00:00
}
2015-08-04 14:26:36 +00:00
if (!string.IsNullOrWhiteSpace(details.originalAirDate))
2015-07-23 05:25:55 +00:00
{
info.OriginalAirDate = DateTime.Parse(details.originalAirDate);
}
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;
}
2015-09-18 01:51:22 +00:00
private DateTime GetDate(string value)
{
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
}
2015-07-23 05:25:55 +00:00
private string GetProgramLogo(string apiUrl, ScheduleDirect.ShowImages images)
{
2015-10-25 18:34:31 +00:00
string url = null;
2015-07-23 05:25:55 +00:00
if (images.data != null)
{
var smallImages = images.data.Where(i => i.size == "Sm").ToList();
if (smallImages.Any())
{
images.data = smallImages;
}
var logoIndex = images.data.FindIndex(i => i.category == "Logo");
if (logoIndex == -1)
{
logoIndex = 0;
}
2015-10-25 18:34:31 +00:00
var uri = images.data[logoIndex].uri;
if (!string.IsNullOrWhiteSpace(uri))
2015-07-23 05:25:55 +00:00
{
2015-10-25 18:34:31 +00:00
if (uri.IndexOf("http", StringComparison.OrdinalIgnoreCase) != -1)
{
url = uri;
}
else
{
url = apiUrl + "/image/" + uri;
}
2015-07-23 05:25:55 +00:00
}
2015-07-23 16:32:34 +00:00
//_logger.Debug("URL for image is : " + url);
2015-07-23 05:25:55 +00:00
}
return url;
}
private async Task<List<ScheduleDirect.ShowImages>> GetImageForPrograms(
ListingsProviderInfo info,
List<string> programIds,
2015-07-23 05:25:55 +00:00
CancellationToken cancellationToken)
{
var imageIdString = "[";
programIds.ForEach(i =>
{
if (!imageIdString.Contains(i.Substring(0, 10)))
{
imageIdString += "\"" + i.Substring(0, 10) + "\",";
}
});
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,
// The data can be large so give it some extra time
TimeoutMs = 60000
2015-07-23 05:25:55 +00:00
};
List<ScheduleDirect.ShowImages> images;
using (var innerResponse2 = await Post(httpOptions, true, info).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
images = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.ShowImages>>(
innerResponse2.Content);
}
return images;
}
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
{
var token = await GetToken(info, cancellationToken);
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
{
using (Stream responce = await Get(options, false, info).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
var root = _jsonSerializer.DeserializeFromStream<List<ScheduleDirect.Headends>>(responce);
2015-08-27 01:31:54 +00:00
2015-07-23 05:25:55 +00:00
if (root != null)
{
foreach (ScheduleDirect.Headends headend in root)
{
foreach (ScheduleDirect.Lineup lineup in headend.lineups)
{
2015-08-14 14:42:40 +00:00
lineups.Add(new NameIdPair
2015-07-23 05:25:55 +00:00
{
Name = string.IsNullOrWhiteSpace(lineup.name) ? lineup.lineup : lineup.name,
Id = lineup.uri.Substring(18)
});
}
}
}
else
{
2015-08-27 01:31:54 +00:00
_logger.Info("No lineups available");
2015-07-23 05:25:55 +00:00
}
}
}
catch (Exception ex)
{
_logger.Error("Error getting headends", ex);
}
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;
if (string.IsNullOrWhiteSpace(password))
{
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
2015-07-23 13:23:22 +00:00
if (!string.IsNullOrWhiteSpace(savedToken.Name) && !string.IsNullOrWhiteSpace(savedToken.Value))
{
long ticks;
if (long.TryParse(savedToken.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out 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
2015-11-21 03:13:03 +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();
}
}
private async Task<HttpResponseInfo> Post(HttpRequestOptions options,
bool enableRetry,
ListingsProviderInfo providerInfo)
2015-11-21 03:13:03 +00:00
{
try
{
return await _httpClient.Post(options).ConfigureAwait(false);
}
catch (HttpException ex)
{
_tokens.Clear();
if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
{
enableRetry = false;
}
if (!enableRetry) {
throw;
}
}
var newToken = await GetToken (providerInfo, options.CancellationToken).ConfigureAwait (false);
options.RequestHeaders ["token"] = newToken;
return await Post (options, false, providerInfo).ConfigureAwait (false);
2015-11-21 03:13:03 +00:00
}
private async Task<Stream> Get(HttpRequestOptions options,
bool enableRetry,
ListingsProviderInfo providerInfo)
2015-11-21 03:13:03 +00:00
{
try
{
return await _httpClient.Get(options).ConfigureAwait(false);
}
catch (HttpException ex)
{
_tokens.Clear();
if (!ex.StatusCode.HasValue || (int)ex.StatusCode.Value >= 500)
{
enableRetry = false;
}
if (!enableRetry) {
throw;
}
}
var newToken = await GetToken (providerInfo, options.CancellationToken).ConfigureAwait (false);
options.RequestHeaders ["token"] = newToken;
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.Info("Obtaining token from Schedules Direct from addres: " + httpOptions.Url + " with body " +
// httpOptions.RequestContent);
using (var responce = await Post(httpOptions, false, null).ConfigureAwait(false))
2015-07-23 05:25:55 +00:00
{
var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Token>(responce.Content);
if (root.message == "OK")
{
_logger.Info("Authenticated with Schedules Direct token: " + root.token);
return root.token;
}
throw new ApplicationException("Could not authenticate with Schedules Direct Error: " + root.message);
}
}
2015-07-23 17:04:54 +00:00
private async Task AddLineupToAccount(ListingsProviderInfo info, CancellationToken cancellationToken)
{
var token = await GetToken(info, cancellationToken);
if (string.IsNullOrWhiteSpace(token))
{
throw new ArgumentException("Authentication required.");
}
2015-07-24 21:44:25 +00:00
if (string.IsNullOrWhiteSpace(info.ListingsId))
{
throw new ArgumentException("Listings Id required");
}
2015-07-23 17:04:54 +00:00
_logger.Info("Adding new LineUp ");
var httpOptions = new HttpRequestOptions()
{
Url = ApiUrl + "/lineups/" + info.ListingsId,
UserAgent = UserAgent,
2015-10-11 16:12:53 +00:00
CancellationToken = cancellationToken,
LogErrorResponseBody = true
2015-07-23 17:04:54 +00:00
};
httpOptions.RequestHeaders["token"] = token;
2015-07-29 17:16:00 +00:00
2015-07-23 17:04:54 +00:00
using (var response = await _httpClient.SendAsync(httpOptions, "PUT"))
{
}
}
2015-07-23 05:25:55 +00:00
public string Name
{
get { return "Schedules Direct"; }
}
2015-07-23 13:23:22 +00:00
public string Type
{
get { return "SchedulesDirect"; }
}
2015-07-23 23:40:54 +00:00
private async Task<bool> HasLineup(ListingsProviderInfo info, CancellationToken cancellationToken)
{
2015-07-24 21:44:25 +00:00
if (string.IsNullOrWhiteSpace(info.ListingsId))
{
throw new ArgumentException("Listings Id required");
}
2015-07-23 23:40:54 +00:00
var token = await GetToken(info, cancellationToken);
2015-07-26 21:02:23 +00:00
if (string.IsNullOrWhiteSpace(token))
{
throw new Exception("token required");
}
2015-07-23 23:40:54 +00:00
_logger.Info("Headends on account ");
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
{
using (var response = await Get(options, false, null).ConfigureAwait(false))
2015-08-21 05:00:56 +00:00
{
var root = _jsonSerializer.DeserializeFromStream<ScheduleDirect.Lineups>(response);
return root.lineups.Any(i => string.Equals(info.ListingsId, i.lineup, StringComparison.OrdinalIgnoreCase));
}
}
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
{
2015-07-25 17:21:10 +00:00
if (string.IsNullOrWhiteSpace(info.Username))
{
throw new ArgumentException("Username is required");
}
if (string.IsNullOrWhiteSpace(info.Password))
{
throw new ArgumentException("Password is required");
}
2015-07-24 21:44:25 +00:00
}
2015-07-25 17:21:10 +00:00
if (validateListings)
{
if (string.IsNullOrWhiteSpace(info.ListingsId))
{
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);
}
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; }
}
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; }
}
//
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; }
public string showType { get; set; }
public bool hasImageArtwork { get; set; }
public string images { get; set; }
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
}
}
2015-09-21 16:23:20 +00:00
}