diff options
| -rw-r--r-- | include/postgresHandler.h | 2 | ||||
| -rw-r--r-- | src/main.cpp | 22 | ||||
| -rw-r--r-- | src/postgresHandler.cpp | 36 |
3 files changed, 39 insertions, 21 deletions
diff --git a/include/postgresHandler.h b/include/postgresHandler.h index 1aaa422..2be688e 100644 --- a/include/postgresHandler.h +++ b/include/postgresHandler.h @@ -7,3 +7,5 @@ const pqxx::result execute_query(pqxx::work &t_transaction, void display_query_result(const pqxx::result &t_r); void add_cards_to_database(const std::string &t_cards, pqxx::work &t_tx); + +void refresh_cards_table(); diff --git a/src/main.cpp b/src/main.cpp index fdd59cb..d0aa649 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,27 +1,7 @@ -#include "hearthstoneAPIHandler.h" #include "postgresHandler.h" -#include <iostream> #include <pqxx/pqxx> int main() { - HearthstoneAPIHandler hs_handler; - pqxx::connection cx{ - "postgresql://aotrix@localhost:5432/hearthstonegameinfo"}; - pqxx::work tx{cx}; - - try { - std::string cards = hs_handler.get_cards( - "/?locale=fr_FR&gameMode=constructed&pageSize=40"); - std::cout << cards << std::endl; - add_cards_to_database(cards, tx); - tx.commit(); - } catch (pqxx::sql_error const &e) { - std::cerr << "SQL error: " << e.what() << std::endl; - std::cerr << "Query was: " << e.query() << std::endl; - return 2; - } catch (std::exception const &e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; - } + refresh_cards_table(); return 0; } diff --git a/src/postgresHandler.cpp b/src/postgresHandler.cpp index 8b8c6ad..26828bc 100644 --- a/src/postgresHandler.cpp +++ b/src/postgresHandler.cpp @@ -1,4 +1,5 @@ #include "postgresHandler.h" +#include "hearthstoneAPIHandler.h" #include <cctype> #include <cstddef> #include <cstdlib> @@ -125,3 +126,38 @@ void add_cards_to_database(const std::string &t_cards, pqxx::work &t_tx) { closing_bracket_pos = t_cards.find("}", closing_bracket_pos + 1); } } + +void refresh_cards_table() { + HearthstoneAPIHandler hs_handler; + pqxx::connection cx{ + "postgresql://aotrix@localhost:5432/hearthstonegameinfo"}; + pqxx::work tx{cx}; + + try { + execute_query(tx, "DELETE FROM cards"); + std::regex key_regex("\"pageCount\":([0-9]+)"); + std::string cards = + hs_handler.get_cards("/?locale=fr_FR&gameMode=constructed"); + add_cards_to_database(cards, tx); + auto nb_pages_begin = + std::sregex_iterator(cards.begin(), cards.end(), key_regex); + auto nb_pages_end = std::sregex_iterator(); + if (nb_pages_begin != nb_pages_end) { + int nb_pages = std::stoi((*nb_pages_begin)[1].str()); + // FIXME: [Process exited 139] + // for (int i = 2; i < nb_pages; i++) { + for (int i = 2; i < 4; i++) { + cards = hs_handler.get_cards(std::format( + "/?locale=fr_FR&gameMode=constructed&page={}", i)); + add_cards_to_database(cards, tx); + } + } + display_query_result(execute_query(tx, "SELECT COUNT(*) FROM cards")); + tx.commit(); + } catch (pqxx::sql_error const &e) { + std::cerr << "SQL error: " << e.what() << std::endl; + std::cerr << "Query was: " << e.query() << std::endl; + } catch (std::exception const &e) { + std::cerr << "Error: " << e.what() << std::endl; + } +} |
