aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Kellermann2018-05-28 11:22:30 -0400
committerAntony Kellermann2018-05-28 11:22:30 -0400
commit9a3c9819e6f47ee33e1a9d450159bc63af74a332 (patch)
treed45188948aecb5b5dec37e1cbb23de9652d74f69
parentd336b6a78cf86239005a8fa5fe2548ef615a06db (diff)
downloadaur-9a3c9819e6f47ee33e1a9d450159bc63af74a332.tar.gz
Renamed graph.c/h to info.c/h and moved functions around
-rw-r--r--Makefile2
-rw-r--r--api.c89
-rw-r--r--api.h11
-rw-r--r--info.c (renamed from graph.c)95
-rw-r--r--info.h (renamed from graph.h)15
-rw-r--r--main.c6
6 files changed, 113 insertions, 105 deletions
diff --git a/Makefile b/Makefile
index 8e8c2c9e4d94..ebffd65b68d6 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ CFLAGS = -g -Wall --std=c99 -D_FORTIFY_SOURCE=2 -O2 \
-Wbad-function-cast -Wstrict-overflow=5 -Wstrict-prototypes -Winline \
-Wundef -Wnested-externs -Wcast-qual -Wunreachable-code \
-ggdb3 -fno-omit-frame-pointer -ffloat-store -fno-common -fstrict-aliasing
-OBJECTS = main.o api.o portfolio.o rc4.o string-tick.o graph.o
+OBJECTS = main.o api.o portfolio.o rc4.o string-tick.o info.o
LIBS = -lcurl -ljson-c -lm -lncurses -lpthread
BIN = tick
DESTDIR = /usr
diff --git a/api.c b/api.c
index c1620f2ad910..378eddcd8111 100644
--- a/api.c
+++ b/api.c
@@ -390,95 +390,6 @@ double* morningstar_get_hist_5y(const char* symbol) {
return api_data;
}
-void iex_print_news(const char* symbol, int num_articles) {
- if (num_articles > 50)
- RET_MSG("You cannot request more than 50 articles.");
-
- char iex_api_string[URL_MAX_LENGTH];
- sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/news/last/%d", symbol, num_articles);
- String* pString = api_curl_data(iex_api_string);
- if (pString == NULL)
- return;
-
- if (strcmp(pString->data, "Unknown symbol") == 0) { // Invalid symbol
- string_destroy(&pString);
- RET_MSG("Invalid symbol.");
- }
-
- Json* jobj = json_tokener_parse(pString->data);
- size_t len = json_object_array_length(jobj);
- if (len == 0) {
- json_object_put(jobj);
- string_destroy(&pString);
- RET_MSG("No articles available.");
- }
-
- Json* idx;
- const char* headline, * source, *url;
- char date[DATE_MAX_LENGTH];
- for (size_t i = 0; i < len; i++) {
- idx = json_object_array_get_idx(jobj, i);
- headline = json_object_get_string(json_object_object_get(idx, "headline")); // Headline
- source = json_object_get_string(json_object_object_get(idx, "source")); // Source
- strncpy(date, json_object_get_string(json_object_object_get(idx, "datetime")), 10); // Date
- date[10] = '\0'; // null terminate date before time
- char summary[strlen(json_object_get_string(json_object_object_get(idx, "summary")))]; // Summary
- strcpy(summary, json_object_get_string(json_object_object_get(idx, "summary")));
- strip_tags(summary); // Summary will be html formatted, so must strip tags
- url = json_object_get_string(json_object_object_get(idx, "url")); // URL
- char related[strlen(json_object_get_string(json_object_object_get(idx, "related")))]; // Related
- strcpy(related, json_object_get_string(json_object_object_get(idx, "related")));
- int related_num = 0;
- for (size_t j = 0; j < strlen(related); j++) { // List only first five related symbols
- if (related[j] == ',')
- related_num++;
- if (related_num == 5) {
- related[j] = '\0';
- break;
- }
- }
- printf("%s | %s | %s\n%s\n%s | Related: %s\n\n", headline, source, date, summary, url, related);
- }
- json_object_put(jobj);
- string_destroy(&pString);
-}
-
-void api_print_info(const char* ticker_name_string) {
- Info* ticker_info;
- if (strlen(ticker_name_string) > 5) { // If symbol length is greater than 5, then it must be a crypto
- ticker_info = coinmarketcap_get_info(ticker_name_string);
- if (ticker_info == NULL)
- RET_MSG("Invalid symbol!")
- }
- else {
- ticker_info = iex_get_info(ticker_name_string);
- if (ticker_info == NULL)
- ticker_info = morningstar_get_info(ticker_name_string);
- if (ticker_info == NULL)
- RET_MSG("Invalid symbol!")
- }
-
- if (strcmp(ticker_info->name, "") != 0)
- printf("Name: %s\n", ticker_info->name);
- if (strcmp(ticker_info->symbol, "") != 0)
- printf("Symbol: %s\n", ticker_info->symbol);
- if (ticker_info->price != EMPTY)
- printf("Price: $%.2lf\n", ticker_info->price);
- if (ticker_info->change_1d != EMPTY)
- printf("Percent change 24h: %.2lf%%\n", ticker_info->change_1d);
- if (ticker_info->change_7d != EMPTY)
- printf("Percent change 7d: %.2lf%%\n", ticker_info->change_7d);
- if (ticker_info->change_30d != EMPTY)
- printf("Percent change 30d: %.2lf%%\n", ticker_info->change_30d);
- if (ticker_info->div_yield != EMPTY)
- printf("Dividend yield: %.2lf%%\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* iex_get_info(const char* symbol) {
Info* symbol_info = api_info_init();
strcpy(symbol_info->symbol, symbol);
diff --git a/api.h b/api.h
index 3e6bb5e54166..c50616426670 100644
--- a/api.h
+++ b/api.h
@@ -146,18 +146,7 @@ double* iex_get_hist_5y(const char* symbol);
*/
double* morningstar_get_hist_5y(const char* symbol);
-/**
- * Prints the top three news articles by popularity pertaining to the given string, ticker_name_string. Spaces and
- * underscores will be url-encoded (replaced by "%20"). News API will be used for data.
- * @param symbol the string to query
- */
-void iex_print_news(const char* symbol, int num_articles);
-/**
- * Prints information about the symbol ticker_name_string by calling the function json_print_news.
- * @param ticker_name_string
- */
-void api_print_info(const char* ticker_name_string);
/**
* Returns a pointer to an Info object containing info pertaining
diff --git a/graph.c b/info.c
index b26218f1b58e..6dcca42860be 100644
--- a/graph.c
+++ b/info.c
@@ -1,7 +1,100 @@
-#include "graph.h"
+#include "info.h"
int zoom_months[] = {60, 48, 36, 24, 12, 9, 6, 3, 1}, zoom_change_x_months[] = {12, 12, 12, 12, 12, 3, 3, 3, 2};
+void symbol_print_info(const char* symbol) {
+ Info* symbol_info;
+ if (strlen(symbol) > 5) { // If symbol length is greater than 5, then it must be a crypto
+ symbol_info = coinmarketcap_get_info(symbol);
+ if (symbol_info == NULL)
+ RET_MSG("Invalid symbol!")
+ }
+ else {
+ symbol_info = iex_get_info(symbol);
+ if (symbol_info == NULL)
+ symbol_info = morningstar_get_info(symbol);
+ if (symbol_info == NULL)
+ RET_MSG("Invalid symbol!")
+ }
+
+
+ if (symbol_info->name[0] != '\0')
+ printf("%s %s $%lf (%lf%%)\n", symbol_info->name, symbol_info->symbol, symbol_info->price,
+ symbol_info->change_1d);
+
+
+ if (strcmp(symbol_info->symbol, "") != 0)
+ printf("Symbol: %s\n", symbol_info->symbol);
+ if (symbol_info->price != EMPTY)
+ printf("Price: $%.2lf\n", symbol_info->price);
+ if (symbol_info->change_1d != EMPTY)
+ printf("Percent change 24h: %.2lf%%\n", symbol_info->change_1d);
+ if (symbol_info->change_7d != EMPTY)
+ printf("Percent change 7d: %.2lf%%\n", symbol_info->change_7d);
+ if (symbol_info->change_30d != EMPTY)
+ printf("Percent change 30d: %.2lf%%\n", symbol_info->change_30d);
+ if (symbol_info->div_yield != EMPTY)
+ printf("Dividend yield: %.2lf%%\n", symbol_info->div_yield);
+ if (symbol_info->marketcap != EMPTY)
+ printf("Market Cap: $%ld\n", symbol_info->marketcap);
+ if (symbol_info->volume_1d != EMPTY)
+ printf("Volume 24h: $%ld\n", symbol_info->volume_1d);
+ api_info_destroy(&symbol_info);
+}
+
+void symbol_print_news(const char* symbol, int num_articles) {
+ if (num_articles > 50)
+ RET_MSG("You cannot request more than 50 articles.");
+
+ char iex_api_string[URL_MAX_LENGTH];
+ sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/news/last/%d", symbol, num_articles);
+ String* pString = api_curl_data(iex_api_string);
+ if (pString == NULL)
+ return;
+
+ if (strcmp(pString->data, "Unknown symbol") == 0) { // Invalid symbol
+ string_destroy(&pString);
+ RET_MSG("Invalid symbol.");
+ }
+
+ Json* jobj = json_tokener_parse(pString->data);
+ size_t len = json_object_array_length(jobj);
+ if (len == 0) {
+ json_object_put(jobj);
+ string_destroy(&pString);
+ RET_MSG("No articles available.");
+ }
+
+ Json* idx;
+ const char* headline, * source, *url;
+ char date[DATE_MAX_LENGTH];
+ for (size_t i = 0; i < len; i++) {
+ idx = json_object_array_get_idx(jobj, i);
+ headline = json_object_get_string(json_object_object_get(idx, "headline")); // Headline
+ source = json_object_get_string(json_object_object_get(idx, "source")); // Source
+ strncpy(date, json_object_get_string(json_object_object_get(idx, "datetime")), 10); // Date
+ date[10] = '\0'; // null terminate date before time
+ char summary[strlen(json_object_get_string(json_object_object_get(idx, "summary")))]; // Summary
+ strcpy(summary, json_object_get_string(json_object_object_get(idx, "summary")));
+ strip_tags(summary); // Summary will be html formatted, so must strip tags
+ url = json_object_get_string(json_object_object_get(idx, "url")); // URL
+ char related[strlen(json_object_get_string(json_object_object_get(idx, "related")))]; // Related
+ strcpy(related, json_object_get_string(json_object_object_get(idx, "related")));
+ int related_num = 0;
+ for (size_t j = 0; j < strlen(related); j++) { // List only first five related symbols
+ if (related[j] == ',')
+ related_num++;
+ if (related_num == 5) {
+ related[j] = '\0';
+ break;
+ }
+ }
+ printf("%s | %s | %s\n%s\n%s | Related: %s\n\n", headline, source, date, summary, url, related);
+ }
+ json_object_put(jobj);
+ string_destroy(&pString);
+}
+
void graph_main(const char* symbol, const char* symbol2, WINDOW* window) {
if (window == NULL)
window = initscr();
diff --git a/graph.h b/info.h
index 1f729ad330cf..0720b816f5bd 100644
--- a/graph.h
+++ b/info.h
@@ -24,6 +24,21 @@
extern int zoom_months[9], zoom_change_x_months[9];
+
+/**
+ * Prints information about the symbol ticker_name_string by calling the function json_print_news.
+ * @param symbol
+ */
+void symbol_print_info(const char* symbol);
+
+
+/**
+ * Prints the top three news articles by popularity pertaining to the given string, ticker_name_string. Spaces and
+ * underscores will be url-encoded (replaced by "%20"). News API will be used for data.
+ * @param symbol the string to query
+ */
+void symbol_print_news(const char* symbol, int num_articles);
+
/**
* -- Main input loop for graphing --
*
diff --git a/main.c b/main.c
index 57b2a835d082..2e726258380a 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,5 @@
#include "portfolio.h"
-#include "graph.h"
+#include "info.h"
int main(int argc, char* argv[]) {
if (argc < 2) {
@@ -32,7 +32,7 @@ int main(int argc, char* argv[]) {
int num_articles = 3; // Default
if (argc == 4)
num_articles = (int) strtol(argv[3], NULL, 10);
- iex_print_news(sym, num_articles);
+ symbol_print_news(sym, num_articles);
}
//Encrypt/decrypt
@@ -41,7 +41,7 @@ int main(int argc, char* argv[]) {
// Info
else if (strcmp(cmd, "info") == 0 && argc == 3)
- api_print_info(sym);
+ symbol_print_info(sym);
// Graph
else if (strcmp(cmd, "graph") == 0 && argc == 3)