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
|
use {
crate::{response::success, route::track_mount},
serde::Deserialize,
};
// static REFERRALS: LazyLock<Vec<Referral>> = LazyLock::new(|| {
// serde_json::from_str(include_str!(
// "../../content/json/stock_market_referrals.json"
// ))
// .unwrap()
// });
// #[derive(Deserialize)]
// struct Referral {
// name: String,
// description: String,
// url: String,
// }
#[derive(Deserialize, Debug)]
struct Quote {
c: f64,
d: Option<f64>,
dp: Option<f64>,
h: f64,
l: f64,
o: f64,
pc: f64,
#[allow(unused)]
t: i64,
}
impl Quote {
pub fn try_to_string(&self) -> Result<String, ()> {
Ok(format!(
"Current Price: ${}\nDaily Change: ${} ({}%)\nHigh of the Day: ${}\nLow \
of the Day: ${}\nOpening Price: ${}\nYesterday's Closing Price: ${}",
self.c,
if let Some(d) = self.d {
d
} else {
return Err(());
},
if let Some(dp) = self.dp {
dp
} else {
return Err(());
},
self.l,
self.h,
self.o,
self.pc,
))
}
}
async fn symbol_to_string(symbol: &str) -> String {
let mut quote = None;
// https://github.com/seanmonstar/reqwest/issues/1017#issuecomment-1157260218
if let Ok(response) = reqwest::get(&format!(
"https://finnhub.io/api/v1/quote?symbol={}&token={}",
symbol,
std::env::var("FINNHUB_TOKEN")
.expect("could not locate FINNHUB_TOKEN environment variable")
))
.await
{
if let Ok(response_content) = response.json::<Quote>().await {
if response_content.dp.is_some() {
quote = Some(response_content);
} else {
return "You have searched for an invalid symbol.".to_string();
}
}
}
quote.as_ref().map_or_else(
|| {
format!(
"An API error has occurred while looking up the {symbol} symbol... \
Take this up with Finnhub.",
)
},
|quote| quote.try_to_string().unwrap(),
)
}
pub fn module(router: &mut windmark::router::Router) {
// track_mount(
// router,
// "/stocks/referrals",
// "Want to start investing in the stock market? Support me by using one of
// \ my referral links!",
// |context| {
// success(
// &format!(
// "# Referrals\n\n=> /stocks Dashboard\n=> /cryptocurrency \
// Cryptocurrency Dashboard\n=> /stocks/telegram Telegram Groups\n=>
// \ /stocks/search Search\n\nWant to start investing? Support me
// by \ using one of my referral links!\n\n{}",
// REFERRALS
// .iter()
// .map(|r| {
// format!("## {}\n\n{}\n\n=> {} {0}", r.name, r.description,
// r.url) })
// .collect::<Vec<String>>()
// .join("\n")
// ),
// &context,
// )
// },
// );
track_mount(
router,
"/stocks",
"Explore and search the stock market using Gemini!",
|context| async move {
success(
&format!(
"# The Stock Market\n\n=> /stocks/search Symbol Search\n=> /stocks/referrals Referrals\n=> /cryptocurrency Cryptocurrency Dashboard\n=> /stocks/telegram Telegram Groups\n\n## Popular \
Symbols\n\n### AAPL\n\n{}\n\n### TSLA\n\n{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub",
symbol_to_string("AAPL").await,
symbol_to_string("TSLA").await
),
&context
)
},
);
track_mount(
router,
"/stocks/search",
"Search for a specific symbol",
|context| async move {
let mut symbol = context.url.query().unwrap_or("Symbol Search");
if symbol.is_empty() {
symbol = "Symbol Search";
}
let mut response = format!(
"# {symbol}\n\n=> /stocks Dashboard\n=> /cryptocurrency \
Cryptocurrency Dashboard\n=> /stocks/telegram Telegram Groups\n=> \
/stocks/search Search",
);
if symbol != "Symbol Search" {
if let Some(query) = context.url.query_pairs().next() {
if query.0 == "action" && query.1 == "go" {
return windmark::response::Response::input(
"Which symbol would you like to explore?",
);
}
let symbol = query.0;
if symbol != "Symbol Search" {
response = format!(
"{}\n\nYou searched for \"{}\"!\n\n## Key Statistics\n\n{}",
response,
symbol,
symbol_to_string(&symbol).await
);
}
}
}
success(
&format!(
"{response}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub",
),
&context
)
},
);
}
|