diff --git a/crunchy-cli-core/src/download/command.rs b/crunchy-cli-core/src/download/command.rs index c1388f1..7d42d97 100644 --- a/crunchy-cli-core/src/download/command.rs +++ b/crunchy-cli-core/src/download/command.rs @@ -203,14 +203,22 @@ async fn get_format( let download_format = DownloadFormat { video: (video.clone(), single_format.audio.clone()), audios: vec![(audio, single_format.audio.clone())], - subtitles: subtitle - .clone() - .map_or(vec![], |s| vec![(s, single_format.audio == Locale::ja_JP || stream.subtitles.len() > 1)]), + subtitles: subtitle.clone().map_or(vec![], |s| { + vec![( + s, + single_format.audio == Locale::ja_JP || stream.subtitles.len() > 1, + )] + }), }; let format = Format::from_single_formats(vec![( single_format.clone(), video, - subtitle.map_or(vec![], |s| vec![(s, single_format.audio == Locale::ja_JP || stream.subtitles.len() > 1)]), + subtitle.map_or(vec![], |s| { + vec![( + s, + single_format.audio == Locale::ja_JP || stream.subtitles.len() > 1, + )] + }), )]); Ok((download_format, format)) diff --git a/crunchy-cli-core/src/lib.rs b/crunchy-cli-core/src/lib.rs index fee019f..fdc801c 100644 --- a/crunchy-cli-core/src/lib.rs +++ b/crunchy-cli-core/src/lib.rs @@ -51,7 +51,7 @@ pub struct Cli { experimental_fixes: bool, #[clap(flatten)] - login_method: LoginMethod, + login_method: login::LoginMethod, #[arg(help = "Use a proxy to route all traffic through")] #[arg(long_help = "Use a proxy to route all traffic through. \ @@ -97,24 +97,6 @@ struct Verbosity { q: bool, } -#[derive(Debug, Parser)] -struct LoginMethod { - #[arg( - help = "Login with credentials (username or email and password). Must be provided as user:password" - )] - #[arg(long)] - credentials: Option, - #[arg(help = "Login with the etp-rt cookie")] - #[arg( - long_help = "Login with the etp-rt cookie. This can be obtained when you login on crunchyroll.com and extract it from there" - )] - #[arg(long)] - etp_rt: Option, - #[arg(help = "Login anonymously / without an account")] - #[arg(long, default_value_t = false)] - anonymous: bool, -} - pub async fn cli_entrypoint() { let mut cli: Cli = Cli::parse(); @@ -148,7 +130,7 @@ pub async fn cli_entrypoint() { } }; - let ctx = match create_ctx(&cli).await { + let ctx = match create_ctx(&mut cli).await { Ok(ctx) => ctx, Err(e) => { error!("{}", e); @@ -222,12 +204,12 @@ async fn execute_executor(executor: impl Execute, ctx: Context) { } } -async fn create_ctx(cli: &Cli) -> Result { +async fn create_ctx(cli: &mut Cli) -> Result { let crunchy = crunchyroll_session(cli).await?; Ok(Context { crunchy }) } -async fn crunchyroll_session(cli: &Cli) -> Result { +async fn crunchyroll_session(cli: &mut Cli) -> Result { let supported_langs = vec![ Locale::ar_ME, Locale::de_DE, @@ -276,12 +258,18 @@ async fn crunchyroll_session(cli: &Cli) -> Result { builder = builder.preferred_audio_locale(download.audio.clone()) } - let login_methods_count = cli.login_method.credentials.is_some() as u8 + let root_login_methods_count = cli.login_method.credentials.is_some() as u8 + cli.login_method.etp_rt.is_some() as u8 + cli.login_method.anonymous as u8; + let mut login_login_methods_count = 0; + if let Command::Login(login) = &cli.command { + login_login_methods_count += login.login_method.credentials.is_some() as u8 + + cli.login_method.etp_rt.is_some() as u8 + + cli.login_method.anonymous as u8 + } let progress_handler = progress!("Logging in"); - if login_methods_count == 0 { + if root_login_methods_count + login_login_methods_count == 0 { if let Some(login_file_path) = login::session_file_path() { if login_file_path.exists() { let session = fs::read_to_string(login_file_path)?; @@ -298,11 +286,21 @@ async fn crunchyroll_session(cli: &Cli) -> Result { } } bail!("Please use a login method ('--credentials', '--etp-rt' or '--anonymous')") - } else if login_methods_count > 1 { + } else if root_login_methods_count + login_login_methods_count > 1 { bail!("Please use only one login method ('--credentials', '--etp-rt' or '--anonymous')") } - let crunchy = if let Some(credentials) = &cli.login_method.credentials { + let login_method = if login_login_methods_count > 0 { + if let Command::Login(login) = &cli.command { + login.login_method.clone() + } else { + unreachable!() + } + } else { + cli.login_method.clone() + }; + + let crunchy = if let Some(credentials) = &login_method.credentials { if let Some((user, password)) = credentials.split_once(':') { builder.login_with_credentials(user, password).await? } else { diff --git a/crunchy-cli-core/src/login/command.rs b/crunchy-cli-core/src/login/command.rs index ad101da..ab16e06 100644 --- a/crunchy-cli-core/src/login/command.rs +++ b/crunchy-cli-core/src/login/command.rs @@ -2,6 +2,7 @@ use crate::utils::context::Context; use crate::Execute; use anyhow::bail; use anyhow::Result; +use clap::Parser; use crunchyroll_rs::crunchyroll::SessionToken; use std::fs; use std::path::PathBuf; @@ -9,7 +10,9 @@ use std::path::PathBuf; #[derive(Debug, clap::Parser)] #[clap(about = "Save your login credentials persistent on disk")] pub struct Login { - #[arg(help = "Remove your stored credentials (instead of save them)")] + #[clap(flatten)] + pub login_method: LoginMethod, + #[arg(help = "Remove your stored credentials (instead of saving them)")] #[arg(long)] pub remove: bool, } @@ -36,6 +39,24 @@ impl Execute for Login { } } +#[derive(Clone, Debug, Parser)] +pub struct LoginMethod { + #[arg( + help = "Login with credentials (username or email and password). Must be provided as user:password" + )] + #[arg(long)] + pub credentials: Option, + #[arg(help = "Login with the etp-rt cookie")] + #[arg( + long_help = "Login with the etp-rt cookie. This can be obtained when you login on crunchyroll.com and extract it from there" + )] + #[arg(long)] + pub etp_rt: Option, + #[arg(help = "Login anonymously / without an account")] + #[arg(long, default_value_t = false)] + pub anonymous: bool, +} + pub fn session_file_path() -> Option { dirs::config_dir().map(|config_dir| config_dir.join("crunchy-cli").join("session")) } diff --git a/crunchy-cli-core/src/login/mod.rs b/crunchy-cli-core/src/login/mod.rs index e2ab88c..8c1220a 100644 --- a/crunchy-cli-core/src/login/mod.rs +++ b/crunchy-cli-core/src/login/mod.rs @@ -1,4 +1,3 @@ mod command; -pub use command::session_file_path; -pub use command::Login; +pub use command::{session_file_path, Login, LoginMethod};