1
0
mirror of https://github.com/shimunn/gitredditor.git synced 2023-11-17 18:42:43 +01:00

implemented thresholds

This commit is contained in:
shimunn 2019-05-08 12:00:44 +02:00
parent 5ead8f310d
commit a46aaff8b0
6 changed files with 49 additions and 14 deletions

7
Cargo.lock generated
View File

@ -246,6 +246,11 @@ name = "dtoa"
version = "0.4.4" version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.17" version = "0.8.17"
@ -361,6 +366,7 @@ dependencies = [
name = "gitredditor" name = "gitredditor"
version = "0.1.0" version = "0.1.0"
dependencies = [ 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)", "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)", "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)", "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 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 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 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 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 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" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"

View File

@ -11,3 +11,4 @@ structopt = "0.2.15"
serde = "1.0.90" serde = "1.0.90"
serde_derive = "1.0.90" serde_derive = "1.0.90"
git2 = "0.8.0" git2 = "0.8.0"
either = "1.5.2"

View File

@ -4,15 +4,19 @@ After=network-online.target
[Service] [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 Environment=ROOT_DIR=/srv/reddit
RestartSec=7200 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 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"

View File

@ -9,11 +9,11 @@ use std::fmt;
use std::fs::{create_dir_all, read_to_string, write as fs_write}; use std::fs::{create_dir_all, read_to_string, write as fs_write};
use std::iter; use std::iter;
use std::iter::Chain; use std::iter::Chain;
use std::iter::IntoIterator;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use std::time::UNIX_EPOCH; use std::time::UNIX_EPOCH;
use std::iter::IntoIterator;
mod model; mod model;
mod opts; mod opts;
@ -24,24 +24,29 @@ use crate::opts::*;
fn main() { fn main() {
let opts = Opts::from_args(); let opts = Opts::from_args();
let comments = Comments::for_user(&opts.redditor); let comments = Comments::for_user(&opts.redditor);
let all = comments.take(opts.fetch).filter_map(|c| c.ok()).collect::<Vec<_>>(); let all = comments
.take(opts.fetch)
.filter_map(|c| c.ok())
.collect::<Vec<_>>();
println!( println!(
"Hello, world! {:?}", "Hello, world! {:?}",
update( update(
&opts.repo.unwrap(), &opts.repo.unwrap(),
&all, &comments.take(opts.fetch).filter_map(|c| c.ok()),
&opts.redditor, &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, repo: &PathBuf,
current: &Vec<Comment>, current: impl IntoIterator<Item = &'a Comment>,
redditor: &str, redditor: &str,
email: &str, email: &str,
threshold: (u32, u8),
) -> Result<usize, Box<Error>> { ) -> Result<usize, Box<Error>> {
let comment_path = |c: &Comment| { let comment_path = |c: &Comment| {
let mut p = repo.clone(); let mut p = repo.clone();
@ -55,6 +60,8 @@ fn update(
let git = Repository::open(&repo)?; let git = Repository::open(&repo)?;
let sig = Signature::now(redditor, email)?; let sig = Signature::now(redditor, email)?;
let mut index = git.index()?; let mut index = git.index()?;
let (threshold, threshold_percent) = threshold;
let threshold_percent = threshold_percent as f32;
for comment in current.into_iter() { for comment in current.into_iter() {
let path = comment_path(&comment); let path = comment_path(&comment);
let path_rel = || { let path_rel = || {
@ -67,7 +74,16 @@ fn update(
if (&path).exists() { if (&path).exists() {
let content = read_to_string(&path)?; let content = read_to_string(&path)?;
let old: Comment = serde_json::from_str(&content)?; 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::<Vec<_>>();
if delta.len() > 0 { if delta.len() > 0 {
fs_write(&path, to_string_pretty(&comment)?)?; fs_write(&path, to_string_pretty(&comment)?)?;
commit_msg = delta.iter().map(|d| d.to_string()).collect::<Vec<_>>()[..].join("\n"); commit_msg = delta.iter().map(|d| d.to_string()).collect::<Vec<_>>()[..].join("\n");

View File

@ -1,3 +1,4 @@
use either::Either;
use serde::de; use serde::de;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value; use serde_json::Value;
@ -76,7 +77,7 @@ pub struct ListItem<T: for<'a> Deserialize<'a>> {
pub struct Comments { pub struct Comments {
pub url: String, pub url: String,
continuation: Option<String>, continuation: Option<String>,
buffer: Option<Box<Iterator<Item = Result<Comment, Box<Error>>>>>, buffer: Option<Result<Box<Iterator<Item=Result<Comment, Box<Error>>>>, Box<Error>>>,
} }
impl Comments { impl Comments {

View File

@ -4,10 +4,16 @@ pub use structopt::StructOpt;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
#[structopt(name = "gitredditor")] #[structopt(name = "gitredditor")]
pub struct Opts { 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, 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, pub redditor: String,
#[structopt(parse(from_os_str))] #[structopt(parse(from_os_str))]