From 79453ffc82f75cf0af0537474923e7b112aa2f87 Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Wed, 1 Oct 2025 19:46:58 +0200 Subject: added more FOLDERS --- CMakeLists.txt | 2 +- include/hearthstoneAPIHandler.hpp | 29 -------- include/postgresHandler.hpp | 12 ++-- resources/token_info.txt | 2 +- src/hearthstoneAPIHandler.cpp | 144 -------------------------------------- src/postgresHandler.cpp | 6 +- 6 files changed, 9 insertions(+), 186 deletions(-) delete mode 100644 include/hearthstoneAPIHandler.hpp delete mode 100644 src/hearthstoneAPIHandler.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 595e0cd..7a8a0ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include) set(SRC_FILES ${PROJECT_SOURCE_DIR}/src/main.cpp ${PROJECT_SOURCE_DIR}/src/cardTypeHandlerInfo.cpp - ${PROJECT_SOURCE_DIR}/src/hearthstoneAPIHandler.cpp + ${PROJECT_SOURCE_DIR}/src/hearthstoneAPI.cpp ${PROJECT_SOURCE_DIR}/src/postgresHandler.cpp) set(SRC_TESTS_FILES ${PROJECT_SOURCE_DIR}/src/tests/tests.cpp) diff --git a/include/hearthstoneAPIHandler.hpp b/include/hearthstoneAPIHandler.hpp deleted file mode 100644 index 9d5ec58..0000000 --- a/include/hearthstoneAPIHandler.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include -#include - -class HearthstoneAPIHandler { - public: - HearthstoneAPIHandler(); - std::string get_cards(const std::string &t_card_parameters); - - private: - static size_t curl_callback_string_output(void *t_contents, size_t t_size, - size_t t_nmemb, std::string *t_s); - std::chrono::seconds get_current_time(); - std::string extract_string_with_regex(const std::string &t_str, - const std::string t_regex); - void write_new_token_info_to_file(const std::string &t_file_path); - void refresh_token_if_needed(); - std::string get_valid_token(); - std::string generate_request_header(); - const std::string get_url(const std::string &t_parameters); - const std::string - get_set_of_cards_url(const std::string &t_card_parameters); - std::string make_api_request(const char *t_url, const char *t_header); - std::string make_api_request(const std::string &t_url, - const std::string &t_header); - - std::string m_access_token; - std::string m_header; -}; diff --git a/include/postgresHandler.hpp b/include/postgresHandler.hpp index 2c3f6f4..444a50f 100644 --- a/include/postgresHandler.hpp +++ b/include/postgresHandler.hpp @@ -1,5 +1,5 @@ #pragma once -#include "hearthstoneAPIHandler.hpp" +#include "hearthstoneAPI.hpp" #include #include @@ -10,8 +10,7 @@ * @param aQuery The SQL query * @return The result of the executed SQL query */ -const pqxx::result executeQuery(pqxx::work &aTransaction, - std::string_view aQuery); +const pqxx::result executeQuery(pqxx::work &aTransaction, std::string_view aQuery); /** * Get the type id of the value element in the "key": value regex match @@ -21,16 +20,13 @@ const pqxx::result executeQuery(pqxx::work &aTransaction, * @param kSregexIt the regex match for a "key": value card information * @return the type identifier of the value element */ -const unsigned int -getTypeIndexOfRegexIterator(const std::sregex_iterator &kSregexIt); +const unsigned int getTypeIndexOfRegexIterator(const std::sregex_iterator &kSregexIt); /** * Add a list of cards (TODO: Complete documentation and function) * * @param kCards * @param aTransaction */ -void addCardsToDatabase(HearthstoneAPIHandler &aAPIIntermediate, - pqxx::work &aTransaction, - const std::string &kURLOfCardsPage); +void addCardsToDatabase(HearthstoneAPI &aAPIIntermediate, pqxx::work &aTransaction, const std::string &kURLOfCardsPage); void refresh_cards_table(); diff --git a/resources/token_info.txt b/resources/token_info.txt index 667a347..2cda08f 100644 --- a/resources/token_info.txt +++ b/resources/token_info.txt @@ -1 +1 @@ -EUjMrxBxM5srVkY9gs5Tfrzfc3kVGudWMs 1755534579 \ No newline at end of file +EU4CFVbfIvpEWAkfG2sH2yKM5gJtb0B4hO 1759427188 diff --git a/src/hearthstoneAPIHandler.cpp b/src/hearthstoneAPIHandler.cpp deleted file mode 100644 index 8845b62..0000000 --- a/src/hearthstoneAPIHandler.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#include "hearthstoneAPIHandler.hpp" -#include -#include -#include -#include -#include -#include - -HearthstoneAPIHandler::HearthstoneAPIHandler() - : m_access_token(get_valid_token()), m_header(generate_request_header()) {} - -size_t HearthstoneAPIHandler::curl_callback_string_output(void *t_contents, - size_t t_size, - size_t t_nmemb, - std::string *t_s) { - size_t newLength = t_size * t_nmemb; - try { - t_s->append((char *)t_contents, newLength); - } catch (std::bad_alloc &e) { - // handle memory problem - return 0; - } - return newLength; -} - -std::chrono::seconds HearthstoneAPIHandler::get_current_time() { - return std::chrono::duration_cast( - std::chrono::utc_clock::now().time_since_epoch()); -} - -std::string -HearthstoneAPIHandler::extract_string_with_regex(const std::string &t_str, - const std::string t_regex) { - std::regex regex(t_regex); - auto it = std::sregex_iterator(t_str.begin(), t_str.end(), regex); - std::smatch match = *it; - return match[1].str(); -} - -void HearthstoneAPIHandler::write_new_token_info_to_file( - const std::string &t_file_path) { - std::ofstream token_file(t_file_path); - CURL *curl = curl_easy_init(); - std::string token_info; - if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, "https://oauth.battle.net/token"); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, - curl_callback_string_output); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token_info); - curl_easy_setopt(curl, CURLOPT_USERPWD, - "2b50f1123b424d5e9c21971a110fdd04:" - "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6"); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, - "grant_type=client_credentials"); - CURLcode result_code; - result_code = curl_easy_perform(curl); - curl_easy_cleanup(curl); - if (result_code != CURLE_OK) { - std::cerr << "ERROR: token request failed" << std::endl; - std::exit(EXIT_FAILURE); - } - } - std::string token = - extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\""); - std::chrono::seconds expires_at = - get_current_time() + std::chrono::hours(24); - token_file << token << " " << expires_at.count(); -} - -void HearthstoneAPIHandler::refresh_token_if_needed() { - std::string file_path = - std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); - std::ifstream token_file(file_path); - std::string line; - std::getline(token_file, line); - if (line.empty()) { - write_new_token_info_to_file(file_path); - } else { - unsigned long expire_at = std::stoull(line.substr(line.find(" ") + 1)); - if (std::chrono::duration_cast( - std::chrono::utc_clock::now().time_since_epoch()) - .count() >= expire_at) { - write_new_token_info_to_file(file_path); - } - } -} -std::string HearthstoneAPIHandler::get_valid_token() { - refresh_token_if_needed(); - std::string file_path = - std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); - std::ifstream token_file(file_path); - std::string line; - std::getline(token_file, line); - std::string token = line.substr(0, line.find(" ")); - return token; -} - -std::string HearthstoneAPIHandler::generate_request_header() { - return std::format("Authorization: Bearer {}", m_access_token); -} - -const std::string -HearthstoneAPIHandler::get_url(const std::string &t_parameters) { - return std::format("https://eu.api.blizzard.com/hearthstone/{}", - t_parameters); -} - -const std::string HearthstoneAPIHandler::get_set_of_cards_url( - const std::string &t_card_parameters) { - return get_url(std::format("cards{}", t_card_parameters)); -} - -std::string HearthstoneAPIHandler::make_api_request(const char *t_url, - const char *t_header) { - CURL *curl = curl_easy_init(); - std::string request_output; - if (curl) { - curl_easy_setopt(curl, CURLOPT_URL, t_url); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, - curl_callback_string_output); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &request_output); - struct curl_slist *chunk = nullptr; - chunk = curl_slist_append(chunk, t_header); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); - curl_easy_perform(curl); - curl_easy_cleanup(curl); - } - return request_output; -} - -std::string -HearthstoneAPIHandler::make_api_request(const std::string &t_url, - const std::string &t_header) { - return make_api_request(t_url.c_str(), t_header.c_str()); -} - -std::string -HearthstoneAPIHandler::get_cards(const std::string &t_card_parameters) { - return make_api_request(get_set_of_cards_url(t_card_parameters), m_header); -} diff --git a/src/postgresHandler.cpp b/src/postgresHandler.cpp index c0276f1..8ca3c20 100644 --- a/src/postgresHandler.cpp +++ b/src/postgresHandler.cpp @@ -1,6 +1,6 @@ #include "postgresHandler.hpp" #include "cardTypeHandlerInfo.hpp" -#include "hearthstoneAPIHandler.hpp" +#include "hearthstoneAPI.hpp" #include #include #include @@ -125,7 +125,7 @@ const std::string::size_type endPosOfCardJSON(const std::string &kCardsPageJSON, return kCardsPageJSON.find("}", kSearchStartPos); } -void addCardsToDatabase(HearthstoneAPIHandler &aAPIIntermediate, pqxx::work &aTransaction, +void addCardsToDatabase(HearthstoneAPI &aAPIIntermediate, pqxx::work &aTransaction, const std::string &kURLOfCardsPage) { const std::regex aRegexKeyValueInJSON = regexKeyValueInJSON(); const std::string &kCardsPageJSON = aAPIIntermediate.get_cards(kURLOfCardsPage); @@ -155,7 +155,7 @@ void refresh_cards_table() { pqxx::connection aConnection{ "postgresql://aotrix@localhost:5432/hearthstonegameinfo"}; // FIXME: load a .xml file instead pqxx::work aTransaction{aConnection}; - HearthstoneAPIHandler aAPIConnector; + HearthstoneAPI aAPIConnector; std::string aCards = aAPIConnector.get_cards("/?locale=fr_FR&gameMode=constructed"); // FIXME: use a struct as a parameter clearCardDatabase(aTransaction); -- cgit v1.2.3 From d6cf50a63e35eb8812b4fb576399336d425c7b76 Mon Sep 17 00:00:00 2001 From: Aotrix <9aotrix@protonmail.com> Date: Wed, 1 Oct 2025 19:46:58 +0200 Subject: added more FOLDERS --- include/hearthstoneAPI.hpp | 25 ++++++++++ src/hearthstoneAPI.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 include/hearthstoneAPI.hpp create mode 100644 src/hearthstoneAPI.cpp diff --git a/include/hearthstoneAPI.hpp b/include/hearthstoneAPI.hpp new file mode 100644 index 0000000..f677080 --- /dev/null +++ b/include/hearthstoneAPI.hpp @@ -0,0 +1,25 @@ +#pragma once +#include +#include + +class HearthstoneAPI { + public: + HearthstoneAPI(); + std::string get_cards(const std::string &t_card_parameters); + + private: + static size_t curl_callback_string_output(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s); + std::chrono::seconds get_current_time(); + std::string extract_string_with_regex(const std::string &t_str, const std::string t_regex); + void write_new_token_info_to_file(const std::string &t_file_path); + void refresh_token_if_needed(); + std::string get_valid_token(); + std::string generate_request_header(); + const std::string get_url(const std::string &t_parameters); + const std::string get_set_of_cards_url(const std::string &t_card_parameters); + std::string make_api_request(const char *t_url, const char *t_header); + std::string make_api_request(const std::string &t_url, const std::string &t_header); + + std::string m_access_token; + std::string m_header; +}; diff --git a/src/hearthstoneAPI.cpp b/src/hearthstoneAPI.cpp new file mode 100644 index 0000000..cb72b11 --- /dev/null +++ b/src/hearthstoneAPI.cpp @@ -0,0 +1,121 @@ +#include "hearthstoneAPI.hpp" +#include +#include +#include +#include +#include +#include + +HearthstoneAPI::HearthstoneAPI() : m_access_token(get_valid_token()), m_header(generate_request_header()) {} + +size_t HearthstoneAPI::curl_callback_string_output(void *t_contents, size_t t_size, size_t t_nmemb, std::string *t_s) { + size_t newLength = t_size * t_nmemb; + try { + t_s->append((char *)t_contents, newLength); + } catch (std::bad_alloc &e) { + // handle memory problem + return 0; + } + return newLength; +} + +std::chrono::seconds HearthstoneAPI::get_current_time() { + return std::chrono::duration_cast(std::chrono::utc_clock::now().time_since_epoch()); +} + +std::string HearthstoneAPI::extract_string_with_regex(const std::string &t_str, const std::string t_regex) { + std::regex regex(t_regex); + auto it = std::sregex_iterator(t_str.begin(), t_str.end(), regex); + std::smatch match = *it; + return match[1].str(); +} + +void HearthstoneAPI::write_new_token_info_to_file(const std::string &t_file_path) { + std::ofstream token_file(t_file_path); + CURL *curl = curl_easy_init(); + std::string token_info; + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, "https://oauth.battle.net/token"); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback_string_output); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token_info); + curl_easy_setopt(curl, CURLOPT_USERPWD, + "2b50f1123b424d5e9c21971a110fdd04:" + "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6"); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); + CURLcode result_code; + result_code = curl_easy_perform(curl); + curl_easy_cleanup(curl); + if (result_code != CURLE_OK) { + std::cerr << "ERROR: token request failed" << std::endl; + std::exit(EXIT_FAILURE); + } + } + std::string token = extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\""); + std::chrono::seconds expires_at = get_current_time() + std::chrono::hours(24); + token_file << token << " " << expires_at.count() << '\n'; +} + +void HearthstoneAPI::refresh_token_if_needed() { + std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); + std::ifstream token_file(file_path); + std::string line; + std::getline(token_file, line); + if (line.empty()) { + write_new_token_info_to_file(file_path); + } else { + long expire_at = std::stoull(line.substr(line.find(" ") + 1)); + if (std::chrono::duration_cast(std::chrono::utc_clock::now().time_since_epoch()) + .count() >= expire_at) { + write_new_token_info_to_file(file_path); + } + } +} +std::string HearthstoneAPI::get_valid_token() { + refresh_token_if_needed(); + std::string file_path = std::format("{}/resources/token_info.txt", SOURCE_DIR_PATH); + std::ifstream token_file(file_path); + std::string line; + std::getline(token_file, line); + std::string token = line.substr(0, line.find(" ")); + return token; +} + +std::string HearthstoneAPI::generate_request_header() { + return std::format("Authorization: Bearer {}", m_access_token); +} + +const std::string HearthstoneAPI::get_url(const std::string &t_parameters) { + return std::format("https://eu.api.blizzard.com/hearthstone/{}", t_parameters); +} + +const std::string HearthstoneAPI::get_set_of_cards_url(const std::string &t_card_parameters) { + return get_url(std::format("cards{}", t_card_parameters)); +} + +std::string HearthstoneAPI::make_api_request(const char *t_url, const char *t_header) { + CURL *curl = curl_easy_init(); + std::string request_output; + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, t_url); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback_string_output); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &request_output); + struct curl_slist *chunk = nullptr; + chunk = curl_slist_append(chunk, t_header); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); + curl_easy_perform(curl); + curl_easy_cleanup(curl); + } + return request_output; +} + +std::string HearthstoneAPI::make_api_request(const std::string &t_url, const std::string &t_header) { + return make_api_request(t_url.c_str(), t_header.c_str()); +} + +std::string HearthstoneAPI::get_cards(const std::string &t_card_parameters) { + return make_api_request(get_set_of_cards_url(t_card_parameters), m_header); +} -- cgit v1.2.3