parent
6cc989eb4c
commit
0782e7194e
@ -1,4 +1,5 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"status_cloud"
|
||||
"status_cloud",
|
||||
"time_tracker"
|
||||
]
|
||||
|
@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "time_tracker"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.26"
|
||||
chrono-tz = "0.8.3"
|
||||
clap = { version = "4.4.0", features = ["derive"] }
|
||||
log = "0.4.20"
|
||||
reqwest = { version = "0.11.18", features = ["blocking", "json"] }
|
||||
serde = { version = "1.0.184", features = ["serde_derive"] }
|
||||
serde_json = "1.0.105"
|
||||
simplelog = "0.12.1"
|
||||
toml = "0.7.6"
|
@ -0,0 +1,225 @@
|
||||
use chrono::{DateTime, Utc, Datelike};
|
||||
use chrono_tz::US::Central;
|
||||
use clap::Parser;
|
||||
use log::{debug, error};
|
||||
use reqwest::blocking::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// --- Loading and setup ---
|
||||
let args = CliArgs::parse();
|
||||
|
||||
simplelog::SimpleLogger::init(
|
||||
match args.debug {
|
||||
true => simplelog::LevelFilter::Debug,
|
||||
false => simplelog::LevelFilter::Info,
|
||||
},
|
||||
simplelog::Config::default(),
|
||||
)?;
|
||||
|
||||
debug!("Opening Config file: {}", args.config_file.display());
|
||||
debug!(
|
||||
"Config file exists: {}",
|
||||
std::fs::metadata(&args.config_file).is_ok()
|
||||
);
|
||||
|
||||
let file_contents = match std::fs::read_to_string(&args.config_file) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
error!("Could not read config file: {e}");
|
||||
panic!("{e}");
|
||||
}
|
||||
};
|
||||
|
||||
let cfg: Config = match toml::from_str(&file_contents) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
error!("Could not parse config file: {e}");
|
||||
panic!("{e}");
|
||||
}
|
||||
};
|
||||
// --- END Loading and setup ---
|
||||
|
||||
// --- Get checked and unchecked ---
|
||||
let primary_note = Client::new()
|
||||
.get(format!(
|
||||
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
||||
&cfg.server_url, &cfg.primary_note_id
|
||||
))
|
||||
.header("Accept", "application/json")
|
||||
.header("Content-Type", "application/json")
|
||||
.basic_auth(&cfg.user, Some(&cfg.pswd))
|
||||
.send()?
|
||||
.json::<Note>()?;
|
||||
|
||||
let primary_note_lines: Vec<&str> = primary_note.content.lines().collect();
|
||||
|
||||
let mut unchecked: Vec<String> = vec![];
|
||||
let mut checked: Vec<String> = vec![];
|
||||
|
||||
for line in primary_note_lines.iter() {
|
||||
// if line is a checkbox
|
||||
if line.starts_with("- [") {
|
||||
if line.starts_with("- [ ] ") {
|
||||
unchecked.push(line.replace("- [ ] ", ""));
|
||||
} else if line.starts_with("- [x] ") {
|
||||
checked.push(line.replace("- [x] ", ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- END Get checked and unchecked ---
|
||||
|
||||
// --- Get current log ---
|
||||
let logging_note = Client::new()
|
||||
.get(format!(
|
||||
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
||||
&cfg.server_url, &cfg.logging_note_id
|
||||
))
|
||||
.header("Accept", "application/json")
|
||||
.header("Content-Type", "application/json")
|
||||
.basic_auth(&cfg.user, Some(&cfg.pswd))
|
||||
.send()?
|
||||
.json::<Note>()?;
|
||||
|
||||
let now = Utc::now().with_timezone(&Central);
|
||||
|
||||
let mut body_content: Vec<String> = vec![format!(
|
||||
"*Last Updated:* {} Central Time",
|
||||
now.format("%D - %H:%M:%S")
|
||||
)];
|
||||
|
||||
let logging_note_lines: Vec<&str> = logging_note.content.lines().collect();
|
||||
|
||||
for line in logging_note_lines {
|
||||
if line.starts_with("*Last Updated") {
|
||||
continue;
|
||||
}
|
||||
if line.starts_with("# ") {
|
||||
body_content.push(line.to_string());
|
||||
continue;
|
||||
}
|
||||
if line.starts_with("### ") {
|
||||
body_content.push(line.to_string());
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Assume it is a started log ---
|
||||
let split: Vec<&str> = line.split('|').collect();
|
||||
|
||||
let item = match split.first() {
|
||||
Some(val) => val,
|
||||
None => {
|
||||
error!("Couldn't split correctly, item 1");
|
||||
panic!("Couldn't split correctly, item 1");
|
||||
}
|
||||
}
|
||||
.replace('#', "")
|
||||
.trim()
|
||||
.to_string();
|
||||
|
||||
if unchecked.contains(&item) {
|
||||
let timestamp = match split.get(1) {
|
||||
Some(val) => val,
|
||||
None => {
|
||||
error!("Couldn't split correctly, item 2");
|
||||
panic!("Couldn't split correctly, item 2");
|
||||
}
|
||||
};
|
||||
let start_time = match DateTime::parse_from_rfc2822(timestamp) {
|
||||
Ok(val) => val.with_timezone(&Central),
|
||||
Err(e) => {
|
||||
error!("Error parsing time: '{timestamp}' : {e}");
|
||||
panic!("Error parsing time: '{timestamp}' : {e}");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let elapsed_time: chrono::Duration = now - start_time;
|
||||
|
||||
body_content.push(format!(
|
||||
"### {item} | {}h {}m | {}/{}/{}",
|
||||
elapsed_time.num_hours(),
|
||||
elapsed_time.num_minutes(),
|
||||
now.month(),
|
||||
now.day(),
|
||||
now.year()
|
||||
));
|
||||
} else {
|
||||
if line != "" {
|
||||
body_content.push(line.to_string());
|
||||
}
|
||||
}
|
||||
checked.retain(|val| val != &item);
|
||||
}
|
||||
|
||||
checked = checked
|
||||
.into_iter()
|
||||
.map(|val| {
|
||||
format!(
|
||||
"## {val} | {}",
|
||||
Utc::now().with_timezone(&Central).to_rfc2822()
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
debug!("checked: {:#?}", checked);
|
||||
debug!("unchecked: {:#?}", unchecked);
|
||||
|
||||
body_content.splice(2..2, checked);
|
||||
|
||||
debug!("{:#?}", body_content);
|
||||
|
||||
Client::new()
|
||||
.put(format!(
|
||||
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
||||
&cfg.server_url, &cfg.logging_note_id
|
||||
))
|
||||
.header("Accept", "application/json")
|
||||
.header("Content-Type", "application/json")
|
||||
.basic_auth(&cfg.user, Some(&cfg.pswd))
|
||||
.body(serde_json::to_string(&NoteUpdate {
|
||||
content: body_content.join("\n"),
|
||||
})?)
|
||||
.send()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Note {
|
||||
id: usize,
|
||||
etag: String,
|
||||
readonly: bool,
|
||||
modified: u64,
|
||||
title: String,
|
||||
category: String,
|
||||
content: String,
|
||||
favorite: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct NoteUpdate {
|
||||
content: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
struct Config {
|
||||
user: String,
|
||||
pswd: String,
|
||||
primary_note_id: String,
|
||||
logging_note_id: String,
|
||||
server_url: String,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about=None)]
|
||||
struct CliArgs {
|
||||
/// Path to config .toml file
|
||||
#[arg(short, long)]
|
||||
config_file: PathBuf,
|
||||
|
||||
#[arg(short, long)]
|
||||
debug: bool,
|
||||
}
|
Loading…
Reference in new issue