<commit_before>/*
* Copyright 2015-2017 <NAME>.
* See LICENSE file for license details.
*/

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "status_bar.h"
#include "sp500_monitor.h"
#include "http_download.h"

void* sp500_init(GArray* arguments) {
  monitor_arg_check("sp500", arguments, "(icon)");

  struct sp500_monitor* m = malloc(sizeof(struct sp500_monitor));

  m->base = base_monitor_init(sp500_sleep_time, sp500_update_text, sp500_free);

  char* icon = g_array_index(arguments, GString*, 0)->str;
  m->icon = g_string_new(icon);

  m->res = g_string_new(NULL);
  m->curl = curl_easy_init();

  m->err = malloc((strlen(m->icon->str) + 2)*sizeof(char));
  sprintf(m->err, "%s!", m->icon->str);

  return m;
}

gboolean sp500_update_text(void* ptr) {
  struct sp500_monitor* m = (struct sp500_monitor*)ptr;
  monitor_null_check(m, "sp500_monitor", "update");

  char* output;
  if (!format_price(m->curl, m->res, m->icon)) {
    output = m->res->str;
  } else {
    output = m->err;
  }

  g_mutex_lock(m->base->mutex);
  m->base->text = g_string_assign(m->base->text, output);
  g_mutex_unlock(m->base->mutex);

  return TRUE;
}

int sp500_sleep_time(void* ptr) {
  return 300;
}

void sp500_free(void* ptr) {
  struct sp500_monitor* m = (struct sp500_monitor*)ptr;
  monitor_null_check(m, "sp500_monitor", "free");

  g_string_free(m->icon, TRUE);

  free(m->err);
  g_string_free(m->res, TRUE);
  curl_easy_cleanup(m->curl);

  base_monitor_free(m->base);

  free(m);
}

int format_price(CURL* curl, GString* res, GString* icon) {
  char* apikey = getenv("STATUS_BAR_SP500_APIKEY");
  if (apikey == NULL) {
    fprintf(stderr, "No apikey found in environment variable STATUS_BAR_SP500_APIKEY\n");
    return -1;
  }

  const char* url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=^gspc&apikey=%s";
  char* quote_request = malloc(sizeof(char*)*(strlen(url)+strlen(apikey)));
  int quote_request_ret = sprintf(quote_request, url, apikey);
  if (quote_request_ret < 0) {
    return -1;
  }

  GString* quote = g_string_new(NULL);
  CURLcode quote_code = download_data(curl, quote_request, quote);
<commit_msg>Switch to SPY for sp500 quote<commit_after>/*
* Copyright 2015-2017 <NAME>.
* See LICENSE file for license details.
*/

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#include "status_bar.h"
#include "sp500_monitor.h"
#include "http_download.h"

void* sp500_init(GArray* arguments) {
  monitor_arg_check("sp500", arguments, "(icon)");

  struct sp500_monitor* m = malloc(sizeof(struct sp500_monitor));

  m->base = base_monitor_init(sp500_sleep_time, sp500_update_text, sp500_free);

  char* icon = g_array_index(arguments, GString*, 0)->str;
  m->icon = g_string_new(icon);

  m->res = g_string_new(NULL);
  m->curl = curl_easy_init();

  m->err = malloc((strlen(m->icon->str) + 2)*sizeof(char));
  sprintf(m->err, "%s!", m->icon->str);

  return m;
}

gboolean sp500_update_text(void* ptr) {
  struct sp500_monitor* m = (struct sp500_monitor*)ptr;
  monitor_null_check(m, "sp500_monitor", "update");

  char* output;
  if (!format_price(m->curl, m->res, m->icon)) {
    output = m->res->str;
  } else {
    output = m->err;
  }

  g_mutex_lock(m->base->mutex);
  m->base->text = g_string_assign(m->base->text, output);
  g_mutex_unlock(m->base->mutex);

  return TRUE;
}

int sp500_sleep_time(void* ptr) {
  return 300;
}

void sp500_free(void* ptr) {
  struct sp500_monitor* m = (struct sp500_monitor*)ptr;
  monitor_null_check(m, "sp500_monitor", "free");

  g_string_free(m->icon, TRUE);

  free(m->err);
  g_string_free(m->res, TRUE);
  curl_easy_cleanup(m->curl);

  base_monitor_free(m->base);

  free(m);
}

int format_price(CURL* curl, GString* res, GString* icon) {
  char* apikey = getenv("STATUS_BAR_SP500_APIKEY");
  if (apikey == NULL) {
    fprintf(stderr, "No apikey found in environment variable STATUS_BAR_SP500_APIKEY\n");
    return -1;
  }

  const char* url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=SPY&apikey=%s";
  char* quote_request = malloc(sizeof(char*)*(strlen(url)+strlen(apikey)));
  int quote_request_ret = sprintf(quote_request, url, apikey);
  if (quote_request_ret < 0) {
    return -1;
  }

  GString* quote = g_string_new(NULL);
  CURLcode quote_code = download_data(curl, quote_request, quote);

{'fraction_non_alphanumeric': 0.16392318244170095, 'fraction_numerical': 0.03406492912665752, 'mean_word_length': 4.848930481283422, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6301956', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1669, 'n_words': 323}