private List <Channel> ConvertKpnDataToChannels(List <Models.Kpn.Program> programs, List <Schedule> schedules) { var channels = new List <Channel>(); foreach (var schedule in schedules) { try { var matchingProgram = programs.FirstOrDefault(p => p.Id == schedule.ProgramId); if (matchingProgram == null) { _logger.Warn($"Missing program for schedule with program id {schedule.ProgramId}"); continue; } var channel = channels.FirstOrDefault(c => c.Name == schedule.ChannelId); if (channel == null) { _logger.Info($"Create channel {schedule.ChannelId}"); channel = new Channel() { Name = schedule.ChannelId, Programs = new List <Models.Program>() }; channels.Add(channel); } var program = new Models.Program(); program.Name = matchingProgram.ProgramName; program.Description = matchingProgram.Description; program.Id = matchingProgram.Id; var end = long.Parse(schedule.StartTime) + int.Parse(schedule.Duration) * 60; program.SetStart(schedule.StartTime); program.SetEnd(end.ToString()); program.Genres = new List <Genre>(); if (!string.IsNullOrWhiteSpace(matchingProgram.Genre1)) { program.Genres.Add(new Genre { Name = matchingProgram.Genre1, Language = "NL" }); } if (!string.IsNullOrWhiteSpace(matchingProgram.Genre2)) { program.Genres.Add(new Genre { Name = matchingProgram.Genre2, Language = "NL" }); } channel.Programs.Add(program); } catch (Exception ex) { _logger.Error($"Failed to convert schedule item with ID {schedule.Id} and ProgramId {schedule.ProgramId}: {ex.Message}"); } } _logger.Info($"Converted data into {channels.Count} channels with {channels.Sum(c => c.Programs.Count)} programs"); return(channels); }
List <Channel> ParseEpgData(string data) { List <Channel> result = new List <Channel>(); var converter = new ExpandoObjectConverter(); dynamic json = JsonConvert.DeserializeObject <ExpandoObject>(data, converter); if (json == null) { return(result); } foreach (var channelName in json) { var channel = result.FirstOrDefault(c => c.Name.Equals((string)channelName.Key, StringComparison.InvariantCultureIgnoreCase)); if (channel == null) { channel = new Channel { Programs = new List <Models.Program>() }; result.Add(channel); channel.Name = (string)channelName.Key; } //Add programms foreach (var program in channelName.Value) { var prog = new Models.Program(); foreach (var programProperty in program) { string key = programProperty.Key?.ToString(); string value = programProperty.Value?.ToString(); if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(value)) { continue; } switch (key.ToLower()) { case "id": prog.Id = value; break; case "name": prog.Name = value.DecodeNonAsciiCharacters(); break; case "start": prog.SetStart(value); break; case "end": prog.SetEnd(value); break; case "disablerestart": //Ignore break; default: //I'm curious which other data is present prog.OtherData = $"{prog.OtherData}{key}={value};"; break; } } //Add the program when it does not exist yet if (!channel.Programs.Any(p => p.Start == prog.Start && p.End == prog.End)) { channel.Programs.Add(prog); } } } return(result); }