From 12ddfd2b8c0474940c03b2a420b7922f44e8f373 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 6 Nov 2022 23:06:25 +0000 Subject: [PATCH] handle tv/movies a bit better --- Cargo.lock | 33 +++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 27 +++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ef90a3..fe8c93c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e" +dependencies = [ + "memchr", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -41,6 +50,7 @@ dependencies = [ "dirs", "fs_extra", "mp3-metadata", + "regex", ] [[package]] @@ -66,6 +76,12 @@ version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + [[package]] name = "mp3-metadata" version = "0.3.3" @@ -91,6 +107,23 @@ dependencies = [ "redox_syscall", ] +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 320effb..5b2db49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" dirs = "^4.0.0" fs_extra = "^1.2.0" mp3-metadata = "^0.3.3" +regex = "^1.6.0" diff --git a/src/main.rs b/src/main.rs index 803e01d..8ebea53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use std::fs::DirEntry; use std::io; use std::path::PathBuf; +use regex::Regex; + fn home_dir() -> PathBuf { dirs::home_dir().expect("Couldn't find home dir") } @@ -100,9 +102,22 @@ fn handle_mp3(file: DirEntry) { } fn handle_video(file: DirEntry) { - // todo if filename contains something like "(1971)" or "[2003]" it's a movie - // todo if filename contains something like "S03E21" it's a TV programme - move_file(file.path(), create_dir_if_not_exists(vec!["Videos"])); + 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()) { + // 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)); } fn handle_dir(path: PathBuf) { @@ -123,8 +138,12 @@ fn handle_dir(path: PathBuf) { "jpeg" => { handle_image(inode) } "png" => { handle_image(inode) } "mp3" => { handle_mp3( inode) } - "mp4" => { handle_video( inode) } + "avi" => { handle_video(inode) } + "m4v" => { handle_video(inode) } + "mkv" => { handle_video(inode) } + "mp4" => { handle_video(inode) } // todo m4a, flac, etc? + // todo pdf, other documents _ => { /*println!("Here's where we would do nothing.");*/ } } }