static regular expressions; if filename contains both episode number and year, favour episode over movie; print out some help text about videos

This commit is contained in:
Max Bradbury 2022-11-06 23:21:28 +00:00
parent 12ddfd2b8c
commit bb26036d19
3 changed files with 37 additions and 10 deletions

7
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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) {