requests to ofd

This commit is contained in:
2024-11-27 01:46:36 +03:00
parent 1843479e6b
commit 47dfc19395
9 changed files with 125 additions and 33 deletions

View File

@@ -1,7 +1,10 @@
#include "utils.h"
#include <codecvt>
#include <cstring>
#include <iostream>
#include <locale>
#include <regex>
#include <string>
std::string to_utf8(std::wstring wide_string) {
@@ -42,3 +45,92 @@ std::vector<std::string> split(std::string s, std::string delimiter) {
return result;
}
std::wstring substring_from_to(std::wstring& text, std::wstring from, std::wstring to) {
unsigned int start_pos = 0;
unsigned int end_pos = 0;
std::wstring substring;
std::wregex start_regex(from);
std::wregex end_regex(to);
for (std::wsregex_iterator it{text.begin(), text.end(), start_regex}, end{};
it != end; it++) {
start_pos = it->position() + it->str().size();
break;
}
if(text == from_utf8("")) return text;
substring = text.substr(start_pos, text.size());
for (std::wsregex_iterator it{substring.begin(), substring.end(), end_regex}, end{};
it != end; it++) {
end_pos = it->position();
break;
}
if (end_pos == 0) return substring;
substring = substring.substr(0, end_pos);
return substring;
}
std::wstring trim_html_response(std::wstring& check) {
std::wstring begin_check_marker = from_utf8("<!-- Products -->");
std::wstring end_check_marker = from_utf8("<!-- \\/Products -->");
std::wstring trimmed = substring_from_to(check, begin_check_marker, end_check_marker);
trimmed += from_utf8("\n</div>");
return trimmed;
}
std::vector<std::wstring> find_in_html(std::string& html, std::string regex, std::string html_start, std::string html_end) {
std::regex searching_regex(regex);
std::vector<std::wstring> parsed;
for (std::sregex_iterator it{html.begin(), html.end(), searching_regex}, end{};
it != end; it++) {
std::wstring found_entry = from_utf8(it->str());
std::wstring extracted = substring_from_to(found_entry, from_utf8(html_start), from_utf8(html_end));
parsed.push_back(extracted);
}
return parsed;
}
std::vector<std::wstring> find_products_in_html(std::string html) {
return find_in_html(html, "<div class=\"ifw-col ifw-col-1 text-left\"><b>.*<\\/b><\\/div>", "<div class=\"ifw-col ifw-col-1 text-left\"><b>", "<\\/b><\\/div>");
}
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> find_prices_in_html(std::string html) {
return find_in_html(html, "X <\\/span><span>\\d+\\.\\d{2}<\\/span>", "X <\\/span><span>", "<\\/span>");
}
Check parseOfdRuAnswer(std::string html) {
std::wstring wstr_html = from_utf8(html);
std::string trimmed = to_utf8(trim_html_response(wstr_html));
std::vector<std::wstring> products = find_products_in_html(trimmed);
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())/products.size() != 3) {
std::cerr << "An error has occured during the parsing of html. Please, contact the developer." << std::endl;
std::exit(-1);
}
Check c;
for (int i = 0; i < products.size(); i ++) {
Goods goods(to_utf8(products[i]), std::stod(prices[i]), std::stod(amounts[i]));
c.add_goods(goods);
}
return c;
}

View File

@@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include "../check/check.h"
std::string to_utf8(std::wstring wide_string);
std::wstring from_utf8(std::string string);
@@ -14,4 +15,8 @@ bool vector_contains_element(const std::vector<T> &vector, const T &to_find);
std::vector<std::string> split(std::string, std::string);
Check parseOfdRuAnswer(std::string);
std::wstring trim_html_response(std::wstring& check);
#endif // UTILS_H