hoards iqair data
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.toml | 8 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | src/main.rs | 52 |
5 files changed, 84 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8b57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +key +Cargo.lock
\ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..938aec2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "airhoard" +version = "0.1.0" +edition = "2021" + +[dependencies] +iqair = "0.1.0" +serde_json = "1.0.138" @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 bendn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..bea5c64 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +hoards iqair data diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..364713b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,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)); + } +} |