From bb26036d1919d814cc9002c186e32eea4da1a7d0 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 6 Nov 2022 23:21:28 +0000 Subject: [PATCH] static regular expressions; if filename contains both episode number and year, favour episode over movie; print out some help text about videos --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe8c93c..f36304a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,6 +49,7 @@ version = "0.1.0" dependencies = [ "dirs", "fs_extra", + "lazy_static", "mp3-metadata", "regex", ] @@ -70,6 +71,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.108" diff --git a/Cargo.toml b/Cargo.toml index 5b2db49..e262849 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] dirs = "^4.0.0" fs_extra = "^1.2.0" +lazy_static = "^1.4.0" mp3-metadata = "^0.3.3" regex = "^1.6.0" diff --git a/src/main.rs b/src/main.rs index 8ebea53..332cc05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::fs::DirEntry; use std::io; use std::path::PathBuf; +use lazy_static::lazy_static; use regex::Regex; fn home_dir() -> PathBuf { @@ -101,23 +102,41 @@ fn handle_mp3(file: DirEntry) { } } +/// if filename contains something like `(1971)` or `[2003]` it's a movie +fn is_movie(file: &DirEntry) -> bool { + lazy_static! { + static ref MOVIE: Regex = Regex::new(r"\(\d{4}\)|\[\d{4}]").unwrap(); + } + + MOVIE.is_match(file.file_name().to_str().unwrap()) +} + +/// if filename contains something like "S03E21" it's a TV programme +fn is_tv_episode(file: &DirEntry) -> bool { + lazy_static! { + static ref TV: Regex = Regex::new(r"s\d{2}e\d{2}").unwrap(); + } + + TV.is_match(file.file_name().to_str().unwrap()) +} + fn handle_video(file: DirEntry) { let mut destination = vec!["Videos"]; - // if filename contains something like "(1971)" or "[2003]" it's a movie - // if filename contains something like "S03E21" it's a TV programme - - let movie = Regex::new(r"\(\d{4}\)|\[\d{4}]").unwrap(); - let tv = Regex::new(r"s\d{2}e\d{2}").unwrap(); - - if movie.is_match(file.file_name().to_str().unwrap()) { + if is_tv_episode(&file) { + destination.push("TV"); + } else if is_movie(&file) { // todo match decade? destination.push("Movies"); - } else if tv.is_match(file.file_name().to_str().unwrap()) { - destination.push("TV"); } - move_file(file.path(), create_dir_if_not_exists(destination)); + println!( + "Move to ~/{}/{}?", destination.join("/"), file.file_name().to_str().unwrap() + ); + + if yes() { + move_file(file.path(), create_dir_if_not_exists(destination)); + } } fn handle_dir(path: PathBuf) {