public static void Main(string[] args) { //Get user info from ini file User u; try { u = getUserInfo(); } catch (FileNotFoundException) { Console.WriteLine("Configuration file not found, please add config.ini to the directory containing program executable"); return; } catch (FieldAccessException) { return; } //Delete database if exists and recreate since sqlite migrations are not good using (var context = new SchedulesDirectContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); } initializeHttpClient(u); addStations(u); addSchedulesToDatabase(); addProgramsToDatabase(); using (var db = new SchedulesDirectContext()) { db.Database.ExecuteSqlCommand("vacuum"); } }
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(); } }
//Get stations in JSON format for use in other request private static String getStationIDs() { StringContent stationsString; using (var db = new SchedulesDirectContext()) { List <dynamic> stations = new List <dynamic>(); foreach (Station s in db.station) { dynamic currentStation = new ExpandoObject(); currentStation.stationID = s.stationID; stations.Add(currentStation); } stationsString = new StringContent(JsonConvert.SerializeObject(stations)); } return(stationsString.ReadAsStringAsync().Result); }
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(); } }
private static void addStations(User u) { using (var db = new SchedulesDirectContext()) { var lineups = getUserLineups(u); if (lineups == null) { throw new Exception(); } foreach (Lineup l in lineups) { foreach (Map m in getMapping(l)) { db.map.Add(m); db.SaveChanges(); } foreach (Station s in getStations(l)) { db.station.Add(s); } db.SaveChanges(); } } }