checks-parser/parser/parser.cpp

122 lines
4.1 KiB
C++

#include "parser.h"
#include <goods/goods.h>
#include <net/net.h>
#include <settings/settings.h>
#include <utils/utils.h>
#include <iostream>
#include <fstream>
#if __GNUC__ <= 8 && __clang_major__ < 17
# include <experimental/filesystem>
using namespace std::experimental;
using namespace std::experimental::filesystem;
#else
#include <QObject>
# include <filesystem>
using namespace std::filesystem;
#endif
Parser::Parser() {}
std::vector<std::string> Parser::search_modules() {
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::string(std::getenv("HOME")) + "/" + STORES_MODULES_DIR;
directory_entry modules_dir(path);
if (!exists(modules_dir)) {
create_directories(path);
std::cout << QObject::tr("No modules directory found. Created one at ").toStdString() << path
<< std::endl;
}
std::vector<std::string> modules_files;
for (auto &file : directory_iterator(path)) {
modules_files.push_back(file.path());
}
return modules_files;
}
std::vector<std::string> Parser::get_modules_names() {
std::vector<std::string> modules = this->search_modules();
std::vector<std::string> names = {};
for (std::string &modulePath : modules) {
std::ifstream inputFile(modulePath);
nlohmann::json module = nlohmann::json::parse(inputFile);
std::string name = module["name"];
names.push_back(name);
}
return names;
}
void Parser::set_module(std::string path) { module = StoreModule(path); }
std::vector<Goods> Parser::parse(std::wstring check_plaintext) {
std::vector<Goods> result;
module.trim_check(check_plaintext);
std::vector<std::string> goods_names = module.parse_name(check_plaintext);
std::vector<std::string> goods_prices = module.parse_price(check_plaintext);
std::vector<std::string> goods_net_weights = module.parse_net_weight(goods_names);
std::vector<std::string> goods_quantities = module.parse_quantity(check_plaintext);
if (!areAllSizesEqual(goods_names, goods_prices, goods_net_weights, goods_quantities)) {
// dumpVectorsToStderr(goods_names, goods_prices, goods_net_weights, goods_quantities);
//Error. Amount of names, prices, net weights or quantities are not equal. That means, that some regex(es) has mismatched.
return {};
}
short goods_amount = goods_names.size();
for (short i = 0; i < goods_amount; i++) {
Goods goods(goods_names[i], std::stod(goods_prices[i]), goods_net_weights[i], std::stod(goods_quantities[i]));
result.push_back(goods);
}
return result;
}
std::string Parser::try_autodetect_store(std::string str) {
std::vector<std::string> 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<std::string> Parser::check_updates() {
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<std::string> to_download;
std::vector<std::string> stored_modules = search_modules();
Net n;
std::cerr << QObject::tr("Downloading modules list from: ").toStdString() << s.get_setting("stores_modules_url") << std::endl;
std::vector<std::string> remote_modules = n.get_all_modules(s.get_setting("stores_modules_url"));
if (stored_modules.empty()) {
to_download = remote_modules;
} else {
for (const std::string& module : remote_modules) {
if (!vector_contains_element(stored_modules, module)) {
std::cout << QObject::tr("Queueing download of ").toStdString() << module << std::endl;
to_download.push_back(module);
}
}
}
return to_download;
}