download-organiser/src/main.rs

106 lines
2.8 KiB
Rust
Raw Normal View History

2021-11-25 23:21:26 +00:00
use std::fs::DirEntry;
2021-11-25 22:39:16 +00:00
use std::io;
use std::path::PathBuf;
fn home_dir() -> PathBuf {
return dirs::home_dir().unwrap();
}
fn downloads_dir() -> PathBuf {
let mut downloads = home_dir();
downloads.push("Downloads");
downloads
}
fn strip_null_bytes(str: &str) -> String {
str.replace('\0', "")
}
/// e.g. for ~/Music/ABBA/ABBA Gold:
/// `create_dir_if_not_exists(vec!["Music", "ABBA", "ABBA Gold"])`
fn create_dir_if_not_exists(path: Vec<&str>) -> PathBuf {
let mut dir = home_dir();
for item in path {
dir.push(item);
}
std::fs::create_dir_all(&dir).unwrap_or(());
dir
}
fn move_file(file: PathBuf, mut destination: PathBuf) {
println!("Moving to {:?}", destination);
destination.push(file.file_name().unwrap().to_str().unwrap());
fs_extra::file::move_file(file, destination, &Default::default())
.expect("Couldn't move file");
}
2021-11-25 23:21:26 +00:00
fn yes() -> bool {
let mut answer = String::new();
io::stdin().read_line(&mut answer)
.expect("Failed to read input");
answer.to_lowercase().starts_with("y")
}
fn handle_mp3(file: DirEntry) {
let meta = mp3_metadata::read_from_file(file.path()).unwrap();
for tag in meta.tag {
println!("----------------------");
println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.trim());
println!("title: {}", tag.title.trim());
println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim());
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();
2021-11-25 22:39:16 +00:00
2021-11-25 23:21:26 +00:00
if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
}
2021-11-25 22:39:16 +00:00
2021-11-25 23:21:26 +00:00
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");*/ }
2021-11-25 22:39:16 +00:00
Some("jpeg") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
2021-11-25 23:21:26 +00:00
Some("png") => { /*println!("Here's where we would move things to ~/Pictures");*/ }
Some("mp3") => { handle_mp3(inode) }
2021-11-25 22:39:16 +00:00
// todo m4a, flac, etc?
2021-11-25 23:21:26 +00:00
_ => { /*println!("Here's where we would do nothing.");*/ }
}
2021-11-25 22:39:16 +00:00
}
}
2021-11-25 23:21:26 +00:00
fn main() {
handle_dir(downloads_dir());
}