Compare commits
9 Commits
ef110fcb94
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| f44272de79 | |||
| 8b0efc287a | |||
| 0be1ec6fca | |||
| 8c0d9a7236 | |||
| c311f3c2cb | |||
| e6b9b927f4 | |||
| 7c26c38ccb | |||
| 825482ba07 | |||
| 3c471c46b0 |
85
Cargo.lock
generated
85
Cargo.lock
generated
@@ -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"
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "download-organiser"
|
name = "download-organiser"
|
||||||
version = "0.1.0"
|
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"
|
||||||
|
|||||||
123
src/main.rs
123
src/main.rs
@@ -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;
|
||||||
@@ -9,6 +9,10 @@ fn home_dir() -> PathBuf {
|
|||||||
dirs::home_dir().expect("Couldn't find home dir")
|
dirs::home_dir().expect("Couldn't find home dir")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn documents_dir() -> PathBuf {
|
||||||
|
dirs::document_dir().expect("Couldn't find downloads dir")
|
||||||
|
}
|
||||||
|
|
||||||
fn downloads_dir() -> PathBuf {
|
fn downloads_dir() -> PathBuf {
|
||||||
dirs::download_dir().expect("Couldn't find downloads dir")
|
dirs::download_dir().expect("Couldn't find downloads dir")
|
||||||
}
|
}
|
||||||
@@ -48,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();
|
||||||
|
|
||||||
@@ -75,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(
|
||||||
@@ -102,10 +132,18 @@ fn handle_mp3(file: DirEntry) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// if filename contains something like `(1971)` or `[2003]` it's a movie
|
fn handle_pdf(file: DirEntry) {
|
||||||
|
let mut dir = documents_dir();
|
||||||
|
|
||||||
|
dir.push("PDFs");
|
||||||
|
|
||||||
|
move_file(file.path(), dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// if filename contains something like `(1971)` or `[2003]` or `.1985.` it's a movie
|
||||||
fn is_movie(file: &DirEntry) -> bool {
|
fn is_movie(file: &DirEntry) -> bool {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref MOVIE: Regex = Regex::new(r"\(\d{4}\)|\[\d{4}]").unwrap();
|
static ref MOVIE: Regex = Regex::new(r"\.\d{4}\.|\(\d{4}\)|\[\d{4}]").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
MOVIE.is_match(file.file_name().to_str().unwrap())
|
MOVIE.is_match(file.file_name().to_str().unwrap())
|
||||||
@@ -114,7 +152,7 @@ fn is_movie(file: &DirEntry) -> bool {
|
|||||||
/// if filename contains something like "S03E21" it's a TV programme
|
/// if filename contains something like "S03E21" it's a TV programme
|
||||||
fn is_tv_episode(file: &DirEntry) -> bool {
|
fn is_tv_episode(file: &DirEntry) -> bool {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref TV: Regex = Regex::new(r"s\d{2}\s*e\d{2}").unwrap();
|
static ref TV: Regex = Regex::new(r"[sS]\d{2}\s*[eE][pP]?\d{2}").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
TV.is_match(file.file_name().to_str().unwrap())
|
TV.is_match(file.file_name().to_str().unwrap())
|
||||||
@@ -140,30 +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");
|
||||||
|
|
||||||
for inode in dir {
|
if dir.next().is_none() {
|
||||||
let inode = inode.expect("Couldn't read inode");
|
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");
|
||||||
|
|
||||||
// recursively handle directories
|
// recursively handle directories
|
||||||
if inode.metadata().unwrap().is_dir() {
|
if inode.metadata().unwrap().is_dir() {
|
||||||
handle_dir(inode.path());
|
handle_dir(inode.path());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() {
|
if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() {
|
||||||
match extension.to_string().to_lowercase().as_ref() {
|
match extension.to_string().to_lowercase().as_ref() {
|
||||||
"gif" => { handle_gif( inode) }
|
"csv" => { handle_document(inode) }
|
||||||
"jpg" => { handle_image(inode) }
|
"doc" => { handle_document(inode) }
|
||||||
"jpeg" => { handle_image(inode) }
|
"docx" => { handle_document(inode) }
|
||||||
"png" => { handle_image(inode) }
|
"odt" => { handle_document(inode) }
|
||||||
"mp3" => { handle_mp3( inode) }
|
"rtf" => { handle_document(inode) }
|
||||||
"avi" => { handle_video(inode) }
|
"xls" => { handle_document(inode) }
|
||||||
"m4v" => { handle_video(inode) }
|
"xlsx" => { handle_document(inode) }
|
||||||
"mkv" => { handle_video(inode) }
|
"gif" => { handle_gif( inode) }
|
||||||
"mp4" => { handle_video(inode) }
|
"jpg" => { handle_image( inode) }
|
||||||
// todo m4a, flac, etc?
|
"jpeg" => { handle_image( inode) }
|
||||||
// todo pdf, other documents
|
"png" => { handle_image( inode) }
|
||||||
_ => { /*println!("Here's where we would do nothing.");*/ }
|
"bin" => { handle_iso( inode) }
|
||||||
|
"cue" => { handle_iso( inode) }
|
||||||
|
"iso" => { handle_iso( inode) }
|
||||||
|
"mp3" => { handle_mp3( inode) }
|
||||||
|
"pdf" => { handle_pdf( inode) }
|
||||||
|
"avi" => { handle_video( inode) }
|
||||||
|
"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()); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user