private static void addProgramsToDatabase() { using (var db = new SchedulesDirectContext()) { List <string> allProgramIDs = new List <string>(); foreach (Program p in db.program) { allProgramIDs.Add(p.programID); } var programIDs = new List <string>(allProgramIDs.Distinct()); string programString; List <ProgramInfo> programInfo = new List <ProgramInfo>(); //Can only request 5000 programs at once so cycle through sending in chunks for (int i = 0; i < programIDs.Count(); i = i + 5000) { if (programIDs.Count() - i > 5000) { programString = JsonConvert.SerializeObject(programIDs.GetRange(i, 5000)); } else { programString = JsonConvert.SerializeObject(programIDs.GetRange(i, programIDs.Count() - i - 1)); } var programDataString = client.PostAsync("programs", new StringContent(programString)).Result.Content.ReadAsStringAsync().Result; programInfo.AddRange((ProgramInfo[])Newtonsoft.Json.JsonConvert.DeserializeObject(programDataString, typeof(ProgramInfo[]))); } // Go through and add each genre associated with each added program List <Genre> genreList = new List <Genre>(); for (int i = 0; i < programInfo.Count(); i++) { if (programInfo[i].genres != null) { programInfo[i].programGenres = new List <ProgramGenre>(); foreach (string s in programInfo[i].genres) { var foundGenre = from g in genreList where g.genre == s select g; Genre currentGenre; if (foundGenre.Count() == 0) { currentGenre = new Genre { genre = s }; genreList.Add(currentGenre); } else { currentGenre = foundGenre.First(); } programInfo[i].programGenres.Add(new ProgramGenre { genre = currentGenre }); } } } db.AddRange(programInfo); db.SaveChanges(); } }
private static void addSchedulesToDatabase() { using (var db = new SchedulesDirectContext()) { dynamic scheduleResponse = JArray.Parse(client.PostAsync("schedules", new StringContent(getStationIDs())).Result.Content.ReadAsStringAsync().Result); Schedule[] schedules = (Schedule[])scheduleResponse.ToObject(typeof(Schedule[])); foreach (Schedule s in schedules) { if (s.programs == null) // once in a while we get schedule with no programs { break; } foreach (Program p in s.programs) { if (p.audioProperties != null && p.audioProperties.Contains <String>("dvs")) { p.dvs = true; } } } db.AddRange(schedules); db.SaveChanges(); } }