Compare commits

..

7 Commits

Author SHA1 Message Date
f44272de79 allow editing artist name 2023-08-21 09:07:07 +01:00
8b0efc287a ignore incomplete downloads; handle spreadsheets 2023-05-11 18:51:42 +01:00
0be1ec6fca handle bin/cue, ignore archives and torrents 2023-05-11 18:45:48 +01:00
8c0d9a7236 handle subtitles 2023-05-04 19:39:22 +01:00
c311f3c2cb handle some documents 2023-05-04 19:35:19 +01:00
e6b9b927f4 handle ISOs 2023-05-04 19:04:18 +01:00
7c26c38ccb delete empty dirs 2023-05-04 18:54:26 +01:00
3 changed files with 145 additions and 52 deletions

85
Cargo.lock generated
View File

@@ -25,27 +25,28 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "dirs" name = "dirs"
version = "4.0.0" version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [ dependencies = [
"dirs-sys", "dirs-sys",
] ]
[[package]] [[package]]
name = "dirs-sys" name = "dirs-sys"
version = "0.3.6" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [ dependencies = [
"libc", "libc",
"option-ext",
"redox_users", "redox_users",
"winapi", "windows-sys",
] ]
[[package]] [[package]]
name = "download-organiser" name = "download-organiser"
version = "0.1.0" version = "0.1.2"
dependencies = [ dependencies = [
"dirs", "dirs",
"fs_extra", "fs_extra",
@@ -95,6 +96,12 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb969bc3573726b0bf60238d5d70f1aa6cc0f9e87f8db3e047b8309317319699" checksum = "eb969bc3573726b0bf60238d5d70f1aa6cc0f9e87f8db3e047b8309317319699"
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.2.10" version = "0.2.10"
@@ -138,23 +145,67 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]] [[package]]
name = "winapi" name = "windows-sys"
version = "0.3.9" version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [ dependencies = [
"winapi-i686-pc-windows-gnu", "windows-targets",
"winapi-x86_64-pc-windows-gnu",
] ]
[[package]] [[package]]
name = "winapi-i686-pc-windows-gnu" name = "windows-targets"
version = "0.4.0" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "windows_aarch64_gnullvm"
version = "0.4.0" version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View File

@@ -1,12 +1,12 @@
[package] [package]
name = "download-organiser" name = "download-organiser"
version = "0.1.1" version = "0.1.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
dirs = "^4.0.0" dirs = "^5.0.1"
fs_extra = "^1.2.0" fs_extra = "^1.2.0"
lazy_static = "^1.4.0" lazy_static = "^1.4.0"
mp3-metadata = "^0.3.3" mp3-metadata = "^0.3.3"

View File

