added ofd scene, working http server

This commit is contained in:
leca 2025-03-14 00:44:14 +03:00
parent b305fba2fd
commit 33b54fb475
12 changed files with 390 additions and 59 deletions

View File

@ -4,6 +4,10 @@ project(checks-parser VERSION 0.1 LANGUAGES CXX)
option(BUILD_TRANSLATIONS "Build translations?" ON)
include(FetchContent)
SET(CMAKE_BUILD_TYPE Debug)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC OFF)
@ -40,7 +44,9 @@ set(PROJECT_SOURCES
ocrscene.h ocrscene.cpp scenes/ocrscene.ui
ofdscene.h ofdscene.cpp scenes/ofdscene.ui
outputdialog.h outputdialog.cpp scenes/outputdialog.ui
# adjustpicturedialog.h adjustpicturedialog.cpp scenes/adjustpicturedialog.ui
adjustpicturedialog.h adjustpicturedialog.cpp scenes/adjustpicturedialog.ui
image_redactor/imageredactor.h image_redactor/imageredactor.cpp
solvecaptchadialog.h solvecaptchadialog.cpp scenes/solvecaptchadialog.ui
)
set(TRANSLATION_SOURCES
@ -50,7 +56,8 @@ set(TRANSLATION_SOURCES
ocrscene.cpp ocrscene.h scenes/ocrscene.ui
ofdscene.cpp ofdscene.h scenes/ofdscene.ui
outputdialog.h outputdialog.cpp scenes/outputdialog.ui
# adjustpicturedialog.h adjustpicturedialog.cpp scenes/adjustpicturedialog.ui
adjustpicturedialog.h adjustpicturedialog.cpp scenes/adjustpicturedialog.ui
solvecaptchadialog.h solvecaptchadialog.cpp scenes/solvecaptchadialog.ui
)
set(TS_FILES
@ -126,14 +133,23 @@ if(WIN32)
set(OpenCV_DIR /usr/local/lib/cmake/opencv4)
endif()
FetchContent_Declare(httplib SYSTEM
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib
GIT_TAG 2eaa2ea64f9fb12773306534d461d9ed63cb76b6 # v0.14.1
GIT_SHALLOW TRUE)
FetchContent_MakeAvailable(httplib)
find_package(OpenCV REQUIRED COMPONENTS core imgproc imgcodecs)
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_include_directories(checks-parser PUBLIC ${OpenCV_INCLUDE_DIRS})
target_link_libraries(checks-parser PRIVATE -lzbar)
target_link_libraries(checks-parser PRIVATE -ltesseract)
target_link_libraries(checks-parser PRIVATE -lcurl)
target_link_libraries(checks-parser PRIVATE ${OpenCV_LIBS} )
target_link_libraries(checks-parser PRIVATE ${OpenCV_LIBS})
target_link_libraries(checks-parser PRIVATE httplib)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8)
target_link_libraries(checks-parser PRIVATE -lstdc++fs)
endif()

View File

@ -1,7 +1,6 @@
#include <opencv2/imgcodecs.hpp>
#include <string>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <opencv2/opencv.hpp>
#include "checkimage.h"
CheckImage::CheckImage(std::string path) {

View File

@ -21,7 +21,6 @@
#include <ocrscene.h>
#include <ofdscene.h>
#include <qpushbutton.h>
#include <parser/parser.h>
static QWidget *loadUI(QWidget *parent, std::string filename) {
@ -61,25 +60,18 @@ int main(int argc, char *argv[]) {
});
QObject::connect(ofd_button, &QPushButton::clicked, [&]() {
// OCR scene
// OFD scene
sceneLayout->setCurrentIndex(3);
sceneLayout->widget(3)->show();
});
// // Text from email setup
// QWidget *emailtextscene = loadUI(window, ":/scenes/scenes/emailtextscene.ui");
// emailtextscene->show();
//OCR scene
// QWidget *ocrscene = loadUI(window, ":/scenes/scenes/ocrscene.ui");
//OFD scene
// QWidget *ofdscene = loadUI(window, ":/scenes/scenes/ofdscene.ui");
EmailTextScene *emailTextScene = new EmailTextScene();
OCRScene *ocrscene = new OCRScene();
OFDScene *ofdscene = new OFDScene();
ofdscene->startHttpServer();
// get_local_ip_address();
sceneLayout->addWidget(mainwindowscene);
sceneLayout->addWidget(emailTextScene);

View File

@ -1,5 +1,17 @@
#include "ofdscene.h"
#include "ui_ofdscene.h"
#include "utils/utils.h"
#include <QFileDialog>
#include <QMessageBox>
#include <adjustpicturedialog.h>
#include <httplib.h>
#include <outputdialog.h>
#include <solvecaptchadialog.h>
#include <net/net.h>
#include <exceptions/ofdrequestexception.h>
OFDScene::OFDScene(QWidget *parent)
: QWidget(parent)
@ -10,3 +22,129 @@ OFDScene::OFDScene(QWidget *parent)
OFDScene::~OFDScene() {
delete ui;
}
void OFDScene::startHttpServer() {
std::string localIp = "";
try {
localIp = get_local_ip_address();
} catch(std::exception e) {
std::cerr << e.what() << std::endl;
return;
}
httplib::Server svr;
//TODO: generate random port from 1024 to 65535 and check if its used.
svr.Get("/", [&](const httplib::Request &, httplib::Response &res){
res.set_redirect("http://"+ localIp +":8080/", 301);
});
svr.listen("0.0.0.0", 8080);
}
void OFDScene::on_choose_image_button_clicked() {
QString filename = QFileDialog::getOpenFileName();
if (filename == "") {
QMessageBox infoDialog;
infoDialog.setText(tr("Please, select a picture where QR code that contains info about check is present"));
infoDialog.setIcon(QMessageBox::Critical);
infoDialog.setWindowTitle(tr("Picture was not selected"));
infoDialog.exec();
return;
}
ui->info_label->setText(tr("Selected image: ") + filename);
AdjustPictureDialog dialog = AdjustPictureDialog(this, filename.toStdString());
connect(&dialog, &AdjustPictureDialog::decodedData, this, &OFDScene::onDataDecode);
dialog.exec();
}
void OFDScene::onDataDecode(std::string data) {
std::vector<std::string> dataSplit = split(data, "&");
ui->fn_line_edit->setText(QString::fromStdString(split(dataSplit[2], "=")[1]));
ui->fd_line_edit->setText(QString::fromStdString(split(dataSplit[3], "=")[1]));
ui->fi_line_edit->setText(QString::fromStdString(split(dataSplit[4], "=")[1]));
QString extractedDateTime = QString::fromStdString(split(dataSplit[0], "=")[1]);
//TODO: some QRs contain datetime in format yyyyMMddThhmmss. Perhaps there is more different formats, should write function to detect them.
QDateTime datetime = QDateTime::fromString(extractedDateTime, "yyyyMMddThhmm");
ui->purchase_datetime_edit->setDateTime(datetime);
int type = std::stoi(split(dataSplit[5], "=")[1]);
ui->operation_type_combo_box->setCurrentIndex(type - 1);
std::string total = split(dataSplit[1], "=")[1];
ui->total_spin_box->setValue(std::stod(total));
}
void OFDScene::on_parse_button_clicked() {
Net net;
net.get_captcha_from_ofdru();
std::string solved_captcha = "";
bool success = true;
bool is_captcha_solved = true;
Check check;
do {
SolveCaptchaDialog dialog = SolveCaptchaDialog(this, &solved_captcha);
dialog.exec();
is_captcha_solved = true;
try {
std::string check_content = net.fetch_check_data_from_ofdru(
ui->fn_line_edit->text().toStdString(),
ui->fd_line_edit->text().toStdString(),
ui->fi_line_edit->text().toStdString(),
ui->purchase_datetime_edit->dateTime().toString(Qt::ISODate).toStdString(),
ui->operation_type_combo_box->currentIndex() + 1,
// In the request to ofd.ru, total is in a format with 2 last digits represent decimal part of a number.
ui->total_spin_box->text().toDouble() * 100,
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(tr("Captcha was not solved correctly!"));
infoDialog.setIcon(QMessageBox::Critical);
infoDialog.setWindowTitle(tr("Captcha is incorrect"));
infoDialog.exec();
break;
} else if (!strcmp(e.what(), "Internal server error")) {
QMessageBox infoDialog;
infoDialog.setText(tr("Internal server error. Please, try again later."));
infoDialog.setIcon(QMessageBox::Critical);
infoDialog.setWindowTitle(tr("Internal server error"));
infoDialog.exec();
return;
} else if (!strcmp(e.what(), "Does not exist")) {
QMessageBox infoDialog;
infoDialog.setText(tr("Check not found. Please, ensure correctness of entered data."));
infoDialog.setIcon(QMessageBox::Critical);
infoDialog.setWindowTitle(tr("Check was not found"));
infoDialog.exec();
return;
}
}
} while (!is_captcha_solved);
if (success) {
OutputDialog d = OutputDialog(this, check);
d.exec();
}
}
void OFDScene::on_binary_eye_button_clicked() {
}

View File

@ -14,6 +14,14 @@ class OFDScene : public QWidget
public:
explicit OFDScene(QWidget *parent = nullptr);
~OFDScene();
void startHttpServer();
private slots:
void on_choose_image_button_clicked();
void onDataDecode(std::string data);
void on_parse_button_clicked();
void on_binary_eye_button_clicked();
private:
Ui::OFDScene *ui;

View File

@ -5,5 +5,6 @@
<file>scenes/ocrscene.ui</file>
<file>scenes/mainwindow.ui</file>
<file>scenes/ofdscene.ui</file>
<file>scenes/solvecaptchadialog.ui</file>
</qresource>
</RCC>

View File

@ -51,7 +51,11 @@
</widget>
</item>
<item row="8" column="2">
<widget class="QDoubleSpinBox" name="total_spin_box"/>
<widget class="QDoubleSpinBox" name="total_spin_box">
<property name="maximum">
<double>4294967296.000000000000000</double>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="fd_label">

View File

@ -4,20 +4,24 @@
<context>
<name>AdjustPictureDialog</name>
<message>
<location filename="../scenes/adjustpicturedialog.ui" line="14"/>
<source>Dialog</source>
<translation type="vanished">Dialog</translation>
<translation>Dialog</translation>
</message>
<message>
<location filename="../scenes/adjustpicturedialog.ui" line="58"/>
<source>Please, zoom to qr code and adjust contrast so that qr code looks sharp</source>
<translation type="vanished">Please, zoom to qr code and adjust contrast so that qr code looks sharp</translation>
<translation>Please, zoom to qr code and adjust contrast so that qr code looks sharp</translation>
</message>
<message>
<location filename="../adjustpicturedialog.cpp" line="39"/>
<source>QR code was not detected on that image. Please edit it again or enter data manually</source>
<translation type="vanished">QR code was not detected on that image. Please edit it again or enter data manually</translation>
<translation>QR code was not detected on that image. Please edit it again or enter data manually</translation>
</message>
<message>
<location filename="../adjustpicturedialog.cpp" line="41"/>
<source>No QR code</source>
<translation type="vanished">No QR code</translation>
<translation>No QR code</translation>
</message>
</context>
<context>
@ -303,65 +307,110 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="65"/>
<location filename="../scenes/ofdscene.ui" line="69"/>
<source>FD (Fiscal Document)</source>
<translation type="unfinished">FD (Fiscal Document)</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="72"/>
<location filename="../scenes/ofdscene.ui" line="76"/>
<source>Date and time of purchase</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="80"/>
<location filename="../scenes/ofdscene.ui" line="84"/>
<source>Funds income</source>
<translation type="unfinished">Funds income</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="85"/>
<location filename="../scenes/ofdscene.ui" line="89"/>
<source>Funds return</source>
<translation type="unfinished">Funds return</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="90"/>
<location filename="../scenes/ofdscene.ui" line="94"/>
<source>Funds spend</source>
<translation type="unfinished">Funds spend</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="95"/>
<location filename="../scenes/ofdscene.ui" line="99"/>
<source>Spends return</source>
<translation type="unfinished">Spends return</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="132"/>
<location filename="../scenes/ofdscene.ui" line="136"/>
<source>Use your phone as a QR code scanner</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="145"/>
<location filename="../scenes/ofdscene.ui" line="149"/>
<source>FN (Fiscal Number)</source>
<translation type="unfinished">FN (Fiscal Number)</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="152"/>
<location filename="../scenes/ofdscene.ui" line="156"/>
<source>FI (Fiscal Identifier)</source>
<translation type="unfinished">FI (Fiscal Identifier)</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="162"/>
<location filename="../scenes/ofdscene.ui" line="166"/>
<source>Choose image on your PC</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="169"/>
<location filename="../scenes/ofdscene.ui" line="173"/>
<source>Operation type</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="182"/>
<location filename="../scenes/ofdscene.ui" line="186"/>
<source>Parse</source>
<translation type="unfinished">Parse</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="48"/>
<source>Please, select a picture where QR code that contains info about check is present</source>
<translation type="unfinished">Please, select a picture where QR code that contains info about check is present</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="50"/>
<source>Picture was not selected</source>
<translation type="unfinished">Picture was not selected</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="56"/>
<source>Selected image: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="115"/>
<source>Captcha was not solved correctly!</source>
<translation type="unfinished">Captcha was not solved correctly!</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="117"/>
<source>Captcha is incorrect</source>
<translation type="unfinished">Captcha is incorrect</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="122"/>
<source>Internal server error. Please, try again later.</source>
<translation type="unfinished">Internal server error. Please, try again later.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="124"/>
<source>Internal server error</source>
<translation type="unfinished">Internal server error</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="129"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation type="unfinished">Check not found. Please, ensure correctness of entered data.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="131"/>
<source>Check was not found</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>OutputDialog</name>
@ -490,16 +539,19 @@
<context>
<name>SolveCaptchaDialog</name>
<message>
<location filename="../scenes/solvecaptchadialog.ui" line="14"/>
<source>Dialog</source>
<translation type="vanished">Dialog</translation>
<translation>Dialog</translation>
</message>
<message>
<location filename="../solvecaptchadialog.cpp" line="23"/>
<source>Please, enter a valid captcha</source>
<translation type="vanished">Please, enter a valid captcha</translation>
<translation>Please, enter a valid captcha</translation>
</message>
<message>
<location filename="../solvecaptchadialog.cpp" line="25"/>
<source>No captcha</source>
<translation type="vanished">No captcha</translation>
<translation>No captcha</translation>
</message>
</context>
<context>

View File

@ -4,20 +4,24 @@
<context>
<name>AdjustPictureDialog</name>
<message>
<location filename="../scenes/adjustpicturedialog.ui" line="14"/>
<source>Dialog</source>
<translation type="vanished">Диалог</translation>
<translation>Диалог</translation>
</message>
<message>
<location filename="../scenes/adjustpicturedialog.ui" line="58"/>
<source>Please, zoom to qr code and adjust contrast so that qr code looks sharp</source>
<translation type="vanished">Пожалуйста, приблизьте QR код и настройте контраст, чтобы он читался</translation>
<translation>Пожалуйста, приблизьте QR код и настройте контраст, чтобы он читался</translation>
</message>
<message>
<location filename="../adjustpicturedialog.cpp" line="39"/>
<source>QR code was not detected on that image. Please edit it again or enter data manually</source>
<translation type="vanished">QR код не найден на этом изображении. Пожалуйста, попытайтесь снова или введите данные вручную</translation>
<translation>QR код не найден на этом изображении. Пожалуйста, попытайтесь снова или введите данные вручную</translation>
</message>
<message>
<location filename="../adjustpicturedialog.cpp" line="41"/>
<source>No QR code</source>
<translation type="vanished">QR код не найден</translation>
<translation>QR код не найден</translation>
</message>
</context>
<context>
@ -303,65 +307,110 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="65"/>
<location filename="../scenes/ofdscene.ui" line="69"/>
<source>FD (Fiscal Document)</source>
<translation type="unfinished">ФД</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="72"/>
<location filename="../scenes/ofdscene.ui" line="76"/>
<source>Date and time of purchase</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="80"/>
<location filename="../scenes/ofdscene.ui" line="84"/>
<source>Funds income</source>
<translation type="unfinished">Приход средств</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="85"/>
<location filename="../scenes/ofdscene.ui" line="89"/>
<source>Funds return</source>
<translation type="unfinished">Возврат средств</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="90"/>
<location filename="../scenes/ofdscene.ui" line="94"/>
<source>Funds spend</source>
<translation type="unfinished">Расход средств</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="95"/>
<location filename="../scenes/ofdscene.ui" line="99"/>
<source>Spends return</source>
<translation type="unfinished">Возврат расхода</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="132"/>
<location filename="../scenes/ofdscene.ui" line="136"/>
<source>Use your phone as a QR code scanner</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="145"/>
<location filename="../scenes/ofdscene.ui" line="149"/>
<source>FN (Fiscal Number)</source>
<translation type="unfinished">ФН</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="152"/>
<location filename="../scenes/ofdscene.ui" line="156"/>
<source>FI (Fiscal Identifier)</source>
<translation type="unfinished">ФП</translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="162"/>
<location filename="../scenes/ofdscene.ui" line="166"/>
<source>Choose image on your PC</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="169"/>
<location filename="../scenes/ofdscene.ui" line="173"/>
<source>Operation type</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../scenes/ofdscene.ui" line="182"/>
<location filename="../scenes/ofdscene.ui" line="186"/>
<source>Parse</source>
<translation type="unfinished">Парсить</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="48"/>
<source>Please, select a picture where QR code that contains info about check is present</source>
<translation type="unfinished">Пожалуйста, выберете изображение, содержащее QR код с информацией о чеке</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="50"/>
<source>Picture was not selected</source>
<translation type="unfinished">Изображение не было выбрано</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="56"/>
<source>Selected image: </source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="115"/>
<source>Captcha was not solved correctly!</source>
<translation type="unfinished">Капча была решена неверно!</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="117"/>
<source>Captcha is incorrect</source>
<translation type="unfinished">Капча введена неверно</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="122"/>
<source>Internal server error. Please, try again later.</source>
<translation type="unfinished">Внутренняя ошибка сервера. Пожалуйста, попробуйте снова позже.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="124"/>
<source>Internal server error</source>
<translation type="unfinished">Внутренняя ошибка сервера</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="129"/>
<source>Check not found. Please, ensure correctness of entered data.</source>
<translation type="unfinished">Чек не найден. Пожалуйста, убедитесь в правильности введённых данных.</translation>
</message>
<message>
<location filename="../ofdscene.cpp" line="131"/>
<source>Check was not found</source>
<translation type="unfinished">Чек не найден</translation>
</message>
</context>
<context>
<name>OutputDialog</name>
@ -490,16 +539,19 @@
<context>
<name>SolveCaptchaDialog</name>
<message>
<location filename="../scenes/solvecaptchadialog.ui" line="14"/>
<source>Dialog</source>
<translation type="vanished">Диалог</translation>
<translation>Диалог</translation>
</message>
<message>
<location filename="../solvecaptchadialog.cpp" line="23"/>
<source>Please, enter a valid captcha</source>
<translation type="vanished">Пожалуйста, введите верную капчу</translation>
<translation>Пожалуйста, введите верную капчу</translation>
</message>
<message>
<location filename="../solvecaptchadialog.cpp" line="25"/>
<source>No captcha</source>
<translation type="vanished">Нет капчи</translation>
<translation>Нет капчи</translation>
</message>
</context>
<context>

View File

@ -1,5 +1,6 @@
#include "utils.h"
#include <arpa/inet.h>
#include <codecvt>
#include <cstring>
#include <iostream>
@ -7,6 +8,37 @@
#include <regex>
#include <string>
#include "../exceptions/ofdrequestexception.h"
#include "settings/settings.h"
#include <QWidget>
#include <fstream>
#include <ifaddrs.h>
#include <netinet/in.h>
std::string get_local_ip_address() {
struct ifaddrs * ifAddrStruct=NULL;
struct ifaddrs * ifa=NULL;
void * tmpAddrPtr=NULL;
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) continue;
if (ifa->ifa_addr->sa_family==AF_INET) {
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[128];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
std::string value(addressBuffer);
if (!strncmp(value.c_str(), "192.168", 7)) {
return value;
}
}
}
if (ifAddrStruct!=NULL)
freeifaddrs(ifAddrStruct);
throw std::runtime_error(QWidget::tr("Could not find any usable local IP address. If you beleive that this is problem with the program, please, contact the developer.").toStdString());
}
std::string to_utf8(std::wstring wide_string) {
static std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
@ -94,8 +126,9 @@ std::vector<std::wstring> find_in_html(std::string& html, std::string regex, std
it != end; it++) {
std::wstring found_entry = from_utf8(it->str());
std::wcout << "Found: " << found_entry << std::endl;
std::wstring extracted = substring_from_to(found_entry, from_utf8(html_start), from_utf8(html_end));
std::wcout << "Extracted: " << extracted << std::endl;
parsed.push_back(extracted);
}
return parsed;
@ -106,11 +139,42 @@ std::vector<std::wstring> find_products_in_html(std::string html) {
}
std::vector<std::wstring> find_amounts_in_html(std::string html) {
return find_in_html(html, "<span>\\d+<\\/span>", "<span>", "<\\/span>");
std::vector<std::wstring> founds = find_in_html(html, "<div><span>\\d+(\\.|\\,)?\\d{0,3}<\\/span>", "<span>", "<\\/span>");
for (auto &found : founds) {
std::replace(found.begin(), found.end(), ',', '.');
}
return founds;
}
std::vector<std::wstring> find_prices_in_html(std::string html) {
return find_in_html(html, "X <\\/span><span>\\d+\\.\\d{2}<\\/span>", "X <\\/span><span>", "<\\/span>");
std::vector<std::wstring> founds = find_in_html(html, "X <\\/span><span>\\d+(\\.|,)\\d{2}<\\/span>", "X <\\/span><span>", "<\\/span>");
for (auto &found : founds) {
std::replace(found.begin(), found.end(), ',', '.');
}
return founds;
}
void dumpVectorsToStderr(std::vector<std::wstring> &products, std::vector<std::wstring> &amounts, std::vector<std::wstring> &prices) {
std::cerr << "Products: ";
for (auto &product : products) {
std::cerr << to_utf8(product) << "|[]|";
}
std::cerr << std::endl;
std::cerr << "Amounts: ";
for (auto &amount : amounts) {
std::wcerr << amount << " ";
}
std::cerr << std::endl;
std::cerr << "Prices: ";
for (auto &price : prices) {
std::wcerr << price << " ";
}
std::cerr << std::endl;
}
Check parseOfdRuAnswer(std::string html) {
@ -133,6 +197,10 @@ Check parseOfdRuAnswer(std::string html) {
}
if ((products.size() + amounts.size() + prices.size())/products.size() != 3) {
dumpVectorsToStderr(products, amounts, prices);
//TOOD: make new setting "app_home" and get all path using it.
std::ofstream error_log(get_path_relative_to_home(".local/share/checks_parser/error_log.txt"), std::ios_base::app);
error_log << trimmed << std::endl;
std::cerr << "An error has occured during the parsing of html. Please, contact the developer." << std::endl;
std::exit(-1);
}

View File

@ -5,6 +5,7 @@
#include <vector>
#include "../check/check.h"
std::string get_local_ip_address();
std::string to_utf8(std::wstring wide_string);
std::wstring from_utf8(std::string string);