Change the API used

This commit is contained in:
Wizzard 2024-05-29 03:37:12 -04:00
parent d7b5bdc15a
commit 93ffd335e4
1 changed files with 28 additions and 14 deletions

View File

@ -16,23 +16,23 @@ double getConversionRate() {
curl = curl_easy_init(); curl = curl_easy_init();
if(curl) { 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_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if(res == CURLE_OK) { if(res == CURLE_OK) {
std::size_t usdPos = readBuffer.find("\"usd\":"); std::size_t pricePos = readBuffer.find("\"Price\":");
if (usdPos != std::string::npos) { if (pricePos != std::string::npos) {
std::size_t start = readBuffer.find(':', usdPos) + 1; std::size_t start = readBuffer.find(':', pricePos) + 1;
std::size_t end = readBuffer.find_first_of("},", start); std::size_t end = readBuffer.find_first_of(",}", start);
if(start < end) { if(start < end) {
std::string usdValue = readBuffer.substr(start, end - start); std::string priceValue = readBuffer.substr(start, end - start);
usdValue.erase(0, usdValue.find_first_not_of(" \n\r\t")); priceValue.erase(0, priceValue.find_first_not_of(" \n\r\t"));
usdValue.erase(usdValue.find_last_not_of(" \n\r\t") + 1); priceValue.erase(priceValue.find_last_not_of(" \n\r\t") + 1);
try { try {
rate = std::stod(usdValue); rate = std::stod(priceValue);
} catch (const std::invalid_argument& ia) { } catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n'; std::cerr << "Invalid argument: " << ia.what() << '\n';
} catch (const std::out_of_range& oor) { } catch (const std::out_of_range& oor) {
@ -40,7 +40,7 @@ double getConversionRate() {
} }
} }
} else { } else {
std::cerr << "USD rate not found in response." << std::endl; std::cerr << "Price not found in response." << std::endl;
} }
} else { } else {
std::cerr << "CURL failed with error code: " << res << std::endl; std::cerr << "CURL failed with error code: " << res << std::endl;
@ -85,7 +85,23 @@ void processChoice(int choice, double rate) {
noecho(); 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(); initscr();
clear(); clear();
noecho(); noecho();
@ -94,8 +110,6 @@ int main() {
start_color(); start_color();
init_pair(1, COLOR_CYAN, COLOR_BLACK); init_pair(1, COLOR_CYAN, COLOR_BLACK);
double rate = 0.0;
WINDOW *menu_win = newwin(7, 60, 4, 4); WINDOW *menu_win = newwin(7, 60, 4, 4);
keypad(menu_win, TRUE); keypad(menu_win, TRUE);
refresh(); refresh();
@ -161,4 +175,4 @@ int main() {
} }
endwin(); endwin();
return 0; return 0;
} }