Skip to content

Commit

Permalink
GOLFING.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceejbot committed Apr 26, 2020
1 parent 72b1dfc commit b80e055
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
Empty file removed PRUNES
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Config vars:

If you gave it a toast channel, you can now see some toasts.

`PRUNE` is an administrative convenience for making LOUDBOT bulk-forget shouts. Put the items you'd like to purge as new-line delimited text in the file `PRUNES`, then run `PRUNE`. I need something better here myself so I'll write it soon. I also need a good backup method better than redis dumps.
`PRUNE` is an administrative convenience for making LOUDBOT bulk-forget shouts. Put the items you'd like to purge as new-line delimited text in some file, then run `PRUNE /path/to/file`. I need something better here myself so I'll write it soon. I also need a good backup method better than redis dumps.

## License

Expand Down
4 changes: 3 additions & 1 deletion src/bin/PRUNE.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ fn main() -> Result<()> {

let yellkey = format!("{}:YELLS", redis_prefix);

prune_from_file(&mut rcon, "PRUNES", &yellkey)
for f in std::env::args().skip(1) {
prune_from_file(&mut rcon, &f, &yellkey)
.with_context(|| "Trying to write to redis failed utterly.")?;
}

Ok(())
}
25 changes: 13 additions & 12 deletions src/bin/SEED.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
use anyhow::{Context, Result};
use dotenv::dotenv;
use redis::Commands;
use regex::Regex;
use std::env;
use std::fs::File;
use std::io::{ BufRead, BufReader };
use std::path::Path;

type RCount = std::result::Result<u32, redis::RedisError>;

// Message store + other data
fn seed_from_file(db: &mut redis::Connection, filename: impl AsRef<Path> + std::fmt::Debug + Copy, key: &str, skip_loud_check: bool) -> Result<u32, redis::RedisError> {
let fp = File::open(filename);
if fp.is_err() {
println!("Skipping {:?}; could not open file for reading.", filename);
return Ok(0)
}

let punc = Regex::new(r"[\W\d[[:punct:]]]").unwrap();
let mut count: u32 = 0;
let file = File::open(filename).expect("no such file");
let mut pipe = redis::pipe();
let file = fp.unwrap();
let reader = BufReader::new(file);

for line in reader.lines() {
let text = line.unwrap();

let result = punc.replace_all(&text, "");
if !skip_loud_check && (result.len() < 3 || result.to_uppercase() != result) {
continue;
}

let res: RCount = db.sadd(key, text);
match res {
Err(e) => println!("{:?}", e),
Ok(i) => { count += i; },
}
pipe.sadd(key, text);
}

let result: Vec<u32> = pipe.query(db)?;
let count = result.iter().sum();
println!("Added {} items from {:#?}", count, filename);
Ok(count)
}
Expand Down

0 comments on commit b80e055

Please sign in to comment.