handling incorrect captcha and check not found cases

This commit is contained in:
leca 2024-11-28 00:28:37 +03:00
parent 47dfc19395
commit 4d658a817b
7 changed files with 81 additions and 13 deletions

View File

@ -56,6 +56,7 @@ else()
imageview/imageview.h imageview/imageview.cpp
image_redactor/imageredactor.h image_redactor/imageredactor.cpp
solvecaptchadialog.h solvecaptchadialog.cpp solvecaptchadialog.ui
exceptions/ofdrequestexception.h exceptions/ofdrequestexception.cpp
)

View File

@ -0,0 +1,6 @@
#include "ofdrequestexception.h"
OfdRequestException::OfdRequestException(const char* msg) : message(msg) {}
const char* OfdRequestException::what() throw() {
return message.c_str();
}

View File

@ -0,0 +1,16 @@
#ifndef OFDREQUESTEXCEPTION_H
#define OFDREQUESTEXCEPTION_H
#include <exception>
#include <string>
class OfdRequestException : public std::exception
{
private:
std::string message;
public:
OfdRequestException(const char* msg);
const char* what() throw();
};
#endif // OFDREQUESTEXCEPTION_H

View File

@ -1,6 +1,7 @@
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "check/check.h"
#include "exceptions/ofdrequestexception.h"
#include "goods/goods.h"
#include "outputdialog.h"
#include "adjustpicturedialog.h"
@ -63,9 +64,6 @@ std::string MainWindow::makeRequestToOfd(std::string captcha) {
void MainWindow::receiveSolvedCaptcha(std::string captcha) {
std::string check_content = makeRequestToOfd(captcha);
check = parseOfdRuAnswer(check_content);
}
void MainWindow::on_parseButton_clicked() {
@ -79,13 +77,46 @@ void MainWindow::on_parseButton_clicked() {
break;
case 2:
Net().get_captcha_from_ofdru();
SolveCaptchaDialog dialog = SolveCaptchaDialog(this);
connect(&dialog, &SolveCaptchaDialog::solvedCaptcha, this, &MainWindow::receiveSolvedCaptcha);
dialog.exec();
OutputDialog d = OutputDialog(this, check);
d.show();
d.exec();
std::string solved_captcha = "";
bool success = true;
bool is_captcha_solved = true;
do {
SolveCaptchaDialog dialog = SolveCaptchaDialog(this, &solved_captcha);
connect(&dialog, &SolveCaptchaDialog::solvedCaptcha, this, &MainWindow::receiveSolvedCaptcha);
dialog.exec();
is_captcha_solved = true;
try {
std::string check_content = makeRequestToOfd(solved_captcha);
check = parseOfdRuAnswer(check_content);
} catch(OfdRequestException e) {
success = false;
if (!strcmp(e.what(), "Incorrect captcha")) {
is_captcha_solved = false;
QMessageBox infoDialog;
infoDialog.setText("Captcha was not solved correctly!");
infoDialog.setIcon(QMessageBox::Critical);
infoDialog.setWindowTitle("Captcha is incorrect");
infoDialog.exec();
break;
} else if (!strcmp(e.what(), "Does not exist")) {
QMessageBox infoDialog;
infoDialog.setText("Check not found. Please, ensure correctness of entered data.");
infoDialog.setIcon(QMessageBox::Critical);
infoDialog.setWindowTitle("Check was not found");
infoDialog.exec();
return;
}
}
} while (!is_captcha_solved);
if (success) {
OutputDialog d = OutputDialog(this, check);
d.exec();
}
return;
}

View File

@ -4,9 +4,10 @@
#include <QMessageBox>
SolveCaptchaDialog::SolveCaptchaDialog(QWidget *parent) :
SolveCaptchaDialog::SolveCaptchaDialog(QWidget *parent, std::string* solved_captcha) :
QDialog(parent),
ui(new Ui::SolveCaptchaDialog) {
ui(new Ui::SolveCaptchaDialog),
solved_captcha(solved_captcha) {
ui->setupUi(this);
QString captcha_path = QString::fromStdString(get_path_relative_to_home(".local/share/checks_parser/captcha.png"));
@ -23,7 +24,9 @@ void SolveCaptchaDialog::accept() {
infoDialog.setWindowTitle("No captcha");
infoDialog.exec();
} else {
emit solvedCaptcha(userInput);
solved_captcha->erase();
solved_captcha->append(userInput);
// emit solvedCaptcha(userInput);
QDialog::accept();
}
}

View File

@ -12,7 +12,7 @@ class SolveCaptchaDialog : public QDialog
Q_OBJECT
public:
explicit SolveCaptchaDialog(QWidget *parent = nullptr);
explicit SolveCaptchaDialog(QWidget *parent = nullptr, std::string* = nullptr) ;
~SolveCaptchaDialog();
@ -22,6 +22,7 @@ signals:
private:
Ui::SolveCaptchaDialog *ui;
std::string* solved_captcha;
private slots:
void accept() override;

View File

@ -6,6 +6,7 @@
#include <locale>
#include <regex>
#include <string>
#include "../exceptions/ofdrequestexception.h"
std::string to_utf8(std::wstring wide_string) {
static std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
@ -120,6 +121,15 @@ Check parseOfdRuAnswer(std::string html) {
std::vector<std::wstring> amounts = find_amounts_in_html(trimmed);
std::vector<std::wstring> prices = find_prices_in_html(trimmed);
if ((products.size() + amounts.size() + prices.size()) == 0) {
if (html == "Bad Request4") { // Failed to solve a captcha
throw OfdRequestException("Incorrect captcha");
} else { // Most likely that the check does not exist
throw OfdRequestException("Does not exist");
}
return Check();
}
if ((products.size() + amounts.size() + prices.size())/products.size() != 3) {
std::cerr << "An error has occured during the parsing of html. Please, contact the developer." << std::endl;
std::exit(-1);