refactor some stuff

This commit is contained in:
Max Bradbury 2021-11-25 23:21:26 +00:00
parent f15d0e0f76
commit 0a4dbbba81
1 changed files with 55 additions and 41 deletions

View File

@ -1,3 +1,4 @@
use std::fs::DirEntry;
use std::io;
use std::path::PathBuf;
@ -40,52 +41,65 @@ fn move_file(file: PathBuf, mut destination: PathBuf) {
.expect("Couldn't move file");
}
fn main() {
let downloads = std::fs::read_dir(downloads_dir()).unwrap();
fn yes() -> bool {
let mut answer = String::new();
// todo break this out into a "handle_dir" function, and recursively call it for child dirs
for file in downloads {
let file = file.unwrap();
io::stdin().read_line(&mut answer)
.expect("Failed to read input");
match file.path().extension().unwrap_or("none".as_ref()).to_str() {
Some("gif") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpeg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("png") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("mp3") => {
let meta = mp3_metadata::read_from_file(file.path()).unwrap();
answer.to_lowercase().starts_with("y")
}
for tag in meta.tag {
println!("----------------------");
println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.trim());
println!("title: {}", tag.title.trim());
fn handle_mp3(file: DirEntry) {
let meta = mp3_metadata::read_from_file(file.path()).unwrap();
println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim());
for tag in meta.tag {
println!("----------------------");
println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.trim());
println!("title: {}", tag.title.trim());
let mut answer = String::new();
println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim());
io::stdin().read_line(&mut answer)
.expect("Failed to read input");
if answer.to_lowercase().starts_with("y") {
move_file(
file.path(),
create_dir_if_not_exists(
vec![
"Music",
strip_null_bytes(&tag.artist).trim(),
strip_null_bytes(&tag.album).trim()
]
)
);
} else {
println!("skipping...");
}
}
}
// todo m4a, flac, etc?
_ => { /*println!("Here's where we would do nothing.");*/ }
};
if yes() {
move_file(
file.path(),
create_dir_if_not_exists(
vec![
"Music",
strip_null_bytes(&tag.artist).trim(),
strip_null_bytes(&tag.album).trim()
]
)
);
} else {
println!("skipping...");
}
}
}
fn handle_dir(path: PathBuf) {
let dir = std::fs::read_dir(path).expect("Couldn't read dir");
for inode in dir {
let inode = inode.unwrap();
if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
}
match inode.path().extension().unwrap_or("none".as_ref()).to_str() {
Some("gif") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("jpeg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("png") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("mp3") => { handle_mp3(inode) }
// todo m4a, flac, etc?
_ => { /*println!("Here's where we would do nothing.");*/ }
}
}
}
fn main() {
handle_dir(downloads_dir());
}