/*********************************************************** * Get Stock Price * * Program to get the value of any stock available from * www.google.com/finance/quote/####### * * REQUIRES: ddgr, wget * * program: get_stock_price * author : yetimach * date : June 08, 2022 * * Usage: get_stock_price STOCK NAME * ************************************************************/ #include #include #include #include #include #include #define MAX_CH 200 // max chars, used for various strings #define MAX_C 30 // smaller max chars--I don't know why : D #define SP "data-last-price=\"" //search pattern #define D '#' //delimiter for separating strings with white space FILE *wdf; //wget dump file char temp_num_st[MAX_C]; // temporary number string float temp_num_float; char g_temp_sn[MAX_CH]; // global temp stock name char g_temp_url[MAX_CH]; // global temp url float g_csp = 0; //global current stock price float get_price_after_pat(FILE *file, char *pattern){ float price; int pattern_len = 0; char num_str[MAX_C]; char ch; while ((ch = pattern[pattern_len++]) != '\0') ; --pattern_len; int matches = 0; int sp = 0; while ((ch = getc(wdf)) != EOF){ if (ch != pattern[matches]) matches = 0; else ++matches; if (matches == pattern_len) break; } if (ch == EOF) return -1; for (int i = 0; i < 10; i++){ ch = getc(wdf); if (isdigit(ch) || ch == '.') num_str[sp++] = ch; } price = atof(num_str); return (price > 0) ? price : -1; } int is_valid_url(char *the_url){ int i; char temp_the_url[MAX_CH]; char *good_url ="www.google.com/finance/quote/"; // the idea here is to put the first 29 chars into temp_the_url for (i = 0; i < 29; i++) temp_the_url[i] = the_url[i]; temp_the_url[i] = '\0'; return ((strcmp(temp_the_url, good_url)) == 0) ? 1 : -1; } // use ddgr to generate a file with the correct name and url for a given stock // nos = name of stock int get_correct_n_and_u(char *nos){ char path[MAX_CH]; strcpy(path, getenv("HOME")); strcat(path, "/.gfst/gfst_temp_file.txt"); char ddgr_cmd[MAX_CH] = "ddgr -n=1 -x --np google finance quote "; strcat(ddgr_cmd, nos); strcat(ddgr_cmd, " > "); strcat(ddgr_cmd, path); system(ddgr_cmd); FILE *gfst_temp_file = fopen(path, "r"); char sn[MAX_CH]; //correct stock name char snt[MAX_CH]; //correct stock name temp int si = 0; //string index // the first line in the file is the stock name (hopefully) while ((snt[si++] = getc(gfst_temp_file)) != '\n') ; si = 0; char url[MAX_CH]; //correct url char urlt[MAX_CH]; //correct url temp // the second line in the file is the google finance url (hopefully) while ((urlt[si++] = getc(gfst_temp_file)) != '\n') ; si = 0; // time to clean up the name, remove "1. " from the beginning and // cut it at the final ")" while (!isalpha(snt[si++])) if (si > 199) break; --si; int si2 = 0; while ((sn[si2++] = snt[si++]) != ')') if (si2 > 199) break; sn[si2] = '\0'; char ch; si = 0; si2 = 0; while ((ch = urlt[si++]) != 'w') if (si > 199) break; --si; while ((url[si2++] = urlt[si++]) != ' ') if (si > 199) break; --si2; url[si2] = '\0'; int counter = 0; //remove the mystery newline... while (url[counter++] != '\0'){ if (url[counter-1] == '\n') url[counter-1] = '\0'; } int success = is_valid_url(url); if (success){ strcpy(g_temp_sn, sn); strcpy(g_temp_url, url); } char rm_cmd[MAX_CH] = "rm "; strcat(rm_cmd, path); system(rm_cmd); return success; } float get_current_st_price(char *gf_url){ char wdfp[MAX_CH]; //wget dump file path strcpy(wdfp, getenv("HOME")); strcat(wdfp, "/.gfst/wget_dump_file.txt"); char touch_cmd[MAX_CH] = "touch "; strcat(touch_cmd, wdfp); system(touch_cmd); char wget_cmd[MAX_CH] = "wget "; strcat(wget_cmd, gf_url); strcat(wget_cmd, " -q -O "); strcat(wget_cmd, wdfp); system(wget_cmd); wdf = fopen(wdfp, "r"); float csp = get_price_after_pat(wdf, SP); return csp; } int main(int argc, char *argv[]) { if (argc >= 2){ char search_term[MAX_CH] = ""; int word = 1; while (argc-- >= 2){ strcat(search_term, argv[word++]); if (argc + 1 != 2) strcat(search_term, " "); } char gfst_dir[MAX_CH]; strcpy(gfst_dir, getenv("HOME")); strcat(gfst_dir, "/.gfst"); DIR* dir = opendir(gfst_dir); if (dir) { // HOME/.gfst exists closedir(dir); } else if (ENOENT == errno) { char mkdir_cmd[MAX_CH] = "mkdir "; strcat(mkdir_cmd, gfst_dir); system(mkdir_cmd); } else { printf("\n\tUnknown error : (\n\n"); return -1; } int check = get_correct_n_and_u(search_term); if (check == -1 || g_csp == -1){ printf("\n\tBummer, couldn't find a stock called \"%s\".\n\n", search_term); return 0; } g_csp = get_current_st_price(g_temp_url); if (g_csp == -1){ printf("\n\tBummer, couldn't find a stock called \"%s\".\n\n", search_term); return 0; } printf("\n\t%s\n\t$%.2f\n\n", g_temp_sn, g_csp); }else printf("\n\tUsage: get_stock_price STOCK NAME\n\n"); return 0; }