diff options
author | Antony Kellermann | 2018-08-21 13:46:56 -0400 |
---|---|---|
committer | Antony Kellermann | 2018-08-21 13:46:56 -0400 |
commit | 2ffe874448a3a98eaff68c8a3fa996a313a92459 (patch) | |
tree | 66fa8bbc825eca135892ec7c13a591a55b0c0218 | |
parent | 44cb5d82862caf3ddd3e753b26dc43ec49fe2815 (diff) | |
download | aur-2ffe874448a3a98eaff68c8a3fa996a313a92459.tar.gz |
Removed non-batch functions
-rw-r--r-- | api.c | 417 | ||||
-rw-r--r-- | api.h | 131 |
2 files changed, 0 insertions, 548 deletions
@@ -166,362 +166,6 @@ String* iex_batch_get_data_string(char* symbol_array[SYMBOL_MAX_LENGTH], size_t return api_curl_data(iex_api_string); } -void* iex_store_company(void* vpInfo) { - Info* symbol_info = vpInfo; - char iex_api_string[URL_MAX_LENGTH]; - sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/company", symbol_info->symbol); - String* pString = api_curl_data(iex_api_string); // API CALL 1 -- Company - if (pString == NULL) - return NULL; - - Json* jobj = json_tokener_parse(pString->data); - if (jobj == NULL) { // Invalid symbol - string_destroy(&pString); - return NULL; - } - - symbol_info->api_provider = IEX; - - if (json_object_object_get(jobj, "symbol") != NULL) - strcpy(symbol_info->symbol, json_object_get_string(json_object_object_get(jobj, "symbol"))); - if (json_object_object_get(jobj, "companyName") != NULL) - strcpy(symbol_info->name, json_object_get_string(json_object_object_get(jobj, "companyName"))); - if (json_object_object_get(jobj, "industry") != NULL) - strcpy(symbol_info->industry, json_object_get_string(json_object_object_get(jobj, "industry"))); - if (json_object_object_get(jobj, "website") != NULL) - strcpy(symbol_info->website, json_object_get_string(json_object_object_get(jobj, "website"))); - if (json_object_object_get(jobj, "description") != NULL) - strcpy(symbol_info->description, json_object_get_string(json_object_object_get(jobj, "description"))); - if (json_object_object_get(jobj, "CEO") != NULL) - strcpy(symbol_info->ceo, json_object_get_string(json_object_object_get(jobj, "CEO"))); - if (json_object_object_get(jobj, "issueType") != NULL) - strcpy(symbol_info->issue_type, json_object_get_string(json_object_object_get(jobj, "issueType"))); - if (json_object_object_get(jobj, "sector") != NULL) - strcpy(symbol_info->sector, json_object_get_string(json_object_object_get(jobj, "sector"))); - json_object_put(jobj); - string_destroy(&pString); - return vpInfo; -} - -void* iex_store_quote(void* vpInfo) { - Info* symbol_info = vpInfo; - if (symbol_info->symbol[0] == '\0') - return NULL; - - char iex_api_string[URL_MAX_LENGTH]; - sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/quote", symbol_info->symbol); - String* pString = api_curl_data(iex_api_string); // API CALL 2 -- Quote - if (pString == NULL) - return NULL; - - Json* jobj = json_tokener_parse(pString->data); - if (jobj == NULL) { // Invalid symbol - string_destroy(&pString); - return NULL; - } - - symbol_info->api_provider = IEX; - - // If latest price is in extended hours - if (json_object_get_int64(json_object_object_get(jobj, "extendedPriceTime")) > - json_object_get_int64(json_object_object_get(jobj, "latestUpdate"))) { - symbol_info->price = json_object_get_double(json_object_object_get(jobj, "extendedPrice")); - symbol_info->intraday_time = json_object_get_int64(json_object_object_get(jobj, "extendedPriceTime")) / 1000; - } else { - symbol_info->price = json_object_get_double(json_object_object_get(jobj, "latestPrice")); - symbol_info->intraday_time = json_object_get_int64(json_object_object_get(jobj, "latestUpdate")) / 1000; - } - symbol_info->price_last_close = json_object_get_double(json_object_object_get(jobj, "previousClose")); - if (symbol_info->price_last_close == 0) // May be 0 over weekend - symbol_info->price_last_close = EMPTY; - symbol_info->marketcap = json_object_get_int64(json_object_object_get(jobj, "marketCap")); - symbol_info->volume_1d = json_object_get_int64(json_object_object_get(jobj, "latestVolume")); - symbol_info->pe_ratio = json_object_get_double(json_object_object_get(jobj, "peRatio")); - json_object_put(jobj); - string_destroy(&pString); - return vpInfo; -} - -void* iex_store_stats(void* vpInfo) { - Info* symbol_info = vpInfo; - if (symbol_info->symbol[0] == '\0') - return NULL; - - char iex_api_string[URL_MAX_LENGTH]; - sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/stats", symbol_info->symbol); - String* pString = api_curl_data(iex_api_string); // API CALL 3 -- Stats - if (pString == NULL) - return NULL; - - Json* jobj = json_tokener_parse(pString->data); - if (jobj == NULL) { // Invalid symbol - string_destroy(&pString); - return NULL; - } - - symbol_info->api_provider = IEX; - - symbol_info->div_yield = json_object_get_double(json_object_object_get(jobj, "dividendYield")); - symbol_info->revenue = json_object_get_int64(json_object_object_get(jobj, "revenue")); - symbol_info->gross_profit = json_object_get_int64(json_object_object_get(jobj, "grossProfit")); - symbol_info->cash = json_object_get_int64(json_object_object_get(jobj, "cash")); - symbol_info->debt = json_object_get_int64(json_object_object_get(jobj, "debt")); - json_object_put(jobj); - string_destroy(&pString); - return vpInfo; -} - -void* iex_store_earnings(void* vpInfo) { - Info* symbol_info = vpInfo; - if (symbol_info->symbol[0] == '\0') - return NULL; - - char iex_api_string[URL_MAX_LENGTH]; - sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/earnings", symbol_info->symbol); - String* pString = api_curl_data(iex_api_string); // API CALL 4 -- Earnings - if (pString == NULL) - return NULL; - - Json* jobj = json_tokener_parse(pString->data); - if (jobj == NULL) { // Invalid symbol - string_destroy(&pString); - return NULL; - } - - symbol_info->api_provider = IEX; - - if (json_object_is_type(json_object_object_get(jobj, "earnings"), json_type_array)) { // ETFs don't report earnings - size_t len = json_object_array_length(json_object_object_get(jobj, "earnings")); - Json* idx; - for (size_t i = 0; i < len; i++) { - idx = json_object_array_get_idx(json_object_object_get(jobj, "earnings"), i); - symbol_info->eps[i] = json_object_get_double(json_object_object_get(idx, "actualEPS")); - symbol_info->eps_year_ago[i] = json_object_get_double(json_object_object_get(idx, "yearAgo")); - if (json_object_object_get(idx, "fiscalPeriod") != NULL) - strcpy(symbol_info->fiscal_period[i], - json_object_get_string(json_object_object_get(idx, "fiscalPeriod"))); - else if (json_object_object_get(idx, "fiscalEndDate") != NULL) - strcpy(symbol_info->fiscal_period[i], - json_object_get_string(json_object_object_get(idx, "fiscalEndDate"))); - else if (json_object_object_get(idx, "EPSReportDate") != NULL) - strcpy(symbol_info->fiscal_period[i], - json_object_get_string(json_object_object_get(idx, "EPSReportDate"))); - } - } - json_object_put(jobj); - string_destroy(&pString); - return vpInfo; -} - -void* iex_store_chart(void* vpInfo) { - Info* symbol_info = vpInfo; - if (symbol_info->symbol[0] == '\0') - return NULL; - - char iex_api_string[URL_MAX_LENGTH]; - sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/chart/5y", symbol_info->symbol); - String* pString = api_curl_data(iex_api_string); - if (pString == NULL) - return NULL; - - Json* jobj = json_tokener_parse(pString->data); - if (jobj == NULL) { // Invalid symbol - string_destroy(&pString); - return NULL; - } - - symbol_info->api_provider = IEX; - - size_t len = json_object_array_length(jobj); - symbol_info->points = calloc(len + 1, sizeof(double)); - pointer_alloc_check(symbol_info->points); - for (size_t i = 0; i < len; i++) - symbol_info->points[i] = json_object_get_double( - json_object_object_get(json_object_array_get_idx(jobj, i), "close")); - if (symbol_info->price_last_close == EMPTY) // May be 0 over weekend, so get last close from points array - symbol_info->price_last_close = symbol_info->points[len - 1]; - if (len > 5) - symbol_info->price_7d = symbol_info->points[len - 5]; - if (len > 21) - symbol_info->price_30d = symbol_info->points[len - 21]; - json_object_put(jobj); - string_destroy(&pString); - return vpInfo; -} - -void* iex_store_news(void* vpInfo) { - Info* symbol_info = vpInfo; - if (symbol_info->symbol[0] == '\0') - return NULL; - - int num_articles = DEFAULT_NUM_ARTICLES; - if (symbol_info->num_articles != EMPTY) - num_articles = symbol_info->num_articles; - - char iex_api_string[URL_MAX_LENGTH]; - sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/news/last/%d", - symbol_info->symbol, num_articles); - String* pString = api_curl_data(iex_api_string); - if (pString == NULL) - return NULL; - - Json* jobj = json_tokener_parse(pString->data), * idx, *headline; - if (jobj == NULL) { // Invalid symbol - string_destroy(&pString); - return NULL; - } - - symbol_info->api_provider = IEX; - - size_t len = json_object_array_length(jobj); - if (len < (unsigned) symbol_info->num_articles) - symbol_info->num_articles = (int)len; - - symbol_info->articles = malloc(sizeof(News*) * symbol_info->num_articles); - pointer_alloc_check(symbol_info->articles); - - for (int i = 0; i < symbol_info->num_articles; i++) { - idx = json_object_array_get_idx(jobj, (size_t) i); - headline = json_object_object_get(idx, "headline"); - // If two articles in a row are the same, change num_articles and break loop. This will happen if there are not - if (i > 0 && headline != NULL && // enough articles supplied by API. - strcmp(json_object_get_string(headline), symbol_info->articles[i - 1]->headline) == 0) { - symbol_info->num_articles = i; - break; - } - - symbol_info->articles[i] = api_news_init(); - if (headline != NULL) - strcpy(symbol_info->articles[i]->headline, json_object_get_string(headline)); - if (json_object_object_get(idx, "source") != NULL) - strcpy(symbol_info->articles[i]->source, json_object_get_string(json_object_object_get(idx, "source"))); - if (json_object_object_get(idx, "dateTime") != NULL) - strncpy(symbol_info->articles[i]->date, - json_object_get_string(json_object_object_get(idx, "datetime")), 10); - symbol_info->articles[i]->date[10] = '\0'; - if (json_object_object_get(idx, "summary") != NULL) - strcpy(symbol_info->articles[i]->summary, json_object_get_string(json_object_object_get(idx, "summary"))); - strip_tags(symbol_info->articles[i]->summary); // Summary will be html formatted, so must strip tags - if (json_object_object_get(idx, "url") != NULL) - strcpy(symbol_info->articles[i]->url, json_object_get_string(json_object_object_get(idx, "url"))); - if (json_object_object_get(idx, "related") != NULL) - strcpy(symbol_info->articles[i]->related, json_object_get_string(json_object_object_get(idx, "related"))); - int related_num = 0; - for (size_t j = 0; j < strlen(symbol_info->articles[i]->related); j++) { // List only first five related symbols - if (symbol_info->articles[i]->related[j] == ',') - related_num++; - if (related_num == 5) { - symbol_info->articles[i]->related[j] = '\0'; - break; - } - } - } - json_object_put(jobj); - string_destroy(&pString); - return vpInfo; -} - -void* iex_store_peers(void* vpInfo) { - Info* symbol_info = vpInfo; - if (symbol_info->symbol[0] == '\0') - return NULL; - - char iex_api_string[URL_MAX_LENGTH]; - sprintf(iex_api_string, "https://api.iextrading.com/1.0/stock/%s/peers", symbol_info->symbol); - String* pString = api_curl_data(iex_api_string); - if (pString == NULL) - return NULL; - - Json* jobj = json_tokener_parse(pString->data); - if (jobj == NULL) { // Invalid symbol - string_destroy(&pString); - return NULL; - } - - symbol_info->api_provider = IEX; - - size_t len = (int) json_object_array_length(jobj); - if (len == 0) { - json_object_put(jobj); - string_destroy(&pString); - return NULL; - } - if (len > MAX_PEERS) - len = MAX_PEERS; - - symbol_info->peers = api_info_array_init_from_length(len); - for (size_t i = 0; i < symbol_info->peers->length; i++) - strcpy(symbol_info->peers->array[i]->symbol, json_object_get_string( - json_object_array_get_idx(jobj, (size_t) i))); - - api_info_array_store_check_data(symbol_info->peers); - - json_object_put(jobj); - string_destroy(&pString); - return vpInfo; -} - -void* iex_store_all_info(void* vpInfo) { - pthread_t threads[7]; - void* (*funcs[7]) (void*) = { - iex_store_company, iex_store_quote, iex_store_stats, iex_store_earnings, - iex_store_chart, iex_store_news, iex_store_peers - }; - for (int i = 0; i < 7; i++) - if (pthread_create(&threads[i], NULL, funcs[i], vpInfo)) - EXIT_MSG("Error creating thread!"); - - void* ret = vpInfo, * thread_ret = NULL; - for (int i = 0; i < 7; i++) { - if (pthread_join(threads[i], &thread_ret)) - EXIT_MSG("Error joining thread!"); - - if (thread_ret == NULL) - ret = NULL; - } - return ret; -} - -void* iex_store_check_info(void* vpInfo) { - pthread_t threads[2]; - void* (*funcs[2]) (void*) = { - iex_store_quote, iex_store_chart - }; - for (int i = 0; i < 2; i++) - if (pthread_create(&threads[i], NULL, funcs[i], vpInfo)) - EXIT_MSG("Error creating thread!"); - - void* ret = vpInfo, * thread_ret = NULL; - for (int i = 0; i < 2; i++) { - if (pthread_join(threads[i], &thread_ret)) - EXIT_MSG("Error joining thread!"); - - if (thread_ret == NULL) - ret = NULL; - } - return ret; -} - -void* iex_store_misc_info(void* vpInfo) { - pthread_t threads[5]; - void* (*funcs[5]) (void*) = { - iex_store_company, iex_store_stats, iex_store_earnings, iex_store_news, iex_store_peers - }; - for (int i = 0; i < 5; i++) - if (pthread_create(&threads[i], NULL, funcs[i], vpInfo)) - EXIT_MSG("Error creating thread!"); - - void* ret = vpInfo, * thread_ret = NULL; - for (int i = 0; i < 5; i++) { - if (pthread_join(threads[i], &thread_ret)) - EXIT_MSG("Error joining thread!"); - - if (thread_ret == NULL) - ret = NULL; - } - return ret; -} - void* morningstar_store_info(void* vpInfo) { Info* symbol_info = vpInfo; char today_str[DATE_MAX_LENGTH], five_year_str[DATE_MAX_LENGTH], morningstar_api_string[URL_MAX_LENGTH]; @@ -649,67 +293,6 @@ void* coinmarketcap_store_info(void* vpInfo) { return vpInfo; } -void* api_store_all_info(void* vpInfo) { - Info* pInfo = 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 && alphavantage_store_info(vpInfo) == NULL && - coinmarketcap_store_info(vpInfo) == NULL) - return NULL; - else return vpInfo; -} - -void* api_store_check_info(void* vpInfo) { - Info* pInfo = 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 && alphavantage_store_info(vpInfo) == NULL && - coinmarketcap_store_info(vpInfo) == NULL) - return NULL; - else return vpInfo; -} - -void* api_store_misc_info(void* vpInfo) { - Info* pInfo = vpInfo; - if (pInfo->api_provider == IEX) - return iex_store_misc_info(pInfo); - - return NULL; -} - -void* api_info_array_store_check_data(void* vpPortfolio_Data) { - Info_Array* portfolio_data = vpPortfolio_Data; - pthread_t threads[portfolio_data->length]; - for (size_t i = 0; i < portfolio_data->length; i++) - if (strcmp(portfolio_data->array[i]->symbol, "USD$") != 0) - if (pthread_create(&threads[i], NULL, api_store_check_info, portfolio_data->array[i])) - EXIT_MSG("Error creating thread!") - - void* ret = vpPortfolio_Data, * thread_ret = NULL; - int load_len = 0; - for (size_t i = 0; i < portfolio_data->length; i++) { - // Print loading string - if (i > 0) - for (int j = 0; j < load_len; j++) - putchar('\b'); - load_len = printf("Loading data (%d/%d)", (int) i + 1, (int) portfolio_data->length); - fflush(stdout); - - if (strcmp(portfolio_data->array[i]->symbol, "USD$") != 0) { - if (pthread_join(threads[i], &thread_ret)) - EXIT_MSG("Error joining thread!") - } - if (thread_ret == NULL) - ret = NULL; - else info_store_check_data(portfolio_data->array[i]); - } - if (ret != NULL) - info_array_store_totals(portfolio_data); - return ret; -} - void api_info_array_store_data_batch(Info_Array* pInfo_Array, Data_Level data_level) { iex_batch_store_data_info_array(pInfo_Array, data_level); @@ -210,104 +210,6 @@ String* iex_batch_get_data_string(char* symbol_array[SYMBOL_MAX_LENGTH], size_t /** * Designed for threading * - * Queries IEX's company endpoint and stores the data in the Info object pointed to by vpInfo. symbol, name, - * industry, website, description, ceo, issue_type, and sector are stored. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_company(void* vpInfo); - -/** - * Designed for threading - * - * Queries IEX's quote endpoint and stores the data in the Info object pointed to by vpInfo. intraday_time, price, - * price_last_close, marketcap, volume_1d, and pe_ratio are stored. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_quote(void* vpInfo); - -/** - * Designed for threading - * - * Queries IEX's stats endpoint and stores the data in the Info object pointed to by vpInfo. div_yield, revenue, - * gross_profit, cash, and debt are stored. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_stats(void* vpInfo); - -/** - * Designed for threading - * - * Queries IEX's stats earnings and stores the data in the Info object pointed to by vpInfo. eps, fiscal_period, and - * eps_year_ago are stored. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_earnings(void* vpInfo); - -/** - * Designed for threading - * - * Queries IEX's chart endpoint and stores the data in the Info object pointed to by vpInfo. change_7d, - * change_30d, and points are stored. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_chart(void* vpInfo); - -/** - * Designed for threading - * - * Queries IEX's news endpoint and stores the data in the Info object pointed to by vpInfo. num_articles number of - * articles are stored. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_news(void* vpInfo); - -/** - * Designed for threading - * - * Queries IEX's peers endpoint and stores the data in the Info object pointed to by vpInfo. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_peers(void* vpInfo); - -/** - * Designed for threading - * - * Calls the above 7 iex store functions to store api data in the Info object pointed to by vpInfo. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_all_info(void* vpInfo); - -/** - * Designed for threading - * - * Calls iex_store_quote and iex_store_chart to store api data in the Info object pointed to by - * vpInfo. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_check_info(void* vpInfo); - -/** - * Designed for threading - * - * Calls the 5 other functions besides those called in iex_store_check_info to store api data in - * the Info object pointed to by vpInfo. - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* iex_store_misc_info(void* vpInfo); - -/** - * Designed for threading - * * 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* @@ -336,39 +238,6 @@ void* alphavantage_store_info(void* vpInfo); void* coinmarketcap_store_info(void* vpInfo); /** - * Stores API data in Info object pointed to by vpInfo. The function will first query IEX. If no - * response, it will query Morningstar. If no response, it will query Coinmarketcap. - * 1. IEX -- NASDAQ/NYSE/NYSEARCA - * 2. Morningstar -- MUTF/OTCMKTS - * 3. Coinmarketcap -- CRYPTO - * - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* api_store_all_info(void* vpInfo); - -/** - * Stores API data in Info object pointed to by vpInfo. The function will first query IEX's check - * data. If no response, it will query Morningstar. If no response, it will query Coinmarketcap. - * 1. IEX -- NASDAQ/NYSE/NYSEARCA - * 2. Morningstar -- MUTF/OTCMKTS - * 3. Coinmarketcap -- CRYPTO - * - * @param vpInfo Info* - * @return vpInfo on success, NULL on error - */ -void* api_store_check_info(void* vpInfo); - -void* api_store_misc_info(void* vpInfo); - -/** - * Fills an Info_Array with check api data. Primarily used for "check" command. - * @param portfolio_data Info_Array - * @param pString portfolio data - */ -void* api_info_array_store_check_data(void* vpPortfolio_Data); - -/** * Queries IEX, AlphaVantage, and Coinmarketcap to store api data in pInfo_Array. data_level will * determine the level of data stored. See iex_batch_store_data for more information on data_level. * @param pInfo_Array the Info_Array |