diff options
| author | Aotrix <[email protected]> | 2025-03-08 03:28:37 +0100 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2025-03-08 03:28:37 +0100 |
| commit | 0f3e727c72309dd37ed9a2276e5554963da18207 (patch) | |
| tree | f79f0c22f0247cab608303e48cb3ea172983d1dc | |
| parent | 47d171cf89eabd15a9332016575fc5c386d44f09 (diff) | |
token generation worksgit add -A
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | resources/token_info.txt | 1 | ||||
| -rw-r--r-- | src/main.cpp | 75 |
3 files changed, 54 insertions, 23 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index dc7a376..90d662c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ set(CMAKE_CXX_STANDARD 20) set(CXX_STANDARD_REQUIRED ON) set(CMAKE_BUILD_TYPE Debug) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20") +add_definitions(-DSOURCE_DIR_PATH="${CMAKE_SOURCE_DIR}") find_package(Catch2 3 REQUIRED) add_executable(tests src/tests/tests.cpp) diff --git a/resources/token_info.txt b/resources/token_info.txt new file mode 100644 index 0000000..fa14edd --- /dev/null +++ b/resources/token_info.txt @@ -0,0 +1 @@ +EU5xHPtrrZdPWXDBDe26WJuNnG0wyXk1rl 483730 diff --git a/src/main.cpp b/src/main.cpp index 5b67817..40a6da7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,9 @@ #include <cctype> +#include <chrono> #include <cstdlib> #include <curl/curl.h> #include <curl/easy.h> +#include <fstream> #include <iostream> #include <pqxx/pqxx> #include <regex> @@ -11,7 +13,7 @@ const pqxx::result execute_query(pqxx::work &t_transaction, return t_transaction.exec(t_query); } -void do_curl_request(const char *t_url, const char *t_header) { +void make_api_request(const char *t_url, const char *t_header) { CURL *curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, t_url); @@ -25,22 +27,17 @@ void do_curl_request(const char *t_url, const char *t_header) { } } -void do_curl_request(const std::string &t_url, const std::string &t_header) { - do_curl_request(t_url.c_str(), t_header.c_str()); +void make_api_request(const std::string &t_url, const std::string &t_header) { + make_api_request(t_url.c_str(), t_header.c_str()); } std::string generate_request_header(const std::string &t_access_token) { return std::format("Authorization: Bearer {}", t_access_token); } -const std::string get_url() { - // FIXME: not flexible - return "https://eu.api.blizzard.com/hearthstone/cards/678?locale=fr_FR"; -} - -bool is_last_generated_token_still_valid() { - // TODO: - return false; +std::chrono::hours get_current_time() { + return std::chrono::duration_cast<std::chrono::hours>( + std::chrono::utc_clock::now().time_since_epoch()); } size_t curl_callback_token_request(void *t_contents, size_t t_size, @@ -63,16 +60,17 @@ std::string extract_string_with_regex(const std::string &t_str, return match[1].str(); } -std::string get_valid_token() { +void write_new_token_info_to_file(const std::string &file_path) { + std::ofstream token_file(file_path); CURL *curl = curl_easy_init(); - std::string output; + 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_token_request); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &output); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &token_info); curl_easy_setopt(curl, CURLOPT_USERPWD, "2b50f1123b424d5e9c21971a110fdd04:" "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6"); @@ -87,14 +85,45 @@ std::string get_valid_token() { } } std::string token = - extract_string_with_regex(output, "\"access_token\":\"(\\w+)\""); - std::string expires_in = - extract_string_with_regex(output, "\"expires_in\":(\\w+)"); - std::cout << std::format("Token trouvé: {}, {}", token, expires_in) - << std::endl; + extract_string_with_regex(token_info, "\"access_token\":\"(\\w+)\""); + std::chrono::hours expires_at = get_current_time() + std::chrono::hours(24); + token_file << token << " " << expires_at.count(); +} + +void 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::hours>( + std::chrono::utc_clock::now().time_since_epoch()) + .count() >= expire_at) { + write_new_token_info_to_file(file_path); + } + } +} + +std::string 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; } +const std::string get_url() { + // FIXME: not flexible + return "https://eu.api.blizzard.com/hearthstone/cards/678?locale=fr_FR"; +} + void display_result(pqxx::result t_r) { // FIXME: not flexible for (auto row : t_r) { @@ -110,9 +139,9 @@ int main() { "postgresql://aotrix@localhost:5432/hearthstonegameinfo"}; pqxx::work tx{cx}; std::string access_token = get_valid_token(); - // std::string header = generate_request_header(access_token); - // do_curl_request(get_url(), header); - // std::cout << std::endl << std::endl; - // display_result(execute_query(tx, "SELECT * FROM cards")); + std::string header = generate_request_header(access_token); + make_api_request(get_url(), header); + std::cout << std::endl << std::endl; + display_result(execute_query(tx, "SELECT * FROM cards")); return 0; } |
