mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
Refactor Yahoo Finance client to fetch symbols concurrently
Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
984275711a
commit
fb03dc8d14
3 changed files with 45 additions and 15 deletions
18
src-tauri/Cargo.lock
generated
18
src-tauri/Cargo.lock
generated
|
|
@ -1094,6 +1094,21 @@ dependencies = [
|
||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures"
|
||||||
|
version = "0.3.32"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d"
|
||||||
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
|
"futures-core",
|
||||||
|
"futures-executor",
|
||||||
|
"futures-io",
|
||||||
|
"futures-sink",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-channel"
|
name = "futures-channel"
|
||||||
version = "0.3.32"
|
version = "0.3.32"
|
||||||
|
|
@ -1101,6 +1116,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d"
|
checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -1168,6 +1184,7 @@ version = "0.3.32"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
|
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
"futures-io",
|
"futures-io",
|
||||||
"futures-macro",
|
"futures-macro",
|
||||||
|
|
@ -3710,6 +3727,7 @@ name = "summarum"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
|
"futures",
|
||||||
"reqwest 0.12.28",
|
"reqwest 0.12.28",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
|
futures = "0.3.32"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
strip = true
|
strip = true
|
||||||
|
|
|
||||||
|
|
@ -295,22 +295,33 @@ async fn fetch_market_data(
|
||||||
// v7/finance/quote now requires a cookie+crumb pair Yahoo stopped issuing
|
// v7/finance/quote now requires a cookie+crumb pair Yahoo stopped issuing
|
||||||
// to plain API clients (returns 401); v8 chart still answers anonymously,
|
// to plain API clients (returns 401); v8 chart still answers anonymously,
|
||||||
// one symbol per request.
|
// one symbol per request.
|
||||||
let mut prices = std::collections::HashMap::new();
|
let futures_reqs = symbols.iter().map(|sym| {
|
||||||
for sym in &symbols {
|
let client = client.clone();
|
||||||
let url = format!(
|
async move {
|
||||||
"https://query1.finance.yahoo.com/v8/finance/chart/{}?range=1d&interval=1d",
|
let url = format!(
|
||||||
sym
|
"https://query1.finance.yahoo.com/v8/finance/chart/{}?range=1d&interval=1d",
|
||||||
);
|
sym
|
||||||
let Ok(resp) = client.get(&url).send().await else {
|
);
|
||||||
continue;
|
if let Ok(resp) = client.get(&url).send().await {
|
||||||
};
|
if let Ok(body) = resp.json::<serde_json::Value>().await {
|
||||||
let Ok(body) = resp.json::<serde_json::Value>().await else {
|
if let Some(price) =
|
||||||
continue;
|
body["chart"]["result"][0]["meta"]["regularMarketPrice"].as_f64()
|
||||||
};
|
{
|
||||||
if let Some(price) = body["chart"]["result"][0]["meta"]["regularMarketPrice"].as_f64() {
|
if price > 0.0 {
|
||||||
if price > 0.0 {
|
return Some((sym.clone(), price));
|
||||||
prices.insert(sym.clone(), price);
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut prices = std::collections::HashMap::new();
|
||||||
|
let results = futures::future::join_all(futures_reqs).await;
|
||||||
|
for res in results {
|
||||||
|
if let Some((sym, price)) = res {
|
||||||
|
prices.insert(sym, price);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue