Enable usage of auth flags behind login command

This commit is contained in:
ByteDream 2023-05-07 01:34:41 +02:00
parent c2e953043e
commit 61766c74fa
4 changed files with 59 additions and 33 deletions

View file

@ -203,14 +203,22 @@ async fn get_format(
let download_format = DownloadFormat { let download_format = DownloadFormat {
video: (video.clone(), single_format.audio.clone()), video: (video.clone(), single_format.audio.clone()),
audios: vec![(audio, single_format.audio.clone())], audios: vec![(audio, single_format.audio.clone())],
subtitles: subtitle subtitles: subtitle.clone().map_or(vec![], |s| {
.clone() vec![(
.map_or(vec![], |s| vec![(s, single_format.audio == Locale::ja_JP || stream.subtitles.len() > 1)]), s,
single_format.audio == Locale::ja_JP || stream.subtitles.len() > 1,
)]
}),
}; };
let format = Format::from_single_formats(vec![( let format = Format::from_single_formats(vec![(
single_format.clone(), single_format.clone(),
video, 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)) Ok((download_format, format))

View file

@ -51,7 +51,7 @@ pub struct Cli {
experimental_fixes: bool, experimental_fixes: bool,
#[clap(flatten)] #[clap(flatten)]
login_method: LoginMethod, login_method: login::LoginMethod,
#[arg(help = "Use a proxy to route all traffic through")] #[arg(help = "Use a proxy to route all traffic through")]
#[arg(long_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, 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<String>,
#[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<String>,
#[arg(help = "Login anonymously / without an account")]
#[arg(long, default_value_t = false)]
anonymous: bool,
}
pub async fn cli_entrypoint() { pub async fn cli_entrypoint() {
let mut cli: Cli = Cli::parse(); 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, Ok(ctx) => ctx,
Err(e) => { Err(e) => {
error!("{}", e); error!("{}", e);
@ -222,12 +204,12 @@ async fn execute_executor(executor: impl Execute, ctx: Context) {
} }
} }
async fn create_ctx(cli: &Cli) -> Result<Context> { async fn create_ctx(cli: &mut Cli) -> Result<Context> {
let crunchy = crunchyroll_session(cli).await?; let crunchy = crunchyroll_session(cli).await?;
Ok(Context { crunchy }) Ok(Context { crunchy })
} }
async fn crunchyroll_session(cli: &Cli) -> Result<Crunchyroll> { async fn crunchyroll_session(cli: &mut Cli) -> Result<Crunchyroll> {
let supported_langs = vec![ let supported_langs = vec![
Locale::ar_ME, Locale::ar_ME,
Locale::de_DE, Locale::de_DE,
@ -276,12 +258,18 @@ async fn crunchyroll_session(cli: &Cli) -> Result<Crunchyroll> {
builder = builder.preferred_audio_locale(download.audio.clone()) 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.etp_rt.is_some() as u8
+ cli.login_method.anonymous 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"); 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 let Some(login_file_path) = login::session_file_path() {
if login_file_path.exists() { if login_file_path.exists() {
let session = fs::read_to_string(login_file_path)?; let session = fs::read_to_string(login_file_path)?;
@ -298,11 +286,21 @@ async fn crunchyroll_session(cli: &Cli) -> Result<Crunchyroll> {
} }
} }
bail!("Please use a login method ('--credentials', '--etp-rt' or '--anonymous')") 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')") 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(':') { if let Some((user, password)) = credentials.split_once(':') {
builder.login_with_credentials(user, password).await? builder.login_with_credentials(user, password).await?
} else { } else {

View file

@ -2,6 +2,7 @@ use crate::utils::context::Context;
use crate::Execute; use crate::Execute;
use anyhow::bail; use anyhow::bail;
use anyhow::Result; use anyhow::Result;
use clap::Parser;
use crunchyroll_rs::crunchyroll::SessionToken; use crunchyroll_rs::crunchyroll::SessionToken;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
@ -9,7 +10,9 @@ use std::path::PathBuf;
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]
#[clap(about = "Save your login credentials persistent on disk")] #[clap(about = "Save your login credentials persistent on disk")]
pub struct Login { 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)] #[arg(long)]
pub remove: bool, 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<String>,
#[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<String>,
#[arg(help = "Login anonymously / without an account")]
#[arg(long, default_value_t = false)]
pub anonymous: bool,
}
pub fn session_file_path() -> Option<PathBuf> { pub fn session_file_path() -> Option<PathBuf> {
dirs::config_dir().map(|config_dir| config_dir.join("crunchy-cli").join("session")) dirs::config_dir().map(|config_dir| config_dir.join("crunchy-cli").join("session"))
} }

View file

@ -1,4 +1,3 @@
mod command; mod command;
pub use command::session_file_path; pub use command::{session_file_path, Login, LoginMethod};
pub use command::Login;