feat: coingecko api
This commit is contained in:
parent
f66a32d6bf
commit
45547536f0
14
config.def.h
14
config.def.h
@ -1,10 +1,12 @@
|
|||||||
#define CUR "$"
|
#define FMT "%s=$%f"
|
||||||
|
|
||||||
struct wallet_s {
|
// curl https://api.coingecko.com/api/v3/simple/supported_vs_currencies
|
||||||
char *from;
|
#define VS "usd"
|
||||||
char *to;
|
|
||||||
float value;
|
// curl https://api.coingecko.com/api/v3/coins/list
|
||||||
|
static const struct wallet_s {
|
||||||
|
char *id; char *fmt; float value;
|
||||||
} WALLET[] = {
|
} WALLET[] = {
|
||||||
{"BTC", "USDT", 1.00000000},
|
{"bitcoin", "BTC", 1.00000000},
|
||||||
{0},
|
{0},
|
||||||
};
|
};
|
||||||
|
69
crypto.c
69
crypto.c
@ -7,18 +7,15 @@
|
|||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#define BUF_SIZE 1024
|
#define ARRLEN(a) (sizeof (a) / sizeof *(a))
|
||||||
|
#define BUF_SIZE 256 * ARRLEN(WALLET)
|
||||||
|
|
||||||
// [{"symbol":"BTCUSDT","price":"69420.69420"}]
|
// {"bitcoin":{"usd":69420.69}}
|
||||||
static int get_next_symbol(char *buf, float *price)
|
static int get_next_symbol(char *buf, float *price)
|
||||||
{
|
{
|
||||||
static const char output[] = "\"symbol\":\"%*[^\"]\",\"price\":\"%f\"";
|
static const char output[] = "\"%*[a-z]\":{\"" VS "\":%f}";
|
||||||
char* tmp = strtok(buf, "{");
|
char *tmp = strtok(buf, ",");
|
||||||
|
|
||||||
if (!tmp) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
tmp = strtok(0, "}");
|
|
||||||
if (!tmp) {
|
if (!tmp) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -27,73 +24,65 @@ static int get_next_symbol(char* buf, float* price)
|
|||||||
|
|
||||||
static size_t write_callback(char *response, size_t s, size_t n, void *buf)
|
static size_t write_callback(char *response, size_t s, size_t n, void *buf)
|
||||||
{
|
{
|
||||||
size_t size = s * n;
|
return snprintf(buf, MIN(s * n, BUF_SIZE), response);
|
||||||
|
|
||||||
return snprintf(buf, MIN(size, BUF_SIZE), response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *build_url(void)
|
static char *build_url(void)
|
||||||
{
|
{
|
||||||
const char base[] =
|
const char base[] = "https://api.coingecko.com/api/v3/simple/price?ids=%s&vs_currencies=" VS;
|
||||||
"https://api.binance.com/api/v3/ticker/price?symbols=[";
|
size_t ids_size = 0;
|
||||||
size_t s = sizeof base;
|
|
||||||
char *url;
|
char *url;
|
||||||
char* tmp = (char*)s - 1;
|
char ids[BUF_SIZE] = {0};
|
||||||
|
|
||||||
for (size_t i = 0; WALLET[i].to; i++) {
|
for (size_t i = 0; WALLET[i].id; i++) {
|
||||||
s += strlen(WALLET[i].from) + strlen(WALLET[i].to) + 3; // ["SYMBOL",]
|
strcpy(ids + ids_size, WALLET[i].id);
|
||||||
|
ids_size += strlen(WALLET[i].id);
|
||||||
|
ids[ids_size++] = ',';
|
||||||
}
|
}
|
||||||
url = malloc(s);
|
ids[ids_size - 1] = '\0';
|
||||||
|
url = malloc(sizeof base + ids_size * (sizeof *url));
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
strcpy(url, base);
|
sprintf(url, base, ids);
|
||||||
tmp += (size_t)url;
|
|
||||||
for (size_t i = 0; WALLET[i].to; i++) {
|
|
||||||
tmp += sprintf(tmp, "\"%s%s\",", WALLET[i].from, WALLET[i].to);
|
|
||||||
}
|
|
||||||
url[strlen(url) - 1] = ']';
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
static CURL* init_curl(char* response)
|
static int perform_request(char *response)
|
||||||
{
|
{
|
||||||
char *url = build_url();
|
char *url = build_url();
|
||||||
CURL *curl = curl_easy_init();
|
CURL *curl = curl_easy_init();
|
||||||
|
|
||||||
|
|
||||||
if (!url || !curl) {
|
if (!url || !curl) {
|
||||||
free(url);
|
free(url);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||||
curl_easy_perform(curl);
|
curl_easy_perform(curl);
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
free(url);
|
free(url);
|
||||||
return curl;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
char response[BUF_SIZE];
|
char response[BUF_SIZE];
|
||||||
CURL* curl = init_curl(response);
|
int err = perform_request(response);
|
||||||
int err;
|
float price, current, sum = 0;
|
||||||
float price, current;
|
|
||||||
float sum = 0;
|
|
||||||
|
|
||||||
if (!curl) {
|
if (err) {
|
||||||
error(1, errno, "cannot init curl");
|
error(1, errno, "cannot init curl");
|
||||||
}
|
}
|
||||||
err = get_next_symbol(response, &price);
|
err = get_next_symbol(response + 1, &price);
|
||||||
for (size_t i = 0; !err && WALLET[i].to; i++) {
|
for (size_t i = 0; !err && WALLET[i].id; i++) {
|
||||||
current = price * WALLET[i].value;
|
current = price * WALLET[i].value;
|
||||||
printf("%s=%.2f" CUR " ", WALLET[i].from, current);
|
printf(FMT " ", WALLET[i].fmt, current);
|
||||||
sum += current;
|
sum += current;
|
||||||
err = get_next_symbol(0, &price);
|
err = get_next_symbol(NULL, &price);
|
||||||
}
|
}
|
||||||
printf("TOT=%.2f" CUR "\n", sum);
|
printf(FMT, "TOT", sum);
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user