diff options
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | api.c | 82 | ||||
-rw-r--r-- | api.h | 12 | ||||
-rw-r--r-- | portfolio.c | 30 |
4 files changed, 60 insertions, 71 deletions
diff --git a/README.md b/README.md index 459846ca9a63..10a9096d9228 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ API. Shortened links are provided by Google's URL Shortener API. Please do not abuse the APIs by repeatedly requesting information. Read the provided license for more information. #### Future Ideas/To-do -* Historical support -- 7d/28d profits +* Historical support -- 7d/28d profits (wait for coinmarketcap release) * Different ways to sort "check all" * Look for API to replace Morningstar for MUTF/OTCMKTS data, preferably with intraday data (wait for iex v2 probably) @@ -97,5 +97,6 @@ intraday data (wait for iex v2 probably) and portfolio distribution (tried to implement, but IEX API is unpredictable; wait for v2) * Make printouts pretty with ncurses?? * Check without re-encrypting in case sigint -* Make strip char work in place instead of allocing -* Format prices with commas (set locale)
\ No newline at end of file +* Format prices with commas (set locale) +* Function to add new attributes to portfolio if not found +* Improve code documentation
\ No newline at end of file @@ -222,39 +222,31 @@ void news_print_top_three(const char* ticker_name_string) { void json_print_news(Json* jobj) { Json* article_list = json_object_object_get(jobj, "articles"), * article; - char* author_string, * title_string, * source_string, * url_string, * stripped, * shortened; - const char* date_string; - int results = (int) strtol(json_object_to_json_string(json_object_object_get(jobj, "totalResults")), NULL, - 10); + char author_string[512], title_string[512], source_string[512], url_string[512], date_string[512], * shortened_url; + int results = (int) strtol(json_object_to_json_string(json_object_object_get(jobj, "totalResults")), NULL, 10); for (int i = 0; i < results && i < 3; i++) { article = json_object_array_get_idx(article_list, (size_t) i); - author_string = strip_char( - json_object_to_json_string(json_object_object_get(article, "author")), - '\\'); //Strip all attributes of backslashes - title_string = strip_char( - json_object_to_json_string(json_object_object_get(article, "title")), '\\'); - source_string = strip_char(json_object_to_json_string( - json_object_object_get(json_object_object_get(article, "source"), "name")), '\\'); - date_string = json_object_to_json_string(json_object_object_get(article, "publishedAt")); - url_string = strip_char(json_object_to_json_string(json_object_object_get(article, "url")), - '\\'); - stripped = strip_char(url_string, '\"'); //Strip url of quotes - shortened = google_shorten_link(stripped); //Shorten link + strcpy(author_string, json_object_to_json_string(json_object_object_get(article, "author"))); + strip_char(author_string, '\\'); // Strip all attributes of escape characters + strcpy(title_string, json_object_to_json_string(json_object_object_get(article, "title"))); + strip_char(title_string, '\\'); + strcpy(source_string, + json_object_to_json_string(json_object_object_get(json_object_object_get(article, "source"), "name"))); + strip_char(source_string, '\\'); + strcpy(date_string, json_object_to_json_string(json_object_object_get(article, "publishedAt"))); + date_string[11] = '\"'; // End string after day of month + date_string[12] = '\0'; + strcpy(url_string, json_object_to_json_string(json_object_object_get(article, "url"))); + strip_char(url_string, '\\'); + strip_char(url_string, '\"'); + shortened_url = google_shorten_link(url_string); // Shorten link with google API printf("Title: %s Source: %s ", title_string, source_string); if (strcmp(author_string, "null") != 0) //Some articles don't list authors or dates printf("Author: %s ", author_string); - if (strcmp(date_string, "null") != 0) { - for (int j = 0; j < 11; j++) - putchar(date_string[j]); - putchar('\"'); - } - printf("Url: \"%s\"\n", shortened); - free(author_string); - free(title_string); - free(source_string); - free(url_string); - free(stripped); - free(shortened); + if (strcmp(date_string, "null") != 0) + printf("Date: %s ", date_string); + printf("Url: \"%s\"\n", shortened_url); + free(shortened_url); } } @@ -394,42 +386,34 @@ Info* coinmarketcap_get_info(const char* ticker_name_string) { } char* google_shorten_link(const char* url_string) { - char post_string[1024], copy[1024]; + char post_string[1024]; sprintf(post_string, "{\"longUrl\": \"%s\"}", url_string); //Format HTTP POST String* pString = api_curl_data( "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyAoMAvMPpc7U8lfrnGMk2ZKl966tU2pppU", post_string); Json* jobj = json_tokener_parse(pString->data); - Json* short_url = json_object_object_get(jobj, "id"); - const char* short_url_string = json_object_to_json_string(short_url); - strcpy(copy, short_url_string); - char* final = calloc(strlen(copy) + 1, 1); - if (final == NULL) { + char* short_url = malloc(32); + if (short_url == NULL) { fprintf(stderr, "calloc() failed\n"); exit(EXIT_FAILURE); } - for (unsigned int i = 0, j = 0; j < strlen(copy); i++, j++) { //Ignore escape characters - if (copy[j] == '\\' || copy[j] == '\"') - j++; - final[i] = copy[j]; - } + strcpy(short_url, json_object_to_json_string(json_object_object_get(jobj, "id"))); + strip_char(short_url, '\\'); + strip_char(short_url, '\"'); json_object_put(jobj); api_string_destroy(&pString); - return final; + return short_url; } -char* strip_char(const char* string, char c) { +char* strip_char(char* string, char c) { size_t len = strlen(string); - char* final_string = calloc(len + 1, 1); - if (final_string == NULL) { - fprintf(stderr, "calloc() failed\n"); - exit(EXIT_FAILURE); - } - for (int i = 0, j = 0; j < (int) len; i++, j++) { + int i, j; + for (i = 0, j = 0; j < (int) len; i++, j++) { while (string[j] == c) j++; - final_string[i] = string[j]; + string[i] = string[j]; } - return final_string; + string[i] = '\0'; + return string; } void api_string_destroy(String** phString) { @@ -130,6 +130,12 @@ void api_print_info(const char* ticker_name_string); */ Info* iex_get_info(const char* ticker_name_string); +/** + * Returns a pointer to an Info object containing info pertaining + * to the given symbol + * @param ticker_name_string mutf/otc symbol + * @return Info object + */ Info* morningstar_get_info(const char* ticker_name_string); /** @@ -148,12 +154,12 @@ Info* coinmarketcap_get_info(const char* ticker_name_string); char* google_shorten_link(const char* url_string); /** - * Returns a string stripped of the given char + * Returns the input string, stripped of the given char * @param string the string to strip the char from * @param c the char to strip - * @return the stripped string + * @return input string */ -char* strip_char(const char* string, char c); +char* strip_char(char* string, char c); /** * Destroys String object and frees memory diff --git a/portfolio.c b/portfolio.c index 9f7f748816e4..4a36fc30a619 100644 --- a/portfolio.c +++ b/portfolio.c @@ -182,15 +182,18 @@ double* portfolio_print_stock(char* ticker_name_string, FILE* fp, Json* current_ * a[1] -- amount spent * a[2] -- 1d gain */ + char symbol[32]; double* data = malloc(sizeof(double) * 3); char* portfolio_string = NULL; char* password = NULL; Json* jobj = NULL; if (fp == NULL) { //if being called from portfolio_print_all - ticker_name_string = strip_char(json_object_get_string(json_object_object_get(current_index, "Symbol")), '\"'); + strcpy(symbol, json_object_get_string(json_object_object_get(current_index, "Symbol"))); + strip_char(symbol, '\"'); data[0] = json_object_get_double(json_object_object_get(current_index, "Shares")); data[1] = json_object_get_double(json_object_object_get(current_index, "USD_Spent")); } else { //if being called directly from main + strcpy(symbol, ticker_name_string); portfolio_string = portfolio_file_get_string(fp, NULL); jobj = json_tokener_parse(portfolio_string); if (jobj == NULL) { //ENCRYPTED PORTFOLIO @@ -207,9 +210,9 @@ double* portfolio_print_stock(char* ticker_name_string, FILE* fp, Json* current_ return NULL; } } - int index = portfolio_symbol_index(ticker_name_string, jobj); + int index = portfolio_symbol_index(symbol, jobj); if (index == -1) { - printf("You do not have %s in your portfolio.\n", ticker_name_string); + printf("You do not have %s in your portfolio.\n", symbol); return data; } current_index = json_object_array_get_idx(jobj, (size_t) index); @@ -218,8 +221,8 @@ double* portfolio_print_stock(char* ticker_name_string, FILE* fp, Json* current_ data[1] = json_object_get_double(json_object_object_get(current_index, "USD_Spent")); data[2] = 0; double* ticker_data, ticker_current_price_usd = 1, ticker_1d_price_usd, ticker_1d_percent_change = 0; - if (strcmp(ticker_name_string, "USD$") != 0) { - ticker_data = api_get_current_price(ticker_name_string); + if (strcmp(symbol, "USD$") != 0) { + ticker_data = api_get_current_price(symbol); ticker_current_price_usd = ticker_data[0]; ticker_1d_price_usd = ticker_data[1]; free(ticker_data); @@ -228,12 +231,10 @@ double* portfolio_print_stock(char* ticker_name_string, FILE* fp, Json* current_ data[0] *= ticker_current_price_usd; } printf("%8.2lf %6s %8.2lf %8.2lf %8.2lf (%6.2lf%%) %8.2lf (%6.2lf%%)\n", - data[0] / ticker_current_price_usd, ticker_name_string, data[0], data[1], data[0] - data[1], + data[0] / ticker_current_price_usd, symbol, data[0], data[1], data[0] - data[1], (100 * (data[0] - data[1])) / data[1], data[2], ticker_1d_percent_change); - if (fp == NULL) - free(ticker_name_string); - else { + if (fp != NULL) { json_object_put(jobj); if (password != NULL) { // If portfolio was decrypted, encrypt again fflush(fp); @@ -247,15 +248,12 @@ double* portfolio_print_stock(char* ticker_name_string, FILE* fp, Json* current_ } int portfolio_symbol_index(const char* ticker_name_string, Json* jarray) { - char* str; + char string[32]; for (int i = 0; i < (int)json_object_array_length(jarray); i++) { - str = strip_char(json_object_to_json_string( - json_object_object_get(json_object_array_get_idx(jarray, (size_t) i), "Symbol")), '\"'); - if (strcmp(str, ticker_name_string) == 0) { - free(str); + strcpy(string, json_object_to_json_string(json_object_object_get(json_object_array_get_idx(jarray, (size_t) i), "Symbol"))); + strip_char(string, '\"'); + if (strcmp(string, ticker_name_string) == 0) return i; - } - free(str); } return -1; } |