Added existing file checking and other fixes
parent
f11fb48ce7
commit
fe66ff977a
|
@ -21,7 +21,7 @@ use librespot_core::authentication::Credentials;
|
|||
use librespot_core::config::SessionConfig;
|
||||
use librespot_core::session::Session;
|
||||
use librespot_core::spotify_id::SpotifyId;
|
||||
use librespot_metadata::{Artist, FileFormat, Metadata, Track, Album};
|
||||
use librespot_metadata::{Artist, FileFormat, Metadata, Track};
|
||||
use regex::Regex;
|
||||
use scoped_threadpool::Pool;
|
||||
use tokio_core::reactor::Core;
|
||||
|
@ -55,6 +55,13 @@ fn main() {
|
|||
.and_then(|capture|SpotifyId::from_base62(&capture[1]).ok())))
|
||||
.for_each(|id|{
|
||||
info!("Getting track {}...", id.to_base62());
|
||||
let fname = format!("{}.ogg", id.to_base62());
|
||||
let pathexists = std::path::Path::new(&fname).exists();
|
||||
use std::path::Path;
|
||||
if !Path::new(&fname).exists() {
|
||||
info!("File {} already exists... Skipping...", id.to_base62());
|
||||
} else {
|
||||
info!("File does not exist, continuing. Errors {}", Path::new(&fname).exists());
|
||||
let mut track = core.run(Track::get(&session, id)).expect("Cannot get track metadata");
|
||||
if !track.available {
|
||||
warn!("Track {} is not available, finding alternative...", id.to_base62());
|
||||
|
@ -70,9 +77,9 @@ fn main() {
|
|||
}
|
||||
let artists_strs: Vec<_> = track.artists.iter().map(|id|core.run(Artist::get(&session, *id)).expect("Cannot get artist metadata").name).collect();
|
||||
debug!("File formats: {}", track.files.keys().map(|filetype|format!("{:?}", filetype)).collect::<Vec<_>>().join(" "));
|
||||
let file_id = track.files.get(&FileFormat::OGG_VORBIS_320)
|
||||
.or(track.files.get(&FileFormat::OGG_VORBIS_160))
|
||||
let file_id = track.files.get(&FileFormat::OGG_VORBIS_160)
|
||||
.or(track.files.get(&FileFormat::OGG_VORBIS_96))
|
||||
.or(track.files.get(&FileFormat::OGG_VORBIS_320))
|
||||
.expect("Could not find a OGG_VORBIS format for the track.");
|
||||
let key = core.run(session.audio_key().request(track.id, *file_id)).expect("Cannot get audio key");
|
||||
let mut encrypted_file = core.run(AudioFile::open(&session, *file_id)).unwrap();
|
||||
|
@ -92,18 +99,23 @@ fn main() {
|
|||
let mut decrypted_buffer = Vec::new();
|
||||
AudioDecrypt::new(key, &buffer[..]).read_to_end(&mut decrypted_buffer).expect("Cannot decrypt stream");
|
||||
if args.len() == 3 {
|
||||
let fname = format!("{} - {}.ogg", artists_strs.join(", "), track.name);
|
||||
let fname = format!("{}.ogg", id.to_base62());
|
||||
std::fs::write(&fname, &decrypted_buffer[0xa7..]).expect("Cannot write decrypted track");
|
||||
info!("Filename: {}", fname);
|
||||
} else {
|
||||
let album = core.run(Album::get(&session, track.album)).expect("Cannot get album metadata");
|
||||
// let album = core.run(Album::get(&session, track.album)).expect("Cannot get album metadata");
|
||||
let fname = format!("{}.ogg", id.to_base62());
|
||||
std::fs::write(&fname, &decrypted_buffer[0xa7..]).expect("Cannot write decrypted track");
|
||||
info!("Filename: {}", fname);
|
||||
let mut cmd = Command::new(args[3].to_owned());
|
||||
cmd.stdin(Stdio::piped());
|
||||
cmd.arg(id.to_base62()).arg(track.name).arg(album.name).args(artists_strs.iter());
|
||||
let mut child = cmd.spawn().expect("Could not run helper program");
|
||||
let pipe = child.stdin.as_mut().expect("Could not open helper stdin");
|
||||
pipe.write_all(&decrypted_buffer[0xa7..]).expect("Failed to write to stdin");
|
||||
assert!(child.wait().expect("Out of ideas for error messages").success(), "Helper script returned an error");
|
||||
cmd.arg(id.to_base62()).arg(track.name).args(artists_strs.iter());
|
||||
info!("Running Helper");
|
||||
cmd.spawn().expect("Could not run helper program");
|
||||
// let pipe = child.stdin.as_mut().expect("Could not open helper stdin");
|
||||
// pipe.write_all(&decrypted_buffer[0xa7..]).expect("Failed to write to stdin");
|
||||
// assert!(child.wait().expect("Out of ideas for error messages").success(), "Helper script returned an error");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue