remove --username --password arguments that are not supported by underlying librespot library

This commit is contained in:
Erik Kaju 2025-06-20 17:49:32 +03:00
parent 4415044926
commit fb03e23ae2
2 changed files with 4 additions and 35 deletions

View file

@ -18,10 +18,6 @@ struct Opt {
help = "A list of Spotify URIs or URLs (songs, podcasts, playlists or albums)"
)]
tracks: Vec<String>,
#[structopt(short = "u", long = "username", help = "Your Spotify username")]
username: Option<String>,
#[structopt(short = "p", long = "password", help = "Your Spotify password")]
password: Option<String>,
#[structopt(
short = "d",
long = "destination",
@ -92,14 +88,7 @@ async fn main() -> anyhow::Result<()> {
eprintln!("Compression level is not supported yet. It will be ignored.");
}
let user_name = opt.username.or_else(|| {
println!("No username provided via arguments. Attempting to fetch from latest credentials cache.");
std::fs::read_to_string("credentials.json").ok()
.and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
.and_then(|v| v.get("username")?.as_str().map(|s| s.to_string()))
});
let session = create_session(user_name.unwrap(), opt.password).await?;
let session = create_session().await?;
let track = get_tracks(opt.tracks, &session).await?;
let downloader = Downloader::new(session);

View file

@ -2,36 +2,16 @@ use anyhow::Result;
use librespot::core::cache::Cache;
use librespot::core::config::SessionConfig;
use librespot::core::session::Session;
use librespot::discovery::Credentials;
pub async fn create_session(username: String, password: Option<String>) -> Result<Session> {
pub async fn create_session() -> Result<Session> {
let credentials_store = dirs::home_dir().map(|p| p.join(".spotify-dl"));
let cache = Cache::new(credentials_store, None, None, None)?;
let session_config = SessionConfig::default();
let credentials = get_credentials(username, password, &cache);
cache.save_credentials(&credentials);
let credentials = cache.credentials().unwrap();
let session = Session::new(session_config, Some(cache));
session.connect(credentials, false).await?;
session.connect(credentials, true).await?;
Ok(session)
}
fn prompt_password() -> Result<String> {
tracing::info!("Spotify password was not provided. Please enter your Spotify password below");
rpassword::prompt_password("Password: ").map_err(|e| e.into())
}
fn get_credentials(username: String, password: Option<String>, cache: &Cache) -> Credentials {
match password {
Some(password) => Credentials::with_password(username, password),
None => cache.credentials().unwrap_or_else(|| {
tracing::warn!("No credentials found in cache");
Credentials::with_password(
username,
prompt_password().unwrap_or_else(|_| panic!("Failed to get password")),
)
}),
}
}