1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#include <cctype>
#include <cstdlib>
#include <curl/curl.h>
#include <curl/easy.h>
#include <iostream>
#include <pqxx/pqxx>
#include <regex>
const pqxx::result execute_query(pqxx::work &t_transaction,
std::string_view t_query) {
return t_transaction.exec(t_query);
}
void do_curl_request(const char *t_url, const char *t_header) {
CURL *curl = curl_easy_init();
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);
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);
}
}
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());
}
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;
}
size_t curl_callback_token_request(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::string 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();
}
std::string get_valid_token() {
CURL *curl = curl_easy_init();
std::string output;
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_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(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;
return token;
}
void display_result(pqxx::result t_r) {
// FIXME: not flexible
for (auto row : t_r) {
std::cout << std::format(
"{}; {}; {}; {}; {}", row[0].as<std::string>(),
row[1].as<std::string>(), row[2].as<std::string>(),
row[3].as<std::string>(), row[4].as<std::string>());
}
}
int main() {
pqxx::connection cx{
"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"));
return 0;
}
|