From 4818e62414a519b25fba2f0aa6128a6aae7a6b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Fri, 7 Jun 2024 19:57:08 -0700 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=BC=95=E5=85=A5=20clap=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E5=92=8C?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E5=8F=82=E6=95=B0=20(#119)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 45 ++++++++++++++-------------- Cargo.toml | 37 ++++++++++++----------- crates/bili_sync/Cargo.toml | 1 + crates/bili_sync/src/config.rs | 14 +++++++++ crates/bili_sync/src/core/command.rs | 8 ++--- crates/bili_sync/src/core/utils.rs | 12 ++++++++ crates/bili_sync/src/main.rs | 19 ++++-------- 7 files changed, 77 insertions(+), 59 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ddf4a6..cb0fc58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,6 +426,7 @@ dependencies = [ "bili_sync_entity", "bili_sync_migration", "chrono", + "clap", "cookie 0.18.1", "dirs", "filenamify", @@ -606,9 +607,9 @@ checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" [[package]] name = "chrono" -version = "0.4.37" +version = "0.4.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" dependencies = [ "android-tzdata", "iana-time-zone", @@ -621,9 +622,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.4" +version = "4.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "a9689a29b593160de5bc4aacab7b5d54fb52231de70122626c178e6a368994c7" dependencies = [ "clap_builder", "clap_derive", @@ -631,9 +632,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "2e5387378c84f6faa26890ebf9f0a92989f8873d4d380467bcd0d8d8620424df" dependencies = [ "anstream", "anstyle", @@ -643,9 +644,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" dependencies = [ "heck 0.5.0", "proc-macro2", @@ -2681,18 +2682,18 @@ checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" [[package]] name = "serde" -version = "1.0.197" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.197" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", @@ -2701,9 +2702,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.115" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" dependencies = [ "itoa", "ryu", @@ -3191,18 +3192,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.58" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.58" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", @@ -3267,9 +3268,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.37.0" +version = "1.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "bytes", @@ -3286,9 +3287,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 38879f1..b42da00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,13 @@ publish = false bili_sync_entity = { path = "crates/bili_sync_entity" } bili_sync_migration = { path = "crates/bili_sync_migration" } -anyhow = { version = "1.0.81", features = ["backtrace"] } -arc-swap = { version = "1.7", features = ["serde"] } -async-std = { version = "1", features = ["attributes", "tokio1"] } +anyhow = { version = "1.0.86", features = ["backtrace"] } +arc-swap = { version = "1.7.1", features = ["serde"] } +async-std = { version = "1.12.0", features = ["attributes", "tokio1"] } async-stream = "0.3.5" -chrono = { version = "0.4.35", features = ["serde"] } -cookie = "0.18.0" +chrono = { version = "0.4.38", features = ["serde"] } +clap = { version = "4.5.6", features = ["env"] } +cookie = "0.18.1" dirs = "5.0.1" filenamify = "0.1.0" float-ord = "0.3.2" @@ -28,12 +29,12 @@ futures = "0.3.30" handlebars = "5.1.2" hex = "0.4.3" log = "0.4.21" -memchr = "2.5.0" +memchr = "2.7.2" once_cell = "1.19.0" -prost = "0.12.4" +prost = "0.12.6" quick-xml = { version = "0.31.0", features = ["async-tokio"] } rand = "0.8.5" -regex = "1.10.3" +regex = "1.10.4" reqwest = { version = "0.12.4", features = [ "charset", "cookies", @@ -44,20 +45,20 @@ reqwest = { version = "0.12.4", features = [ "stream", ], default-features = false } rsa = { version = "0.9.6", features = ["sha2"] } -sea-orm = { version = "0.12", features = [ +sea-orm = { version = "0.12.15", features = [ "macros", "runtime-tokio-rustls", "sqlx-sqlite", ] } -sea-orm-migration = { version = "0.12.0", features = [] } -serde = { version = "1.0.197", features = ["derive"] } -serde_json = "1.0" -strum = { version = "0.26", features = ["derive"] } -thiserror = "1.0.58" -tokio = { version = "1", features = ["full"] } -toml = "0.8.12" -tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["chrono"] } +sea-orm-migration = { version = "0.12.15", features = [] } +serde = { version = "1.0.203", features = ["derive"] } +serde_json = "1.0.117" +strum = { version = "0.26.2", features = ["derive"] } +thiserror = "1.0.61" +tokio = { version = "1.38.0", features = ["full"] } +toml = "0.8.14" +tracing = "0.1.40" +tracing-subscriber = { version = "0.3.18", features = ["chrono"] } [profile.release] strip = true diff --git a/crates/bili_sync/Cargo.toml b/crates/bili_sync/Cargo.toml index fcd7f9b..dc1529d 100644 --- a/crates/bili_sync/Cargo.toml +++ b/crates/bili_sync/Cargo.toml @@ -15,6 +15,7 @@ async-stream = { workspace = true } bili_sync_entity = { workspace = true } bili_sync_migration = { workspace = true } chrono = { workspace = true } +clap = { workspace = true } cookie = { workspace = true } dirs = { workspace = true } filenamify = { workspace = true } diff --git a/crates/bili_sync/src/config.rs b/crates/bili_sync/src/config.rs index 575d3d8..22bc4b3 100644 --- a/crates/bili_sync/src/config.rs +++ b/crates/bili_sync/src/config.rs @@ -30,6 +30,8 @@ pub static CONFIG: Lazy = Lazy::new(|| { config }); +pub static ARGS: Lazy = Lazy::new(Args::parse); + pub static CONFIG_DIR: Lazy = Lazy::new(|| dirs::config_dir().expect("No config path found").join("bili-sync")); @@ -138,3 +140,15 @@ impl Config { Ok(()) } } + +use clap::Parser; + +#[derive(Parser)] +#[command(version, about, long_about = None)] +pub struct Args { + #[arg(short, long, env = "SCAN_ONLY")] + pub scan_only: bool, + + #[arg(short, long, default_value = "None,bili_sync=info", env = "RUST_LOG")] + pub log_level: String, +} diff --git a/crates/bili_sync/src/core/command.rs b/crates/bili_sync/src/core/command.rs index 2acd054..878f4fc 100644 --- a/crates/bili_sync/src/core/command.rs +++ b/crates/bili_sync/src/core/command.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::env::{args, var}; use std::path::{Path, PathBuf}; use std::pin::Pin; @@ -8,7 +7,6 @@ use bili_sync_entity::{favorite, page, video}; use filenamify::filenamify; use futures::stream::{FuturesOrdered, FuturesUnordered}; use futures::{pin_mut, Future, StreamExt}; -use once_cell::sync::Lazy; use sea_orm::entity::prelude::*; use sea_orm::ActiveValue::Set; use sea_orm::TransactionTrait; @@ -17,7 +15,7 @@ use tokio::fs; use tokio::sync::{Mutex, Semaphore}; use crate::bilibili::{BestStream, BiliClient, BiliError, Dimension, FavoriteList, PageInfo, Video}; -use crate::config::CONFIG; +use crate::config::{ARGS, CONFIG}; use crate::core::status::{PageStatus, VideoStatus}; use crate::core::utils::{ create_video_pages, create_videos, exist_labels, filter_unfilled_videos, handle_favorite_info, total_video_count, @@ -26,8 +24,6 @@ use crate::core::utils::{ use crate::downloader::Downloader; use crate::error::{DownloadAbortError, ProcessPageError}; -pub static SCAN_ONLY: Lazy = Lazy::new(|| var("SCAN_ONLY").is_ok() || args().any(|arg| arg == "--scan-only")); - /// 处理某个收藏夹,首先刷新收藏夹信息,然后下载收藏夹中未下载成功的视频 pub async fn process_favorite_list( bili_client: &BiliClient, @@ -37,7 +33,7 @@ pub async fn process_favorite_list( ) -> Result<()> { let favorite_model = refresh_favorite_list(bili_client, fid, path, connection).await?; let favorite_model = fetch_video_details(bili_client, favorite_model, connection).await?; - if *SCAN_ONLY { + if ARGS.scan_only { warn!("已开启仅扫描模式,跳过视频下载..."); return Ok(()); } diff --git a/crates/bili_sync/src/core/utils.rs b/crates/bili_sync/src/core/utils.rs index 299b890..f4a099f 100644 --- a/crates/bili_sync/src/core/utils.rs +++ b/crates/bili_sync/src/core/utils.rs @@ -15,6 +15,7 @@ use sea_orm::ActiveValue::Set; use sea_orm::QuerySelect; use serde_json::json; use tokio::io::AsyncWriteExt; +use tracing_subscriber::util::SubscriberInitExt; use crate::bilibili::{FavoriteListInfo, PageInfo, VideoInfo}; use crate::config::{NFOTimeType, CONFIG}; @@ -482,6 +483,17 @@ impl<'a> NFOSerializer<'a> { } } +pub fn init_logger(log_level: &str) { + tracing_subscriber::fmt::Subscriber::builder() + .with_env_filter(tracing_subscriber::EnvFilter::builder().parse_lossy(log_level)) + .with_timer(tracing_subscriber::fmt::time::ChronoLocal::new( + "%Y-%m-%d %H:%M:%S%.3f".to_owned(), + )) + .finish() + .try_init() + .expect("初始化日志失败"); +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/bili_sync/src/main.rs b/crates/bili_sync/src/main.rs index bf7df44..035d49d 100644 --- a/crates/bili_sync/src/main.rs +++ b/crates/bili_sync/src/main.rs @@ -10,27 +10,20 @@ mod error; use std::time::Duration; +use config::ARGS; use once_cell::sync::Lazy; use tokio::time; -use tracing_subscriber::util::SubscriberInitExt; use crate::bilibili::BiliClient; use crate::config::CONFIG; -use crate::core::command::{process_favorite_list, SCAN_ONLY}; +use crate::core::command::process_favorite_list; +use crate::core::utils::init_logger; use crate::database::{database_connection, migrate_database}; #[tokio::main] -async fn main() -> ! { - let default_log_level = std::env::var("RUST_LOG").unwrap_or("None,bili_sync=info".to_owned()); - tracing_subscriber::fmt::Subscriber::builder() - .with_env_filter(tracing_subscriber::EnvFilter::builder().parse_lossy(default_log_level)) - .with_timer(tracing_subscriber::fmt::time::ChronoLocal::new( - "%Y-%m-%d %H:%M:%S%.3f".to_owned(), - )) - .finish() - .try_init() - .expect("初始化日志失败"); - Lazy::force(&SCAN_ONLY); +async fn main() { + Lazy::force(&ARGS); + init_logger(&ARGS.log_level); Lazy::force(&CONFIG); let mut anchor = chrono::Local::now().date_naive(); let bili_client = BiliClient::new();