aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Kellermann2018-02-23 12:03:42 -0500
committerAntony Kellermann2018-02-23 12:03:42 -0500
commitb7aedebf1aefa690005751757d05bb213569ba2c (patch)
tree40cc02f8860837115092836a12204407b8cb41f6
parenteda8083231efffd452c0ed4376f424d6d0c6c86b (diff)
downloadaur-b7aedebf1aefa690005751757d05bb213569ba2c.tar.gz
Added new command 'info' for cryptos
-rw-r--r--api.c85
-rw-r--r--api.h34
-rw-r--r--main.c12
3 files changed, 128 insertions, 3 deletions
diff --git a/api.c b/api.c
index 2fc7ec17c9c8..37c6ad7e6822 100644
--- a/api.c
+++ b/api.c
@@ -1,6 +1,6 @@
#include "api.h"
-String* api_string_init() {
+String* api_string_init(void) {
String* pString = (String*) malloc(sizeof(String));
if (pString != NULL) {
pString->len = 0;
@@ -17,6 +17,29 @@ String* api_string_init() {
return pString;
}
+Info* api_info_init(void){
+ Info* pInfo = malloc(sizeof(Info));
+ if (pInfo != NULL){
+ pInfo->name = malloc(64);
+ pInfo->symbol = malloc(64);
+ if (pInfo->name == NULL || pInfo->symbol == NULL){
+ fprintf(stderr, "malloc() failed\n");
+ exit(EXIT_FAILURE);
+ }
+ pInfo->price = EMPTY;
+ pInfo->change_1d = EMPTY;
+ pInfo->change_7d = EMPTY;
+ pInfo->change_30d = EMPTY;
+ pInfo->div_yield = EMPTY;
+ pInfo->marketcap = EMPTY;
+ pInfo->volume_1d = EMPTY;
+ } else {
+ fprintf(stderr, "malloc() failed\n");
+ exit(EXIT_FAILURE);
+ }
+ return pInfo;
+}
+
size_t api_string_writefunc(void* ptr, size_t size, size_t nmemb, String* hString) {
String* pString = hString;
size_t new_len = pString->len + size * nmemb;
@@ -234,6 +257,58 @@ void json_print_news(Json* jobj) {
}
}
+void api_print_info(char* ticker_name_string){
+ Info* ticker_info = coinmarketcap_get_info(ticker_name_string);
+ if (ticker_info == NULL){
+ printf("Invalid symbol!\n");
+ return;
+ }
+ if (ticker_info->name != NULL)
+ printf("Name: %s\n", ticker_info->name);
+ if (ticker_info->symbol != NULL)
+ printf("Symbol: %s\n", ticker_info->symbol);
+ if (ticker_info->price != EMPTY)
+ printf("Price: $%lf\n", ticker_info->price);
+ if (ticker_info->change_1d != EMPTY)
+ printf("Percent change 24h: %lf%%\n", ticker_info->change_1d);
+ if (ticker_info->change_7d != EMPTY)
+ printf("Percent change 7d: %lf%%\n", ticker_info->change_7d);
+ if (ticker_info->change_30d != EMPTY)
+ printf("Percent change 30d: %lf%%\n", ticker_info->change_30d);
+ if (ticker_info->div_yield != EMPTY)
+ printf("Dividend yield: %lf%%\n", ticker_info->div_yield);
+ if (ticker_info->marketcap != EMPTY)
+ printf("Market Cap: $%ld\n", ticker_info->marketcap);
+ if (ticker_info->volume_1d != EMPTY)
+ printf("Volume 24h: $%ld\n", ticker_info->volume_1d);
+ api_info_destroy(&ticker_info);
+}
+
+Info* coinmarketcap_get_info(char* ticker_name_string){
+ char coinmarketcap_api_string[64];
+ sprintf(coinmarketcap_api_string, "https://api.coinmarketcap.com/v1/ticker/%s", ticker_name_string);
+ String* pString = api_curl_data(coinmarketcap_api_string, NULL);
+ if (pString->data[0] == '{') { //Invalid symbol
+ api_string_destroy(&pString);
+ return NULL;
+ }
+ Info* ticker_info = api_info_init();
+ Json* jobj = json_tokener_parse(pString->data);
+ Json* data = json_object_array_get_idx(jobj, 0);
+ strcpy(ticker_info->name, json_object_get_string(json_object_object_get(data, "name")));
+ //ticker_info->name = json_object_get_string(json_object_object_get(data, "name"));
+ strcpy(ticker_info->symbol, json_object_get_string(json_object_object_get(data, "symbol")));
+ //ticker_info->symbol = json_object_get_string(json_object_object_get(data, "symbol"));
+ ticker_info->price = strtod(json_object_get_string(json_object_object_get(data, "price_usd")), NULL);
+ ticker_info->change_1d = strtod(json_object_get_string(json_object_object_get(data, "percent_change_24h")), NULL);
+ ticker_info->change_7d = strtod(json_object_get_string(json_object_object_get(data, "percent_change_7d")), NULL);
+ ticker_info->marketcap = strtol(json_object_get_string(json_object_object_get(data, "market_cap_usd")), NULL, 10);
+ ticker_info->volume_1d = strtol(json_object_get_string(json_object_object_get(data, "24h_volume_usd")), NULL, 10);
+ json_object_put(jobj);
+ api_string_destroy(&pString);
+ return ticker_info;
+}
+
const char* google_shorten_link(char* url_string) {
char post_string[1024], copy[1024];
sprintf(post_string, "{\"longUrl\": \"%s\"}", url_string); //Format HTTP POST
@@ -278,4 +353,12 @@ void api_string_destroy(String** phString) {
free(pString->data);
free(*phString);
*phString = NULL;
+}
+
+void api_info_destroy(Info** phInfo) {
+ Info* pInfo = *phInfo;
+ free((void*)pInfo->name);
+ free((void*)pInfo->symbol);
+ free(*phInfo);
+ *phInfo = NULL;
} \ No newline at end of file
diff --git a/api.h b/api.h
index 365525756faa..1fab36f297b5 100644
--- a/api.h
+++ b/api.h
@@ -9,6 +9,8 @@
#ifndef API_H
#define API_H
+#define EMPTY (-999)
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -23,6 +25,18 @@ struct string {
typedef struct string String;
+struct info {
+ char* name;
+ char* symbol;
+ double price;
+ double change_1d, change_7d, change_30d;
+ double div_yield;
+ long marketcap;
+ long volume_1d;
+};
+
+typedef struct info Info;
+
typedef struct json_object Json;
/**
@@ -33,6 +47,12 @@ typedef struct json_object Json;
String* api_string_init(void);
/**
+ * Creates and returns an Info object
+ * @return Info object
+ */
+Info* api_info_init(void);
+
+/**
* GETs data from API server and returns it in a String
* @param url API url to GET
* @param post_field data needed for POST
@@ -96,6 +116,14 @@ void news_print_top_three(char* ticker_name_string);
*/
void json_print_news(Json* jobj);
+void api_print_info(char* ticker_name_string);
+
+/**
+ * Prints info related to the given cryptocurrency
+ * @param ticker_name_string the cryptocurrency's name
+ */
+Info* coinmarketcap_get_info(char* ticker_name_string);
+
/**
* Given a url, returns a shorter link using goo.gl
* @param url_string the link to shorten
@@ -117,4 +145,10 @@ const char* strip_char(char* string, char c);
*/
void api_string_destroy(String** phString);
+/**
+ * Destroys Info object and frees memory
+ * @param phInfo the Info to destroy
+ */
+void api_info_destroy(Info** phString);
+
#endif \ No newline at end of file
diff --git a/main.c b/main.c
index de8d7855f99c..626397fae951 100644
--- a/main.c
+++ b/main.c
@@ -44,11 +44,19 @@ int main(int argc, char* argv[]) {
portfolio_legacy_convert();
//Encrypt/decrypt
- else if (strcmp(argv[1], "encrypt") == 0 && argc == 2)
+ else if (strcmp(cmd, "encrypt") == 0 && argc == 2)
portfolio_encrypt_decrypt(ENCRYPT, fp, NULL);
- else if (strcmp(argv[1], "decrypt") == 0 && argc == 2)
+ else if (strcmp(cmd, "decrypt") == 0 && argc == 2)
portfolio_encrypt_decrypt(DECRYPT, fp, NULL);
+ // Info
+ else if (strcmp(cmd, "info") == 0 && argc == 3) {
+ char sym[strlen(argv[2]) + 1];
+ strcpy(sym, argv[2]);
+ strtoupper(sym);
+ api_print_info(sym);
+ }
+
// Check
else if (strcmp(cmd, "check") == 0) {
if (argc < 3) {