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 = [ dependencies = [
"dirs", "dirs",
"fs_extra", "fs_extra",
"lazy_static",
"mp3-metadata", "mp3-metadata",
"regex", "regex",
] ]
@ -70,6 +71,12 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.108" version = "0.2.108"

View File

@ -8,5 +8,6 @@ edition = "2021"
[dependencies] [dependencies]
dirs = "^4.0.0" dirs = "^4.0.0"
fs_extra = "^1.2.0" fs_extra = "^1.2.0"
lazy_static = "^1.4.0"
mp3-metadata = "^0.3.3" mp3-metadata = "^0.3.3"
regex = "^1.6.0" regex = "^1.6.0"

View File

@ -2,6 +2,7 @@ use std::fs::DirEntry;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
fn home_dir() -> PathBuf { fn home_dir() -> PathBuf {
@ -101,24 +102,42 @@ 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) { fn handle_video(file: DirEntry) {
let mut destination = vec!["Videos"]; let mut destination = vec!["Videos"];
// if filename contains something like "(1971)" or "[2003]" it's a movie if is_tv_episode(&file) {
// if filename contains something like "S03E21" it's a TV programme destination.push("TV");
} else if is_movie(&file) {
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()) {
// todo match decade? // todo match decade?
destination.push("Movies"); destination.push("Movies");
} else if tv.is_match(file.file_name().to_str().unwrap()) {
destination.push("TV");
} }
println!(
"Move to ~/{}/{}?", destination.join("/"), file.file_name().to_str().unwrap()
);
if yes() {
move_file(file.path(), create_dir_if_not_exists(destination)); move_file(file.path(), create_dir_if_not_exists(destination));
} }
}
fn handle_dir(path: PathBuf) { fn handle_dir(path: PathBuf) {
let dir = std::fs::read_dir(path).expect("Couldn't read dir"); let dir = std::fs::read_dir(path).expect("Couldn't read dir");