private void CreateTables() { try { ExecuteSQLCommand(string.Format( @"CREATE TABLE IF NOT EXISTS [{0}] ([{1}] VARCHAR(15) NOT NULL PRIMARY KEY, [{2}] TEXT NULL, [{3}] TEXT NULL, [{4}] VARCHAR(30) NULL);", kProgramsTableName, kProgramIdColumnName, kProgramDataColumnName, kProgramRawResponseColumnName, kMD5ColumnName)); ExecuteSQLCommand(string.Format( @"CREATE TABLE IF NOT EXISTS [{0}] ([{1}] VARCHAR(15) NOT NULL, [{2}] VARCHAR(11) NOT NULL, [{3}] TEXT NOT NULL, [{4}] VARCHAR(30) NOT NULL, PRIMARY KEY ({1}, {2}));", kSchedulesTableName, kStationIDColumnName, kDayColumnName, kScheduleEntriesColumnName, kMD5ColumnName)); ExecuteSQLCommand(string.Format( @"CREATE TABLE IF NOT EXISTS[{0}] ([{1}] VARCHAR (11) NOT NULL PRIMARY KEY, [{2}] TEXT NOT NULL);", kImagesTableName, kImageIdColumnName, kImageDataColumnName)); ExecuteSQLCommand(string.Format( @"CREATE TABLE IF NOT EXISTS[{0}] ([{1}] VARCHAR (20) NOT NULL PRIMARY KEY, [{2}] TEXT NOT NULL);", kSeriesInfoTableName, kSeriesIdColumnName, kSeriesDataColumnName)); } catch (Exception e) { Misc.OutputException(e); } }
public static HttpWebResponse GetHttpResponse(string url, object json_request, string token = null, string command = null) { HttpWebRequest http_request = BuildRequest(url, json_request, token, command); HttpWebResponse response = null; try { response = (HttpWebResponse)http_request.GetResponse(); } catch (WebException we) { Misc.OutputException(we); Console.WriteLine("WebException response: " + we.Response); if (we.Response is HttpWebResponse) { HttpWebResponse hrw = we.Response as HttpWebResponse; Console.WriteLine("Status Code: " + hrw.StatusCode); } throw new Exception("WebException returned from JSON request, response:" + we.Response, we); } catch (Exception e) { Misc.OutputException(e); throw new Exception("Unexpected error returned from JSON request", e); } return(response); }
private ConfigManager(string config_path) { this.config_path_ = config_path; config_ = new SDGrabberConfig(); try { using (Stream input_stream = File.OpenRead(config_path_)) { using (StreamReader reader = new StreamReader(input_stream)) { config_ = JSONClient.Deserialize <SDGrabberConfig>(reader.ReadToEnd()); } } if (config_.lineups == null) { config_.lineups = new List <LineupConfig>(); } if (config_.scanned_lineups == null) { config_.scanned_lineups = new List <ScannedLineupConfig>(); } config_.ValidateScannedLineupConfigs(); } catch (Exception ex) { Misc.OutputException(ex); } }
private List <DBProgram> DownloadPrograms(IEnumerable <string> programIDs) { const int kBatchSize = 5000; List <string> currentBatch = new List <string>(); List <DBProgram> fetchedPrograms = new List <DBProgram>(); Func <int> FetchCurrentBatch = new Func <int>(() => { Console.WriteLine("Downloading program info from SchedulesDirect"); List <object> response = JSONClient.GetJSONResponse <List <object> >( UrlBuilder.BuildWithAPIPrefix("/programs"), currentBatch, SDTokenManager.token_manager.token); List <DBProgram> programsThisBatch = new List <DBProgram>(); // Only include programs fetched successfully, so any errors do not replace good data. List <SDProgram> successfulResponses = new List <SDProgram>(); foreach (object genericJson in response) { SDProgram sdProgram = null; try { sdProgram = JSONClient.Deserialize <SDProgram>(genericJson.ToString()); } catch (Exception exc) { Console.WriteLine("Failed to deserialize program JSON: {0}", genericJson); Misc.OutputException(exc); continue; } if (sdProgram.code > 0) { Console.WriteLine("Failed to download updated program info for programID {0}, response {1}, message: {2}", sdProgram.programID, sdProgram.response, sdProgram.message); continue; } successfulResponses.Add(sdProgram); var dbProgram = new DBProgram(sdProgram); fetchedPrograms.Add(dbProgram); programsThisBatch.Add(dbProgram); } DBManager.instance.SaveRawSDProgramResponses(successfulResponses); DBManager.instance.SaveProgramData(programsThisBatch); currentBatch.Clear(); return(0); }); foreach (string id in programIDs) { currentBatch.Add(id); if (currentBatch.Count >= kBatchSize) { FetchCurrentBatch(); } } if (currentBatch.Count > 0) { FetchCurrentBatch(); } return(fetchedPrograms); }
// returns # of rows affected private int ExecuteSQLCommand(string sql) { try { using (SQLiteCommand command = CreateSQLCommand(sql)) return(command.ExecuteNonQuery()); } catch (Exception e) { Misc.OutputException(e); throw; } }
private void SubscribeButton_Click(object sender, EventArgs e) { try { SDAccountManagement.AddLineupToAccount((LineupSearchResultListBox.SelectedItem as SDLineup).lineup); UpdateSubscribedLineups(); } catch (Exception ex) { Misc.OutputException(ex); MessageBox.Show("Failed to add lineup."); } }
private void UnsubscribeButton_Click(object sender, EventArgs e) { try { SDAccountManagement.RemoveLineupFromAccount(selected_available_lineup.lineup); UpdateSubscribedLineups(); } catch (Exception ex) { Misc.OutputException(ex); MessageBox.Show("Failed to remove lineup."); } }
private IDictionary <string, Dictionary <string, SDStationMD5Response> > GetStationMD5Responses(IEnumerable <string> stationIDs) { // TODO: batch by 5000 const int kBatchSize = 5000; List <Dictionary <string, string> > request = new List <Dictionary <string, string> >(); Dictionary <string, Dictionary <string, SDStationMD5Response> > stationMD5Responses = new Dictionary <string, Dictionary <string, SDStationMD5Response> >(); Func <int> DoBatch = new Func <int>(() => { //var batchResponse = JSONClient.GetJSONResponse<Dictionary<string, Dictionary<string, SDStationMD5Response>>>( var batchResponse = JSONClient.GetJSONResponse <Dictionary <string, object> >( UrlBuilder.BuildWithAPIPrefix("/schedules/md5"), request, SDTokenManager.token_manager.token); foreach (var keyval in batchResponse) { string stationID = keyval.Key; try { Dictionary <string, SDStationMD5Response> dailyResponses = JSONClient.Deserialize <Dictionary <string, SDStationMD5Response> >(keyval.Value.ToString()); stationMD5Responses[stationID] = dailyResponses; } catch (Exception exc) { Console.WriteLine("Failed to deserialize schedule MD5s for station {0}, JSON: {1}", stationID, keyval.Value); Misc.OutputException(exc); } } request.Clear(); return(0); }); foreach (string stationID in stationIDs) { request.Add(new Dictionary <string, string>() { { "stationID", stationID } }); if (request.Count >= kBatchSize) { DoBatch(); } } if (request.Count > 0) { DoBatch(); } return(stationMD5Responses); }
public static T GetJSONResponse <T>(string url, object json_request = null, string token = null, string command = null, string output_debug_file = null) where T : new() { HttpWebResponse response = GetHttpResponse(url, json_request, token, command); using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string response_string = reader.ReadToEnd(); if (output_debug_file != null) { File.WriteAllText(output_debug_file, response_string); } try { return(JsonConvert.DeserializeObject <T>(response_string, deserializer_settings)); } catch (Exception ex) { Console.WriteLine("Failed to parse JSON response: " + response_string); Misc.OutputException(ex); throw; } } }