hoards iqair data
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
use std::{
    env::args,
    thread::sleep,
    time::{Duration, SystemTime},
};
fn main() {
    if matches!(
        args().nth(1).as_deref(),
        Some("--help" | "-h" | "help" | "h")
    ) {
        println!("{} [OUTFILE] (KEY | env[AIR_KEY])", args().next().unwrap());
        std::process::exit(0);
    }
    let Some(output) = args().nth(1) else {
        println!("\x1b[31;1mno output file provided!\x1b[0m");
        std::process::exit(0);
    };
    let Some(key) = args().nth(2).or(std::env::var("AIR_KEY").ok()) else {
        println!("\x1b[31;1mno key provided!\x1b[0m");
        println!("get one at https://dashboard.iqair.com/personal/api-keys for free");
        println!("then set AIR_KEY or pass it to this binary");
        std::process::exit(0);
    };

    loop {
        let mut result = iqair::nearest(&key);
        while result.is_err() {
            result = iqair::nearest(&key);
            sleep(Duration::from_secs(5 * 60));
        }
        let result = result.unwrap();
        let mut f = std::fs::OpenOptions::new()
            .append(true)
            .create(true)
            .open(&output)
            .expect("file accessible");
        let json = serde_json::to_string(&result.current).unwrap();
        use std::io::Write;
        writeln!(f, "{json}").unwrap();
        f.flush().unwrap();
        drop(f);

        let unix = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();
        // see you next hour
        let next = 65 * 60 - (unix % (60 * 60));
        println!("sleeping {} minutes...", next / 60);
        sleep(Duration::from_secs(next));
    }
}