Skip to content

Commit 0ffcb95

Browse files
committed
Optimized connection creation for yahoo
1 parent dc5d7e4 commit 0ffcb95

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

src/divanalysis/main.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use calamine::{open_workbook, Xlsx};
22
use clap::Parser;
3-
use polars::prelude::*;
43
use indicatif::ProgressBar;
4+
use polars::prelude::*;
55

66
// TODO: Make progressbar
77
// TODO: convert dividends derived elements into TTM data
@@ -235,8 +235,6 @@ fn get_companies_data(
235235
database: Option<String>,
236236
target_yield: f64,
237237
) -> Result<(), &'static str> {
238-
239-
240238
// If we have explicitly given companies then make progress bar with specific length
241239
// otherwise just make the one without length
242240
let pb = if companies.is_empty() {
@@ -321,6 +319,10 @@ fn get_companies_data(
321319
df
322320
};
323321

322+
let use_polygon = std::env::var("POLYGON_AUTH_KEY").is_ok();
323+
let mut provider = investments_forecasting::get_yahoo_connector()
324+
.map_err(|_| "Error: Unable to create yahoo connector")?;
325+
324326
let maybe_success = companies.iter().try_for_each(|symbol| {
325327
let (
326328
share_price,
@@ -335,11 +337,12 @@ fn get_companies_data(
335337
years_of_growth,
336338
payout_ratio,
337339
sector_desc,
338-
) = if std::env::var("POLYGON_AUTH_KEY").is_ok() {
340+
) = if use_polygon {
339341
investments_forecasting::get_polygon_data(symbol)
340342
.expect("Error: unable to get Data from polygon IO for forecasting")
341343
} else {
342-
investments_forecasting::get_yahoo_data(symbol)
344+
let myref = provider.as_mut().unwrap();
345+
investments_forecasting::get_yahoo_data(symbol, myref)
343346
.expect("Error: unable to get Data from yahoo finance for forecasting")
344347
};
345348

@@ -515,18 +518,13 @@ fn main() -> Result<(), &'static str> {
515518
None
516519
};
517520

518-
519-
520521
//let company = <std::string::String as AsRef<str>>::as_ref(&args.company).to_uppercase();
521522
let companies = args
522523
.company
523524
.iter()
524525
.map(|x| x.to_uppercase())
525526
.collect::<Vec<String>>();
526527

527-
528-
529-
530528
// For no handpicked companies just make overall analysis
531529
if companies.len() == 0 {
532530
if args.list_all {

src/divforecasting/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ fn forecast_dividend_stocks(
342342
if std::env::var("POLYGON_AUTH_KEY").is_ok() {
343343
investments_forecasting::get_polygon_data(&name).expect("Error: unable to get Data from polygon IO for forecasting")
344344
} else {
345-
investments_forecasting::get_yahoo_data(&name).expect("Error: unable to get Data from yahoo finance for forecasting")
345+
let provider = investments_forecasting::get_yahoo_connector().expect("Unable to create yahoo connector");
346+
investments_forecasting::get_yahoo_data(&name,&mut provider.unwrap()).expect("Error: unable to get Data from yahoo finance for forecasting")
346347
};
347348
num_capitalizations = frequency.expect("Cannot forecast dividend gains as there is no dividend data") as u32;
348349
let divy = divy.expect("Cannot forecast dividend gains as there is no dividend data");

src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ use polygon_client::rest::RESTClient;
1010
use std::collections::BTreeMap;
1111
use std::collections::HashMap;
1212

13-
// TODO: connector reuse of yahoo
14-
13+
pub fn get_yahoo_connector() -> Result<Option<yahoo::YahooConnector>, String> {
14+
if std::env::var("POLYGON_AUTH_KEY").is_ok() {
15+
Ok(None)
16+
} else {
17+
Ok(Some(yahoo::YahooConnector::new().map_err(|_| {
18+
"Could not create Yahoo provider".to_owned()
19+
})?))
20+
}
21+
}
1522

1623
pub fn load_list<R>(excel: &mut Xlsx<R>, category: &str) -> Result<DataFrame, &'static str>
1724
where
@@ -591,6 +598,7 @@ async fn get_dividiend_data(
591598
}
592599
pub fn get_yahoo_data(
593600
company: &str,
601+
provider: &mut yahoo::YahooConnector,
594602
) -> Result<
595603
(
596604
f64,
@@ -609,9 +617,6 @@ pub fn get_yahoo_data(
609617
&'static str,
610618
> {
611619
log::info!("Yahoo: Getting Ticker: {}", company);
612-
// create provider
613-
let mut provider =
614-
yahoo::YahooConnector::new().map_err(|_| "Could not create Yahoo provider")?;
615620

616621
// stock price
617622
let response = provider.get_latest_quotes(company, "1d").map_err(|e| {

0 commit comments

Comments
 (0)