aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Kellermann2018-08-04 02:15:38 -0400
committerAntony Kellermann2018-08-04 02:15:38 -0400
commit2b640895c54348daedd9cc0e4c32284562289b1d (patch)
treeecc87a9efcaa72c9b2238161e387b8645f39ddad
parent22539aa6a4286102216225d96b09411447ec40ba (diff)
downloadaur-2b640895c54348daedd9cc0e4c32284562289b1d.tar.gz
Added temporary alphavantage API function and changed store info functions to use it instead of morningstar
-rw-r--r--api.c47
-rw-r--r--api.h12
2 files changed, 56 insertions, 3 deletions
diff --git a/api.c b/api.c
index 6243425a3888..04b3964e2eac 100644
--- a/api.c
+++ b/api.c
@@ -482,6 +482,49 @@ void* morningstar_store_info(void* vpInfo) {
return vpInfo;
}
+void* alphavantage_store_info(void* vpInfo) {
+ Info* symbol_info = vpInfo;
+ if (symbol_info->symbol[0] == '\0')
+ return NULL;
+
+ char alphavantage_api_string[URL_MAX_LENGTH];
+ sprintf(alphavantage_api_string, "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY"
+ "&symbol=%s&apikey=DFUMLJ1ILOM2G7IH&outputsize=full&datatype"
+ "=csv", symbol_info->symbol);
+ String* pString = api_curl_data(alphavantage_api_string);
+ if (pString == NULL)
+ return NULL;
+
+ if (pString->data[0] == '{') { // Invalid symbol/error
+ string_destroy(&pString);
+ return NULL;
+ }
+
+ size_t len = string_get_num_lines(pString) - 1, idx = 0;
+ if (len > 1260) // 5 years
+ len = 1260;
+
+ symbol_info->points = calloc(len + 1, sizeof(double));
+ pointer_alloc_check(symbol_info->points);
+
+ csv_goto_next_line(pString, &idx); // skip columns line
+ for (int i = (int) len - 1; i >= 0; i--) {
+ for (size_t j = 0; j < 4; j++)
+ csv_goto_next_value(pString, &idx);
+
+ symbol_info->points[i] = csv_read_next_double(pString, &idx);
+ if (symbol_info->points[i] == 0 && i < (int) len - 1) // API Error
+ symbol_info->points[i] = symbol_info->points[i + 1];
+ csv_goto_next_line(pString, &idx);
+ }
+
+ symbol_info->price = symbol_info->points[len - 1];
+ symbol_info->price_last_close = symbol_info->points[len - 2];
+ symbol_info->price_7d = symbol_info->points[len - 6];
+ symbol_info->price_30d = symbol_info->points[len - 22];
+ return vpInfo;
+}
+
void* coinmarketcap_store_info(void* vpInfo) {
Info* symbol_info = vpInfo;
char coinmarketcap_api_string[URL_MAX_LENGTH];
@@ -518,7 +561,7 @@ void* api_store_all_info(void* vpInfo) {
if (strlen(pInfo->symbol) > 5) // If symbol length is greater than 5, then it must be a crypto
return coinmarketcap_store_info(vpInfo);
- if (iex_store_all_info(vpInfo) == NULL && morningstar_store_info(vpInfo) == NULL &&
+ if (iex_store_all_info(vpInfo) == NULL && alphavantage_store_info(vpInfo) == NULL &&
coinmarketcap_store_info(vpInfo) == NULL)
return NULL;
else return vpInfo;
@@ -529,7 +572,7 @@ void* api_store_check_info(void* vpInfo) {
if (strlen(pInfo->symbol) > 5) // If symbol length is greater than 5, then it must be a crypto
return coinmarketcap_store_info(vpInfo);
- if (iex_store_check_info(vpInfo) == NULL && morningstar_store_info(vpInfo) == NULL &&
+ if (iex_store_check_info(vpInfo) == NULL && alphavantage_store_info(vpInfo) == NULL &&
coinmarketcap_store_info(vpInfo) == NULL)
return NULL;
else return vpInfo;
diff --git a/api.h b/api.h
index c8c38587aee8..84e26ca68f89 100644
--- a/api.h
+++ b/api.h
@@ -268,7 +268,7 @@ void* iex_store_check_info(void* vpInfo);
/**
* Designed for threading
*
- * Queries Morningstar's API and stores the data in the Info object pointed to by vpInfo. price, change_1d,
+ * Queries Morningstar's API and stores the data in the Info object pointed to by vpInfo. change_1d,
* change_7d, change_30d, points, and volume_1d are stored.
* @param vpInfo Info*
* @return vpInfo on success, NULL on error
@@ -278,6 +278,16 @@ void* morningstar_store_info(void* vpInfo);
/**
* Designed for threading
*
+ * Queries AlpvaVantage's API and stores the data in the Info object pointed to by vpInfo. change_1d,
+ * change_7d, change_30d, and points are stored.
+ * @param vpInfo Info*
+ * @return vpInfo on Success, NULL on error
+ */
+void* alphavantage_store_info(void* vpInfo);
+
+/**
+ * Designed for threading
+ *
* Queries Coinmarketcaps's API and stores the data in the Info object pointed to by vpInfo. name, symbol, price,
* change_1d, change_7d, marketcap, and volume_1d are stored.
* @param vpInfo Info*