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
extern crate chrono;
extern crate reqwest;
extern crate serde_json;
use reqwest::StatusCode;
use serde_json::Value;
pub mod gurus;
pub use gurus::*;
pub mod stock;
pub use stock::*;
pub mod financials;
pub use financials::*;
pub mod keyratios;
pub use keyratios::*;
pub mod insiders;
pub use insiders::*;
pub mod portfolio;
pub use portfolio::*;
pub mod strnum;
pub mod hexnum;
pub struct GuruFocusConnector {
url: &'static str,
user_token: String,
}
impl GuruFocusConnector {
pub fn new(token: String) -> GuruFocusConnector {
GuruFocusConnector {
url: "https://api.gurufocus.com/public/user/",
user_token: token,
}
}
pub fn get_financials(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/financials", stock);
self.send_request(args.as_str())
}
pub fn get_key_ratios(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/keyratios", stock);
self.send_request(args.as_str())
}
pub fn get_quotes(&self, stocks: &[&str]) -> Result<Value, String> {
let args = format!("stock/{}/quote", compact_list(&stocks));
self.send_request(args.as_str())
}
pub fn get_price_hist(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/price", stock);
self.send_request(args.as_str())
}
pub fn get_unadj_price_hist(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/unadjusted_price", stock);
self.send_request(args.as_str())
}
pub fn get_stock_summary(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/summary", stock);
self.send_request(args.as_str())
}
pub fn get_guru_trades(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/gurus", stock);
self.send_request(args.as_str())
}
pub fn get_insider_trades(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/insider", stock);
self.send_request(args.as_str())
}
pub fn get_gurus(&self) -> Result<Value, String> {
self.send_request("gurulist")
}
pub fn get_guru_picks(
&self,
gurus: &[&str],
start_date: chrono::NaiveDate,
) -> Result<Value, String> {
let args = format!(
"guru/{}/picks/{}",
compact_list(&gurus),
start_date.format("%F")
);
self.send_request(args.as_str())
}
pub fn get_guru_portfolios(&self, gurus: &[&str]) -> Result<Value, String> {
let args = format!("guru/{}/aggregated", compact_list(&gurus));
self.send_request(args.as_str())
}
pub fn get_exchanges(&self) -> Result<Value, String> {
self.send_request("exchange_list")
}
pub fn get_listed_stocks(&self, exchange: &str) -> Result<Value, String> {
let args = format!("exchange_stocks/{}", exchange);
self.send_request(args.as_str())
}
pub fn get_insider_updates(&self) -> Result<Value, String> {
self.send_request("insider_updates")
}
pub fn get_dividend_history(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/dividend", stock);
self.send_request(args.as_str())
}
pub fn get_analyst_estimate(&self, stock: &str) -> Result<Value, String> {
let args = format!("stock/{}/analyst_estimate ", stock);
self.send_request(args.as_str())
}
pub fn get_personal_portfolio(&self) -> Result<Value, String> {
self.send_request("portfolio/my_portfolios")
}
pub fn get_updated_stocks(&self, date: chrono::NaiveDate) -> Result<Value, String> {
let args = format!("funda_updated/{}", date);
self.send_request(args.as_str())
}
fn send_request(&self, args: &str) -> Result<Value, String> {
let url: String = format!("{}{}/{}", self.url, self.user_token, args);
let resp = reqwest::get(url.as_str());
if resp.is_err() {
return Err(String::from("Connection to server failed."));
}
let mut resp = resp.unwrap();
match resp.status() {
StatusCode::OK => match resp.json() {
Ok(json) => Ok(json),
err => Err(format!("Parsing json failed: {:?}", err)),
},
StatusCode::FORBIDDEN => match resp.json() {
Ok(json) => Err(format!("Access forbidden, {}.", get_error(json))),
_ => Err(format!("Access forbidden.")),
},
StatusCode::NOT_FOUND => match resp.json() {
Ok(json) => Err(format!("Not found, {}.", get_error(json))),
_ => Err(format!("Access forbidden.")),
},
err => match resp.json() {
Ok(json) => Err(format!("Unspecified error, {}.", get_error(json))),
_ => Err(format!("Received bad response from server: {}", err)),
},
}
}
}
fn get_error(err: serde_json::Value) -> String {
match err {
Value::Object(map) => match &map["error"] {
Value::String(msg) => msg.to_string(),
val => format!("error was '{}'.", val),
},
val => format!("response was '{}'", val),
}
}
fn compact_list(a: &[&str]) -> String {
if a.len() == 0 {
return String::new();
}
let mut it = a.iter();
let mut res = format!("{}", it.next().unwrap());
for n in it {
res.push_str(&format!(",{}", n));
}
res
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compact_list() {
assert_eq!(compact_list(&["1", "2", "3"]), "1,2,3");
assert_eq!(compact_list(&[]), "");
assert_eq!(compact_list(&["3"]), "3");
}
}