18_fundamentals_update/
18_fundamentals_update.rs

1use gurufocus_api as gfapi;
2use std::{convert::TryFrom, env};
3use time::{Date, UtcDateTime};
4
5type UpdatedStocks = Vec<String>;
6
7fn get_months_before(date: Date, months: u8) -> Date {
8    let mut day = date.day();
9    let mut month = date.month() as u8;
10    let mut year = date.year();
11
12    while month + 1 < months {
13        month += 12;
14        year -= 1;
15    }
16
17    month -= months;
18    let month: time::Month = time::Month::try_from(month).unwrap();
19    day = day.min(month.length(year));
20
21    Date::from_calendar_date(year, month, day).unwrap()
22}
23
24#[tokio::main]
25async fn main() {
26    let token = env::var("GURUFOCUS_TOKEN").unwrap();
27    let gf_connect = gfapi::GuruFocusConnector::new(token);
28
29    let now = UtcDateTime::now().date();
30    let one_months_ago = get_months_before(now, 6);
31    let stocks = gf_connect.get_updated_stocks(one_months_ago).await.unwrap();
32
33    let stocks: UpdatedStocks = serde_json::from_value(stocks).unwrap();
34    println!(
35        "List of stocks with updated fundamental data since {}\n{:#?}",
36        one_months_ago, stocks
37    );
38}