diff --git a/Cargo.lock b/Cargo.lock index 3db39b6..c042cba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,6 +246,11 @@ name = "dtoa" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "either" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "encoding_rs" version = "0.8.17" @@ -361,6 +366,7 @@ dependencies = [ name = "gitredditor" version = "0.1.0" dependencies = [ + "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.16 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1516,6 +1522,7 @@ dependencies = [ "checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" "checksum curl-sys 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)" = "9d91a0052d5b982887d8e829bee0faffc7218ea3c6ebd3d6c2c8f678a93c9a42" "checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e" +"checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" "checksum encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)" = "4155785c79f2f6701f185eb2e6b4caf0555ec03477cb4c70db67b465311620ed" "checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" diff --git a/Cargo.toml b/Cargo.toml index d497e1e..b209db0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ structopt = "0.2.15" serde = "1.0.90" serde_derive = "1.0.90" git2 = "0.8.0" +either = "1.5.2" diff --git a/gitedditor.service b/gitedditor.service index f248895..6f2552d 100644 --- a/gitedditor.service +++ b/gitedditor.service @@ -4,15 +4,19 @@ After=network-online.target [Service] -Environment=REDDIT_USERNAME=root +#Default Environment=GITREDDITOR_CNT=20 +#Default Environment=GITREDDITOR_TH=5 +#Default Environment=GITREDDITOR_THP=5 +Environment=GITREDDITOR_U=root Environment=ROOT_DIR=/srv/reddit RestartSec=7200 +Restart=always -ExecStart=/usr/bin/rkt --insecure-options=image run --dns 8.8.8.8 --volume volume-repo,kind=host,source=${ROOT_DIR}/${REDDIT_USERNAME} \ +ExecStart=/usr/bin/rkt --insecure-options=image run --dns 8.8.8.8 --volume volume-repo,kind=host,source=${ROOT_DIR}/${GITREDDITOR_U} \ docker://repo.shimun.net/shimun/gitredditor -ExecStopPost=/bin/bash -c "cd ${ROOT_DIR}/${REDDIT_USERNAME} && /usr/bin/git push" +ExecStopPost=/bin/bash -c "cd ${ROOT_DIR}/${GITREDDITOR_U} && /usr/bin/git push" diff --git a/src/main.rs b/src/main.rs index 5b17a9c..1f51afa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,11 @@ use std::fmt; use std::fs::{create_dir_all, read_to_string, write as fs_write}; use std::iter; use std::iter::Chain; +use std::iter::IntoIterator; use std::path::{Path, PathBuf}; use std::thread; use std::time::Duration; use std::time::UNIX_EPOCH; -use std::iter::IntoIterator; mod model; mod opts; @@ -24,24 +24,29 @@ use crate::opts::*; fn main() { let opts = Opts::from_args(); let comments = Comments::for_user(&opts.redditor); - let all = comments.take(opts.fetch).filter_map(|c| c.ok()).collect::>(); - + let all = comments + .take(opts.fetch) + .filter_map(|c| c.ok()) + .collect::>(); + println!( "Hello, world! {:?}", update( &opts.repo.unwrap(), - &all, + &comments.take(opts.fetch).filter_map(|c| c.ok()), &opts.redditor, - &("reddit.com/u/".to_owned() + &opts.redditor) + &("reddit.com/u/".to_owned() + &opts.redditor), + (opts.threshold, opts.thresholdp) ) ); } -fn update( +fn update<'a>( repo: &PathBuf, - current: &Vec, + current: impl IntoIterator, redditor: &str, email: &str, + threshold: (u32, u8), ) -> Result> { let comment_path = |c: &Comment| { let mut p = repo.clone(); @@ -55,6 +60,8 @@ fn update( let git = Repository::open(&repo)?; let sig = Signature::now(redditor, email)?; let mut index = git.index()?; + let (threshold, threshold_percent) = threshold; + let threshold_percent = threshold_percent as f32; for comment in current.into_iter() { let path = comment_path(&comment); let path_rel = || { @@ -67,7 +74,16 @@ fn update( if (&path).exists() { let content = read_to_string(&path)?; let old: Comment = serde_json::from_str(&content)?; - let delta = CommentDelta::from(&old, &comment); + let delta = CommentDelta::from(&old, &comment) + .into_iter() + .filter(|d| match d { + CommentDelta::Votes(change) => { + change.abs() as u32 > threshold + && change.abs() as f32 > old.score as f32 * (threshold_percent / 100.0) + } + _ => true, + }) + .collect::>(); if delta.len() > 0 { fs_write(&path, to_string_pretty(&comment)?)?; commit_msg = delta.iter().map(|d| d.to_string()).collect::>()[..].join("\n"); diff --git a/src/model.rs b/src/model.rs index 639970a..7897375 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,3 +1,4 @@ +use either::Either; use serde::de; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; @@ -76,7 +77,7 @@ pub struct ListItem Deserialize<'a>> { pub struct Comments { pub url: String, continuation: Option, - buffer: Option>>>>, + buffer: Option>>>, Box>>, } impl Comments { diff --git a/src/opts.rs b/src/opts.rs index 75b8569..a701676 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -4,10 +4,16 @@ pub use structopt::StructOpt; #[derive(StructOpt, Debug)] #[structopt(name = "gitredditor")] pub struct Opts { - #[structopt(short = "f", long = "fetch", default_value = "20")] + #[structopt(short = "f", long = "fetch", default_value = "20", env = "GITREDDITOR_CNT")] pub fetch: usize, - #[structopt(short = "r", long = "redditor")] + #[structopt(short = "t", long = "threshold", default_value = "5", env = "GITREDDITOR_TH")] + pub threshold: u32, + + #[structopt(short = "p", long = "threshold-percent", default_value = "5", env = "GITREDDITOR_THP")] + pub thresholdp: u8, + + #[structopt(short = "r", long = "redditor", env = "GITREDDITOR_U")] pub redditor: String, #[structopt(parse(from_os_str))]