public bool Fetch() { List <string> missingParameters = ParameterRequired.GetMissingParameters(m_parameters); if (missingParameters.Count > 0) { this.Response = new DTO.ResponseError(HttpStatusCode.BadRequest) { friendly_error = "The bank could not be contacted because of missing information.", detailed_error = string.Concat("Missing parameter(s): ", string.Join(", ", missingParameters)) }; } else { try { Directory.CreateDirectory(m_debugWorkingPath); if (!File.Exists(m_scriptPath)) { throw new NotSupportedException(string.Format("FID:{0} not supported", m_parameters.FID)); } //Will execute command from /OpenBank/OpenBank/casperjs directory in format: // phantomjs /home/bholt/OpenBank/OpenBank/casperjs/scripts/3000_accounts.js ~/dev/OpenBank/OpenBank/casperjs --user_id="bank_username" ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = ConfigurationManager.AppSettings ["phantomjs_path"]; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.WorkingDirectory = m_casperJsPath; startInfo.Arguments = "--ssl-protocol=any"; startInfo.Arguments += string.Format(" {0} {1}", m_scriptPath, m_casperJsPath); startInfo.Arguments += string.Format(" --user_id=\"{0}\"", m_parameters.UserID); startInfo.Arguments += string.Format(" --password=\"{0}\"", m_parameters.Password); startInfo.Arguments += string.Format(" --security_answers=\"{0}\"", m_parameters.SecurityAnswers); startInfo.Arguments += string.Format(" --output_path=\"{0}\"", m_debugWorkingPath); // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = new Process()) { exeProcess.StartInfo = startInfo; exeProcess.OutputDataReceived += new DataReceivedEventHandler(exeProcess_OutputDataReceived); exeProcess.ErrorDataReceived += new DataReceivedEventHandler(exeProcess_ErrorDataReceived); PrepScrape(exeProcess); m_outputData += string.Format("[START] {0} {1}{2}", startInfo.FileName, startInfo.Arguments, Environment.NewLine); exeProcess.Start(); exeProcess.BeginOutputReadLine(); exeProcess.BeginErrorReadLine(); exeProcess.WaitForExit(MAX_WAIT_MILLISECONDS); } if (File.Exists(Path.Combine(m_debugWorkingPath, "challenge_question.txt"))) { string challengeQuestion = File.ReadAllText(Path.Combine(m_debugWorkingPath, "challenge_question.txt")); this.Response = new DTO.ResponseError(HttpStatusCode.BadRequest) { friendly_error = string.Concat("The following security question was asked: ", challengeQuestion), detailed_error = challengeQuestion, is_security_question_asked = true }; } else { this.Response = ProcessScrape(m_debugWorkingPath, m_workingID); } } catch (NotSupportedException nex) { this.Response = new DTO.ResponseError(HttpStatusCode.BadRequest) { friendly_error = "Bank is not supported.", detailed_error = nex.Message }; } catch (System.ComponentModel.Win32Exception) { this.Response = new DTO.ResponseError(HttpStatusCode.InternalServerError) { friendly_error = "An error occured when attempting to connect to the bank.", detailed_error = "executable could not be found" }; } catch (Exception ex) { this.Response = new DTO.ResponseError(HttpStatusCode.InternalServerError) { friendly_error = "An error occured when processing the response from the bank.", detailed_error = ex.Message }; } } bool isError = (this.Response is DTO.ResponseError); if (isError || ConfigurationManager.AppSettings ["keep_scrape_debug_output"] == "true") { File.WriteAllText(Path.Combine(m_debugWorkingPath, "error.log"), m_errorData); File.WriteAllText(Path.Combine(m_debugWorkingPath, "output.log"), m_outputData); } else { Directory.Delete(m_debugWorkingPath, true); } return(isError); }
public bool Fetch() { List <string> missingParameters = ParameterRequired.GetMissingParameters(m_parameters); if (missingParameters.Count > 0) { this.Response = new DTO.ResponseError(HttpStatusCode.BadRequest) { friendly_error = "The bank could not be contacted because of missing information.", detailed_error = string.Concat("Missing parameter(s): ", string.Join(", ", missingParameters)) }; } else { try { Directory.CreateDirectory(m_debugWorkingPath); //request body this.OfxRequestBody = this.BuildOfxRequest(); File.WriteAllText(Path.Combine(m_debugWorkingPath, "request.ofx"), this.OfxRequestBody); //get raw ofx response this.OfxResponseBody = SendRequestAndGetResponse(this.OfxRequestBody, m_parameters.OFX_URL); File.WriteAllText(Path.Combine(m_debugWorkingPath, "response.ofx"), this.OfxResponseBody); //parse ofx to xml Parse.OfxToXmlParser parser = new Parse.OfxToXmlParser(this.OfxResponseBody); this.ParsedOfx = parser.Parse(); //build response this.Response = this.BuildResponse(this.ParsedOfx); } catch (System.UriFormatException uriEx) { this.Response = new DTO.ResponseError(HttpStatusCode.BadRequest) { friendly_error = "The bank could not be contacted.", detailed_error = uriEx.Message }; } catch (WebException e) { using (WebResponse response = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data)) { this.OfxResponseBody = reader.ReadToEnd(); this.Response = new DTO.ResponseError((HttpStatusCode)((int)httpResponse.StatusCode)) { friendly_error = "An error occured when communicating with the bank.", detailed_error = this.OfxResponseBody }; } } } catch (Parse.OfxStatusException statusEx) { this.Response = new DTO.ResponseError(HttpStatusCode.BadRequest) { friendly_error = string.Concat("Bank response: " + statusEx.Message), detailed_error = string.Format("{0} ({1})", statusEx.Message, statusEx.Code) }; } catch (Parse.OfxParseException pex) { this.Response = new DTO.ResponseError(HttpStatusCode.InternalServerError) { friendly_error = "An error occured when reading the response from the bank.", detailed_error = pex.Message }; } catch (Exception ex) { this.Response = new DTO.ResponseError(HttpStatusCode.InternalServerError) { friendly_error = "An error occured when processing the response from the bank.", detailed_error = ex.Message }; } } bool isError = (this.Response is DTO.ResponseError); if (!isError) { if (ConfigurationManager.AppSettings["keep_scrape_debug_output"] == "false") { Directory.Delete(m_debugWorkingPath, true); } } return(isError); }