From 025ffb62551277dae781ea6068db4934e4b552df Mon Sep 17 00:00:00 2001 From: GenshinMinecraft Date: Tue, 19 Aug 2025 09:35:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(aria2):=20=E9=87=8D=E6=9E=84=20download=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=96=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构 download 函数,返回通知接收器以支持进度更新 - 新增 search 模块,实现关键词搜索功能 - 更新 single模块,增加音频格式判断- 引入日志模块,优化调试输出 - 新增特殊字符替换函数,用于文件名处理 --- Cargo.lock | 134 ++++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 5 +- src/aria2.rs | 14 ++++-- src/main.rs | 7 +-- src/search.rs | 43 ++++++++++++++++ src/single.rs | 26 ++++++++-- 6 files changed, 206 insertions(+), 23 deletions(-) create mode 100644 src/search.rs diff --git a/Cargo.lock b/Cargo.lock index 09150b4..78eb39e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,7 +68,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -140,6 +140,16 @@ dependencies = [ "windows-link", ] +[[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 = "core-foundation-sys" version = "0.8.7" @@ -722,6 +732,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.175" @@ -777,10 +793,13 @@ name = "netease-downloader" version = "0.1.0" dependencies = [ "aria2-ws", + "log", "reqwest", "serde", "serde_json", + "simple_logger", "tokio", + "urlencoding", ] [[package]] @@ -798,6 +817,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_threads" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" +dependencies = [ + "libc", +] + [[package]] name = "object" version = "0.36.7" @@ -1211,6 +1239,18 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "simple_logger" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8c5dfa5e08767553704aa0ffd9d9794d527103c736aba9854773851fd7497eb" +dependencies = [ + "colored", + "log", + "time", + "windows-sys 0.48.0", +] + [[package]] name = "slab" version = "0.4.11" @@ -1341,7 +1381,9 @@ checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40" dependencies = [ "deranged", "itoa", + "libc", "num-conv", + "num_threads", "powerfmt", "serde", "time-core", @@ -1556,6 +1598,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "utf-8" version = "0.7.6" @@ -1757,13 +1805,22 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1772,7 +1829,22 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -1781,28 +1853,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "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_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[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_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1815,24 +1905,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index dbc2942..9ab68e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,7 @@ tokio = { version = "1.47.1", default-features = false, features = ["rt-multi-th reqwest = { version = "0.12.23", default-features = false, features = ["rustls-tls", "json"] } serde = { version = "1.0.219", default-features = false, features = ["std", "derive"] } serde_json = { version = "1.0.142", default-features = false, features = ["std"] } -aria2-ws = "0.5.1" \ No newline at end of file +aria2-ws = "0.5.1" +simple_logger = { version = "5.0.0", features = ["colored", "colors", "threads"] } +log = { version = "0.4.27",default-features = false, features = ["std"] } +urlencoding = "2.1.3" \ No newline at end of file diff --git a/src/aria2.rs b/src/aria2.rs index 9382b8e..071cfa2 100644 --- a/src/aria2.rs +++ b/src/aria2.rs @@ -1,18 +1,22 @@ use crate::ErrorString; use aria2_ws::{Client, TaskOptions}; +use tokio::sync::broadcast::Receiver; -pub async fn download(url: String, path: String) -> Result<(), ErrorString> { +pub async fn download( + url: String, + path: String, +) -> Result, ErrorString> { let client = Client::connect("ws://127.0.0.1:16800/jsonrpc", Some("GenshinMinecraft")) .await .map_err(|e| format!("无法创建 Client: {e}"))?; let options = TaskOptions { - out: Some(path), + out: Some(path.clone()), ..Default::default() }; client - .add_uri(vec![url], Some(options.clone()), None, None) + .add_uri(vec![url.clone()], Some(options.clone()), None, None) .await .map_err(|e| format!("无法添加任务: {e}"))?; - - Ok(()) + let not = client.subscribe_notifications(); + Ok(not) } diff --git a/src/main.rs b/src/main.rs index 7d651d4..bbf629c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ +use log::Level::Debug; + mod aria2; +mod search; mod single; pub mod utils; @@ -6,7 +9,5 @@ type ErrorString = String; #[tokio::main] async fn main() { - let single_songs_info = single::single(448317056).await.unwrap(); - println!("{:?}", single_songs_info); - aria2::download(single_songs_info.download_url, format!("{}.flac", single_songs_info.name)).await.unwrap(); + simple_logger::init_with_level(Debug).unwrap(); } diff --git a/src/search.rs b/src/search.rs new file mode 100644 index 0000000..b82e3b9 --- /dev/null +++ b/src/search.rs @@ -0,0 +1,43 @@ +use crate::utils::create_reqwest_client; +use crate::ErrorString; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Search { + pub result: SearchResult, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SearchResult { + pub songs: Vec, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SearchResultSong { + pub id: i64, + pub name: String, + pub artists: Vec, + pub duration: u64, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SearchResultSongArtist { + pub name: String, +} + +pub async fn search(keyword: String) -> Result, ErrorString> { + let client = create_reqwest_client().await?; + + let resp = client + .get(format!( + "https://163.genshinmc.eu.org/search?keywords={}&limit=20", + urlencoding::encode(keyword.as_str()) + )) + .send() + .await + .map_err(|e| format!("请求错误: {}", e))?; + + let json: Search = resp.json().await.map_err(|e| format!("解析错误: {}", e))?; + + Ok(json.result.songs) +} diff --git a/src/single.rs b/src/single.rs index bca54b5..78cfe57 100644 --- a/src/single.rs +++ b/src/single.rs @@ -1,16 +1,17 @@ -use crate::ErrorString; use crate::utils::create_reqwest_client; +use crate::ErrorString; use serde::{Deserialize, Serialize}; use tokio::task::JoinHandle; #[derive(Debug)] pub struct SingleSongsInfo { - pub id: u64, + pub id: i64, pub name: String, pub artist: String, pub album: String, pub pic_url: String, pub download_url: String, + pub format: String, } #[derive(Serialize, Deserialize)] @@ -22,7 +23,7 @@ struct DetailJson { url: String, } -pub async fn single(id: u64) -> Result { +pub async fn single(id: i64) -> Result { let client = create_reqwest_client().await?; let info_handle: JoinHandle> = tokio::spawn(async move { @@ -48,6 +49,23 @@ pub async fn single(id: u64) -> Result { artist: info_result.ar_name, album: info_result.al_name, pic_url: info_result.pic, - download_url: info_result.url, + download_url: info_result.url.clone(), + format: if info_result.url.contains(".flac") { + String::from("flac") + } else { + String::from("mp3") + }, }) } + +pub fn replace_special_char(s: String) -> String { + s.replace(r#"\"#, "-") + .replace(r#"/"#, "-") + .replace(r#"*"#, "-") + .replace(r#"?"#, "-") + .replace(r#"<"#, "-") + .replace(r#">"#, "-") + .replace(r#":"#, "-") + .replace(r#"|"#, "-") + .replace(r#"""#, "-") +}