Change the API used
This commit is contained in:
parent
d7b5bdc15a
commit
93ffd335e4
42
main.cpp
42
main.cpp
|
@ -16,23 +16,23 @@ double getConversionRate() {
|
|||
|
||||
curl = curl_easy_init();
|
||||
if(curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd");
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "https://api.diadata.org/v1/assetQuotation/Solana/0x0000000000000000000000000000000000000000");
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if(res == CURLE_OK) {
|
||||
std::size_t usdPos = readBuffer.find("\"usd\":");
|
||||
if (usdPos != std::string::npos) {
|
||||
std::size_t start = readBuffer.find(':', usdPos) + 1;
|
||||
std::size_t end = readBuffer.find_first_of("},", start);
|
||||
std::size_t pricePos = readBuffer.find("\"Price\":");
|
||||
if (pricePos != std::string::npos) {
|
||||
std::size_t start = readBuffer.find(':', pricePos) + 1;
|
||||
std::size_t end = readBuffer.find_first_of(",}", start);
|
||||
if(start < end) {
|
||||
std::string usdValue = readBuffer.substr(start, end - start);
|
||||
usdValue.erase(0, usdValue.find_first_not_of(" \n\r\t"));
|
||||
usdValue.erase(usdValue.find_last_not_of(" \n\r\t") + 1);
|
||||
std::string priceValue = readBuffer.substr(start, end - start);
|
||||
priceValue.erase(0, priceValue.find_first_not_of(" \n\r\t"));
|
||||
priceValue.erase(priceValue.find_last_not_of(" \n\r\t") + 1);
|
||||
try {
|
||||
rate = std::stod(usdValue);
|
||||
rate = std::stod(priceValue);
|
||||
} catch (const std::invalid_argument& ia) {
|
||||
std::cerr << "Invalid argument: " << ia.what() << '\n';
|
||||
} catch (const std::out_of_range& oor) {
|
||||
|
@ -40,7 +40,7 @@ double getConversionRate() {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
std::cerr << "USD rate not found in response." << std::endl;
|
||||
std::cerr << "Price not found in response." << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "CURL failed with error code: " << res << std::endl;
|
||||
|
@ -85,7 +85,23 @@ void processChoice(int choice, double rate) {
|
|||
noecho();
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(int argc, char *argv[]) {
|
||||
bool standardMode = false;
|
||||
|
||||
if (argc > 1 && std::string(argv[1]) == "-standard") {
|
||||
standardMode = true;
|
||||
}
|
||||
|
||||
double rate = getConversionRate();
|
||||
if (standardMode) {
|
||||
if (rate == 0.0) {
|
||||
std::cerr << "Failed to fetch conversion rate. Please try again later.\n";
|
||||
} else {
|
||||
std::cout << "Solana is Currently $" << rate << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
initscr();
|
||||
clear();
|
||||
noecho();
|
||||
|
@ -94,8 +110,6 @@ int main() {
|
|||
start_color();
|
||||
init_pair(1, COLOR_CYAN, COLOR_BLACK);
|
||||
|
||||
double rate = 0.0;
|
||||
|
||||
WINDOW *menu_win = newwin(7, 60, 4, 4);
|
||||
keypad(menu_win, TRUE);
|
||||
refresh();
|
||||
|
@ -161,4 +175,4 @@ int main() {
|
|||
}
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue