mirror of
https://github.com/GuillemCastro/spotify-dl.git
synced 2025-07-03 04:25:32 +02:00
remove --username --password arguments that are not supported by underlying librespot library
This commit is contained in:
parent
4415044926
commit
fb03e23ae2
2 changed files with 4 additions and 35 deletions
13
src/main.rs
13
src/main.rs
|
@ -18,10 +18,6 @@ struct Opt {
|
||||||
help = "A list of Spotify URIs or URLs (songs, podcasts, playlists or albums)"
|
help = "A list of Spotify URIs or URLs (songs, podcasts, playlists or albums)"
|
||||||
)]
|
)]
|
||||||
tracks: Vec<String>,
|
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(
|
#[structopt(
|
||||||
short = "d",
|
short = "d",
|
||||||
long = "destination",
|
long = "destination",
|
||||||
|
@ -92,14 +88,7 @@ async fn main() -> anyhow::Result<()> {
|
||||||
eprintln!("Compression level is not supported yet. It will be ignored.");
|
eprintln!("Compression level is not supported yet. It will be ignored.");
|
||||||
}
|
}
|
||||||
|
|
||||||
let user_name = opt.username.or_else(|| {
|
let session = create_session().await?;
|
||||||
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 track = get_tracks(opt.tracks, &session).await?;
|
let track = get_tracks(opt.tracks, &session).await?;
|
||||||
|
|
||||||
let downloader = Downloader::new(session);
|
let downloader = Downloader::new(session);
|
||||||
|
|
|
@ -2,36 +2,16 @@ use anyhow::Result;
|
||||||
use librespot::core::cache::Cache;
|
use librespot::core::cache::Cache;
|
||||||
use librespot::core::config::SessionConfig;
|
use librespot::core::config::SessionConfig;
|
||||||
use librespot::core::session::Session;
|
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 credentials_store = dirs::home_dir().map(|p| p.join(".spotify-dl"));
|
||||||
let cache = Cache::new(credentials_store, None, None, None)?;
|
let cache = Cache::new(credentials_store, None, None, None)?;
|
||||||
|
|
||||||
let session_config = SessionConfig::default();
|
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));
|
let session = Session::new(session_config, Some(cache));
|
||||||
session.connect(credentials, false).await?;
|
session.connect(credentials, true).await?;
|
||||||
Ok(session)
|
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")),
|
|
||||||
)
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue