checks-parser/parser/parser.cpp

104 lines
3.6 KiB
C++
Raw Normal View History

2024-11-22 23:26:42 +03:00
#include "parser.h"
#include "../goods/goods.h"
#include "../net/net.h"
#include "../settings/settings.h"
#include "../utils/utils.h"
#include <iostream>
#if __GNUC__ < 8 && __clang_major__ < 17
2024-12-01 18:53:16 +03:00
# include <experimental/filesystem>
2024-12-01 20:04:51 +03:00
using namespace std::experimental;
using namespace std::experimental::filesystem;
2024-12-01 18:53:16 +03:00
#else
# include <filesystem>
using namespace std::filesystem;
2024-12-01 18:53:16 +03:00
#endif
2024-11-22 23:26:42 +03:00
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);
2024-11-22 23:26:42 +03:00
2024-12-01 18:53:16 +03:00
if (!exists(modules_dir)) {
create_directories(path);
2024-11-22 23:26:42 +03:00
std::cout << "No modules directory found. Created one at " << path
<< std::endl;
std::cout << "Please, download modules to that directory from my git."
<< std::endl;
}
std::vector<std::string> modules_files;
for (auto file : directory_iterator(path)) {
2024-11-22 23:26:42 +03:00
modules_files.push_back(file.path());
}
return modules_files;
}
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_quantities =
module.parse_quantity(check_plaintext);
if (goods_names.size() != goods_prices.size() ||
goods_names.size() != goods_quantities.size() ||
goods_prices.size() != goods_quantities.size()) {
//Error. Amount of names, prices 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::stof(goods_prices[i]), std::stof(goods_quantities[i]));
result.push_back(goods);
}
return result;
}
std::vector<std::string> Parser::check_updates() {
std::cout << "Checking updates for stores modules" << 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;
directory_entry modules_dir(path);
2024-12-01 18:53:16 +03:00
if (!exists(modules_dir)) {
create_directories(path);
2024-11-22 23:26:42 +03:00
}
for (const auto& file : directory_iterator(path)) {
2024-12-01 18:53:16 +03:00
if (!is_regular_file(file)) continue;
2024-11-22 23:26:42 +03:00
stored_modules.push_back(file.path().filename());
std::cout << file.path().filename() << " detected store module" << std::endl;
}
Net n;
2024-12-03 15:24:21 +03:00
std::cerr << "Downloading modules list from: " << s.get_setting("stores_modules_url") << std::endl;
2024-11-22 23:26:42 +03:00
std::vector<std::string> 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;
to_download.push_back(module);
}
}
}
return to_download;
}