1
0
Fork 0

Refactor habitask

This commit is contained in:
Malte Brandy 2018-07-28 13:04:24 +02:00
parent 11f62453c8
commit 1b28767805
No known key found for this signature in database
GPG key ID: 226A2D41EF5378C9
2 changed files with 36 additions and 33 deletions

View file

@ -0,0 +1,5 @@
with import <unstable> {};
stdenv.mkDerivation {
name = "habitask";
buildInputs = [ pkgs.openssl pkgs.pkgconfig ];
}

View file

@ -12,6 +12,7 @@ extern crate config;
use reqwest::{IntoUrl, Client, RequestBuilder}; use reqwest::{IntoUrl, Client, RequestBuilder};
use task_hookrs::task::Task; use task_hookrs::task::Task;
use serde_json::from_str; use serde_json::from_str;
use std::str::from_utf8;
use std::process::Command; use std::process::Command;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
@ -33,15 +34,14 @@ fn blink() {
} }
fn query(filter: String) -> Vec<task_hookrs::task::Task> { fn query(filter: String) -> Vec<Task> {
let stdout = &Command::new("task") let stdout = &Command::new("task")
.arg("export") .arg("export")
.arg(filter) .arg(filter)
.output() .output()
.unwrap() .unwrap()
.stdout; .stdout;
let string = std::str::from_utf8(stdout).unwrap(); from_str(from_utf8(stdout).unwrap()).unwrap()
from_str::<Vec<Task>>(string).unwrap()
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -66,8 +66,8 @@ struct Response {
impl Habitask { impl Habitask {
fn new() -> Habitask { fn new() -> Habitask {
let mut s = Config::new(); let mut s = Config::new();
s.merge(Environment::with_prefix("habitask")); s.merge(Environment::with_prefix("habitask")).unwrap();
let s: Settings = s.try_into().unwrap(); let s = s.try_into().unwrap();
Habitask { Habitask {
client: Client::new(), client: Client::new(),
settings: s, settings: s,
@ -104,58 +104,55 @@ impl Habitask {
} }
fn create_todo(&self, name: &str, prio: &str) -> Todo { fn create_todo(&self, name: &str, prio: &str) -> Todo {
let mut map = std::collections::HashMap::new(); let map = vec![("text", name), ("type", "todo"), ("priority", prio)];
map.insert("text", name); self.post("https://habitica.com/api/v3/tasks/user")
map.insert("type", "todo");
map.insert("priority", prio);
let mut res = self.post("https://habitica.com/api/v3/tasks/user")
.json(&map) .json(&map)
.send() .send()
.unwrap(); .unwrap()
let Response { data } = res.json().unwrap(); .json::<Response>()
data .unwrap()
.data
} }
fn add_item(&self, id: &str, item: &str) -> Todo { fn add_item(&self, id: &str, item: &str) -> Todo {
let mut map = std::collections::HashMap::new(); let map = vec![("text", item)];
map.insert("text", item); let url = format!("https://habitica.com/api/v3/tasks/{}/checklist", id);
let mut res = self.post( self.post(&url)
&("https://habitica.com/api/v3/tasks/".to_owned() + id + "/checklist"), .json(&map)
).json(&map)
.send() .send()
.unwrap(); .unwrap()
let Response { data } = res.json().unwrap(); .json::<Response>()
data .unwrap()
.data
} }
fn check_item(&self, id: &str, item: &str) { fn check_item(&self, id: &str, item: &str) {
let map: std::collections::HashMap<String, String> = std::collections::HashMap::new(); let url = format!(
let res = self.post( "https://habitica.com/api/v3/tasks/{}/checklist/{}/score",
&("https://habitica.com/api/v3/tasks/".to_owned() + id + "/checklist/" + id,
item + "/score"), item
).json(&map) );
self.post(&url)
.json(&Vec::<(&str, &str)>::new())
.send() .send()
.unwrap(); .unwrap();
} }
fn score_task(&self, id: &str) { fn score_task(&self, id: &str) {
let client = Client::new(); let url = format!("https://habitica.com/api/v3/tasks/{}/score/up", id);
let map: std::collections::HashMap<String, String> = std::collections::HashMap::new(); self.post(&url)
let res = self.post( .json(&Vec::<(&str, &str)>::new())
&("https://habitica.com/api/v3/tasks/".to_owned() + id + "/score/up"),
).json(&map)
.send() .send()
.unwrap(); .unwrap();
} }
} }
fn main() { fn main() {
let habitask = Habitask::new();
let new = "-DELETED entry.after:now-24h"; let new = "-DELETED entry.after:now-24h";
let done = "+COMPLETED end.after:now-24h"; let done = "+COMPLETED end.after:now-24h";
let after = "entry.after:now-"; let after = "entry.after:now-";
let before = "entry.before:now-"; let before = "entry.before:now-";
let mask = "-auto"; let mask = "-auto";
let habitask = Habitask::new();
let instant_done = query(format!("{} -TAGGED {}48h {}", done, after, mask)); let instant_done = query(format!("{} -TAGGED {}48h {}", done, after, mask));
let created = query(format!("{} {}", new, mask)); let created = query(format!("{} {}", new, mask));
let routines = query(format!("{} +auto", done)); let routines = query(format!("{} +auto", done));
@ -170,6 +167,7 @@ fn main() {
)); ));
let very_old = query(format!("{} {}1year {}3month {}", done, after, before, mask)); let very_old = query(format!("{} {}1year {}3month {}", done, after, before, mask));
let crazy_old = query(format!("{} {}1year {}", done, before, mask)); let crazy_old = query(format!("{} {}1year {}", done, before, mask));
let habitask = Habitask::new();
for _ in created { for _ in created {
habitask.score_task("note"); habitask.score_task("note");
blink(); blink();