Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Add anonymous login #80

Merged
merged 2 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ You can pass your account via credentials (username & password) or refresh token
- ```shell
$ crunchy --credentials "user:password"
```
- Anonymous
- Login without an account at all is also possible.
- ```shell
$ crunchy --anonymous
```

### Login

Expand All @@ -89,6 +94,7 @@ $ crunchy --etp-rt "abcd1234-zyxw-9876-98zy-a1b2c3d4e5f6" login
```

Once set, you do not need to provide `--etp-rt` / `--credentials` anymore when using the cli.
This does not work if you've using this with `--anonymous`.

### Download

Expand Down
20 changes: 14 additions & 6 deletions crunchy-cli-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ struct Verbosity {

#[derive(Debug, Parser)]
struct LoginMethod {
#[arg(help = "Login with credentials (username or email and password)")]
#[arg(
long_help = "Login with credentials (username or email and password). Must be provided as user:password"
help = "Login with credentials (username or email and password). Must be provided as user:password"
)]
#[arg(long)]
credentials: Option<String>,
Expand All @@ -93,6 +92,9 @@ struct LoginMethod {
)]
#[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() {
Expand Down Expand Up @@ -190,8 +192,12 @@ async fn crunchyroll_session(cli: &Cli) -> Result<Crunchyroll> {
let mut builder = Crunchyroll::builder();
builder.locale(cli.lang.clone().unwrap_or_else(system_locale));

let 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 _progress_handler = progress!("Logging in");
if cli.login_method.credentials.is_none() && cli.login_method.etp_rt.is_none() {
if login_methods_count == 0 {
if let Some(login_file_path) = cli::login::login_file_path() {
if login_file_path.exists() {
let session = fs::read_to_string(login_file_path)?;
Expand All @@ -207,9 +213,9 @@ async fn crunchyroll_session(cli: &Cli) -> Result<Crunchyroll> {
bail!("Could not read stored session ('{}')", session)
}
}
bail!("Please use a login method ('--credentials' or '--etp_rt')")
} else if cli.login_method.credentials.is_some() && cli.login_method.etp_rt.is_some() {
bail!("Please use only one login method ('--credentials' or '--etp_rt')")
bail!("Please use a login method ('--credentials', '--etp-rt' or '--anonymous')")
} else if 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 {
Expand All @@ -220,6 +226,8 @@ async fn crunchyroll_session(cli: &Cli) -> Result<Crunchyroll> {
}
} else if let Some(etp_rt) = &cli.login_method.etp_rt {
builder.login_with_etp_rt(etp_rt).await?
} else if cli.login_method.anonymous {
builder.login_anonymously().await?
} else {
bail!("should never happen")
};
Expand Down