1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-04 02:09:26 +02:00

Save reusable credentials to disk.

After the first login, credentials may be omitted from the command line
and the stored ones will be used instead.
This commit is contained in:
Paul Lietar 2016-03-13 22:35:09 +00:00
parent 39af43728a
commit 4b73f83c5e
4 changed files with 91 additions and 17 deletions

View file

@ -55,7 +55,7 @@ fn main() {
};
let username = matches.opt_str("u");
let cache_location = matches.opt_str("c").unwrap();
let cache_location = PathBuf::from(matches.opt_str("c").unwrap());
let name = matches.opt_str("n").unwrap();
let credentials = username.map(|u| {
@ -84,23 +84,30 @@ fn main() {
application_key: appkey,
user_agent: version_string(),
device_name: name,
cache_location: PathBuf::from(cache_location),
cache_location: cache_location.clone(),
bitrate: bitrate,
};
let session = Session::new(config);
let credentials = credentials.map_or_else(|| {
let credentials_path = cache_location.join("credentials.json");
let credentials = credentials.map(|(username, password)| {
Credentials::with_password(username, password)
}).or_else(|| {
File::open(&credentials_path).map(|file| {
Credentials::from_reader(file)
}).ok()
}).unwrap_or_else(|| {
let mut discovery = DiscoveryManager::new(session.clone());
discovery.run()
}, |(username, password)| {
Credentials::with_password(username, password)
});
session.login(credentials).unwrap();
let reusable_credentials = session.login(credentials).unwrap();
reusable_credentials.save_to_file(credentials_path);
let player = Player::new(session.clone());
let mut spirc = SpircManager::new(session.clone(), player);
let spirc = SpircManager::new(session.clone(), player);
thread::spawn(move || spirc.run());
loop {