From 6b815dbe0affb76658e36cb49db60a0799adb7e1 Mon Sep 17 00:00:00 2001 From: leca Date: Sun, 30 Mar 2025 20:48:53 +0300 Subject: [PATCH] added store autodetect in email scene --- emailtextscene.cpp | 14 +++++++++++++- emailtextscene.h | 3 +++ modules/magnit.json | 1 + modules/pyaterochka.json | 4 ++-- parser/module.cpp | 8 +++++++- parser/module.h | 8 +++++--- parser/parser.cpp | 35 +++++++++++++++++------------------ parser/parser.h | 3 +++ translations/en_US.ts | 9 +++++++-- translations/ru_RU.ts | 9 +++++++-- 10 files changed, 65 insertions(+), 29 deletions(-) diff --git a/emailtextscene.cpp b/emailtextscene.cpp index 5c70c11..b964adf 100644 --- a/emailtextscene.cpp +++ b/emailtextscene.cpp @@ -1,5 +1,6 @@ #include "emailtextscene.h" #include "ui_emailtextscene.h" + #include #include #include @@ -10,7 +11,7 @@ EmailTextScene::EmailTextScene(QWidget *parent) , ui(new Ui::EmailTextScene) { ui->setupUi(this); - auto modules = parser.get_modules_names(); + modules = parser.get_modules_names(); for (auto &module : modules) { ui->store_combo_box->addItem(QString::fromStdString(module)); @@ -43,3 +44,14 @@ void EmailTextScene::on_parse_button_clicked() { d.exec(); } + +void EmailTextScene::on_check_content_textChanged() { + std::string store = parser.try_autodetect_store(ui->check_content->toPlainText().toStdString()); + if (store == "") { + std::cerr << tr("Could not autodetect store. If you beleive that this is an error, please, report to the developer.").toStdString() << std::endl; + return; + } + unsigned int index = ui->store_combo_box->findText(QString::fromStdString(store)); + ui->store_combo_box->setCurrentIndex(index); +} + diff --git a/emailtextscene.h b/emailtextscene.h index 4a8739a..68211be 100644 --- a/emailtextscene.h +++ b/emailtextscene.h @@ -19,9 +19,12 @@ public: private slots: void on_parse_button_clicked(); + void on_check_content_textChanged(); + private: Ui::EmailTextScene *ui; Parser parser; + std::vector modules; }; #endif // EMAILTEXTSCENE_H diff --git a/modules/magnit.json b/modules/magnit.json index 4992590..f678a91 100644 --- a/modules/magnit.json +++ b/modules/magnit.json @@ -1,5 +1,6 @@ { "name":"Магнит", + "autodetect_regex":"", "goods_name_regex": "([\\(\\)\\%\\*a-zA-Z0-9\u0401\u0451\u0410-\u044f \\.\\-\/]{17,100})", "goods_price_regex": "[0-9]{0,4}[^%]\\.[0-9]{2} ", "goods_quantity_regex": "([0-9]{0,4}[^%]\\.[0-9]{3} )|(\t\\d )", diff --git a/modules/pyaterochka.json b/modules/pyaterochka.json index 29f3796..936e67f 100644 --- a/modules/pyaterochka.json +++ b/modules/pyaterochka.json @@ -1,8 +1,8 @@ { "name":"Пятёрочка", - "goods_name_regex": "^\\d+\\s\\t\\*?[\\(\\)\\%\\*a-zA-Z0-9ёЁА-я\\.\\-\\/ ]+", + "autodetect_regex":"ООО\s*\"Агроторг\"", + "goods_name_regex": "([\\(\\)\\%\\*a-zA-Z0-9\u0401\u0451\u0410-\u044f \\.\\-\/]{17,100})", "goods_price_regex": "[0-9]{0,4}[^%]\\.[0-9]{2} ", - "goods_net_weight_regex": "((\\d+(\\.|,)?\\d{0,}((м|)л|(к|м|)г|т|ц|шт|(pc|)s|(m|k|)g|(m|)l|t))(\\s|\\t){0,})+", "goods_quantity_regex": "([0-9]{0,4}[^%]\\.[0-9]{3} )|(\t\\d )", "check_start_regex": "КАССОВЫЙ ЧЕК\nприход", "check_end_regex": "Итог\\:.{0,3}[0-9]{0,6}\\.[0-9]{2}" diff --git a/parser/module.cpp b/parser/module.cpp index d7319ae..11f1dcb 100644 --- a/parser/module.cpp +++ b/parser/module.cpp @@ -16,7 +16,7 @@ std::vector StoreModule::parse(std::wstring str, std::wstring regex std::cout << "Handling: " << to_utf8(str) << std::endl; for (boost::wsregex_iterator it{str.begin(), str.end(), r}, end{}; it != end; it++) { - std::wcout << "Parsed: " << it->str() << std::endl; + std::cout << "Parsed: " << to_utf8(it->str()) << std::endl; result.push_back(to_utf8(it->str())); } @@ -28,6 +28,7 @@ StoreModule::StoreModule(std::string path) { nlohmann::json settings = nlohmann::json::parse(settings_file); this->name = from_utf8(settings["name"]); + this->autodetect_regex = from_utf8(settings["autodetect_regex"]); this->goods_name_regex = from_utf8(settings["goods_name_regex"]); this->goods_price_regex = from_utf8(settings["goods_price_regex"]); this->goods_net_weight_regex = from_utf8(settings["goods_net_weight_regex"]); @@ -89,3 +90,8 @@ std::wstring StoreModule::trim_check(std::wstring& check) { } std::wstring StoreModule::get_name() { return this->name; } + +bool StoreModule::search_autodetect_regex(std::wstring str) { + std::vector parsed = parse(str, this->autodetect_regex, boost::regex_constants::collate); + return parsed.size() > 0; +} diff --git a/parser/module.h b/parser/module.h index 09c7efe..7bf459e 100644 --- a/parser/module.h +++ b/parser/module.h @@ -8,6 +8,7 @@ class StoreModule { std::string path; std::wstring name; + std::wstring autodetect_regex; std::wstring goods_name_regex; std::wstring goods_price_regex; std::wstring goods_net_weight_regex; @@ -15,9 +16,9 @@ class StoreModule { std::wstring check_start_regex; std::wstring check_end_regex; - std::vector parse(std::wstring, std::wstring, boost::regex_constants::flag_type_); + std::vector parse(std::wstring str, std::wstring regexp, boost::regex_constants::flag_type_ flag); public: - StoreModule(std::string); + StoreModule(std::string path); StoreModule(); std::vector parse_name(std::wstring); @@ -27,8 +28,9 @@ public: std::wstring trim_check(std::wstring&); - std::wstring get_name(); + + bool search_autodetect_regex(std::wstring str); }; #endif // STORE_MODULE_H diff --git a/parser/parser.cpp b/parser/parser.cpp index 2f04214..fd85118 100644 --- a/parser/parser.cpp +++ b/parser/parser.cpp @@ -12,6 +12,7 @@ using namespace std::experimental; using namespace std::experimental::filesystem; #else +#include # include using namespace std::filesystem; #endif @@ -25,9 +26,7 @@ std::vector Parser::search_modules() { if (!exists(modules_dir)) { create_directories(path); - std::cout << "No modules directory found. Created one at " << path - << std::endl; - std::cout << "Please, download modules to that directory from my git." + std::cout << QObject::tr("No modules directory found. Created one at ").toStdString() << path << std::endl; } @@ -86,33 +85,33 @@ std::vector Parser::parse(std::wstring check_plaintext) { return result; } +std::string Parser::try_autodetect_store(std::string str) { + std::vector stored_modules_paths = search_modules(); + + for (auto &module_path : stored_modules_paths) { + StoreModule module(module_path); + if (module.search_autodetect_regex(from_utf8(str))) return to_utf8(module.get_name()); + } + + return ""; +} + std::vector Parser::check_updates() { - std::cout << "Checking updates for stores modules" << std::endl; + std::cout << QObject::tr("Checking updates for stores modules").toStdString() << std::endl; Settings s(get_path_relative_to_home(".local/share/checks_parser/settings.json")); std::string path = get_path_relative_to_home(s.get_setting("stores_modules_dir")); std::vector to_download; - std::vector stored_modules; - - directory_entry modules_dir(path); - if (!exists(modules_dir)) { - create_directories(path); - } - for (const auto& file : directory_iterator(path)) { - if (!is_regular_file(file)) continue; - stored_modules.push_back(file.path().filename()); - std::cout << file.path().filename() << " detected local store module" << std::endl; - } + std::vector stored_modules = search_modules(); Net n; - std::cerr << "Downloading modules list from: " << s.get_setting("stores_modules_url") << std::endl; + std::cerr << QObject::tr("Downloading modules list from: ").toStdString() << s.get_setting("stores_modules_url") << std::endl; std::vector remote_modules = n.get_all_modules(s.get_setting("stores_modules_url")); if (stored_modules.empty()) { - std::cout << "I need to download everything" << std::endl; to_download = remote_modules; } else { for (const std::string& module : remote_modules) { if (!vector_contains_element(stored_modules, module)) { - std::cout << "I need to download store module " << module << std::endl; + std::cout << QObject::tr("Queueing download of ").toStdString() << module << std::endl; to_download.push_back(module); } } diff --git a/parser/parser.h b/parser/parser.h index c845785..f833818 100644 --- a/parser/parser.h +++ b/parser/parser.h @@ -22,6 +22,9 @@ public: void set_module(std::string); std::vector parse(std::wstring); + + std::string try_autodetect_store(std::string); + }; #endif // PARSER_H diff --git a/translations/en_US.ts b/translations/en_US.ts index ebdc699..a0820c3 100644 --- a/translations/en_US.ts +++ b/translations/en_US.ts @@ -56,15 +56,20 @@ Back - + An error has occured. Check was matched incorrectly. Vector sizes are different. Please, contact the developer. An error has occured. Check was matched incorrectly. Vector sizes are different. Please, contact the developer. - + Error in parsing Error in parsing + + + Could not autodetect store. If you beleive that this is an error, please, report to the developer. + + MainWindow diff --git a/translations/ru_RU.ts b/translations/ru_RU.ts index 85c306a..ab75136 100644 --- a/translations/ru_RU.ts +++ b/translations/ru_RU.ts @@ -56,15 +56,20 @@ Назад - + An error has occured. Check was matched incorrectly. Vector sizes are different. Please, contact the developer. Произошла ошибка. Чек был прочитан неверно. Размеры векторов различаются. Пожалуйста, сообщите об этом разработчику. - + Error in parsing Ошибка в парсинге + + + Could not autodetect store. If you beleive that this is an error, please, report to the developer. + + MainWindow