From 7677eb1425d2bdab23b006f7bad79b18dac58a2d Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Mon, 21 Aug 2023 18:50:05 -0700 Subject: [PATCH] working push to notes api --- .gitignore | 4 +- Cargo.lock | 8 ++++ status_cloud/Cargo.toml | 2 + status_cloud/src/main.rs | 84 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 89 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index cb8c9e0..f49064e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target -.direnv \ No newline at end of file +.direnv + +.env diff --git a/Cargo.lock b/Cargo.lock index a5bb69d..32ef61a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,6 +99,12 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "encoding_rs" version = "0.8.32" @@ -726,8 +732,10 @@ dependencies = [ name = "status_cloud" version = "0.1.0" dependencies = [ + "dotenv", "reqwest", "serde", + "serde_json", ] [[package]] diff --git a/status_cloud/Cargo.toml b/status_cloud/Cargo.toml index 1bd73d7..9cef393 100644 --- a/status_cloud/Cargo.toml +++ b/status_cloud/Cargo.toml @@ -6,5 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +dotenv = "0.15.0" reqwest = { version = "0.11.18", features = ["blocking", "json"] } serde = { version = "1.0.184", features = ["serde_derive"] } +serde_json = "1.0.105" diff --git a/status_cloud/src/main.rs b/status_cloud/src/main.rs index 9b7b36a..99658ba 100644 --- a/status_cloud/src/main.rs +++ b/status_cloud/src/main.rs @@ -1,15 +1,78 @@ use serde::{Deserialize, Serialize}; use reqwest::blocking::Client; -fn main() -> Result<(), Box> { - let client = Client::new(); - let res = client - .get("https://username:password@files.nickiel.net/index.php/apps/notes/api/v1/notes") - .header("Accept", "application/json") - .send()? - .json::>()?; +use dotenv::dotenv; +use std::env; +use std::process::Command; + +fn main() -> Result<(), Box> { + dotenv().ok(); + + let user = env::var("NEXTCLOUD_USER").unwrap(); + let pswd = env::var("NEXTCLOUD_PWD").unwrap(); + let note_id = env::var("NOTE_ID").unwrap(); + + // let client = Client::new(); + // let res = client + // .get(format!("https://files.nickiel.net/index.php/apps/notes/api/v1/notes/{}", note_id)) + // .header("Accept", "application/json") + // .basic_auth(user.clone(), Some(pswd.clone())) + // .send()? + // .json::()?; + // println!("{:#?}", res); + + let mut drives: Vec = vec![]; + + { + let rust_drives = std::fs::read_dir("/dev/").unwrap(); + + for path in rust_drives { + match path { + Ok(ref val) => { + let tmp = val.path().to_string_lossy().to_string(); + if tmp.starts_with("/dev/sd") { + if tmp.len() == 8 { + drives.push(tmp); + } + } + }, + _ => {} + } + } + } + + let mut body_content: String = String::new(); + + { + let mut drive_temps: Vec = vec![]; + + for drive in drives { + let output = Command::new("hddtemp").arg(drive).output()?; + + let tmp = std::str::from_utf8(&output.stdout)?.to_string(); + + if !tmp.contains("sensor") { + drive_temps.push(tmp.replace("\n", "")); + } else { + drive_temps.push(tmp[0..9].to_string() + " No Sensor"); + } + } + body_content.push_str("## Hard Drive Temps\n"); + body_content.push_str(drive_temps.join("\n").as_str()); + } + + + + let update = Client::new() + .put(format!("https://files.nickiel.net/index.php/apps/notes/api/v1/notes/{}", note_id)) + .header("Accept", "application/json") + .header("Content-Type", "application/json") + .basic_auth(user, Some(pswd)) + .body(serde_json::to_string(&NoteUpdate {content: body_content}).unwrap()); + + println!("{:#?}", update.send()? + .json::()?); - println!("{:#?}", res); Ok(()) } @@ -25,3 +88,8 @@ struct Note { content: String, favorite: bool } + +#[derive(Serialize, Deserialize, Debug)] +struct NoteUpdate { + content: String, +}