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
|
// This file is part of Locus <https://github.com/gemrest/locus>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
use std::lazy::SyncLazy;
use serde::Deserialize;
use crate::{route::track_mount, success};
static REFERRALS: SyncLazy<Vec<Referral>> = SyncLazy::new(|| {
serde_json::from_str(include_str!("../../content/json/stock_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,
))
}
}
fn symbol_to_string(symbol: &str) -> String {
let mut quote = None;
if let Ok(response) = reqwest::blocking::get(format!(
"https://finnhub.io/api/v1/quote?symbol={}&token={}",
symbol,
std::env::var("FINNHUB_TOKEN")
.expect("could not locate FINNHUB_TOKEN environment variable")
)) {
if let Ok(response_content) = response.json::<Quote>() {
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... Take \
this up with Finnhub.",
symbol
)
},
|quote| quote.try_to_string().unwrap(),
)
}
pub fn module(router: &mut windmark::Router) {
track_mount(
router,
"/stocks/referrals",
"Want to start investing? Support me by using one of my referral links!",
Box::new(|context| {
success!(
format!(
"# Referrals\n\n=> /stocks Home\n=> /stocks/search?action=go \
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!",
Box::new(|context| {
success!(
format!(
"# Stocks\n\n=> /stocks/search Symbol Search\n=> /stocks/referrals Referrals\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"),
symbol_to_string("TSLA")
),
context
)
}),
);
track_mount(
router,
"/stocks/search",
"Search for a specific symbol",
Box::new(|context| {
let mut symbol = context.url.query().unwrap_or("Symbol Search");
if symbol.is_empty() {
symbol = "Symbol Search";
}
let mut response = format!(
"# {}\n\n=> /stocks Home\n=> /stocks/search?action=go Search!",
symbol
);
if symbol != "Symbol Search" {
if let Some(query) = context.url.query_pairs().next() {
if query.0 == "action" && query.1 == "go" {
return windmark::Response::Input(
"Which symbol would you like to explore?".to_string(),
);
}
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)
);
}
}
}
success!(
format!(
"{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub",
response
),
context
)
}),
);
}
|