private void AddToChannelPairCache(string listingsId, string channelNumber, ScheduleDirect.Station schChannel) { lock (_channelCacheLock) { Dictionary <string, ScheduleDirect.Station> cache; if (_channelPairingCache.TryGetValue(listingsId, out cache)) { cache[channelNumber] = schChannel; } else { cache = new Dictionary <string, ScheduleDirect.Station>(); cache[channelNumber] = schChannel; _channelPairingCache[listingsId] = cache; } } }
public async Task <List <ChannelInfo> > GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken) { var listingsId = info.ListingsId; if (string.IsNullOrWhiteSpace(listingsId)) { throw new Exception("ListingsId required"); } var token = await GetToken(info, cancellationToken); if (string.IsNullOrWhiteSpace(token)) { throw new Exception("token required"); } var httpOptions = new HttpRequestOptions() { Url = ApiUrl + "/lineups/" + listingsId, UserAgent = UserAgent, CancellationToken = cancellationToken, LogErrorResponseBody = true, // The data can be large so give it some extra time TimeoutMs = 60000 }; httpOptions.RequestHeaders["token"] = token; var list = new List <ChannelInfo>(); using (var response = await Get(httpOptions, true, info).ConfigureAwait(false)) { 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"); var allStations = root.stations ?? new List <ScheduleDirect.Station>(); foreach (ScheduleDirect.Map map in root.map) { var channelNumber = GetChannelNumber(map); var station = allStations.FirstOrDefault(item => string.Equals(item.stationID, map.stationID, StringComparison.OrdinalIgnoreCase)); if (station == null) { station = new ScheduleDirect.Station { stationID = map.stationID }; } var name = channelNumber; var channelInfo = new ChannelInfo { Number = channelNumber, Name = name }; if (station != null) { if (!string.IsNullOrWhiteSpace(station.name)) { channelInfo.Name = station.name; } channelInfo.Id = station.stationID; channelInfo.CallSign = station.callsign; if (station.logo != null) { channelInfo.ImageUrl = station.logo.URL; channelInfo.HasImage = true; } } list.Add(channelInfo); } } return(list); }
public async Task <List <ChannelInfo> > GetChannels(ListingsProviderInfo info, CancellationToken cancellationToken) { var listingsId = info.ListingsId; if (string.IsNullOrEmpty(listingsId)) { throw new Exception("ListingsId required"); } var token = await GetToken(info, cancellationToken).ConfigureAwait(false); if (string.IsNullOrEmpty(token)) { throw new Exception("token required"); } using var options = new HttpRequestMessage(HttpMethod.Get, ApiUrl + "/lineups/" + listingsId); options.Headers.TryAddWithoutValidation("token", token); var list = new List <ChannelInfo>(); using var httpResponse = await Send(options, true, info, cancellationToken).ConfigureAwait(false); await using var stream = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false); var root = await _jsonSerializer.DeserializeFromStreamAsync <ScheduleDirect.Channel>(stream).ConfigureAwait(false); _logger.LogInformation("Found {ChannelCount} channels on the lineup on ScheduleDirect", root.map.Count); _logger.LogInformation("Mapping Stations to Channel"); var allStations = root.stations ?? Enumerable.Empty <ScheduleDirect.Station>(); foreach (ScheduleDirect.Map map in root.map) { var channelNumber = GetChannelNumber(map); var station = allStations.FirstOrDefault(item => string.Equals(item.stationID, map.stationID, StringComparison.OrdinalIgnoreCase)); if (station == null) { station = new ScheduleDirect.Station { stationID = map.stationID }; } var channelInfo = new ChannelInfo { Id = station.stationID, CallSign = station.callsign, Number = channelNumber, Name = string.IsNullOrWhiteSpace(station.name) ? channelNumber : station.name }; if (station.logo != null) { channelInfo.ImageUrl = station.logo.URL; } list.Add(channelInfo); } return(list); }
public async Task <IEnumerable <ProgramInfo> > GetProgramsAsync(ListingsProviderInfo info, string channelNumber, string channelName, DateTime startDateUtc, DateTime endDateUtc, CancellationToken cancellationToken) { List <ProgramInfo> programsInfo = new List <ProgramInfo>(); var token = await GetToken(info, cancellationToken).ConfigureAwait(false); if (string.IsNullOrWhiteSpace(token)) { return(programsInfo); } if (string.IsNullOrWhiteSpace(info.ListingsId)) { return(programsInfo); } var httpOptions = new HttpRequestOptions() { Url = ApiUrl + "/schedules", UserAgent = UserAgent, CancellationToken = cancellationToken, // The data can be large so give it some extra time TimeoutMs = 60000, LogErrorResponseBody = true }; httpOptions.RequestHeaders["token"] = token; var dates = GetScheduleRequestDates(startDateUtc, endDateUtc); ScheduleDirect.Station station = GetStation(channelNumber, channelName); if (station == null) { _logger.Info("No Schedules Direct Station found for channel {0} with name {1}", channelNumber, channelName); return(programsInfo); } 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)) { StreamReader reader = new StreamReader(response.Content); string responseString = reader.ReadToEnd(); var dailySchedules = _jsonSerializer.DeserializeFromString <List <ScheduleDirect.Day> >(responseString); _logger.Debug("Found " + dailySchedules.Count() + " programs on " + channelNumber + " ScheduleDirect"); httpOptions = new HttpRequestOptions() { Url = ApiUrl + "/programs", UserAgent = UserAgent, CancellationToken = cancellationToken, LogErrorResponseBody = true, // The data can be large so give it some extra time TimeoutMs = 60000 }; 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)) { StreamReader innerReader = new StreamReader(innerResponse.Content); responseString = innerReader.ReadToEnd(); 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); var schedules = dailySchedules.SelectMany(d => d.programs); foreach (ScheduleDirect.Program schedule in schedules) { //_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); if (images != null) { var imageIndex = images.FindIndex(i => i.programID == schedule.programID.Substring(0, 10)); if (imageIndex > -1) { programDict[schedule.programID].images = GetProgramLogo(ApiUrl, images[imageIndex]); } } programsInfo.Add(GetProgram(channelNumber, schedule, programDict[schedule.programID])); } _logger.Info("Finished with EPGData"); } } return(programsInfo); }