-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
325 lines (282 loc) · 8.72 KB
/
main.cpp
File metadata and controls
325 lines (282 loc) · 8.72 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdarg>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <thread>
#include <mutex>
#include <vector>
#include <termbox.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <rapidjson/rapidjson.h>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <set>
#include "stock.h"
#include "utility.h"
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
void read_config();
void do_fetch();
void print_tb(const char *str, int x, int y, uint16_t fg, uint16_t bg)
{
while (*str) {
uint32_t uni;
str += tb_utf8_char_to_unicode(&uni, str);
tb_change_cell(x, y, uni, fg, bg);
x++;
}
}
void printf_tb(int x, int y, uint16_t fg, uint16_t bg, const char *fmt, ...)
{
char buf[4096];
va_list vl;
va_start(vl, fmt);
vsnprintf(buf, sizeof(buf), fmt, vl);
va_end(vl);
print_tb(buf, x, y, fg, bg);
}
uint32_t update_time = 5; // seconds
constexpr Property color_property = Property::ChangePercent;
constexpr double color_threshold = 0.5;
std::mutex g_stocks_mutex;
std::map<std::string, Stock> g_stocks;
std::map<std::string, Stock> g_exchanges;
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
read_config();
//g_stocks["EA"] = Stock("EA");
//g_stocks["GOOGL"] = Stock("GOOGL");
//g_stocks["TSLA"] = Stock("TSLA");
//g_stocks["AMZN"] = Stock("AMZN");
//g_stocks["SPY"] = Stock("SPY");
//g_stocks["IBM"] = Stock("IBM");
int ret = tb_init();
if(ret)
{
std::cerr<<"tb_init() failed with error code "<<ret<<"\n";
return 1;
}
// init
tb_clear();
do_fetch();
tb_present();
struct tb_event ev;
while (tb_peek_event(&ev, 1000 * update_time) >= 0)
{
switch (ev.type)
{
case TB_EVENT_KEY:
switch (ev.key) {
case TB_KEY_ESC:
goto done;
break;
case TB_KEY_CTRL_R:
Stock::toggle_sort_mode();
break;
}
break;
case TB_EVENT_RESIZE:
//do_fetch();
break;
}
do_fetch();
}
done:
tb_shutdown();
return 0;
}
void fetch_quote(Stock& stock)
{
using namespace rapidjson;
try {
// build the query
std::string query = "https://finance.google.com/finance?q=";
query += stock.get_ticker();
query += "&output=json";
// perform the query
curlpp::options::Url url(query);
curlpp::Easy myRequest;
myRequest.setOpt(url);
myRequest.setOpt(curlpp::options::HttpGet(true));
std::stringstream ss;
curlpp::options::WriteStream ws(&ss);
myRequest.setOpt(ws);
myRequest.perform();
std::string json_reply = ss.str();
// but for some reason, what we get back isnt valid json....
// need to remove the first two lines and the trailing ]
json_reply = json_reply.substr(5, json_reply.length()-7);
// parse the query
Document d;
d.Parse(json_reply.c_str());
// update the entry
stock.set_valid();
stock.set(Property::Name, d["name"].GetString());
stock.set(Property::Exchange, d["exchange"].GetString());
stock.set(Property::Last, parse_float(d["l"].GetString()));
stock.set(Property::Change, parse_float(d["c"].GetString()));
stock.set(Property::ChangePercent, parse_float(d["cp"].GetString()));
stock.set(Property::Open, parse_float(d["op"].GetString()));
stock.set(Property::High, parse_float(d["hi"].GetString()));
stock.set(Property::Low, parse_float(d["lo"].GetString()));
stock.set(Property::Low52, parse_float(d["lo52"].GetString()));
stock.set(Property::High52, parse_float(d["hi52"].GetString()));
stock.set(Property::Eps, parse_float(d["eps"].GetString()));
stock.set(Property::Pe, parse_float(d["pe"].GetString()));
stock.set(Property::Dividend, parse_float(d["ldiv"].GetString()));
stock.set(Property::Yield, parse_float(d["dy"].GetString()));
stock.set(Property::Shares, parse_float(d["shares"].GetString()));
stock.set(Property::Volume, parse_float(d["vo"].GetString()));
stock.set(Property::AvgVolume, parse_float(d["avvo"].GetString()));
}
catch ( ... )
{
std::cerr<<"do_fetch() failed\n";
}
}
void do_fetch()
{
g_stocks_mutex.lock();
tb_clear();
// query the various exchanges
std::string exchange_string = "";
for(auto& itr : g_exchanges)
{
fetch_quote(itr.second);
const Stock& exch = itr.second;
char buffer[1000];
sprintf(buffer, "%s(%.2f %.2f %.2f%%) ", itr.first.c_str(), exch.get(Property::Last), exch.get(Property::Change), exch.get(Property::ChangePercent));
exchange_string += buffer;
}
printf_tb(0, 0, TB_YELLOW, TB_DEFAULT, exchange_string.c_str());
std::set<Stock> stocks;
for(auto& stock : g_stocks)
{
fetch_quote(stock.second);
stocks.insert(stock.second);
}
printf_tb(0, 1, TB_YELLOW, TB_DEFAULT,
"%-8s%8s%8s%8s%8s%8s%8s%8s%8s%8s%8s",
"Name", "Last", "Change", "Percent", "Open", "52w Hi", "52w Lo", "EPS", "PE", "Volume", "VolumeA"
);
int row = 2;
for(auto& stock : stocks)
{
uint32_t fg_color = TB_YELLOW;
if(stock.get(color_property) > color_threshold)
{
fg_color = TB_GREEN;
}
else if(stock.get(color_property) < -color_threshold)
{
fg_color = TB_RED;
}
printf_tb(0, row++, fg_color, TB_DEFAULT, "%-8s%8.2f%8.2f%7.2f%%%8.2f%8.2f%8.2f%8.2f%8.2f%8.2lf%8.2lf",
stock.get_ticker().c_str(),
stock.get(Property::Last),
stock.get(Property::Change),
stock.get(Property::ChangePercent),
stock.get(Property::Open),
stock.get(Property::High52),
stock.get(Property::Low52),
stock.get(Property::Eps),
stock.get(Property::Pe),
stock.get(Property::Volume) / 100000.0,
stock.get(Property::AvgVolume) / 100000.0
);
}
tb_present();
g_stocks_mutex.unlock();
}
void read_config()
{
// Find the user home directory
const char* homedir = nullptr;
if ((homedir = getenv("HOME")) == NULL)
{
homedir = getpwuid(getuid())->pw_dir;
}
// Check if the configuration file exists there
uint32_t bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
char *buffer = new char[bufsize];
sprintf(buffer, "%s/.cquote", homedir);
FILE *fp = fopen(buffer, "rt");
if(!fp)
{
// We will write a default configuration out
fp = fopen(buffer, "wt");
fprintf(stderr, buffer);
if(!fp)
{
exit(1);
}
fprintf(fp,
"{\n"
"\t\"exchanges\" : [\n"
"\t\t{\"Dow\" : \".DJI\"},\n"
"\t\t{\"S&P500\" : \".INX\"},\n"
"\t\t{\"NASDAQ\" : \".IXIC\"}\n"
"\t],\n"
"\t\"stocks\" : [\n"
"\t\t\"EA\",\n"
"\t\t\"GOOGL\",\n"
"\t\t\"TSLA\",\n"
"\t\t\"AMZN\",\n"
"\t\t\"IBM\",\n"
"\t\t\"MSFT\"\n"
"\t],\n"
"\t\"update\" : 5\n"
"}\n"
);
fclose(fp);
read_config();
return;
}
fclose(fp);
// parse the config file here
using namespace rapidjson;
std::ifstream config (buffer);
std::stringstream ss;
ss << config.rdbuf();
config.close();
Document reader;
reader.Parse(ss.str().c_str());
// Parse exchanges
if(reader.HasMember("exchanges") && reader["exchanges"].IsArray())
{
const Value& a = reader["exchanges"];
for(uint32_t i=0; i<a.Size(); ++i)
{
const Value& exch = a[i];
for(Value::ConstMemberIterator itr = exch.MemberBegin(); itr != exch.MemberEnd(); ++itr)
{
g_exchanges[itr->name.GetString()] = Stock(itr->value.GetString());
}
}
}
// Parse stocks
if(reader.HasMember("stocks") && reader["stocks"].IsArray())
{
const Value& a = reader["stocks"];
for(uint32_t i=0; i<a.Size(); ++i)
{
const Value& exch = a[i];
g_stocks[exch.GetString()] = Stock(exch.GetString());
}
}
if(reader.HasMember("update") && reader["update"].IsInt())
{
update_time = reader["update"].GetInt();
}
}