@@ -1,5 +1,5 @@
use std::fs::DirEntry; use std::fs::DirEntry;
use std::io; use std::{fs, io};
use std::path::PathBuf; use std::path::PathBuf;
use lazy_static::lazy_static; use lazy_static::lazy_static;
@@ -52,19 +52,36 @@ fn move_file(file: PathBuf, mut destination: PathBuf) {
} }
} }
fn yes() -> bool { fn user_input() -> String {
let mut answer = String::new(); let mut answer = String::new();
io::stdin().read_line(&mut answer) io::stdin().read_line(&mut answer)
.expect("Failed to read input"); .expect("Failed to read input");
answer.to_lowercase().starts_with("y") answer
}
// todo: add other input options e.g. "d" for delete, "a" for "yes to this whole album"
fn yes() -> bool {
user_input().to_lowercase().starts_with("y")
}
fn handle_document(file: DirEntry) {
move_file(file.path(), documents_dir());
} }
fn handle_image(file: DirEntry) { fn handle_image(file: DirEntry) {
move_file(file.path(), pictures_dir()); move_file(file.path(), pictures_dir());
} }
fn handle_iso(file: DirEntry) {
let mut dir = home_dir();
dir.push("ISOs");
move_file(file.path(), dir);
}
fn handle_gif(file: DirEntry) { fn handle_gif(file: DirEntry) {
let mut dir = pictures_dir(); let mut dir = pictures_dir();
@@ -79,15 +96,24 @@ fn handle_mp3(file: DirEntry) {
if meta.is_err() { if meta.is_err() {
println!("Couldn't read ID3 metadata for file {:?}", file.path()); println!("Couldn't read ID3 metadata for file {:?}", file.path());
} } else if let Some(tag) = meta.unwrap().tag {
let mut artist = tag.artist.trim().to_string();
let album = tag.album.trim();
let title = tag.title.trim();
if let Some(tag) = meta.unwrap().tag {
println!("----------------------"); println!("----------------------");
println!("artist: {}", tag.artist.trim()); println!("file: {:?}", file.path());
println!("album: {}", tag.album.trim()); println!("artist: {}", artist);
println!("title: {}", tag.title.trim()); println!("album: {}", album);
println!("title: {}", title);
println!("move to: ~/Music/{}/{}/?", tag.artist.trim(), tag.album.trim()); if artist.len() == 0 {
println!("Enter artist name?");
artist = user_input();
// is there a way to update mp3 metadata here?
}
println!("move to: ~/Music/{}/{}/?", artist.clone(), album);
if yes() { if yes() {
move_file( move_file(
@@ -152,33 +178,49 @@ fn handle_video(file: DirEntry) {
} }
fn handle_dir(path: PathBuf) { fn handle_dir(path: PathBuf) {
let dir = std::fs::read_dir(path).expect("Couldn't read dir"); let mut dir = std::fs::read_dir(&path).expect("Couldn't read dir");
// todo if dir is empty, delete if dir.next().is_none() {
println!("Deleting empty dir: {:?}", path);
fs::remove_dir(path).expect("Couldn't delete dir")
} else {
for inode in dir {
let inode = inode.expect("Couldn't read inode");
for inode in dir { // recursively handle directories
let inode = inode.expect("Couldn't read inode"); if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
}
// recursively handle directories if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() {
if inode.metadata().unwrap().is_dir() { match extension.to_string().to_lowercase().as_ref() {
handle_dir(inode.path()); "csv" => { handle_document(inode) }
} "doc" => { handle_document(inode) }
"docx" => { handle_document(inode) }
if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() { "odt" => { handle_document(inode) }
match extension.to_string().to_lowercase().as_ref() { "rtf" => { handle_document(inode) }
"gif" => { handle_gif( inode) } "xls" => { handle_document(inode) }
"jpg" => { handle_image(inode) } "xlsx" => { handle_document(inode) }
"jpeg" => { handle_image(inode) } "gif" => { handle_gif( inode) }
"png" => { handle_image(inode) } "jpg" => { handle_image( inode) }
"mp3" => { handle_mp3( inode) } "jpeg" => { handle_image( inode) }
"pdf" => { handle_pdf( inode) } "png" => { handle_image( inode) }
"avi" => { handle_video(inode) } "bin" => { handle_iso( inode) }
"m4v" => { handle_video(inode) } "cue" => { handle_iso( inode) }
"mkv" => { handle_video(inode) } "iso" => { handle_iso( inode) }
"mp4" => { handle_video(inode) } "mp3" => { handle_mp3( inode) }
// todo m4a, flac, etc? "pdf" => { handle_pdf( inode) }
// todo pdf, other documents "avi" => { handle_video( inode) }
_ => { /*println!("Here's where we would do nothing.");*/ } "m4v" => { handle_video( inode) }
"mkv" => { handle_video( inode) }
"mp4" => { handle_video( inode) }
"srt" => { handle_video( inode) }
"7z" => { }
"parts" => { }
"torrent" => { }
"zip" => { }
_ => { println!("Not sure what to do with file: {:?}.", inode.path()); }
}
} }
} }
} }