Compare commits

..

2 Commits

Author SHA1 Message Date
Max Bradbury 12ddfd2b8c handle tv/movies a bit better 2022-11-06 23:06:25 +00:00
Max Bradbury 6e9e8df3bf handle videos 2022-11-06 22:51:47 +00:00
3 changed files with 60 additions and 0 deletions

33
Cargo.lock generated
View File

@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -41,6 +50,7 @@ dependencies = [
"dirs", "dirs",
"fs_extra", "fs_extra",
"mp3-metadata", "mp3-metadata",
"regex",
] ]
[[package]] [[package]]
@ -66,6 +76,12 @@ version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]] [[package]]
name = "mp3-metadata" name = "mp3-metadata"
version = "0.3.3" version = "0.3.3"
@ -91,6 +107,23 @@ dependencies = [
"redox_syscall", "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]] [[package]]
name = "wasi" name = "wasi"
version = "0.10.2+wasi-snapshot-preview1" version = "0.10.2+wasi-snapshot-preview1"

View File

@ -9,3 +9,4 @@ edition = "2021"
dirs = "^4.0.0" dirs = "^4.0.0"
fs_extra = "^1.2.0" fs_extra = "^1.2.0"
mp3-metadata = "^0.3.3" mp3-metadata = "^0.3.3"
regex = "^1.6.0"

View File

@ -2,6 +2,8 @@ use std::fs::DirEntry;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use regex::Regex;
fn home_dir() -> PathBuf { fn home_dir() -> PathBuf {
dirs::home_dir().expect("Couldn't find home dir") dirs::home_dir().expect("Couldn't find home dir")
} }
@ -99,6 +101,25 @@ fn handle_mp3(file: DirEntry) {
} }
} }
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()) {
// 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) { 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");
@ -117,7 +138,12 @@ fn handle_dir(path: PathBuf) {
"jpeg" => { handle_image(inode) } "jpeg" => { handle_image(inode) }
"png" => { handle_image(inode) } "png" => { handle_image(inode) }
"mp3" => { handle_mp3( inode) } "mp3" => { handle_mp3( inode) }
"avi" => { handle_video(inode) }
"m4v" => { handle_video(inode) }
"mkv" => { handle_video(inode) }
"mp4" => { handle_video(inode) }
// todo m4a, flac, etc? // todo m4a, flac, etc?
// todo pdf, other documents
_ => { /*println!("Here's where we would do nothing.");*/ } _ => { /*println!("Here's where we would do nothing.");*/ }
} }
} }