ffxiv-screenshot-organiser/src/main.rs

211 lines
5.5 KiB
Rust
Raw Normal View History

#[macro_use] extern crate crossbeam_channel;
2018-08-13 23:01:36 +00:00
#[macro_use] extern crate serde_derive;
mod config;
mod state;
use crate::{
config::Config,
state::State,
};
use crossbeam_channel::Receiver;
2018-08-13 23:01:36 +00:00
use chrono::{DateTime, Duration, Local, TimeZone, Utc};
use notify::{DebouncedEvent, RecursiveMode, Watcher};
use rayon::prelude::*;
use tempdir::TempDir;
use std::{
fs::{self, DirEntry},
path::PathBuf,
sync::{
Arc,
mpsc::{self, TryRecvError},
},
};
2018-08-13 23:01:36 +00:00
2021-02-17 21:50:27 +00:00
pub type Result<T, E = anyhow::Error> = std::result::Result<T, E>;
2018-08-13 23:01:36 +00:00
fn main() -> Result<()> {
2018-08-14 00:04:16 +00:00
println!("Starting FFXIV Screenshot Organiser.");
2018-08-14 23:40:31 +00:00
let ctrlc_rx = set_ctrlc_handler(num_cpus::get() + 1)?;
2018-08-14 00:04:16 +00:00
2018-08-13 23:01:36 +00:00
let config_path = match std::env::args().nth(1) {
Some(x) => x,
2021-02-17 20:45:36 +00:00
None => "config.yaml".into(),
2018-08-13 23:01:36 +00:00
};
2018-08-14 00:04:16 +00:00
println!("Attempting to read config from `{}`.", config_path);
2018-08-13 23:01:36 +00:00
let f = fs::File::open(config_path)?;
2021-02-17 20:45:36 +00:00
let config: Config = serde_yaml::from_reader(f)?;
2018-08-13 23:01:36 +00:00
2018-08-14 00:04:16 +00:00
println!("Config successfully read.");
2018-08-13 23:01:36 +00:00
let screenshots_dir = config.options.screenshots_dir.canonicalize()?;
2018-08-14 00:04:16 +00:00
println!("Screenshots are located at `{}`.", screenshots_dir.to_string_lossy());
2018-08-14 23:40:31 +00:00
let mut handles = Vec::with_capacity(num_cpus::get() + 1);
2018-08-13 23:01:36 +00:00
let (tx, rx) = mpsc::channel();
let (event_tx, event_rx) = crossbeam_channel::unbounded();
2018-08-13 23:01:36 +00:00
let temp_dir = TempDir::new("fso-")?;
let temp_path = temp_dir.path().to_owned();
2018-08-14 00:04:16 +00:00
println!("Storing temporary files in `{}`.", temp_path.to_string_lossy());
2018-08-13 23:01:36 +00:00
let mut watcher = notify::watcher(
tx,
Duration::milliseconds(config.options.event_delay).to_std().unwrap(),
)?;
watcher.watch(
screenshots_dir.to_string_lossy().to_string(),
2018-12-23 15:43:21 +00:00
RecursiveMode::NonRecursive,
2018-08-13 23:01:36 +00:00
)?;
{
let thread_ctrlc_rx = ctrlc_rx.clone();
handles.push(std::thread::spawn(move || {
let tick = crossbeam_channel::tick(Duration::milliseconds(50).to_std().unwrap());
loop {
select! {
2018-12-23 15:43:21 +00:00
recv(tick) -> _ => match rx.try_recv() {
Ok(e) => { event_tx.send(e).ok(); },
Err(TryRecvError::Empty) => {},
Err(TryRecvError::Disconnected) => break,
},
2018-12-23 15:43:21 +00:00
recv(thread_ctrlc_rx) -> _ => break,
}
}
}));
}
2018-08-14 00:04:16 +00:00
println!("Waiting for new files on {} thread(s).", num_cpus::get());
let config = Arc::new(config);
let mut handles = Vec::with_capacity(num_cpus::get());
for i in 0..num_cpus::get() {
let thread_ctrlc_rx = ctrlc_rx.clone();
let thread_event_rx = event_rx.clone();
let temp_path = temp_path.clone();
let thread_config = Arc::clone(&config);
let handle = std::thread::spawn(move || {
loop {
select! {
2018-12-23 15:43:21 +00:00
recv(thread_ctrlc_rx) -> _ => break,
recv(thread_event_rx) -> event => {
match event {
Ok(DebouncedEvent::Create(p)) => if let Err(e) = handle(&thread_config, Some(i), temp_path.clone(), p) {
eprintln!("{}", e);
},
2018-12-23 15:43:21 +00:00
Ok(_) => {},
Err(e) => eprintln!("{:#?}", e),
}
2018-08-14 00:04:16 +00:00
},
}
}
println!("Thread {} shutting down.", i);
});
handles.push(handle);
}
println!("Collecting existing files.");
let mut existing_files: Vec<DirEntry> = std::fs::read_dir(&screenshots_dir)?.collect::<std::result::Result<_, _>>()?;
existing_files.retain(|x| x.path().is_file());
println!("Processing {} existing file(s).", existing_files.len());
existing_files.into_par_iter().for_each(|entry| {
if let Err(e) = handle(&config, None, temp_path.clone(), entry.path()) {
eprintln!("{}", e);
}
});
println!("Done!");
for handle in handles {
handle.join().ok();
2018-08-13 23:01:36 +00:00
}
2018-08-14 00:04:16 +00:00
println!("Exiting.");
Ok(())
2018-08-13 23:01:36 +00:00
}
fn handle(config: &Config, thread: Option<usize>, temp_dir: PathBuf, p: PathBuf) -> Result<()> {
2018-08-13 23:01:36 +00:00
let screenshots_dir = config.options.screenshots_dir.canonicalize()?;
// if the path doesn't exist, ignore
// no need to print out an error usually a result of duplicate events
if !p.exists() {
return Ok(());
}
// if the path isn't at the top level, ignore
if p.strip_prefix(&screenshots_dir).unwrap().components().count() != 1 {
return Ok(());
}
// ignore paths without file names
let file_name: String = match p.file_name() {
Some(f) => f.to_string_lossy().to_string(),
None => return Ok(()),
};
// ignore paths that don't match
let time = match parse_screenshot_name(&config, &file_name) {
Some(t) => t,
None => return Ok(()),
};
match thread {
Some(i) => println!("Handling `{}` on thread {}.", file_name, i),
None => println!("Handling `{}`.", file_name),
}
2018-08-14 00:13:31 +00:00
2018-08-13 23:01:36 +00:00
// execute the jobs in the pipeline
let mut state = State::new(p, time, temp_dir);
2018-08-13 23:01:36 +00:00
for job in &config.pipeline {
job.execute(&config, &mut state)?;
}
Ok(())
}
fn parse_screenshot_name(config: &Config, s: &str) -> Option<DateTime<Utc>> {
let caps = config.options.patterns.iter().find_map(|p| p.captures(s))?;
let dt = Local
.ymd(
caps.name("year")?.as_str().parse().ok()?,
caps.name("month")?.as_str().parse().ok()?,
caps.name("day")?.as_str().parse().ok()?,
)
.and_hms(
caps.name("hour")?.as_str().parse().ok()?,
caps.name("minute")?.as_str().parse().ok()?,
caps.name("second")?.as_str().parse().ok()?,
);
Some(dt.with_timezone(&Utc))
}
2018-08-14 00:04:16 +00:00
2018-08-14 23:40:31 +00:00
fn set_ctrlc_handler(num_threads: usize) -> Result<Receiver<()>> {
let (tx, rx) = crossbeam_channel::unbounded();
2018-08-14 00:04:16 +00:00
ctrlc::set_handler(move || {
println!("Received interrupt.");
2018-08-14 23:40:31 +00:00
for _ in 0..num_threads {
2018-12-23 15:43:21 +00:00
tx.send(()).ok();
}
2018-08-14 00:04:16 +00:00
})?;
Ok(rx)
}