From a2f9b0b1256c04cf2fb850ef082e7a27e9e06cde Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Wed, 25 Feb 2026 01:22:26 +0800 Subject: init --- .gitignore | 1 + Cargo.lock | 2569 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 35 + README.md | 152 +++ src/adaptive.rs | 72 ++ src/cli.rs | 149 +++ src/csv.rs | 391 ++++++++ src/download.rs | 584 +++++++++++ src/error.rs | 111 +++ src/main.rs | 2330 ++++++++++++++++++++++++++++++++++++++++++++ src/progress.rs | 262 +++++ src/services/mod.rs | 21 + src/services/tidal.rs | 524 ++++++++++ src/types.rs | 224 +++++ src/ui.rs | 384 ++++++++ 15 files changed, 7809 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/adaptive.rs create mode 100644 src/cli.rs create mode 100644 src/csv.rs create mode 100644 src/download.rs create mode 100644 src/error.rs create mode 100644 src/main.rs create mode 100644 src/progress.rs create mode 100644 src/services/mod.rs create mode 100644 src/services/tidal.rs create mode 100644 src/types.rs create mode 100644 src/ui.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d51a1e8 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,2569 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] +name = "anyhow" +version = "1.0.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" + +[[package]] +name = "async-trait" +version = "0.1.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" + +[[package]] +name = "bumpalo" +version = "3.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" + +[[package]] +name = "bytecount" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175812e0be2bccb6abe50bb8d566126198344f707e304f45c648fd8f2cc0365e" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "cc" +version = "1.2.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "chrono" +version = "0.4.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "serde", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "clap" +version = "4.5.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2797f34da339ce31042b27d23607e051786132987f595b02ba4f6a6dffb7030a" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24a241312cea5059b13574bb9b3861cabf758b879c15190b37b6d6fd63ab6876" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck 0.5.0", + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "clap_lex" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "colored" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" +dependencies = [ + "lazy_static", + "windows-sys 0.59.0", +] + +[[package]] +name = "console" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03e45a4a8926227e4197636ba97a9fc9b00477e9f4bd711395687c5f0734bec4" +dependencies = [ + "encode_unicode", + "libc", + "once_cell", + "unicode-width", + "windows-sys 0.61.2", +] + +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "csv" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde_core", +] + +[[package]] +name = "csv-core" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782" +dependencies = [ + "memchr", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + +[[package]] +name = "encoding_rs" +version = "0.8.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-executor" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718" + +[[package]] +name = "futures-macro" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "futures-sink" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi", + "wasip2", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139ef39800118c7683f2fd3c98c1b23c09ae076556b435f8e9064ae108aaeeec" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", + "wasip3", +] + +[[package]] +name = "h2" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f44da3a8150a6703ed5d34e164b875fd14c2cdab9af1252a9a1020bde2bdc54" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "http" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hyper" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab2d4f250c3d7b1c9fcdff1cece94ea4e2dfbec68614f7b87cb205f24ca9d11" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2", + "http", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "pin-utils", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "system-configuration", + "tokio", + "tower-service", + "tracing", + "windows-registry", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" + +[[package]] +name = "icu_properties" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af" + +[[package]] +name = "icu_provider" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "id-arena" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954" + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown 0.16.1", + "serde", + "serde_core", +] + +[[package]] +name = "indicatif" +version = "0.18.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25470f23803092da7d239834776d653104d551bc4d7eacaf31e6837854b8e9eb" +dependencies = [ + "console", + "portable-atomic", + "unicode-width", + "unit-prefix", + "web-time", +] + +[[package]] +name = "ipnet" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" + +[[package]] +name = "iri-string" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c91338f0783edbd6195decb37bae672fd3b165faffb89bf7b9e6942f8b1a731a" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" + +[[package]] +name = "js-sys" +version = "0.3.88" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7e709f3e3d22866f9c25b3aff01af289b18422cc8b4262fb19103ee80fe513d" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "libc" +version = "0.2.182" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "mio" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "native-tls" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "465500e14ea162429d264d44189adc38b199b62b1c21eea9f69e4b73cb03bbf2" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "openssl" +version = "0.10.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08838db121398ad17ab8531ce9de97b244589089e290a384c900cb9ff7434328" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "openssl-probe" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" + +[[package]] +name = "openssl-sys" +version = "0.9.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82cab2d520aa75e3c58898289429321eb788c3106963d0dc886ec7a5f4adc321" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "papergrid" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b0f8def1f117e13c895f3eda65a7b5650688da29d6ad04635f61bc7b92eebd" +dependencies = [ + "bytecount", + "fnv", + "unicode-width", +] + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "potential_utf" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +dependencies = [ + "zerovec", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.117", +] + +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quinn" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror 2.0.18", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1906b49b0c3bc04b5fe5d86a77925ae6524a19b816ae38ce1e426255f1d8a31" +dependencies = [ + "bytes", + "getrandom 0.3.4", + "lru-slab", + "rand", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.18", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.60.2", +] + +[[package]] +name = "quote" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" + +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-tls", + "hyper-util", + "js-sys", + "log", + "mime", + "native-tls", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-native-tls", + "tokio-rustls", + "tokio-util", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "wasm-streams", + "web-sys", + "webpki-roots", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + +[[package]] +name = "rustix" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c665f33d38cea657d9614f766881e4d510e0eda4239891eea56b4cadcf01801b" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7df23109aa6c1567d1c575b9952556388da57401e4ace1d15f79eedad0d8f53" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "sanitize-filename" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc984f4f9ceb736a7bb755c3e3bd17dc56370af2600c9780dcc48c66453da34d" +dependencies = [ + "regex", +] + +[[package]] +name = "schannel" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891d81b926048e76efe18581bf793546b4c0eaf8448d72be8de2bbee5fd166e1" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags", + "core-foundation 0.10.1", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "semver" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0" +dependencies = [ + "libc", + "windows-sys 0.60.2", +] + +[[package]] +name = "squid-downloader" +version = "0.1.0" +dependencies = [ + "async-trait", + "base64", + "chrono", + "clap", + "colored", + "csv", + "futures", + "indicatif", + "num_cpus", + "regex", + "reqwest", + "sanitize-filename", + "serde", + "serde_json", + "strsim", + "tabled", + "thiserror 1.0.69", + "tokio", + "url", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "system-configuration" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a13f3d0daba03132c0aa9767f98351b3488edc2c100cda2d2ec2b04f3d8d3c8b" +dependencies = [ + "bitflags", + "core-foundation 0.9.4", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "tabled" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6709222f3973137427ce50559cd564dc187a95b9cfe01613d2f4e93610e510a" +dependencies = [ + "papergrid", + "tabled_derive", +] + +[[package]] +name = "tabled_derive" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "931be476627d4c54070a1f3a9739ccbfec9b36b39815106a20cce2243bbcefe1" +dependencies = [ + "heck 0.4.1", + "proc-macro-error2", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "tempfile" +version = "3.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0136791f7c95b1f6dd99f9cc786b91bb81c3800b639b3478e561ddb7be95e5f1" +dependencies = [ + "fastrand", + "getrandom 0.4.1", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl 2.0.18", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "tinystr" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.49.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4e6559d53cc268e5031cd8429d05415bc4cb4aefc4aa5d6cc35fbf5b924a1f8" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "iri-string", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "unit-prefix" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81e544489bf3d8ef66c953931f56617f423cd4b5494be343d9b9d3dda037b9a3" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.2+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasip3" +version = "0.4.0+wasi-0.3.0-rc-2026-01-06" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5428f8bf88ea5ddc08faddef2ac4a67e390b88186c703ce6dbd955e1c145aca5" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec1adf1535672f5b7824f817792b1afd731d7e843d2d04ec8f27e8cb51edd8ac" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe88540d1c934c4ec8e6db0afa536876c5441289d7f9f9123d4f065ac1250a6b" +dependencies = [ + "cfg-if", + "futures-util", + "js-sys", + "once_cell", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e638317c08b21663aed4d2b9a2091450548954695ff4efa75bff5fa546b3b1" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c64760850114d03d5f65457e96fc988f11f01d38fbaa51b254e4ab5809102af" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 2.0.117", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60eecd4fe26177cfa3339eb00b4a36445889ba3ad37080c2429879718e20ca41" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasm-encoder" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990065f2fe63003fe337b932cfb5e3b80e0b4d0f5ff650e6985b1048f62c8319" +dependencies = [ + "leb128fmt", + "wasmparser", +] + +[[package]] +name = "wasm-metadata" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0e353e6a2fbdc176932bbaab493762eb1255a7900fe0fea1a2f96c296cc909" +dependencies = [ + "anyhow", + "indexmap", + "wasm-encoder", + "wasmparser", +] + +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "wasmparser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" +dependencies = [ + "bitflags", + "hashbrown 0.15.5", + "indexmap", + "semver", +] + +[[package]] +name = "web-sys" +version = "0.3.88" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d6bb20ed2d9572df8584f6dc81d68a41a625cadc6f15999d649a70ce7e3597a" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cfaf3c063993ff62e73cb4311efde4db1efb31ab78a3e5c457939ad5cc0bed" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-registry" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" +dependencies = [ + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "wit-bindgen" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5" +dependencies = [ + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea61de684c3ea68cb082b7a88508a8b27fcc8b797d738bfc99a82facf1d752dc" +dependencies = [ + "anyhow", + "heck 0.5.0", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c566e0f4b284dd6561c786d9cb0142da491f46a9fbed79ea69cdad5db17f21" +dependencies = [ + "anyhow", + "heck 0.5.0", + "indexmap", + "prettyplease", + "syn 2.0.117", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.51.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c0f9bfd77e6a48eccf51359e3ae77140a7f50b1e2ebfe62422d8afdaffab17a" +dependencies = [ + "anyhow", + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.117", + "wit-bindgen-core", + "wit-bindgen-rust", +] + +[[package]] +name = "wit-component" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.244.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8ac4bc1dc3381b7f59c34f00b67e18f910c2c0f50015669dde7def656a736" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", + "wasmparser", +] + +[[package]] +name = "writeable" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" + +[[package]] +name = "yoke" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db6d35d663eadb6c932438e763b262fe1a70987f9ae936e60158176d710cae4a" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" + +[[package]] +name = "zerotrie" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..23d1edf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "squid-downloader" +version = "0.1.0" +edition = "2021" +description = "A CLI tool for downloading high-quality music from Tidal" +authors = ["Squid Downloader"] +license = "MIT" + +[dependencies] +clap = { version = "4.5", features = ["derive", "color", "suggestions"] } +tokio = { version = "1.42", features = ["full"] } +async-trait = "0.1" +reqwest = { version = "0.12", features = ["json", "rustls-tls", "stream"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +colored = "2.1" +tabled = "0.17" +thiserror = "1.0" +base64 = "0.22" +regex = "1.11" +url = "2.5" +sanitize-filename = "0.6" +strsim = "0.11" +csv = "1.3" +num_cpus = "1.17.0" +chrono = { version = "0.4.44", features = ["serde"] } +indicatif = "0.18.4" +futures = "0.3" + +[profile.release] +opt-level = 3 +lto = true +codegen-units = 1 +panic = "abort" +strip = true diff --git a/README.md b/README.md new file mode 100644 index 0000000..055f5ea --- /dev/null +++ b/README.md @@ -0,0 +1,152 @@ +# Full disclosure, completely vibe coded, uses squid.wtf's downloading service, but it works :+1 +# CSV is downloaded from https://www.tunemymusic.com/transfer/spotify-to-file + +# Squid Downloader + +A CLI tool for downloading high-quality music from multiple streaming services. + +## Features + +- **Multiple Services**: Tidal, Amazon Music, SoundCloud, KHInsider +- **High-Quality Audio**: Support for Hi-Res FLAC (24-bit/192kHz), CD-quality FLAC, and lossy formats +- **Artist Filtering**: Optionally provide artist name to improve search accuracy +- **CSV Import**: Import tracks from CSV files with smart fuzzy matching +- **Pretty Output**: Beautiful CLI output with colors, tables, and progress bars +- **Error Handling**: Clear, formatted error messages + +## Installation + +```bash +cargo build --release +``` + +The binary will be at `./target/release/squid-downloader`. + +## Usage + +### Search for tracks + +```bash +# Basic search +squid-downloader search "Waiting For Love" + +# Search with artist filter for better accuracy +squid-downloader search "Waiting For Love" -a "Avicii" + +# Limit results +squid-downloader search "Bohemian Rhapsody" -n 5 +``` + +### Download a track + +```bash +# Download by search query (will prompt for selection) +squid-downloader track "Levels" + +# Download with artist filter +squid-downloader track "Levels" -a "Avicii" + +# Download first result without prompting +squid-downloader track "Levels" -a "Avicii" -f + +# Download by track ID +squid-downloader track 48717877 +``` + +### Import from CSV + +```bash +# Import and download tracks from CSV +squid-downloader csv tracks.csv + +# Set minimum confidence threshold (default: 0.6) +squid-downloader csv tracks.csv -t 0.8 + +# Skip confirmation prompt +squid-downloader csv tracks.csv -y +``` + +#### CSV Format + +The CSV file should have the following headers: + +```csv +Track name,Artist name,Album,Playlist name,Type,ISRC,Spotify - id +"Bleed","Connor Kauffman","Bleed","Favorite Songs","Favorite","QZPY62100085","4k8D363TGNjILbQRciQfBD" +"Running Up That Hill","Kate Bush","Hounds Of Love","Favorite Songs","Favorite","GBCNR8500002","1PtQJZVZIdWIYdARpZRDFO" +``` + +#### Matching Algorithm + +The CSV import uses a smart matching algorithm: + +1. Searches for each track with the artist name +2. If multiple artists are detected (separated by `,`, `&`, `/`, `;`), searches for each separately +3. Also searches for the track name alone +4. Scores candidates using: + - Title similarity (45% weight) + - Artist similarity (35% weight) + - Album similarity (20% weight) +5. Uses Jaro-Winkler distance for fuzzy matching +6. Only downloads tracks above the confidence threshold + +### Download an album + +```bash +squid-downloader album "Stories" +squid-downloader album 12345678 # by album ID +``` + +### Download a playlist + +```bash +squid-downloader playlist 12345678 +``` + +### Get track info + +```bash +squid-downloader info 48717877 +``` + +### List available services and qualities + +```bash +squid-downloader list +``` + +## Global Options + +| Option | Description | +|--------|-------------| +| `-o, --output` | Output directory for downloads (default: `./downloads`) | +| `-q, --quality` | Audio quality: `hires`, `lossless`, `high`, `low`, `mp3` | +| `-s, --service` | Streaming service: `tidal`, `amazon`, `soundcloud`, `khinsider` | +| `--quiet` | Suppress non-essential output | + +## Audio Qualities + +| Quality | Format | Description | +|---------|--------|-------------| +| `hires` | FLAC | 24-bit/192kHz (Hi-Res) | +| `lossless` | FLAC | 16-bit/44.1kHz (CD Quality) | +| `high` | AAC | 320kbps | +| `low` | AAC | 96kbps | +| `mp3` | MP3 | 320kbps | + +## Examples + +```bash +# Download a Hi-Res FLAC from Tidal +squid-downloader track "Wake Me Up" -a "Avicii" -q hires -f + +# Download to custom directory +squid-downloader track "Levels" -o ~/Music/downloads + +# Import playlist from CSV with high confidence threshold +squid-downloader csv my_playlist.csv -t 0.8 -y -q lossless +``` + +## License + +MIT diff --git a/src/adaptive.rs b/src/adaptive.rs new file mode 100644 index 0000000..ca9e0fc --- /dev/null +++ b/src/adaptive.rs @@ -0,0 +1,72 @@ +use std::sync::Arc; +use tokio::sync::{OwnedSemaphorePermit, Semaphore}; + +pub struct AdaptiveConcurrency { + current_concurrent: usize, + min_concurrent: usize, + max_concurrent: usize, + failure_streak: usize, + success_count: usize, + fail_count: usize, + semaphore: Arc, +} + +impl AdaptiveConcurrency { + pub fn new(max_concurrent: usize) -> Self { + let initial = num_cpus::get().min(max_concurrent); + AdaptiveConcurrency { + current_concurrent: initial, + min_concurrent: 1, + max_concurrent, + failure_streak: 0, + success_count: 0, + fail_count: 0, + semaphore: Arc::new(Semaphore::new(initial)), + } + } + + pub fn on_success(&mut self) { + self.success_count += 1; + self.failure_streak = 0; + + if self.success_count % 10 == 0 && self.current_concurrent < self.max_concurrent { + self.current_concurrent += 1; + self.semaphore + .add_permits(1); + } + } + + pub fn on_failure(&mut self) { + self.fail_count += 1; + self.failure_streak += 1; + + if self.failure_streak >= 3 && self.current_concurrent > self.min_concurrent { + let new_concurrent = (self.current_concurrent / 2).max(self.min_concurrent); + let reduction = self.current_concurrent - new_concurrent; + self.current_concurrent = new_concurrent; + + for _ in 0..reduction { + let _ = self + .semaphore + .try_acquire(); + } + } + } + + pub fn get_concurrent(&self) -> usize { + self.current_concurrent + } + + pub fn semaphore(&self) -> Arc { + self.semaphore + .clone() + } +} + +pub async fn acquire_slot(semaphore: &Arc) -> OwnedSemaphorePermit { + semaphore + .clone() + .acquire_owned() + .await + .unwrap() +} diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..5063187 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,149 @@ +use clap::{Parser, Subcommand}; +use std::path::PathBuf; + +#[derive(Parser)] +#[command(name = "squid-downloader")] +#[command(author = "Squid Downloader")] +#[command(version = "0.1.0")] +#[command(about = "Download high-quality music from multiple streaming services", long_about = None)] +#[command(color = clap::ColorChoice::Always)] +pub struct Cli { + #[command(subcommand)] + pub command: Commands, + + #[arg( + short, + long, + global = true, + help = "Output directory for downloads" + )] + pub output: Option, + + #[arg(short, long, global = true, help = "Audio quality", value_parser = ["hires", "lossless", "high", "low", "mp3"])] + pub quality: Option, + + #[arg(short, long, global = true, help = "Streaming service", value_parser = ["tidal", "amazon", "soundcloud", "khinsider"])] + pub service: Option, + + #[arg( + long, + global = true, + help = "Suppress non-essential output" + )] + pub quiet: bool, + + #[arg( + short = 'j', + long, + global = true, + help = "Max concurrent downloads (default: CPU cores)" + )] + pub concurrent: Option, + + #[arg(short = 'd', long, global = true, help = "Directory organization mode", value_parser = ["playlist", "album", "artist", "flat"])] + pub dir_mode: Option, +} + +#[derive(Subcommand)] +pub enum Commands { + #[command(about = "Search for tracks")] + Search { + #[arg(help = "Search query (song title)")] + query: String, + + #[arg( + short, + long, + help = "Artist name to improve accuracy" + )] + artist: Option, + + #[arg( + short = 'n', + long, + default_value = "10", + help = "Number of results" + )] + limit: usize, + }, + + #[command(about = "Download a single track")] + Track { + #[arg(help = "Track ID or search query")] + input: String, + + #[arg( + short, + long, + help = "Artist name to improve accuracy" + )] + artist: Option, + + #[arg( + short = 'f', + long, + help = "Download first result without prompting" + )] + first: bool, + }, + + #[command(about = "Download an entire album")] + Album { + #[arg(help = "Album ID or search query")] + input: String, + }, + + #[command(about = "Download tracks from an artist")] + Artist { + #[arg(help = "Artist ID or search query")] + input: String, + }, + + #[command(about = "Download a playlist")] + Playlist { + #[arg(help = "Playlist ID or URL")] + input: String, + }, + + #[command(about = "Import and download tracks from CSV file")] + Csv { + #[arg(help = "Path to CSV file")] + file: PathBuf, + + #[arg( + short = 't', + long, + default_value = "0.6", + help = "Minimum confidence threshold (0.0-1.0)" + )] + threshold: f64, + + #[arg( + short = 'y', + long, + help = "Skip confirmation prompt" + )] + yes: bool, + }, + + #[command(about = "Show information about a track")] + Info { + #[arg(help = "Track ID")] + id: u64, + }, + + #[command(about = "Get recommendations for a track")] + Recommend { + #[arg(help = "Track ID")] + id: u64, + }, + + #[command(about = "List available qualities and services")] + List, +} + +impl Cli { + pub fn parse() -> Self { + Parser::parse() + } +} diff --git a/src/csv.rs b/src/csv.rs new file mode 100644 index 0000000..e35d9fc --- /dev/null +++ b/src/csv.rs @@ -0,0 +1,391 @@ +use crate::{ + error::Result, + services::{tidal::TidalService, MusicService}, + types::TrackInfo, +}; +use colored::Colorize; +use serde::Deserialize; +use std::{fs::File, io::BufReader, path::Path}; + +#[derive(Debug, Clone, Deserialize)] +pub struct CsvTrack { + #[serde(rename = "Track name")] + pub track_name: String, + #[serde(rename = "Artist name")] + pub artist_name: String, + #[serde(rename = "Album")] + pub album: Option, + #[serde(rename = "Playlist name")] + pub playlist_name: Option, + #[serde(rename = "Type")] + pub track_type: Option, + #[serde(rename = "ISRC")] + pub isrc: Option, + #[serde(rename = "Spotify - id")] + pub spotify_id: Option, +} + +impl CsvTrack { + pub fn parse_artists(&self) -> Vec { + let artists: Vec = self + .artist_name + .split( + &[ + ',', '&', '/', ';', + ][..], + ) + .map( + |s| { + s.trim() + .to_string() + }, + ) + .filter(|s| !s.is_empty()) + .collect(); + + if artists.is_empty() { + vec![ + self.artist_name + .clone(), + ] + } else { + artists + } + } +} + +#[derive(Debug, Clone)] +pub struct CsvMatchResult { + pub csv_track: CsvTrack, + pub best_match: Option, + pub confidence: f64, + pub search_attempts: Vec, +} + +#[derive(Clone)] +pub struct CsvMatcher { + tidal: TidalService, +} + +impl CsvMatcher { + pub fn new() -> Self { + CsvMatcher { + tidal: TidalService::new(), + } + } + + pub async fn find_best_match(&self, csv_track: &CsvTrack) -> Result { + let mut search_attempts = Vec::new(); + let mut candidates: Vec = Vec::new(); + + let artists = csv_track.parse_artists(); + + for artist in &artists { + search_attempts.push( + format!( + "'{}' by '{}'", + csv_track.track_name, artist + ), + ); + + if let Ok(results) = self + .tidal + .search( + &csv_track.track_name, + Some(artist), + ) + .await + { + candidates.extend(results.tracks); + } + } + + search_attempts.push( + format!( + "'{}' (track only)", + csv_track.track_name + ), + ); + + if let Ok(results) = self + .tidal + .search( + &csv_track.track_name, + None, + ) + .await + { + candidates.extend(results.tracks); + } + + candidates.sort_by_key(|t| t.id); + candidates.dedup_by_key(|t| t.id); + + let best = Self::score_and_select_best( + &candidates, + csv_track, + ); + + Ok( + CsvMatchResult { + csv_track: csv_track.clone(), + best_match: best + .as_ref() + .map(|(track, _)| track.clone()), + confidence: best + .map(|(_, score)| score) + .unwrap_or(0.0), + search_attempts, + }, + ) + } + + pub fn score_and_select_best( + candidates: &[TrackInfo], + csv_track: &CsvTrack, + ) -> Option<( + TrackInfo, + f64, + )> { + if candidates.is_empty() { + return None; + } + + let mut best_score = 0.0_f64; + let mut best_match: Option = None; + + let csv_title_lower = csv_track + .track_name + .to_lowercase(); + let csv_artists = csv_track.parse_artists(); + let csv_artists_lower: Vec = csv_artists + .iter() + .map(|a| a.to_lowercase()) + .collect(); + let csv_album_lower = csv_track + .album + .as_ref() + .map(|a| a.to_lowercase()); + + for candidate in candidates { + let mut score = 0.0_f64; + + let title_lower = candidate + .title + .to_lowercase(); + + let title_similarity: f64 = if title_lower == csv_title_lower { + 1.0 + } else if title_lower.contains(&csv_title_lower) || csv_title_lower.contains(&title_lower) { + 0.85 + } else { + strsim::jaro_winkler( + &title_lower, + &csv_title_lower, + ) + }; + score += title_similarity * 0.45; + + if !candidate + .artist + .is_empty() + && !csv_artists_lower.is_empty() + { + let artist_lower = candidate + .artist + .to_lowercase(); + + let artist_similarity: f64 = csv_artists_lower + .iter() + .map( + |csv_artist| { + let sim: f64 = if artist_lower.contains(csv_artist) || csv_artist.contains(&artist_lower) { + 1.0 + } else { + strsim::jaro_winkler( + &artist_lower, + csv_artist, + ) + }; + sim + }, + ) + .fold( + 0.0_f64, + |max, sim| max.max(sim), + ); + + score += artist_similarity * 0.35; + } + + if let Some(ref csv_album) = csv_album_lower { + if let Some(ref candidate_album) = candidate.album { + let album_lower = candidate_album.to_lowercase(); + + let album_similarity: f64 = if album_lower == *csv_album { + 1.0 + } else if album_lower.contains(csv_album) || csv_album.contains(&album_lower) { + 0.9 + } else { + strsim::jaro_winkler( + &album_lower, + csv_album, + ) + }; + + score += album_similarity * 0.20; + } + } else { + score += 0.10; + } + + if score > best_score { + best_score = score; + best_match = Some(candidate.clone()); + } + } + + best_match.map( + |m| { + ( + m, best_score, + ) + }, + ) + } +} + +pub fn parse_csv_file(path: &Path) -> Result> { + let file = File::open(path)?; + let reader = BufReader::new(file); + + let mut csv_r = csv::Reader::from_reader(reader); + let mut tracks = Vec::new(); + + for result in csv_r.deserialize() { + let track: CsvTrack = result?; + tracks.push(track); + } + + Ok(tracks) +} + +pub fn print_csv_summary(results: &[CsvMatchResult]) { + println!(); + println!( + "{}", + "═" + .repeat(70) + .bright_blue() + ); + println!( + "{}", + "CSV Import Summary" + .cyan() + .bold() + ); + println!( + "{}", + "═" + .repeat(70) + .bright_blue() + ); + + let matched = results + .iter() + .filter( + |r| { + r.best_match + .is_some() + }, + ) + .count(); + let unmatched = results.len() - matched; + + println!( + "{} {} / {} {}", + "Matched:".green(), + matched + .to_string() + .green() + .bold(), + "Unmatched:".red(), + unmatched + .to_string() + .red() + ); + println!(); + + for result in results { + match &result.best_match { + Some(track) => { + let confidence_color = if result.confidence > 0.8 { + "✓".green() + } else if result.confidence > 0.6 { + "~".yellow() + } else { + "?".red() + }; + + println!( + " {} {} {} {}", + confidence_color, + format!( + "[{:.0}%]", + result.confidence * 100.0 + ) + .dimmed(), + result + .csv_track + .track_name + .white(), + format!( + "→ {}", + track.title + ) + .cyan() + ); + println!( + " {} {}", + "Artist:".dimmed(), + track + .artist + .white() + ); + if let Some(ref album) = track.album { + println!( + " {} {}", + "Album:".dimmed(), + album.white() + ); + } + } + None => { + println!( + " {} {}", + "✗".red(), + result + .csv_track + .track_name + .white() + ); + println!( + " {} {}", + "Searched:".dimmed(), + result + .search_attempts + .join(", ") + .white() + ); + } + } + } + + println!( + "{}", + "═" + .repeat(70) + .bright_blue() + ); + println!(); +} diff --git a/src/download.rs b/src/download.rs new file mode 100644 index 0000000..c77aaf5 --- /dev/null +++ b/src/download.rs @@ -0,0 +1,584 @@ +use crate::{ + error::{DownloadError, Result}, + types::*, +}; +use colored::Colorize; +use futures::StreamExt; +use reqwest::{Client, StatusCode}; +use std::{ + fs::File, + io::Write, + path::{Path, PathBuf}, + sync::Arc, + time::{Duration, Instant}, +}; +use tokio::sync::Mutex; + +const MAX_RETRIES: usize = 5; +const BACKOFF_DELAYS: [Duration; 5] = [ + Duration::from_secs(5), + Duration::from_secs(15), + Duration::from_secs(30), + Duration::from_secs(60), + Duration::from_secs(120), +]; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum DirMode { + Playlist, + Album, + Artist, + Flat, +} + +#[derive(Debug, Clone)] +pub struct DownloadProgressUpdate { + pub bytes_downloaded: u64, + pub total_bytes: Option, + pub speed_bps: Option, +} + +pub type ProgressCallback = Arc>>; + +#[derive(Clone)] +pub struct DownloadManager { + client: Client, + output_dir: PathBuf, + album: Option, + playlist: Option, + dir_mode: DirMode, +} + +impl DownloadManager { + pub fn new(output_dir: &Path) -> Self { + DownloadManager { + client: Client::builder() + .user_agent("SquidDownloader/0.1.0") + .build() + .unwrap(), + output_dir: output_dir.to_path_buf(), + album: None, + playlist: None, + dir_mode: DirMode::Playlist, + } + } + + pub fn with_album(mut self, album: Option<&str>) -> Self { + self.album = album.map(|s| s.to_string()); + self + } + + pub fn with_playlist(mut self, playlist: Option<&str>) -> Self { + self.playlist = playlist.map(|s| s.to_string()); + self + } + + pub fn with_dir_mode(mut self, mode: DirMode) -> Self { + self.dir_mode = mode; + self + } + + fn sanitize_filename(name: &str) -> String { + sanitize_filename::sanitize(name) + } + + pub fn generate_filename(&self, track: &TrackInfo, quality: &AudioQuality) -> PathBuf { + let artist = Self::sanitize_filename(&track.artist); + let title = Self::sanitize_filename(&track.title); + let extension = quality.file_extension(); + let filename = format!( + "{} - {}.{}", + artist, title, extension + ); + + match self.dir_mode { + DirMode::Playlist => { + if let Some(ref playlist) = self.playlist { + let playlist_dir = Self::sanitize_filename(playlist); + self.output_dir + .join(playlist_dir) + .join(filename) + } else if let Some(ref album) = self.album { + let album_dir = format!( + "{} - {}", + artist, + Self::sanitize_filename(album) + ); + self.output_dir + .join(album_dir) + .join(filename) + } else { + self.output_dir + .join(filename) + } + } + DirMode::Album => { + if let Some(ref album) = self.album { + let album_dir = format!( + "{} - {}", + artist, + Self::sanitize_filename(album) + ); + self.output_dir + .join(album_dir) + .join(filename) + } else { + self.output_dir + .join(filename) + } + } + DirMode::Artist => self + .output_dir + .join(&artist) + .join(filename), + DirMode::Flat => self + .output_dir + .join(filename), + } + } + + fn should_retry(status: StatusCode) -> bool { + status == StatusCode::TOO_MANY_REQUESTS || status.is_server_error() + } + + pub async fn download_file(&self, url: &str, dest: &Path) -> Result<()> { + if let Some(parent) = dest.parent() { + tokio::fs::create_dir_all(parent).await?; + } + + let mut last_error = None; + + for attempt in 0..MAX_RETRIES { + let response = self + .client + .get(url) + .send() + .await; + + match response { + Ok(resp) => { + let status = resp.status(); + + if status.is_success() { + let mut file = File::create(dest)?; + let bytes = resp + .bytes() + .await?; + file.write_all(&bytes)?; + return Ok(()); + } + + let error = DownloadError::DownloadFailed( + format!( + "HTTP status: {}", + status + ), + ); + + if !Self::should_retry(status) || attempt == MAX_RETRIES - 1 { + return Err(error); + } + + last_error = Some(error); + } + Err(e) => { + if attempt == MAX_RETRIES - 1 { + return Err(DownloadError::NetworkError(e)); + } + last_error = Some(DownloadError::NetworkError(e)); + } + } + + tokio::time::sleep(BACKOFF_DELAYS[attempt]).await; + } + + Err(last_error.unwrap_or(DownloadError::DownloadFailed("Max retries exceeded".to_string()))) + } + + pub async fn download_file_with_progress(&self, url: &str, dest: &Path, mut progress_callback: F) -> Result + where + F: FnMut(DownloadProgressUpdate) + Send, + { + if let Some(parent) = dest.parent() { + tokio::fs::create_dir_all(parent).await?; + } + + let mut last_error = None; + + for attempt in 0..MAX_RETRIES { + let response = self + .client + .get(url) + .send() + .await; + + match response { + Ok(resp) => { + let status = resp.status(); + + if status.is_success() { + let total_size = resp.content_length(); + let mut file = File::create(dest)?; + let mut downloaded: u64 = 0; + let start_time = Instant::now(); + let mut last_update = Instant::now(); + + let mut stream = resp.bytes_stream(); + + while let Some(chunk_result) = stream + .next() + .await + { + let chunk = chunk_result.map_err(DownloadError::NetworkError)?; + file.write_all(&chunk)?; + downloaded += chunk.len() as u64; + + let now = Instant::now(); + let elapsed = now + .duration_since(last_update) + .as_millis(); + if elapsed >= 100 || downloaded == total_size.unwrap_or(0) { + let total_elapsed = start_time + .elapsed() + .as_secs_f64(); + let speed = if total_elapsed > 0.0 { + Some((downloaded as f64) / total_elapsed) + } else { + None + }; + + progress_callback( + DownloadProgressUpdate { + bytes_downloaded: downloaded, + total_bytes: total_size, + speed_bps: speed, + }, + ); + + last_update = now; + } + } + + return Ok(downloaded); + } + + let error = DownloadError::DownloadFailed( + format!( + "HTTP status: {}", + status + ), + ); + + if !Self::should_retry(status) || attempt == MAX_RETRIES - 1 { + return Err(error); + } + + last_error = Some(error); + } + Err(e) => { + if attempt == MAX_RETRIES - 1 { + return Err(DownloadError::NetworkError(e)); + } + last_error = Some(DownloadError::NetworkError(e)); + } + } + + tokio::time::sleep(BACKOFF_DELAYS[attempt]).await; + } + + Err(last_error.unwrap_or(DownloadError::DownloadFailed("Max retries exceeded".to_string()))) + } + + pub async fn download_segments(&self, segment_urls: &[String], dest: &Path) -> Result<()> { + if let Some(parent) = dest.parent() { + tokio::fs::create_dir_all(parent).await?; + } + + let mut file = File::create(dest)?; + let total = segment_urls.len(); + + for (i, url) in segment_urls + .iter() + .enumerate() + { + let mut segment_error = None; + + for attempt in 0..MAX_RETRIES { + let response = self + .client + .get(url) + .send() + .await; + + match response { + Ok(resp) => { + let status = resp.status(); + + if status.is_success() { + let bytes = resp + .bytes() + .await?; + file.write_all(&bytes)?; + break; + } + + let error = DownloadError::SegmentDownloadFailed( + format!( + "Segment {}/{} failed: {}", + i + 1, + total, + status + ), + ); + + if !Self::should_retry(status) || attempt == MAX_RETRIES - 1 { + let _ = std::fs::remove_file(dest); + return Err(error); + } + + segment_error = Some(error); + } + Err(e) => { + let error = DownloadError::NetworkError(e); + if attempt == MAX_RETRIES - 1 { + let _ = std::fs::remove_file(dest); + return Err(error); + } + segment_error = Some( + DownloadError::SegmentDownloadFailed( + format!( + "Segment {}/{} network error", + i + 1, + total + ), + ), + ); + } + } + + tokio::time::sleep(BACKOFF_DELAYS[attempt]).await; + } + + if let Some(e) = segment_error { + let _ = std::fs::remove_file(dest); + return Err(e); + } + } + + Ok(()) + } + + pub async fn download_segments_with_progress(&self, segment_urls: &[String], dest: &Path, mut progress_callback: F) -> Result + where + F: FnMut(DownloadProgressUpdate) + Send, + { + if let Some(parent) = dest.parent() { + tokio::fs::create_dir_all(parent).await?; + } + + let mut file = File::create(dest)?; + let total = segment_urls.len(); + let mut downloaded: u64 = 0; + let start_time = Instant::now(); + let mut last_update = Instant::now(); + + for (i, url) in segment_urls + .iter() + .enumerate() + { + let mut segment_error = None; + + for attempt in 0..MAX_RETRIES { + let response = self + .client + .get(url) + .send() + .await; + + match response { + Ok(resp) => { + let status = resp.status(); + + if status.is_success() { + let bytes = resp + .bytes() + .await?; + file.write_all(&bytes)?; + downloaded += bytes.len() as u64; + + let now = Instant::now(); + let elapsed = now + .duration_since(last_update) + .as_millis(); + if elapsed >= 100 || i == total - 1 { + let total_elapsed = start_time + .elapsed() + .as_secs_f64(); + let speed = if total_elapsed > 0.0 { + Some((downloaded as f64) / total_elapsed) + } else { + None + }; + + progress_callback( + DownloadProgressUpdate { + bytes_downloaded: downloaded, + total_bytes: None, + speed_bps: speed, + }, + ); + + last_update = now; + } + break; + } + + let error = DownloadError::SegmentDownloadFailed( + format!( + "Segment {}/{} failed: {}", + i + 1, + total, + status + ), + ); + + if !Self::should_retry(status) || attempt == MAX_RETRIES - 1 { + let _ = std::fs::remove_file(dest); + return Err(error); + } + + segment_error = Some(error); + } + Err(e) => { + let error = DownloadError::NetworkError(e); + if attempt == MAX_RETRIES - 1 { + let _ = std::fs::remove_file(dest); + return Err(error); + } + segment_error = Some( + DownloadError::SegmentDownloadFailed( + format!( + "Segment {}/{} network error", + i + 1, + total + ), + ), + ); + } + } + + tokio::time::sleep(BACKOFF_DELAYS[attempt]).await; + } + + if let Some(e) = segment_error { + let _ = std::fs::remove_file(dest); + return Err(e); + } + } + + Ok(downloaded) + } + + pub async fn download_from_manifest(&self, manifest: &DownloadManifest, track: &TrackInfo) -> Result { + let dest = self.generate_filename( + track, + &manifest.quality, + ); + + if let Some(ref bit_depth) = manifest.bit_depth { + if let Some(ref sample_rate) = manifest.sample_rate { + let sample_str = format_sample_rate(*sample_rate); + println!( + " {} {}, {}-bit FLAC", + "Format:".cyan(), + sample_str.white(), + bit_depth + .to_string() + .white() + ); + } + } + + println!( + " {} {}", + "Output:".cyan(), + dest.display() + .to_string() + .green() + ); + + let result = if let Some(ref url) = manifest.url { + self.download_file( + url, &dest, + ) + .await + } else if let Some(ref segments) = manifest.segment_urls { + println!( + " {} {} segments", + "Downloading:".cyan(), + segments + .len() + .to_string() + .white() + ); + self.download_segments( + segments, &dest, + ) + .await + } else { + Err(DownloadError::NoDownloadUrl) + }; + + if result.is_err() { + let _ = std::fs::remove_file(&dest); + } + + result?; + Ok(dest) + } + + pub async fn download_from_manifest_with_progress(&self, manifest: &DownloadManifest, track: &TrackInfo, mut progress_callback: F) -> Result + where + F: FnMut(DownloadProgressUpdate) + Send, + { + let dest = self.generate_filename( + track, + &manifest.quality, + ); + + let result = if let Some(ref url) = manifest.url { + self.download_file_with_progress( + url, + &dest, + &mut progress_callback, + ) + .await?; + } else if let Some(ref segments) = manifest.segment_urls { + self.download_segments_with_progress( + segments, + &dest, + &mut progress_callback, + ) + .await?; + } else { + return Err(DownloadError::NoDownloadUrl); + }; + + Ok(dest) + } +} + +fn format_sample_rate(sample_rate: u32) -> String { + if sample_rate >= 1000 { + let khz = sample_rate as f64 / 1000.0; + format!( + "{:.1}kHz", + khz + ) + } else { + format!( + "{}Hz", + sample_rate + ) + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..2b5954f --- /dev/null +++ b/src/error.rs @@ -0,0 +1,111 @@ +use colored::Colorize; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum DownloadError { + #[error("Network request failed: {0}")] + NetworkError(#[from] reqwest::Error), + + #[error("Failed to parse JSON response: {0}")] + JsonParseError(#[from] serde_json::Error), + + #[error("Failed to decode base64 manifest: {0}")] + Base64DecodeError(#[from] base64::DecodeError), + + #[error("Failed to parse XML manifest: {0}")] + XmlParseError(String), + + #[error("Track not found: {0}")] + TrackNotFound(String), + + #[error("Album not found: {0}")] + AlbumNotFound(String), + + #[error("Artist not found: {0}")] + ArtistNotFound(String), + + #[error("Playlist not found: {0}")] + PlaylistNotFound(String), + + #[error("No download URL available for the requested quality")] + NoDownloadUrl, + + #[error("Rate limited. Please wait before making more requests")] + RateLimited, + + #[error("IO error: {0}")] + IoError(#[from] std::io::Error), + + #[error("Download failed: {0}")] + DownloadFailed(String), + + #[error("Segment download failed: {0}")] + SegmentDownloadFailed(String), + + #[error("CSV parsing error: {0}")] + CsvError(String), + + #[error("Service unavailable: {0}")] + ServiceUnavailable(String), +} + +impl From for DownloadError { + fn from(err: csv::Error) -> Self { + DownloadError::CsvError(err.to_string()) + } +} + +impl DownloadError { + pub fn pretty_print(&self) { + let error_type = match self { + DownloadError::NetworkError(_) => "Network Error", + DownloadError::JsonParseError(_) => "Parse Error", + DownloadError::Base64DecodeError(_) => "Decode Error", + DownloadError::XmlParseError(_) => "XML Error", + DownloadError::TrackNotFound(_) => "Not Found", + DownloadError::AlbumNotFound(_) => "Not Found", + DownloadError::ArtistNotFound(_) => "Not Found", + DownloadError::PlaylistNotFound(_) => "Not Found", + DownloadError::NoDownloadUrl => "Download Error", + DownloadError::RateLimited => "Rate Limit", + DownloadError::IoError(_) => "IO Error", + DownloadError::DownloadFailed(_) => "Download Error", + DownloadError::SegmentDownloadFailed(_) => "Download Error", + DownloadError::CsvError(_) => "CSV Error", + DownloadError::ServiceUnavailable(_) => "Service Error", + }; + + eprintln!(); + eprintln!( + "{}", + "╭─────────────────────────────────────╮".bright_red() + ); + eprintln!( + "{} {} {:>24} {}", + "│".bright_red(), + error_type + .red() + .bold(), + "", + "│".bright_red() + ); + eprintln!( + "{}", + "├─────────────────────────────────────┤".bright_red() + ); + eprintln!( + "{} {} {}", + "│".bright_red(), + self.to_string() + .white(), + "│".bright_red() + ); + eprintln!( + "{}", + "╰─────────────────────────────────────╯".bright_red() + ); + eprintln!(); + } +} + +pub type Result = std::result::Result; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..35f31ff --- /dev/null +++ b/src/main.rs @@ -0,0 +1,2330 @@ +mod adaptive; +mod cli; +mod csv; +mod download; +mod error; +mod progress; +mod services; +mod types; +mod ui; + +use adaptive::{acquire_slot, AdaptiveConcurrency}; +use cli::{Cli, Commands}; +use colored::Colorize; +use csv::{CsvMatchResult, CsvMatcher}; +use download::{DirMode, DownloadManager, DownloadProgressUpdate}; +use error::{DownloadError, Result}; +use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; +use progress::DownloadProgress; +use regex::Regex; +use services::{tidal::TidalService, MusicService}; +use std::{ + path::PathBuf, + sync::{ + atomic::{AtomicU64, AtomicUsize, Ordering}, + Arc, + }, + time::Duration, +}; +use tokio::sync::Mutex; +use types::{AudioQuality, Service}; +use ui::Ui; + +const NOT_FOUND_DELAY_MS: u64 = 1000; +const RATE_LIMIT_DELAY_MS: u64 = 15000; +const CONSECUTIVE_FAILURE_THRESHOLD: usize = 3; +const RETRY_BACKOFF_SECS: [u64; 5] = [ + 5, 15, 30, 60, 120, +]; + +pub struct ProgressManager { + multi: MultiProgress, + download_bars: Vec, + total_bar: ProgressBar, + status_bar: ProgressBar, + matched_count: Arc, + downloaded_count: Arc, + total_tracks: u64, +} + +impl ProgressManager { + pub fn new(max_concurrent: usize, total_tracks: u64) -> Self { + let multi = MultiProgress::new(); + + let mut download_bars = Vec::new(); + for i in 0..max_concurrent { + let pb = multi.add(ProgressBar::new(0)); + pb.set_style( + ProgressStyle::default_bar() + .template( + &format!( + "Slot {}: [{{bar:25.cyan/blue}}] {{msg}}", + i + 1 + ), + ) + .unwrap() + .progress_chars("█▓▒░ "), + ); + pb.set_message( + "Idle" + .dimmed() + .to_string(), + ); + pb.enable_steady_tick(Duration::from_millis(100)); + download_bars.push(pb); + } + + let separator = multi.add(ProgressBar::new_spinner()); + separator.set_style( + ProgressStyle::default_spinner() + .template("─────────────────────────────────────────────────────────────────────") + .unwrap(), + ); + separator.finish(); + + let total_bar = multi.add(ProgressBar::new(total_tracks * 2)); + total_bar.set_style( + ProgressStyle::default_bar() + .template("Progress: [{bar:40.cyan/blue}] {pos}/{len} ({eta}) {percent}%") + .unwrap() + .progress_chars("█▓▒░ "), + ); + + let status_bar = multi.add(ProgressBar::new_spinner()); + status_bar.set_style( + ProgressStyle::default_spinner() + .template("Stage: {msg}") + .unwrap(), + ); + status_bar.set_message("1/2 - Matching tracks".to_string()); + status_bar.enable_steady_tick(Duration::from_millis(100)); + + Self { + multi, + download_bars, + total_bar, + status_bar, + matched_count: Arc::new(AtomicU64::new(0)), + downloaded_count: Arc::new(AtomicU64::new(0)), + total_tracks, + } + } + + pub fn set_stage(&self, stage: &str) { + self.status_bar + .set_message(stage.to_string()); + self.status_bar + .tick(); + } + + pub fn inc_matched(&self) { + self.matched_count + .fetch_add( + 1, + Ordering::Relaxed, + ); + self.total_bar + .inc(1); + self.update_total_message(); + } + + pub fn inc_downloaded(&self) { + self.downloaded_count + .fetch_add( + 1, + Ordering::Relaxed, + ); + self.total_bar + .inc(1); + self.update_total_message(); + } + + fn update_total_message(&self) { + let matched = self + .matched_count + .load(Ordering::Relaxed); + let downloaded = self + .downloaded_count + .load(Ordering::Relaxed); + self.total_bar + .set_message( + format!( + "{} matched + {} downloaded", + matched, downloaded + ), + ); + } + + pub fn set_downloading(&self, slot: usize, artist: &str, title: &str) { + if slot + < self + .download_bars + .len() + { + self.download_bars[slot].set_message( + format!( + "{} - {}", + artist, title + ), + ); + self.download_bars[slot].set_length(0); + self.download_bars[slot].set_position(0); + } + } + + pub fn update_download_progress(&self, slot: usize, progress: DownloadProgressUpdate) { + if slot + < self + .download_bars + .len() + { + if let Some(total) = progress.total_bytes { + self.download_bars[slot].set_length(total); + } + self.download_bars[slot].set_position(progress.bytes_downloaded); + if let Some(speed) = progress.speed_bps { + let speed_str = format_speed(speed); + self.download_bars[slot].set_message( + format!( + "{} ({})", + self.download_bars[slot] + .message() + .split_whitespace() + .next() + .unwrap_or(""), + speed_str + ), + ); + } + } + } + + pub fn finish_download(&self, slot: usize, success: bool, artist: &str, title: &str) { + if slot + < self + .download_bars + .len() + { + if success { + self.download_bars[slot].set_message( + format!( + "✓ {} - {}", + artist.green(), + title + ), + ); + } else { + self.download_bars[slot].set_message( + format!( + "✗ {} - {}", + artist.red(), + title + ), + ); + } + self.download_bars[slot].finish(); + } + } + + pub fn clear_slot(&self, slot: usize) { + if slot + < self + .download_bars + .len() + { + self.download_bars[slot].set_message( + "Idle" + .dimmed() + .to_string(), + ); + self.download_bars[slot].set_length(0); + self.download_bars[slot].set_position(0); + } + } + + pub fn update_download(&self, slot: usize, msg: String) { + if slot + < self + .download_bars + .len() + { + self.download_bars[slot].set_message(msg); + self.download_bars[slot].tick(); + } + } + + pub fn inc_total(&self) { + self.total_bar + .inc(1); + } + + pub fn finish(&self) { + for bar in &self.download_bars { + bar.finish_and_clear(); + } + self.total_bar + .finish(); + self.status_bar + .finish(); + } + + pub fn finish_with_message(&self, msg: &str) { + for bar in &self.download_bars { + bar.finish_and_clear(); + } + self.total_bar + .finish_with_message(msg.to_string()); + self.status_bar + .finish(); + } + + pub fn clear(&self) { + for bar in &self.download_bars { + bar.finish_and_clear(); + } + self.total_bar.finish_and_clear(); + self.status_bar.finish_and_clear(); + } + + pub fn reset_for_download(&self, download_count: u64) { + for bar in &self.download_bars { + bar.reset(); + bar.set_message("Idle".dimmed().to_string()); + bar.set_length(0); + bar.set_position(0); + } + self.total_bar.set_length(self.total_tracks + download_count); + } +} + +fn format_speed(bytes_per_sec: f64) -> String { + if bytes_per_sec >= 1_000_000.0 { + format!( + "{:.1} MB/s", + bytes_per_sec / 1_000_000.0 + ) + } else if bytes_per_sec >= 1_000.0 { + format!( + "{:.1} KB/s", + bytes_per_sec / 1_000.0 + ) + } else { + format!( + "{:.0} B/s", + bytes_per_sec + ) + } +} + +pub struct SimpleProgressManager { + multi: MultiProgress, + download_bars: Vec, + total_bar: ProgressBar, +} + +impl SimpleProgressManager { + pub fn new(max_concurrent: usize, total: u64, prefix: &str) -> Self { + let multi = MultiProgress::new(); + + let mut download_bars = Vec::new(); + for _ in 0..max_concurrent { + let pb = multi.add(ProgressBar::new_spinner()); + pb.set_style( + ProgressStyle::default_spinner() + .template("{msg}") + .unwrap(), + ); + pb.set_message( + "Idle" + .dimmed() + .to_string(), + ); + pb.enable_steady_tick(Duration::from_millis(100)); + download_bars.push(pb); + } + + let separator = multi.add(ProgressBar::new_spinner()); + separator.set_style( + ProgressStyle::default_spinner() + .template("─────────────────────────────────────────────────────────────────────") + .unwrap(), + ); + separator.finish(); + + let total_bar = multi.add(ProgressBar::new(total)); + total_bar.set_style( + ProgressStyle::default_bar() + .template("{prefix}: [{bar:40.cyan/blue}] {pos}/{len} ({eta}) {percent}%") + .unwrap() + .progress_chars("█▓▒░ "), + ); + total_bar.set_prefix(prefix.to_string()); + + Self { + multi, + download_bars, + total_bar, + } + } + + pub fn update_download(&self, slot: usize, msg: String) { + if slot + < self + .download_bars + .len() + { + self.download_bars[slot].set_message(msg); + self.download_bars[slot].tick(); + } + } + + pub fn clear_slot(&self, slot: usize) { + if slot + < self + .download_bars + .len() + { + self.download_bars[slot].set_message( + "Idle" + .dimmed() + .to_string(), + ); + } + } + + pub fn inc_total(&self) { + self.total_bar + .inc(1); + } + + pub fn finish_with_message(&self, msg: &str) { + for bar in &self.download_bars { + bar.finish_and_clear(); + } + self.total_bar + .finish_with_message(msg.to_string()); + } +} + +#[tokio::main] +async fn main() { + colored::control::set_override(true); + let args = Cli::parse(); + + if !args.quiet { + Ui::print_banner(); + } + + let output_dir = args + .output + .clone() + .unwrap_or_else(|| PathBuf::from("./downloads")); + let quality = parse_quality( + args.quality + .as_deref(), + ); + let service = parse_service( + args.service + .as_deref(), + ); + let max_concurrent = args + .concurrent + .unwrap_or_else(num_cpus::get); + let dir_mode = parse_dir_mode( + args.dir_mode + .as_deref(), + ); + + if let Err(e) = run_command( + args, + &output_dir, + quality, + service, + max_concurrent, + dir_mode, + ) + .await + { + e.pretty_print(); + std::process::exit(1); + } +} + +fn parse_quality(quality: Option<&str>) -> AudioQuality { + match quality { + Some("hires") => AudioQuality::HiResLossless, + Some("lossless") => AudioQuality::Lossless, + Some("high") => AudioQuality::High, + Some("low") => AudioQuality::Low, + Some("mp3") => AudioQuality::Mp3_320, + None => AudioQuality::HiResLossless, + _ => AudioQuality::HiResLossless, + } +} + +fn parse_service(service: Option<&str>) -> Service { + match service { + Some("tidal") => Service::Tidal, + Some("amazon") => Service::AmazonMusic, + Some("soundcloud") => Service::SoundCloud, + Some("khinsider") => Service::KHInsider, + None => Service::Tidal, + _ => Service::Tidal, + } +} + +fn parse_dir_mode(dir_mode: Option<&str>) -> DirMode { + match dir_mode { + Some("playlist") => DirMode::Playlist, + Some("album") => DirMode::Album, + Some("artist") => DirMode::Artist, + Some("flat") => DirMode::Flat, + None => DirMode::Playlist, + _ => DirMode::Playlist, + } +} + +async fn ensure_output_dir(output_dir: &PathBuf) -> Result<()> { + if !output_dir.exists() { + tokio::fs::create_dir_all(output_dir).await?; + } + Ok(()) +} + +async fn run_command(args: Cli, output_dir: &PathBuf, quality: AudioQuality, service: Service, max_concurrent: usize, dir_mode: DirMode) -> Result<()> { + match args.command { + Commands::Search { + query, + artist, + limit, + } => { + Ui::print_info( + &format!( + "Searching for: {}", + query.cyan() + ), + ); + + let tidal = TidalService::new(); + let results = tidal + .search( + &query, + artist.as_deref(), + ) + .await?; + + if results + .tracks + .is_empty() + { + Ui::print_warning("No results found"); + return Ok(()); + } + + Ui::print_info( + &format!( + "Found {} results", + results + .tracks + .len() + .to_string() + .green() + ), + ); + Ui::print_search_results( + &results.tracks, + limit, + ); + } + + Commands::Track { + input, + artist, + first, + } => { + ensure_output_dir(output_dir).await?; + download_track( + &input, + artist.as_deref(), + first, + output_dir, + quality, + service, + ) + .await?; + } + + Commands::Album { + input, + } => { + ensure_output_dir(output_dir).await?; + download_album( + &input, + output_dir, + quality, + service, + max_concurrent, + ) + .await?; + } + + Commands::Artist { + input, + } => { + download_artist( + &input, output_dir, quality, service, + ) + .await?; + } + + Commands::Playlist { + input, + } => { + ensure_output_dir(output_dir).await?; + download_playlist( + &input, + output_dir, + quality, + service, + max_concurrent, + ) + .await?; + } + + Commands::Csv { + file, + threshold, + yes, + } => { + ensure_output_dir(output_dir).await?; + download_from_csv( + &file, + threshold, + yes, + output_dir, + quality, + max_concurrent, + dir_mode, + ) + .await?; + } + + Commands::Info { + id, + } => { + let tidal = TidalService::new(); + let track = tidal + .get_track_info(id) + .await?; + Ui::print_track_detail( + &track, None, + ); + } + + Commands::Recommend { + id, + } => { + Ui::print_info("Fetching recommendations..."); + let tidal = TidalService::new(); + let results = tidal + .search( + "recommendations", + None, + ) + .await?; + Ui::print_search_results( + &results.tracks, + 10, + ); + let _ = id; + } + + Commands::List => { + Ui::print_services(); + Ui::print_qualities(); + } + } + + Ok(()) +} + +async fn download_track(input: &str, artist: Option<&str>, first: bool, output_dir: &PathBuf, quality: AudioQuality, _service: Service) -> Result<()> { + let tidal = TidalService::new(); + + let track_id = if input + .parse::() + .is_ok() + { + input + .parse::() + .unwrap() + } else { + Ui::print_info( + &format!( + "Searching for: {}", + input.cyan() + ), + ); + + let search_results = tidal + .search( + input, artist, + ) + .await?; + + if search_results + .tracks + .is_empty() + { + return Err(DownloadError::TrackNotFound(input.to_string())); + } + + let selected_index = if first { + 0 + } else { + Ui::select_track(&search_results.tracks).ok_or_else(|| DownloadError::TrackNotFound("No track selected".to_string()))? + }; + + let track = &search_results.tracks[selected_index]; + Ui::print_info( + &format!( + "Selected: {} by {}", + track + .title + .cyan(), + track + .artist + .white() + ), + ); + + track.id + }; + + let track_info = tidal + .get_track_info(track_id) + .await?; + let manifest = tidal + .get_manifest( + track_id, quality, + ) + .await?; + + let downloader = DownloadManager::new(output_dir); + downloader + .download_from_manifest( + &manifest, + &track_info, + ) + .await?; + + Ui::print_success("Download complete!"); + + Ok(()) +} + +async fn download_album(input: &str, output_dir: &PathBuf, quality: AudioQuality, _service: Service, max_concurrent: usize) -> Result<()> { + let tidal = TidalService::new(); + + let album_id = if input + .parse::() + .is_ok() + { + input + .parse::() + .unwrap() + } else { + Ui::print_info( + &format!( + "Searching for album: {}", + input.cyan() + ), + ); + + let search_results = tidal + .search( + input, None, + ) + .await?; + + if search_results + .tracks + .is_empty() + { + return Err(DownloadError::AlbumNotFound(input.to_string())); + } + + let track = &search_results.tracks[0]; + let album_name = track + .album + .as_ref() + .ok_or_else(|| DownloadError::AlbumNotFound("No album info found".to_string()))?; + + Ui::print_info( + &format!( + "Found album: {} by {}", + album_name.cyan(), + track + .artist + .white() + ), + ); + + if !Ui::confirm("Download this album?") { + Ui::print_info("Cancelled"); + return Ok(()); + } + + return Ok(()); + }; + + let album_info = tidal + .get_album_info(album_id) + .await?; + Ui::print_album_detail(&album_info); + + let tracks = tidal + .get_album_tracks(album_id) + .await?; + + let total_tracks = tracks.len(); + let progress_mgr = Arc::new( + SimpleProgressManager::new( + max_concurrent, + total_tracks as u64, + "Album", + ), + ); + + let album_name = Some( + album_info + .title + .clone(), + ); + let output_dir = output_dir.clone(); + let adaptive = Arc::new(Mutex::new(AdaptiveConcurrency::new(max_concurrent))); + let semaphore = adaptive + .lock() + .await + .semaphore(); + let success_count = Arc::new(Mutex::new(0usize)); + let fail_count = Arc::new(Mutex::new(0usize)); + let slot_counter = Arc::new(AtomicUsize::new(0)); + + let mut handles = vec![]; + + for track in tracks { + let permit = acquire_slot(&semaphore).await; + let tidal = TidalService::new(); + let output_dir = output_dir.clone(); + let quality = quality; + let album_name = album_name.clone(); + let success_count = success_count.clone(); + let fail_count = fail_count.clone(); + let adaptive_inner = adaptive.clone(); + let progress_inner = progress_mgr.clone(); + let slot_counter_inner = slot_counter.clone(); + + let handle = tokio::spawn( + async move { + let _permit = permit; + let slot = slot_counter_inner.fetch_add( + 1, + Ordering::Relaxed, + ) % max_concurrent; + let downloader = DownloadManager::new(&output_dir).with_album(album_name.as_deref()); + + progress_inner.update_download( + slot, + format!( + "Matching: {} - {}", + track + .artist + .white(), + track + .title + .cyan() + ), + ); + + let result = retry_download( + || async { + let manifest = tidal + .get_manifest( + track.id, quality, + ) + .await?; + downloader + .download_from_manifest( + &manifest, &track, + ) + .await + }, + adaptive_inner.clone(), + ) + .await; + + match result { + Ok(_) => { + adaptive_inner + .lock() + .await + .on_success(); + *success_count + .lock() + .await += 1; + progress_inner.update_download( + slot, + format!( + "✓ {} - {}", + track + .artist + .white(), + track + .title + .green() + ), + ); + } + Err(e) => { + adaptive_inner + .lock() + .await + .on_failure(); + *fail_count + .lock() + .await += 1; + progress_inner.update_download( + slot, + format!( + "✗ {}: {}", + track + .title + .white(), + e.to_string() + .dimmed() + ), + ); + } + } + progress_inner.inc_total(); + }, + ); + + handles.push(handle); + } + + for handle in handles { + handle + .await + .unwrap(); + } + + let success = *success_count + .lock() + .await; + let fail = *fail_count + .lock() + .await; + + let final_concurrent = adaptive + .lock() + .await + .get_concurrent(); + + progress_mgr.finish_with_message( + &format!( + "{} successful, {} failed", + success + .to_string() + .green(), + fail.to_string() + .red(), + ), + ); + + println!(); + println!( + "Album download complete. Final concurrency: {}", + final_concurrent + .to_string() + .yellow() + ); + + Ok(()) +} + +async fn download_artist(input: &str, _output_dir: &PathBuf, _quality: AudioQuality, _service: Service) -> Result<()> { + let tidal = TidalService::new(); + + Ui::print_info( + &format!( + "Searching for artist: {}", + input.cyan() + ), + ); + + let search_results = tidal + .search( + input, None, + ) + .await?; + + if search_results + .tracks + .is_empty() + { + return Err(DownloadError::ArtistNotFound(input.to_string())); + } + + Ui::print_search_results( + &search_results.tracks, + 5, + ); + Ui::print_warning("Artist downloads are not fully implemented yet."); + Ui::print_info("Use 'track' command to download individual tracks."); + + Ok(()) +} + +async fn download_playlist(input: &str, output_dir: &PathBuf, quality: AudioQuality, _service: Service, max_concurrent: usize) -> Result<()> { + let tidal = TidalService::new(); + + let playlist_id = if input + .parse::() + .is_ok() + { + input + .parse::() + .unwrap() + } else { + return Err(DownloadError::PlaylistNotFound("Please provide a playlist ID".to_string())); + }; + + let playlist_info = tidal + .get_playlist_info(playlist_id) + .await?; + Ui::print_playlist_detail(&playlist_info); + + if !Ui::confirm("Download this playlist?") { + Ui::print_info("Cancelled"); + return Ok(()); + } + + let tracks = tidal + .get_playlist_tracks(playlist_id) + .await?; + + let total_tracks = tracks.len(); + let progress_mgr = Arc::new( + SimpleProgressManager::new( + max_concurrent, + total_tracks as u64, + "Playlist", + ), + ); + + let output_dir = output_dir.clone(); + let adaptive = Arc::new(Mutex::new(AdaptiveConcurrency::new(max_concurrent))); + let semaphore = adaptive + .lock() + .await + .semaphore(); + let success_count = Arc::new(Mutex::new(0usize)); + let fail_count = Arc::new(Mutex::new(0usize)); + let slot_counter = Arc::new(AtomicUsize::new(0)); + + let mut handles = vec![]; + + for track in tracks { + let permit = acquire_slot(&semaphore).await; + let tidal = TidalService::new(); + let output_dir = output_dir.clone(); + let quality = quality; + let success_count = success_count.clone(); + let fail_count = fail_count.clone(); + let adaptive_inner = adaptive.clone(); + let progress_inner = progress_mgr.clone(); + let slot_counter_inner = slot_counter.clone(); + + let handle = tokio::spawn( + async move { + let _permit = permit; + let slot = slot_counter_inner.fetch_add( + 1, + Ordering::Relaxed, + ) % max_concurrent; + let downloader = DownloadManager::new(&output_dir); + + progress_inner.update_download( + slot, + format!( + "Matching: {} - {}", + track + .artist + .white(), + track + .title + .cyan() + ), + ); + + let result = retry_download( + || async { + let manifest = tidal + .get_manifest( + track.id, quality, + ) + .await?; + downloader + .download_from_manifest( + &manifest, &track, + ) + .await + }, + adaptive_inner.clone(), + ) + .await; + + match result { + Ok(_) => { + adaptive_inner + .lock() + .await + .on_success(); + *success_count + .lock() + .await += 1; + progress_inner.update_download( + slot, + format!( + "✓ {} - {}", + track + .artist + .white(), + track + .title + .green() + ), + ); + } + Err(e) => { + adaptive_inner + .lock() + .await + .on_failure(); + *fail_count + .lock() + .await += 1; + progress_inner.update_download( + slot, + format!( + "✗ {}: {}", + track + .title + .white(), + e.to_string() + .dimmed() + ), + ); + } + } + progress_inner.inc_total(); + }, + ); + + handles.push(handle); + } + + for handle in handles { + handle + .await + .unwrap(); + } + + let success = *success_count + .lock() + .await; + let fail = *fail_count + .lock() + .await; + + let final_concurrent = adaptive + .lock() + .await + .get_concurrent(); + + progress_mgr.finish_with_message( + &format!( + "{} successful, {} failed", + success + .to_string() + .green(), + fail.to_string() + .red(), + ), + ); + + println!(); + println!( + "Playlist download complete. Final concurrency: {}", + final_concurrent + .to_string() + .yellow() + ); + + Ok(()) +} + +#[derive(Debug, Clone)] +struct DownloadTask { + track: crate::types::TrackInfo, + csv_track: csv::CsvTrack, + confidence: f64, + album: Option, +} + +#[derive(Debug, Clone)] +struct DownloadJob { + track: crate::types::TrackInfo, + album: Option, + playlist: Option, + dir_mode: DirMode, + retry_count: usize, + max_retries: usize, + last_error: Option, +} + +async fn download_from_csv(file: &PathBuf, threshold: f64, skip_confirm: bool, output_dir: &PathBuf, quality: AudioQuality, max_concurrent: usize, dir_mode: DirMode) -> Result<()> { + Ui::print_info( + &format!( + "Parsing CSV file: {}", + file.display() + .to_string() + .cyan() + ), + ); + + let csv_tracks = csv::parse_csv_file(file)?; + let total = csv_tracks.len(); + let progress_file = output_dir.join(".download_progress.json"); + let source_str = file + .display() + .to_string(); + + let (progress, resume) = if progress_file.exists() { + if let Ok(existing) = DownloadProgress::load_from_file(&progress_file) { + if existing.source == source_str { + let completed = existing.get_completed_count(); + let failed_matches = existing + .failed_matches + .len(); + println!(); + println!( + "{} Previous progress found: {}/{} tracks completed, {} failed matches", + "→".cyan(), + completed + .to_string() + .green(), + total + .to_string() + .white(), + failed_matches + .to_string() + .red() + ); + let resume_prompt = "Resume from where you left off?"; + if Ui::confirm(resume_prompt) { + ( + existing, true, + ) + } else { + let _ = std::fs::remove_file(&progress_file); + ( + DownloadProgress::new( + &source_str, + total, + ), + false, + ) + } + } else { + ( + DownloadProgress::new( + &source_str, + total, + ), + false, + ) + } + } else { + ( + DownloadProgress::new( + &source_str, + total, + ), + false, + ) + } + } else { + ( + DownloadProgress::new( + &source_str, + total, + ), + false, + ) + }; + + Ui::print_info( + &format!( + "Found {} tracks in CSV", + total + .to_string() + .green() + ), + ); + + let matched_indices: std::collections::HashSet = if resume { + progress + .matched_csv_indices + .iter() + .copied() + .collect() + } else { + std::collections::HashSet::new() + }; + + let completed_track_ids: std::collections::HashSet = if resume { + progress + .completed_track_ids + .iter() + .copied() + .collect() + } else { + std::collections::HashSet::new() + }; + + Ui::print_info( + &format!( + "Matching with adaptive concurrency (max {})...", + max_concurrent + .to_string() + .cyan() + ), + ); + + let progress_mgr = Arc::new( + ProgressManager::new( + max_concurrent, + total as u64, + ), + ); + let matcher = Arc::new(CsvMatcher::new()); + let adaptive = Arc::new(Mutex::new(AdaptiveConcurrency::new(max_concurrent))); + let semaphore = adaptive + .lock() + .await + .semaphore(); + let match_results = Arc::new(Mutex::new(Vec::new())); + let progress_counter = Arc::new(Mutex::new(0usize)); + let consecutive_failures = Arc::new(Mutex::new(0usize)); + let failed_match_indices = Arc::new(Mutex::new(Vec::::new())); + let matched_indices_shared = Arc::new(Mutex::new(matched_indices.clone())); + let progress_shared = Arc::new(Mutex::new(progress.clone())); + let csv_tracks_shared = Arc::new(csv_tracks.clone()); + let slot_counter = Arc::new(AtomicUsize::new(0)); + + let mut handles = vec![]; + + for (index, track) in csv_tracks + .clone() + .into_iter() + .enumerate() + { + if resume && matched_indices.contains(&index) { + let mut prog = progress_counter + .lock() + .await; + *prog += 1; + progress_mgr.inc_matched(); + continue; + } + + let permit = acquire_slot(&semaphore).await; + let matcher = matcher.clone(); + let match_results = match_results.clone(); + let progress_counter = progress_counter.clone(); + let consecutive_failures = consecutive_failures.clone(); + let failed_match_indices = failed_match_indices.clone(); + let matched_indices_inner = matched_indices_shared.clone(); + let progress_inner = progress_shared.clone(); + let csv_tracks_inner = csv_tracks_shared.clone(); + let track_name = track + .track_name + .clone(); + let progress_mgr_inner = progress_mgr.clone(); + let slot_counter_inner = slot_counter.clone(); + + let handle = tokio::spawn( + async move { + let _permit = permit; + let slot = slot_counter_inner.fetch_add( + 1, + Ordering::Relaxed, + ) % max_concurrent; + progress_mgr_inner.update_download( + slot, + format!( + "Matching: {}", + track_name.white() + ), + ); + + let result = matcher + .find_best_match(&track) + .await + .unwrap_or_else( + |e| CsvMatchResult { + csv_track: track.clone(), + best_match: None, + confidence: 0.0, + search_attempts: vec![ + format!( + "Error: {}", + e + ), + ], + }, + ); + + let is_match = result + .best_match + .is_some(); + + if !is_match { + let mut failures = consecutive_failures + .lock() + .await; + *failures += 1; + + if *failures >= CONSECUTIVE_FAILURE_THRESHOLD { + drop(failures); + progress_mgr_inner.update_download( + slot, + format!( + "Rate limited, waiting {}s...", + RATE_LIMIT_DELAY_MS / 1000 + ), + ); + tokio::time::sleep(Duration::from_millis(RATE_LIMIT_DELAY_MS)).await; + } else { + drop(failures); + tokio::time::sleep(Duration::from_millis(NOT_FOUND_DELAY_MS)).await; + } + + failed_match_indices + .lock() + .await + .push(index); + } else { + *consecutive_failures + .lock() + .await = 0; + matched_indices_inner + .lock() + .await + .insert(index); + + if let Some(ref matched_track) = result.best_match { + let csv_track = &csv_tracks_inner[index]; + progress_inner + .lock() + .await + .add_matched_track( + index, + matched_track, + result.confidence, + &csv_track.track_name, + &csv_track.artist_name, + csv_track + .album + .as_deref(), + csv_track + .playlist_name + .as_deref(), + ); + } + } + + let status = if is_match { + if result.confidence >= threshold { + format!( + "✓ [{:.0}%] {}", + result.confidence * 100.0, + track_name.green() + ) + } else { + format!( + "? [{:.0}%] {}", + result.confidence * 100.0, + track_name.yellow() + ) + } + } else { + format!( + "✗ Not found: {}", + track_name.red() + ) + }; + + let mut prog = progress_counter + .lock() + .await; + *prog += 1; + progress_mgr_inner.update_download( + slot, status, + ); + progress_mgr_inner.inc_matched(); + + match_results + .lock() + .await + .push( + ( + index, result, + ), + ); + }, + ); + + handles.push(handle); + } + + for handle in handles { + handle + .await + .unwrap(); + } + + let match_results_inner = Arc::try_unwrap(match_results) + .unwrap() + .into_inner(); + let match_results_sorted: Vec<( + usize, + CsvMatchResult, + )> = match_results_inner; + let mut match_results: Vec> = vec![None; total]; + + for (index, result) in match_results_sorted { + match_results[index] = Some(result); + } + + if resume { + for m in &progress.matched_tracks { + let csv_index = m.csv_index; + if match_results[csv_index].is_none() { + match_results[csv_index] = Some( + CsvMatchResult { + csv_track: csv::CsvTrack { + track_name: m + .csv_track_name + .clone(), + artist_name: m + .csv_artist_name + .clone(), + album: m + .csv_album + .clone(), + playlist_name: m + .playlist_name + .clone(), + track_type: None, + isrc: None, + spotify_id: None, + }, + best_match: Some( + crate::types::TrackInfo { + id: m.track_id, + title: m + .track_title + .clone(), + artist: m + .track_artist + .clone(), + album: m + .album + .clone(), + duration: None, + quality: None, + cover_url: None, + }, + ), + confidence: m.confidence, + search_attempts: vec![], + }, + ); + } + } + } + + let mut match_results: Vec = match_results + .into_iter() + .enumerate() + .map( + |(index, opt)| { + opt.unwrap_or_else( + || CsvMatchResult { + csv_track: csv_tracks[index].clone(), + best_match: None, + confidence: 0.0, + search_attempts: vec![], + }, + ) + }, + ) + .collect(); + + let failed_indices = Arc::try_unwrap(failed_match_indices) + .unwrap() + .into_inner(); + + for idx in &failed_indices { + let track = &csv_tracks[*idx]; + progress_shared + .lock() + .await + .add_failed_match( + &track.track_name, + Some(&track.artist_name), + track + .album + .as_deref(), + ); + } + + { + let mut prog = progress_shared + .lock() + .await; + prog.total_matched = prog + .matched_csv_indices + .len(); + let _ = prog.save_to_file(&progress_file); + } + + if !failed_indices.is_empty() { + let retry_spinner = ProgressBar::new_spinner(); + retry_spinner.set_style(ProgressStyle::default_spinner()); + retry_spinner.set_message( + format!( + "Retrying {} failed matches...", + failed_indices.len() + ), + ); + + let retry_match_results = Arc::new(Mutex::new(Vec::new())); + let retry_progress = Arc::new(Mutex::new(0usize)); + + for idx in &failed_indices { + let csv_track = csv_tracks[*idx].clone(); + let tidal = TidalService::new(); + let retry_match_results = retry_match_results.clone(); + let retry_progress = retry_progress.clone(); + + let cleaned_name = clean_track_name(&csv_track.track_name); + retry_spinner.set_message( + format!( + "Retrying: '{}' by '{}'", + cleaned_name, csv_track.artist_name + ), + ); + retry_spinner.tick(); + + let mut candidates: Vec = Vec::new(); + + let query1 = format!( + "{} {}", + cleaned_name, csv_track.artist_name + ); + if let Ok(results) = tidal + .search( + &query1, None, + ) + .await + { + candidates.extend(results.tracks); + tokio::time::sleep(Duration::from_millis(300)).await; + } + + if let Ok(results) = tidal + .search( + &cleaned_name, + Some(&csv_track.artist_name), + ) + .await + { + candidates.extend(results.tracks); + tokio::time::sleep(Duration::from_millis(300)).await; + } + + if let Ok(results) = tidal + .search( + &cleaned_name, + None, + ) + .await + { + candidates.extend(results.tracks); + } + + candidates.sort_by_key(|t| t.id); + candidates.dedup_by_key(|t| t.id); + + let best = csv::CsvMatcher::score_and_select_best( + &candidates, + &csv_track, + ); + + let result = CsvMatchResult { + csv_track: csv_track.clone(), + best_match: best + .as_ref() + .map(|(t, _)| t.clone()), + confidence: best + .map(|(_, s)| s) + .unwrap_or(0.0), + search_attempts: vec![ + query1, + cleaned_name.clone(), + ], + }; + + let mut prog = retry_progress + .lock() + .await; + *prog += 1; + + if result + .best_match + .is_some() + { + let mut prog = progress_shared + .lock() + .await; + prog.total_matched += 1; + prog.mark_csv_matched(*idx); + if let Some(ref track) = result.best_match { + let csv_track = &csv_tracks[*idx]; + prog.add_matched_track( + *idx, + track, + result.confidence, + &csv_track.track_name, + &csv_track.artist_name, + csv_track + .album + .as_deref(), + csv_track + .playlist_name + .as_deref(), + ); + } + } + + retry_match_results + .lock() + .await + .push( + ( + *idx, result, + ), + ); + } + + retry_spinner.finish_with_message("Retry complete"); + + let retry_results = Arc::try_unwrap(retry_match_results) + .unwrap() + .into_inner(); + + for (idx, result) in retry_results { + match_results[idx] = result; + } + } + + progress_mgr.clear(); + + csv::print_csv_summary(&match_results); + + let valid_matches: Vec<( + usize, + &CsvMatchResult, + )> = match_results + .iter() + .enumerate() + .filter( + |(_, r)| { + r.best_match + .is_some() + && r.confidence >= threshold + }, + ) + .collect(); + + if valid_matches.is_empty() { + Ui::print_warning("No tracks matched with sufficient confidence."); + progress_shared + .lock() + .await + .save_to_file(&progress_file)?; + return Ok(()); + } + + let skipped = match_results.len() - valid_matches.len(); + if skipped > 0 { + Ui::print_warning( + &format!( + "{} tracks skipped (below {:.0}% confidence threshold)", + skipped, + threshold * 100.0 + ), + ); + } + + let tracks_to_download: Vec<_> = valid_matches + .iter() + .filter( + |(_, r)| { + if let Some(ref track) = r.best_match { + !completed_track_ids.contains(&track.id) + } else { + false + } + }, + ) + .collect(); + + let already_completed = valid_matches.len() - tracks_to_download.len(); + if already_completed > 0 && resume { + println!( + "{} {} tracks already downloaded, skipping...", + "→".cyan(), + already_completed + .to_string() + .green() + ); + } + + if tracks_to_download.is_empty() { + Ui::print_info("All tracks already downloaded."); + progress_shared + .lock() + .await + .save_to_file(&progress_file)?; + return Ok(()); + } + + println!(); + if !skip_confirm { + if !Ui::confirm( + &format!( + "Download {} tracks with adaptive concurrency?", + tracks_to_download.len() + ), + ) { + Ui::print_info("Cancelled"); + progress_shared + .lock() + .await + .save_to_file(&progress_file)?; + return Ok(()); + } + } + + Ui::print_info( + &format!( + "Downloading {} tracks with parallel retry queue (max {})...", + tracks_to_download + .len() + .to_string() + .green(), + max_concurrent + .to_string() + .cyan() + ), + ); + + progress_mgr.reset_for_download(tracks_to_download.len() as u64); + progress_mgr.set_stage("2/2 - Downloading"); + + let output_dir = output_dir.clone(); + let download_adaptive = Arc::new(Mutex::new(AdaptiveConcurrency::new(max_concurrent))); + let success_count = Arc::new(Mutex::new(0usize)); + let final_fail_count = Arc::new(Mutex::new(0usize)); + + let queue: Arc>> = Arc::new(Mutex::new(std::collections::VecDeque::new())); + let in_flight = Arc::new(Mutex::new(0usize)); + let completed = Arc::new(Mutex::new(0usize)); + let failed_jobs = Arc::new( + Mutex::new( + Vec::<( + DownloadJob, + String, + )>::new(), + ), + ); + let progress_file_shared = progress_file.clone(); + let download_slot_counter = Arc::new(AtomicUsize::new(0)); + + for (_, result) in &tracks_to_download { + if let Some(ref track) = result.best_match { + let job = DownloadJob { + track: track.clone(), + album: result + .csv_track + .album + .clone(), + playlist: result + .csv_track + .playlist_name + .clone(), + dir_mode, + retry_count: 0, + max_retries: 5, + last_error: None, + }; + queue + .lock() + .await + .push_back(job); + } + } + + let mut worker_handles = vec![]; + + for _worker_id in 0..max_concurrent { + let queue = queue.clone(); + let in_flight = in_flight.clone(); + let completed = completed.clone(); + let success_count = success_count.clone(); + let final_fail_count = final_fail_count.clone(); + let failed_jobs = failed_jobs.clone(); + let download_adaptive = download_adaptive.clone(); + let output_dir = output_dir.clone(); + let quality = quality; + let progress_shared = progress_shared.clone(); + let progress_file_inner = progress_file_shared.clone(); + let progress_mgr_inner = progress_mgr.clone(); + let download_slot_counter_inner = download_slot_counter.clone(); + + let handle = tokio::spawn( + async move { + loop { + let job_opt = { + let mut q = queue + .lock() + .await; + q.pop_front() + }; + + let job = match job_opt { + Some(j) => j, + None => { + let in_flight_val = *in_flight + .lock() + .await; + if in_flight_val == 0 { + break; + } + tokio::time::sleep(Duration::from_millis(50)).await; + continue; + } + }; + + *in_flight + .lock() + .await += 1; + + let slot = download_slot_counter_inner.fetch_add( + 1, + Ordering::Relaxed, + ) % max_concurrent; + let tidal = TidalService::new(); + let downloader = DownloadManager::new(&output_dir) + .with_album( + job.album + .as_deref(), + ) + .with_playlist( + job.playlist + .as_deref(), + ) + .with_dir_mode(job.dir_mode); + + progress_mgr_inner.set_downloading( + slot, + &job.track + .artist, + &job.track + .title, + ); + + let progress_mgr_clone = progress_mgr_inner.clone(); + let slot_for_callback = slot; + let result = async { + let manifest = tidal + .get_manifest( + job.track + .id, + quality, + ) + .await?; + downloader + .download_from_manifest_with_progress( + &manifest, + &job.track, + |progress| { + progress_mgr_clone.update_download_progress( + slot_for_callback, + progress, + ); + }, + ) + .await + } + .await; + + match result { + Ok(_) => { + download_adaptive + .lock() + .await + .on_success(); + *success_count + .lock() + .await += 1; + *completed + .lock() + .await += 1; + *in_flight + .lock() + .await -= 1; + + { + let mut prog = progress_shared + .lock() + .await; + prog.mark_completed( + job.track + .id, + ); + let _ = prog.save_to_file(&progress_file_inner); + } + + progress_mgr_inner.finish_download( + slot, + true, + &job.track + .artist, + &job.track + .title, + ); + progress_mgr_inner.inc_downloaded(); + } + Err(e) => { + let error_str = e.to_string(); + let is_retryable = matches!( + e, + DownloadError::RateLimited | DownloadError::NetworkError(_) | DownloadError::DownloadFailed(_) | DownloadError::SegmentDownloadFailed(_) | DownloadError::ServiceUnavailable(_) + ); + + download_adaptive + .lock() + .await + .on_failure(); + + if is_retryable && job.retry_count + 1 < job.max_retries { + let delay = Duration::from_secs( + RETRY_BACKOFF_SECS[job + .retry_count + .min(4) as usize], + ); + progress_mgr_inner.update_download( + slot, + format!( + "Retrying: {} (attempt {}/{})", + job.track + .title + .yellow(), + job.retry_count + 1, + job.max_retries + ), + ); + tokio::time::sleep(delay).await; + + let retry_job = DownloadJob { + track: job.track, + album: job.album, + playlist: job.playlist, + dir_mode: job.dir_mode, + retry_count: job.retry_count + 1, + max_retries: job.max_retries, + last_error: Some(error_str), + }; + queue + .lock() + .await + .push_back(retry_job); + *in_flight + .lock() + .await -= 1; + } else { + *completed + .lock() + .await += 1; + *in_flight + .lock() + .await -= 1; + *final_fail_count + .lock() + .await += 1; + failed_jobs + .lock() + .await + .push( + ( + job.clone(), + error_str.clone(), + ), + ); + progress_shared + .lock() + .await + .add_failure( + &job.track, + job.album + .as_deref(), + error_str.clone(), + ); + progress_mgr_inner.finish_download( + slot, + false, + &job.track + .title, + &error_str, + ); + progress_mgr_inner.inc_downloaded(); + } + } + } + + tokio::time::sleep(Duration::from_millis(100)).await; + } + }, + ); + + worker_handles.push(handle); + } + + for handle in worker_handles { + let _ = handle.await; + } + + let _success = *success_count + .lock() + .await; + let fail = *final_fail_count + .lock() + .await; + let failed_list = Arc::try_unwrap(failed_jobs) + .unwrap() + .into_inner(); + + let mut progress = Arc::try_unwrap(progress_shared) + .unwrap() + .into_inner(); + progress.total_downloaded = progress.get_completed_count(); + progress.successful = progress.get_completed_count(); + + progress.save_to_file(&progress_file)?; + + let final_concurrent = download_adaptive + .lock() + .await + .get_concurrent(); + + progress_mgr.finish(); + + println!(); + println!( + "{}", + "═" + .repeat(60) + .bright_blue() + ); + println!( + "{}", + "Download Summary" + .cyan() + .bold() + ); + println!( + "{}", + "═" + .repeat(60) + .bright_blue() + ); + println!( + " {} {}", + "Total tracks:".dimmed(), + total + .to_string() + .white() + ); + println!( + " {} {}", + "Successfully matched:".green(), + progress + .total_matched + .to_string() + .green() + ); + + let failed_matches_count = progress + .failed_matches + .len(); + if failed_matches_count > 0 { + println!( + " {} {}", + "Failed matches:".red(), + failed_matches_count + .to_string() + .red() + ); + for fm in &progress.failed_matches { + println!( + " {} {}", + "•".red(), + fm.track_name + .white() + ); + } + } + + println!( + " {} {}", + "Successfully downloaded:".green(), + progress + .get_completed_count() + .to_string() + .green() + ); + + if fail > 0 { + println!( + " {} {}", + "Failed downloads:".red(), + fail.to_string() + .red() + ); + for (job, error) in &failed_list { + println!( + " {} {} - {}", + "•".red(), + job.track + .title + .white(), + error.dimmed() + ); + } + } + + println!( + " {} {}", + "Final concurrency:".dimmed(), + final_concurrent + .to_string() + .yellow() + ); + println!( + "{}", + "═" + .repeat(60) + .bright_blue() + ); + println!( + "{} {}", + "Progress saved to:".dimmed(), + progress_file + .display() + .to_string() + .white() + ); + + Ok(()) +} + +fn clean_track_name(name: &str) -> String { + let cleaned: String = name + .chars() + .filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-') + .collect(); + let result = cleaned + .split_whitespace() + .collect::>() + .join(" "); + + let removed_brackets = Regex::new(r"\s*[\(\[][^)\]]*[\)\]]\s*").unwrap(); + removed_brackets + .replace_all( + &result, " ", + ) + .trim() + .to_string() +} + +async fn retry_download(f: F, adaptive: Arc>) -> Result +where + F: Fn() -> Fut, + Fut: std::future::Future>, +{ + const MAX_RETRIES: usize = 5; + const BACKOFF_DELAYS: [Duration; 5] = [ + Duration::from_secs(5), + Duration::from_secs(15), + Duration::from_secs(30), + Duration::from_secs(60), + Duration::from_secs(120), + ]; + + let mut last_error = None; + + for attempt in 0..MAX_RETRIES { + match f().await { + Ok(result) => return Ok(result), + Err(e) => { + let should_retry = matches!( + e, + DownloadError::RateLimited | DownloadError::NetworkError(_) | DownloadError::DownloadFailed(_) | DownloadError::SegmentDownloadFailed(_) | DownloadError::ServiceUnavailable(_) + ); + + if !should_retry || attempt == MAX_RETRIES - 1 { + return Err(e); + } + + if should_retry { + adaptive + .lock() + .await + .on_failure(); + } + + last_error = Some(e); + tokio::time::sleep(BACKOFF_DELAYS[attempt]).await; + } + } + } + + Err(last_error.unwrap_or(DownloadError::DownloadFailed("Max retries exceeded".to_string()))) +} diff --git a/src/progress.rs b/src/progress.rs new file mode 100644 index 0000000..8822bdb --- /dev/null +++ b/src/progress.rs @@ -0,0 +1,262 @@ +use crate::{error::Result, types::TrackInfo}; +use chrono::Utc; +use serde::{Deserialize, Serialize}; +use std::path::Path; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct MatchedTrack { + #[serde(rename = "trackId")] + pub track_id: u64, + #[serde(rename = "trackTitle")] + pub track_title: String, + #[serde(rename = "trackArtist")] + pub track_artist: String, + pub album: Option, + pub confidence: f64, + #[serde(rename = "csvIndex")] + pub csv_index: usize, + #[serde(rename = "csvTrackName")] + pub csv_track_name: String, + #[serde(rename = "csvArtistName")] + pub csv_artist_name: String, + #[serde(rename = "csvAlbum")] + pub csv_album: Option, + #[serde(rename = "playlistName")] + pub playlist_name: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FailedMatch { + #[serde(rename = "trackName")] + pub track_name: String, + pub artist: Option, + pub album: Option, + #[serde(rename = "searchAttempts")] + pub search_attempts: Vec, + pub attempts: usize, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct CsvTrackInfo { + #[serde(rename = "trackName")] + pub track_name: String, + pub artist: Option, + pub album: Option, + pub isrc: Option, + #[serde(rename = "spotifyId")] + pub spotify_id: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FailedDownload { + #[serde(rename = "trackId")] + pub track_id: u64, + #[serde(rename = "trackTitle")] + pub track_title: String, + #[serde(rename = "trackArtist")] + pub track_artist: String, + pub album: Option, + pub error: String, + pub attempts: usize, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DownloadProgress { + pub source: String, + pub total: usize, + pub successful: usize, + pub failed: Vec, + #[serde(rename = "startedAt")] + pub started_at: String, + #[serde(rename = "completedAt")] + pub completed_at: Option, + #[serde(rename = "failedMatches")] + pub failed_matches: Vec, + #[serde(rename = "totalMatched")] + pub total_matched: usize, + #[serde(rename = "totalDownloaded")] + pub total_downloaded: usize, + #[serde(rename = "completedTrackIds")] + pub completed_track_ids: Vec, + #[serde(rename = "matchedCsvIndices")] + pub matched_csv_indices: Vec, + #[serde(rename = "matchedTracks")] + pub matched_tracks: Vec, +} + +impl DownloadProgress { + pub fn new(source: &str, total: usize) -> Self { + Self { + source: source.to_string(), + total, + successful: 0, + failed: Vec::new(), + started_at: Utc::now().to_rfc3339(), + completed_at: None, + failed_matches: Vec::new(), + total_matched: 0, + total_downloaded: 0, + completed_track_ids: Vec::new(), + matched_csv_indices: Vec::new(), + matched_tracks: Vec::new(), + } + } + + pub fn mark_completed(&mut self, track_id: u64) { + if !self + .completed_track_ids + .contains(&track_id) + { + self.completed_track_ids + .push(track_id); + } + } + + pub fn is_completed(&self, track_id: u64) -> bool { + self.completed_track_ids + .contains(&track_id) + } + + pub fn get_completed_count(&self) -> usize { + self.completed_track_ids + .len() + } + + pub fn mark_csv_matched(&mut self, index: usize) { + if !self + .matched_csv_indices + .contains(&index) + { + self.matched_csv_indices + .push(index); + } + } + + pub fn is_csv_matched(&self, index: usize) -> bool { + self.matched_csv_indices + .contains(&index) + } + + pub fn add_matched_track(&mut self, csv_index: usize, track: &TrackInfo, confidence: f64, csv_track_name: &str, csv_artist: &str, csv_album: Option<&str>, playlist: Option<&str>) { + self.matched_tracks + .push( + MatchedTrack { + track_id: track.id, + track_title: track + .title + .clone(), + track_artist: track + .artist + .clone(), + album: track + .album + .clone(), + confidence, + csv_index, + csv_track_name: csv_track_name.to_string(), + csv_artist_name: csv_artist.to_string(), + csv_album: csv_album.map(|s| s.to_string()), + playlist_name: playlist.map(|s| s.to_string()), + }, + ); + self.mark_csv_matched(csv_index); + } + + pub fn get_matched_track(&self, csv_index: usize) -> Option<&MatchedTrack> { + self.matched_tracks + .iter() + .find(|m| m.csv_index == csv_index) + } + + pub fn add_success(&mut self) { + self.successful += 1; + } + + pub fn add_failure(&mut self, track: &TrackInfo, album: Option<&str>, error: String) { + self.failed + .push( + FailedDownload { + track_id: track.id, + track_title: track + .title + .clone(), + track_artist: track + .artist + .clone(), + album: album.map(|s| s.to_string()), + error, + attempts: 1, + }, + ); + } + + pub fn add_failed_match(&mut self, track_name: &str, artist: Option<&str>, album: Option<&str>) { + if let Some(existing) = self + .failed_matches + .iter_mut() + .find( + |m| { + m.track_name == track_name + && m.artist + .as_deref() + == artist + && m.album + .as_deref() + == album + }, + ) + { + existing.attempts += 1; + } else { + self.failed_matches + .push( + FailedMatch { + track_name: track_name.to_string(), + artist: artist.map(|s| s.to_string()), + album: album.map(|s| s.to_string()), + search_attempts: Vec::new(), + attempts: 1, + }, + ); + } + } + + pub fn get_failed_matches(&self) -> &[FailedMatch] { + &self.failed_matches + } + + pub fn total_failed(&self) -> usize { + self.failed_matches + .len() + + self + .failed + .len() + } + + pub fn save_to_file(&self, path: &Path) -> Result<()> { + let json = serde_json::to_string_pretty(self)?; + std::fs::write( + path, json, + )?; + Ok(()) + } + + pub fn load_from_file(path: &Path) -> Result { + let content = std::fs::read_to_string(path)?; + let progress: Self = serde_json::from_str(&content)?; + Ok(progress) + } + + pub fn has_failures(&self) -> bool { + !self + .failed + .is_empty() + } + + pub fn retry_count(&self) -> usize { + self.failed + .iter() + .map(|f| f.attempts) + .sum() + } +} diff --git a/src/services/mod.rs b/src/services/mod.rs new file mode 100644 index 0000000..4691028 --- /dev/null +++ b/src/services/mod.rs @@ -0,0 +1,21 @@ +pub mod tidal; + +use crate::{error::Result, types::*}; +use async_trait::async_trait; + +#[async_trait] +pub trait MusicService: Send + Sync { + async fn search(&self, query: &str, artist: Option<&str>) -> Result; + + async fn get_track_info(&self, track_id: u64) -> Result; + + async fn get_album_info(&self, album_id: u64) -> Result; + + async fn get_playlist_info(&self, playlist_id: u64) -> Result; + + async fn get_album_tracks(&self, album_id: u64) -> Result>; + + async fn get_playlist_tracks(&self, playlist_id: u64) -> Result>; + + async fn get_manifest(&self, track_id: u64, quality: AudioQuality) -> Result; +} diff --git a/src/services/tidal.rs b/src/services/tidal.rs new file mode 100644 index 0000000..a3c6781 --- /dev/null +++ b/src/services/tidal.rs @@ -0,0 +1,524 @@ +use crate::{ + error::{DownloadError, Result}, + services::MusicService, + types::*, +}; +use async_trait::async_trait; +use base64::{engine::general_purpose::STANDARD as BASE64, Engine}; +use regex::Regex; +use reqwest::Client; +use serde::Deserialize; + +const API_BASE: &str = "https://triton.squid.wtf"; +const FALLBACK_APIS: &[&str] = &[ + "https://aether.squid.wtf", + "https://zeus.squid.wtf", +]; + +#[derive(Debug, Deserialize)] +struct ApiResponse { + data: T, +} + +#[derive(Debug, Deserialize)] +struct SearchResponse { + items: Vec, +} + +#[derive(Debug, Deserialize)] +struct TidalTrack { + id: u64, + title: String, + #[serde(default)] + artist: Option, + #[serde(default)] + album: Option, + #[serde(default)] + duration: Option, + #[serde( + rename = "audioQuality", + default + )] + audio_quality: Option, +} + +#[derive(Debug, Deserialize)] +struct TidalArtist { + name: String, +} + +#[derive(Debug, Deserialize)] +struct TidalAlbum { + title: String, +} + +#[derive(Debug, Deserialize)] +struct TrackInfoData { + id: u64, + title: String, + #[serde(default)] + artist: Option, + #[serde(default)] + album: Option, + #[serde(default)] + duration: Option, + #[serde( + rename = "audioQuality", + default + )] + audio_quality: Option, +} + +#[derive(Debug, Deserialize)] +struct TrackManifestData { + #[serde(default)] + manifest: Option, + #[serde( + rename = "bitDepth", + default + )] + bit_depth: Option, + #[serde( + rename = "sampleRate", + default + )] + sample_rate: Option, +} + +#[derive(Debug, Deserialize)] +struct AlbumData { + id: u64, + title: String, + #[serde(default)] + artist: Option, + #[serde( + rename = "numberOfTracks", + default + )] + number_of_tracks: Option, +} + +#[derive(Debug, Deserialize)] +struct PlaylistData { + id: u64, + title: String, + #[serde(default)] + creator: Option, + #[serde( + rename = "numberOfTracks", + default + )] + number_of_tracks: Option, +} + +#[derive(Debug, Deserialize)] +struct TidalCreator { + name: String, +} + +#[derive(Debug, Deserialize)] +struct ManifestJson { + #[serde(default)] + urls: Vec, +} + +#[derive(Clone)] +pub struct TidalService { + client: Client, + api_index: usize, +} + +impl TidalService { + pub fn new() -> Self { + TidalService { + client: Client::builder() + .user_agent("SquidDownloader/0.1.0") + .build() + .unwrap(), + api_index: 0, + } + } + + fn get_api_base(&self) -> &str { + if self.api_index == 0 { + API_BASE + } else { + FALLBACK_APIS[self.api_index - 1] + } + } + + fn parse_quality(quality_str: &str) -> AudioQuality { + match quality_str + .to_uppercase() + .as_str() + { + "HI_RES_LOSSLESS" | "HI_RES" => AudioQuality::HiResLossless, + "LOSSLESS" => AudioQuality::Lossless, + "HIGH" => AudioQuality::High, + "LOW" => AudioQuality::Low, + _ => AudioQuality::Auto, + } + } + + fn map_track_to_info(track: TidalTrack) -> TrackInfo { + TrackInfo { + id: track.id, + title: track.title, + artist: track + .artist + .map(|a| a.name) + .unwrap_or_default(), + album: track + .album + .map(|a| a.title), + duration: track.duration, + quality: track + .audio_quality + .map(|q| Self::parse_quality(&q)), + cover_url: None, + } + } + + fn parse_mpd_manifest(mpd_xml: &str) -> Result> { + let mut segment_urls = Vec::new(); + let mut base_url: Option = None; + + let re = Regex::new(r"([^<]+)").unwrap(); + if let Some(caps) = re.captures(mpd_xml) { + base_url = Some(caps[1].to_string()); + } + + let segment_re = Regex::new(r#"media="([^"]+)""#).unwrap(); + for caps in segment_re.captures_iter(mpd_xml) { + let segment = caps[1].to_string(); + if let Some(ref base) = base_url { + let full_url = if base.ends_with('/') { + format!( + "{}{}", + base, segment + ) + } else { + format!( + "{}/{}", + base, segment + ) + }; + segment_urls.push(full_url); + } else { + segment_urls.push(segment); + } + } + + if segment_urls.is_empty() { + let seg_pattern = r#"]*media="([^"]+)""#; + let seg_re = Regex::new(seg_pattern).unwrap(); + for caps in seg_re.captures_iter(mpd_xml) { + segment_urls.push(caps[1].to_string()); + } + } + + if segment_urls.is_empty() { + return Err(DownloadError::XmlParseError("Could not parse segment URLs from MPD manifest".to_string())); + } + + Ok(segment_urls) + } + + async fn make_request Deserialize<'de>>(&self, endpoint: &str) -> Result { + let url = format!( + "{}{}", + self.get_api_base(), + endpoint + ); + + let response = self + .client + .get(&url) + .send() + .await?; + + if response.status() == 429 { + return Err(DownloadError::RateLimited); + } + + let status = response.status(); + if !status.is_success() { + return Err( + DownloadError::ServiceUnavailable( + format!( + "API returned status {}", + status + ), + ), + ); + } + + let text = response + .text() + .await?; + + serde_json::from_str(&text).map_err(|e| DownloadError::JsonParseError(e)) + } +} + +impl Default for TidalService { + fn default() -> Self { + Self::new() + } +} + +#[async_trait] +impl MusicService for TidalService { + async fn search(&self, query: &str, artist: Option<&str>) -> Result { + let search_query = match artist { + Some(a) => format!( + "{} {}", + query, a + ), + None => query.to_string(), + }; + + let encoded = urlencoding::encode(&search_query); + let endpoint = format!( + "/search/?s={}", + encoded + ); + + let response: ApiResponse = self + .make_request(&endpoint) + .await?; + + let tracks: Vec = response + .data + .items + .into_iter() + .map(Self::map_track_to_info) + .collect(); + + let filtered_tracks = if let Some(artist_name) = artist { + tracks + .into_iter() + .filter( + |t| { + strsim::jaro_winkler( + t.artist + .to_lowercase() + .as_str(), + artist_name + .to_lowercase() + .as_str(), + ) > 0.7 + }, + ) + .collect() + } else { + tracks + }; + + Ok( + SearchResults { + tracks: filtered_tracks, + }, + ) + } + + async fn get_track_info(&self, track_id: u64) -> Result { + let endpoint = format!( + "/info/?id={}", + track_id + ); + let response: ApiResponse = self + .make_request(&endpoint) + .await?; + + Ok( + TrackInfo { + id: response + .data + .id, + title: response + .data + .title, + artist: response + .data + .artist + .map(|a| a.name) + .unwrap_or_default(), + album: response + .data + .album + .map(|a| a.title), + duration: response + .data + .duration, + quality: response + .data + .audio_quality + .map(|q| Self::parse_quality(&q)), + cover_url: None, + }, + ) + } + + async fn get_album_info(&self, album_id: u64) -> Result { + let endpoint = format!( + "/album/?id={}", + album_id + ); + let response: ApiResponse = self + .make_request(&endpoint) + .await?; + + Ok( + AlbumInfo { + id: response + .data + .id, + title: response + .data + .title, + artist: response + .data + .artist + .map(|a| a.name) + .unwrap_or_default(), + track_count: response + .data + .number_of_tracks + .unwrap_or(0), + year: None, + cover_url: None, + }, + ) + } + + async fn get_playlist_info(&self, playlist_id: u64) -> Result { + let endpoint = format!( + "/playlist/?id={}", + playlist_id + ); + let response: ApiResponse = self + .make_request(&endpoint) + .await?; + + Ok( + PlaylistInfo { + id: response + .data + .id, + title: response + .data + .title, + creator: response + .data + .creator + .map(|c| c.name) + .unwrap_or_default(), + track_count: response + .data + .number_of_tracks + .unwrap_or(0), + cover_url: None, + }, + ) + } + + async fn get_album_tracks(&self, album_id: u64) -> Result> { + let endpoint = format!( + "/album/tracks/?id={}", + album_id + ); + let response: ApiResponse = self + .make_request(&endpoint) + .await?; + + Ok( + response + .data + .items + .into_iter() + .map(Self::map_track_to_info) + .collect(), + ) + } + + async fn get_playlist_tracks(&self, playlist_id: u64) -> Result> { + let endpoint = format!( + "/playlist/tracks/?id={}", + playlist_id + ); + let response: ApiResponse = self + .make_request(&endpoint) + .await?; + + Ok( + response + .data + .items + .into_iter() + .map(Self::map_track_to_info) + .collect(), + ) + } + + async fn get_manifest(&self, track_id: u64, quality: AudioQuality) -> Result { + let endpoint = format!( + "/track/?id={}&quality={}", + track_id, + quality.as_str() + ); + let response: ApiResponse = self + .make_request(&endpoint) + .await?; + + let manifest_b64 = response + .data + .manifest + .ok_or(DownloadError::NoDownloadUrl)?; + let manifest_decoded = BASE64.decode(&manifest_b64)?; + let manifest_str = String::from_utf8_lossy(&manifest_decoded); + + let bit_depth = response + .data + .bit_depth; + let sample_rate = response + .data + .sample_rate; + + if manifest_str.contains("application/dash+xml") || manifest_str.contains(" String { + url::form_urlencoded::byte_serialize(s.as_bytes()).collect() + } +} diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..5b26c7d --- /dev/null +++ b/src/types.rs @@ -0,0 +1,224 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)] +pub enum AudioQuality { + #[serde(rename = "HI_RES_LOSSLESS")] + HiResLossless, + #[serde(rename = "LOSSLESS")] + Lossless, + #[serde(rename = "HIGH")] + High, + #[serde(rename = "LOW")] + Low, + #[serde(rename = "MP3_320")] + Mp3_320, + #[default] + #[serde(rename = "AUTO")] + Auto, +} + +impl AudioQuality { + pub fn as_str(&self) -> &'static str { + match self { + AudioQuality::HiResLossless => "HI_RES_LOSSLESS", + AudioQuality::Lossless => "LOSSLESS", + AudioQuality::High => "HIGH", + AudioQuality::Low => "LOW", + AudioQuality::Mp3_320 => "MP3_320", + AudioQuality::Auto => "AUTO", + } + } + + pub fn description(&self) -> &'static str { + match self { + AudioQuality::HiResLossless => "24-bit/192kHz FLAC (Hi-Res)", + AudioQuality::Lossless => "16-bit/44.1kHz FLAC (CD Quality)", + AudioQuality::High => "320kbps AAC", + AudioQuality::Low => "96kbps AAC", + AudioQuality::Mp3_320 => "320kbps MP3", + AudioQuality::Auto => "Best available", + } + } + + pub fn file_extension(&self) -> &'static str { + match self { + AudioQuality::HiResLossless | AudioQuality::Lossless => "flac", + AudioQuality::High | AudioQuality::Low => "m4a", + AudioQuality::Mp3_320 => "mp3", + AudioQuality::Auto => "flac", + } + } + + pub fn all() -> Vec { + vec![ + AudioQuality::HiResLossless, + AudioQuality::Lossless, + AudioQuality::High, + AudioQuality::Low, + ] + } +} + +impl std::fmt::Display for AudioQuality { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + self.description() + ) + } +} + +impl std::str::FromStr for AudioQuality { + type Err = String; + + fn from_str(s: &str) -> Result { + match s + .to_uppercase() + .as_str() + { + "HI_RES_LOSSLESS" | "HIRES" | "HI-RES" | "MASTER" => Ok(AudioQuality::HiResLossless), + "LOSSLESS" | "FLAC" | "CD" => Ok(AudioQuality::Lossless), + "HIGH" | "AAC320" | "320" => Ok(AudioQuality::High), + "LOW" | "AAC96" | "96" => Ok(AudioQuality::Low), + "MP3_320" | "MP3" => Ok(AudioQuality::Mp3_320), + "AUTO" => Ok(AudioQuality::Auto), + _ => Err( + format!( + "Unknown quality: {}", + s + ), + ), + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub enum Service { + Tidal, + AmazonMusic, + SoundCloud, + KHInsider, +} + +impl Service { + pub fn as_str(&self) -> &'static str { + match self { + Service::Tidal => "tidal", + Service::AmazonMusic => "amazon", + Service::SoundCloud => "soundcloud", + Service::KHInsider => "khinsider", + } + } + + pub fn display_name(&self) -> &'static str { + match self { + Service::Tidal => "Tidal", + Service::AmazonMusic => "Amazon Music", + Service::SoundCloud => "SoundCloud", + Service::KHInsider => "KHInsider", + } + } + + pub fn all() -> Vec { + vec![ + Service::Tidal, + Service::AmazonMusic, + Service::SoundCloud, + Service::KHInsider, + ] + } +} + +impl std::fmt::Display for Service { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{}", + self.display_name() + ) + } +} + +impl std::str::FromStr for Service { + type Err = String; + + fn from_str(s: &str) -> Result { + match s + .to_lowercase() + .as_str() + { + "tidal" | "tdl" => Ok(Service::Tidal), + "amazon" | "amazonmusic" | "amz" => Ok(Service::AmazonMusic), + "soundcloud" | "sc" => Ok(Service::SoundCloud), + "khinsider" | "khi" | "kh" => Ok(Service::KHInsider), + _ => Err( + format!( + "Unknown service: {}", + s + ), + ), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct TrackInfo { + pub id: u64, + pub title: String, + pub artist: String, + pub album: Option, + pub duration: Option, + pub quality: Option, + pub cover_url: Option, +} + +impl TrackInfo { + pub fn format_duration(&self) -> String { + self.duration + .map( + |d| { + let mins = d / 60; + let secs = d % 60; + format!( + "{:02}:{:02}", + mins, secs + ) + }, + ) + .unwrap_or_else(|| "--:--".to_string()) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct AlbumInfo { + pub id: u64, + pub title: String, + pub artist: String, + pub track_count: u32, + pub year: Option, + pub cover_url: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PlaylistInfo { + pub id: u64, + pub title: String, + pub creator: String, + pub track_count: u32, + pub cover_url: Option, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct SearchResults { + pub tracks: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DownloadManifest { + pub url: Option, + pub segment_urls: Option>, + pub quality: AudioQuality, + pub bit_depth: Option, + pub sample_rate: Option, +} diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..8fc2c90 --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,384 @@ +use crate::types::*; +use colored::Colorize; +use std::io::{self, Write}; +use tabled::{ + settings::{object::Rows, Alignment, Modify, Style}, + Table, Tabled, +}; + +#[derive(Tabled)] +struct TrackRow { + #[tabled(rename = "#")] + index: usize, + #[tabled(rename = "Title")] + title: String, + #[tabled(rename = "Artist")] + artist: String, + #[tabled(rename = "Album")] + album: String, + #[tabled(rename = "Duration")] + duration: String, +} + +pub struct Ui; + +impl Ui { + pub fn print_banner() { + println!(); + println!( + "{}", + r#" + ██████ ██▓███ ▄▄▄ █ █░ ▄▄▄ ███▄ █ +▒██ ▒ ▓██░ ██▒▒████▄ ▓█░ █ ░█░▒████▄ ██ ▀█ █ +░ ▓██▄ ▓██░ ██▓▒▒██ ▀█▄ ▒█░ █ ░█ ▒██ ▀█▄ ▓██ ▀█ ██▒ + ▒ ██▒▒██▄█▓▒ ▒░██▄▄▄▄██ ░█░ █ ░█ ░██▄▄▄▄██ ▓██▒ ▐▌██▒ +▒██████▒▒▒██▒ ░ ░ ▓█ ▓██▒░░██▒██▓ ▓█ ▓██▒▒██░ ▓██░ +▒ ▒▓▒ ▒ ░▒▓▒░ ░ ░ ▒▒ ▓▒█░░ ▓░▒ ▒ ▒▒ ▓▒█░░ ▒░ ▒ ▒ +░ ░▒ ░ ░░▒ ░ ▒ ▒▒ ░ ▒ ░ ░ ▒ ▒▒ ░░ ░░ ░ ▒░ +░ ░ ░ ░░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░ + ░ ░ ░ ░ ░ ░ ░ +"# + .bright_cyan() + ); + println!( + " {} {} • {} {}", + "High-quality music downloader".white(), + "v0.1.0".yellow(), + "Supports:".white(), + "Tidal".green() + ); + println!(); + } + + pub fn print_info(message: &str) { + println!( + "{} {}", + "ℹ" + .blue() + .bold(), + message.white() + ); + } + + pub fn print_success(message: &str) { + println!( + "{} {}", + "✓" + .green() + .bold(), + message.white() + ); + } + + pub fn print_warning(message: &str) { + println!( + "{} {}", + "!".yellow() + .bold(), + message.white() + ); + } + + pub fn print_error(message: &str) { + eprintln!( + "{} {}", + "✗" + .red() + .bold(), + message.white() + ); + } + + pub fn print_search_results(tracks: &[TrackInfo], limit: usize) { + if tracks.is_empty() { + Self::print_warning("No results found"); + return; + } + + let rows: Vec = tracks + .iter() + .take(limit) + .enumerate() + .map( + |(i, t)| TrackRow { + index: i + 1, + title: t + .title + .clone(), + artist: t + .artist + .clone(), + album: t + .album + .clone() + .unwrap_or_else(|| "-".to_string()), + duration: t.format_duration(), + }, + ) + .collect(); + + let mut table = Table::new(rows); + table + .with(Style::rounded()) + .with(Modify::new(Rows::new(1..)).with(Alignment::left())); + + println!(); + println!( + "{}", + table + ); + println!(); + } + + pub fn print_track_detail(track: &TrackInfo, index: Option) { + let prefix = index + .map( + |i| { + format!( + "{}.", + i + ) + }, + ) + .unwrap_or_default(); + + println!( + "{} {}", + prefix.cyan(), + track + .title + .white() + .bold() + ); + println!( + " {} {}", + "Artist:".dimmed(), + track + .artist + .white() + ); + if let Some(ref album) = track.album { + println!( + " {} {}", + "Album:".dimmed(), + album.white() + ); + } + println!( + " {} {}", + "Duration:".dimmed(), + track + .format_duration() + .white() + ); + if let Some(ref quality) = track.quality { + println!( + " {} {}", + "Quality:".dimmed(), + quality + .to_string() + .green() + ); + } + println!( + " {} {}", + "ID:".dimmed(), + track + .id + .to_string() + .yellow() + ); + } + + pub fn print_album_detail(album: &AlbumInfo) { + println!(); + println!( + "{}", + album + .title + .white() + .bold() + ); + println!( + " {} {}", + "Artist:".cyan(), + album + .artist + .white() + ); + println!( + " {} {} tracks", + "Tracks:".cyan(), + album + .track_count + .to_string() + .white() + ); + if let Some(ref year) = album.year { + println!( + " {} {}", + "Year:".cyan(), + year.to_string() + .white() + ); + } + println!(); + } + + pub fn print_playlist_detail(playlist: &PlaylistInfo) { + println!(); + println!( + "{}", + playlist + .title + .white() + .bold() + ); + println!( + " {} {}", + "Creator:".cyan(), + playlist + .creator + .white() + ); + println!( + " {} {} tracks", + "Tracks:".cyan(), + playlist + .track_count + .to_string() + .white() + ); + println!(); + } + + pub fn print_qualities() { + println!(); + println!( + "{}", + "Available Audio Qualities:" + .cyan() + .bold() + ); + println!(); + + for quality in AudioQuality::all() { + println!( + " {} {}", + format!( + "{:15}", + quality.as_str() + ) + .yellow(), + quality + .description() + .white() + ); + } + println!(); + } + + pub fn print_services() { + println!(); + println!( + "{}", + "Available Services:" + .cyan() + .bold() + ); + println!(); + + println!( + " {} {}", + format!( + "{:12}", + "tidal" + ) + .yellow(), + "Tidal (Hi-Res FLAC)".white() + ); + println!(); + } + + pub fn select_track(tracks: &[TrackInfo]) -> Option { + if tracks.is_empty() { + Self::print_warning("No tracks to select from"); + return None; + } + + if tracks.len() == 1 { + return Some(0); + } + + Self::print_search_results( + tracks, + tracks.len(), + ); + print!( + "{} {}", + "?".green() + .bold(), + "Select track (1-".white(), + ); + print!( + "{}): ", + tracks + .len() + .to_string() + .white() + ); + + io::stdout() + .flush() + .ok()?; + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .ok()?; + + let selection: usize = input + .trim() + .parse() + .ok()?; + + if selection == 0 || selection > tracks.len() { + Self::print_error( + &format!( + "Invalid selection: {}", + selection + ), + ); + return None; + } + + Some(selection - 1) + } + + pub fn confirm(message: &str) -> bool { + print!( + "{} {} [y/N]: ", + "?".green() + .bold(), + message + ); + io::stdout() + .flush() + .ok() + .unwrap_or(()); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .ok() + .unwrap_or_default(); + + matches!( + input + .trim() + .to_lowercase() + .as_str(), + "y" | "yes" + ) + } +} -- cgit v1.2.3