/
Buy_target.c
198 lines (185 loc) · 6.46 KB
/
Buy_target.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* Buy_target.c
* monitor real-time price for a given symbol, notify via EMAIL and/or popup when target buy price is reached.
* Parms: Sym Price(as 12.34)
*
* compile: gcc -Wall -O2 -ffast-math -o Buy_target Buy_target.c -liniparser `mysql_config --include --libs` `curl-config --libs`
*/
// set DEBUG to 1 for testing, 0 for production
#define DEBUG 0
#define INIFILE "notify.list"
#define SLEEP_INTERVAL 90
#define MARKET_CLOSE "15:00"
#define MARKET_OPEN "8:30"
#define _XOPEN_SOURCE
#define _XOPENSOURCE
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include <ctype.h>
#include <iniparser.h>
MYSQL *mysql;
struct MemStruct {
char *memory;
size_t size;
};
struct MemStruct chunk;
#include "Includes/print_error.inc"
#include "Includes/valid_sym.inc"
#include "Includes/ParseData.inc"
void Usage(char *prog) {
printf("Usage: %s Sym Price(format as 43.21)\n", prog);
exit(EXIT_FAILURE);
}
int main(int argc, char * argv[]) {
char query[1024];
char Sym[16];
char Price[16];
char SymExch[32];
char mail_msg[1024];
char ini_file[1024];
char errbuf[CURL_ERROR_SIZE];
char market_open[12]=MARKET_OPEN;
char market_close[12]=MARKET_CLOSE;
char *saveptr;
int x;
float TargetPrice=FLT_MAX;
float cur_price=0.0;
CURL *curl;
CURLcode res=0;
MYSQL_RES *result;
MYSQL_ROW row;
time_t t;
struct tm *TM=0;
dictionary *d;
// parse cli parms
if (argc < 3) Usage(argv[0]);
// set up variables
memset(query,0,sizeof(query));
memset(SymExch,0,sizeof(SymExch));
memset(Sym,0,sizeof(Sym));
for (x=0;x<strlen(argv[1]);x++) Sym[x]=toupper(argv[1][x]);
errno=0;
if ((TargetPrice = strtof(argv[2],NULL)) == 0) Usage(argv[0]);
if (errno == ERANGE) { puts("Error converting price"); Usage(argv[0]); }
// initialize CURL
if ((chunk.memory = calloc(1,1)) == NULL) {
fprintf(stderr,"calloc failed, aborting process\n");
exit(EXIT_FAILURE);
}
curl_global_init(CURL_GLOBAL_NOTHING);
curl=curl_easy_init();
if(!curl) {
fprintf(stderr,"curl init failed, aborting process\n");
exit(EXIT_FAILURE);
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ParseData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
#include "Includes/beancounter-conn.inc"
valid_sym(Sym);
// get exchange for Sym
sprintf(query,"select trim(exchange) from stockinfo where symbol = \"%s\"",Sym);
if (mysql_query(mysql,query)) print_error(mysql, "Failed to query database");
if ((result=mysql_store_result(mysql)) == NULL) print_error(mysql, "store_results failed");
row=mysql_fetch_row(result);
sprintf(SymExch,"%s:%s",row[0],Sym);
if (DEBUG) printf("%s\n",SymExch);
// finished with the database
mysql_free_result(result);
#include "Includes/mysql-disconn.inc"
sprintf(query,"http://www.google.com/finance?q=%s",SymExch);
curl_easy_setopt(curl, CURLOPT_URL, query);
// Loop while price is less than target
if (DEBUG) printf("Monitoring price for %s\n",Sym);
do {
// check the time
t = time(NULL);
if ((TM = localtime(&t)) == NULL) { perror("localtime"); exit(EXIT_FAILURE); }
if (DEBUG) printf("Close: %lu\tTM %d\n",strtoul(market_close,&saveptr,10),TM->tm_hour);
if (TM->tm_hour>strtoul(market_close,&saveptr,10) ||
(TM->tm_hour==strtoul(market_close,&saveptr,10) && TM->tm_min>(strtoul(saveptr+1,NULL,10)+(SLEEP_INTERVAL/60)))) {
// market is closed for the day
printf("Stock market is closed for the day (after %s)\n",market_close);
exit(EXIT_FAILURE);
}
if (DEBUG) printf("Open: %lu\n",strtoul(market_open,&saveptr,10));
if (TM->tm_hour<strtoul(market_open,&saveptr,10) ||
(TM->tm_hour==strtoul(market_open,&saveptr,10) && TM->tm_min<atoi(saveptr+1))) {
printf("Stock market not open yet, sleeping until %s\n",market_open);
sleep((strtoul(market_open,&saveptr,10)-TM->tm_hour)*3600);
while (atoi(saveptr+1)>TM->tm_min) {
sleep(SLEEP_INTERVAL);
t = time(NULL);
if ((TM = localtime(&t)) == NULL) { perror("localtime"); exit(EXIT_FAILURE); }
x=strtoul(market_open,&saveptr,10);
}
}
// download current price
chunk.size = 0;
res=curl_easy_perform(curl);
if (res) { // 0 means no errors
if (res != 56 && res != 28) {
printf("01 Error %d Unable to access the internet\n",res);
printf("%s\n",errbuf);
exit(EXIT_FAILURE);
} else { // retry
if (DEBUG) puts("CURL error connecting to the internet, retrying");
sleep(SLEEP_INTERVAL);
continue;
}
}
// was any data returned?
if (strstr(chunk.memory,"404 Not Found")) {
printf("%s not found\n",Sym);
curl_easy_cleanup(curl);
free(chunk.memory);
exit(EXIT_FAILURE);
}
if (chunk.size == 0) {
if (DEBUG) printf("Error receiving data for %s, retrying\n",Sym);
sleep(SLEEP_INTERVAL);
continue;
}
// do the token parsing here
if ((saveptr = strstr(chunk.memory,"<span class=\"pr\">")) == NULL) {
if (DEBUG) printf("Error parsing data for %s, retrying\n",Sym);
sleep(SLEEP_INTERVAL);
continue;
}
saveptr += strlen("<span class=\"pr\">")+1;
saveptr = strchr(saveptr,'>')+1; // should now be pointing at the current price
memset(Price,0,sizeof(Price));
strncpy(Price,saveptr,(strchr(saveptr,'<'))-saveptr);
errno=0;
if ((cur_price = strtof(Price,NULL)) == 0) break;
if (errno == ERANGE) break;
if (DEBUG) printf("cur_price: %.2f Target: %.2f\n",cur_price,TargetPrice);
if (DEBUG) printf("Pausing for %d seconds\n",SLEEP_INTERVAL);
if (cur_price <= TargetPrice) break;
sleep(SLEEP_INTERVAL);
} while(cur_price > TargetPrice);
// end While loop
curl_easy_cleanup(curl);
free(chunk.memory);
// process the output message
sprintf(ini_file, "%s/%s", getenv("HOME"), INIFILE);
d = iniparser_load(ini_file);
if (d==NULL) {
puts("INI file not opened, aborting");
exit(EXIT_FAILURE);
}
sprintf(query,"Buy %s at %.2f target price",Sym,cur_price);
printf("%s\n",query);
if (iniparser_find_entry(d, "Dest:email")) {
sprintf(mail_msg,"echo %s |mailx -s \"%s\" %s",query,query, iniparser_getstring(d, "Dest:email", "null"));
system(mail_msg);
}
if (iniparser_find_entry(d, "Dest:sms")) {
sprintf(mail_msg,"echo %s |mailx -s \"%s\" %s",query,query, iniparser_getstring(d, "Dest:sms", "null"));
system(mail_msg);
}
iniparser_freedict(d);
exit(EXIT_SUCCESS);
}