mirror of
https://github.com/GuillemCastro/spotify-dl.git
synced 2024-11-22 02:10:26 +01:00
commit
60bcf14403
2 changed files with 62 additions and 28 deletions
|
@ -32,16 +32,12 @@ impl FileSink {
|
||||||
|
|
||||||
impl Open for FileSink {
|
impl Open for FileSink {
|
||||||
fn open(path: Option<String>, _audio_format: AudioFormat) -> Self {
|
fn open(path: Option<String>, _audio_format: AudioFormat) -> Self {
|
||||||
if let Some(path) = path {
|
|
||||||
let file = path;
|
let file_path = path.unwrap_or_else(|| panic!());
|
||||||
FileSink {
|
FileSink {
|
||||||
sink: file,
|
sink: file_path,
|
||||||
content: Vec::new(),
|
content: Vec::new(),
|
||||||
metadata: None,
|
metadata: None
|
||||||
compression: 4,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
52
src/main.rs
52
src/main.rs
|
@ -3,6 +3,7 @@ mod file_sink;
|
||||||
extern crate rpassword;
|
extern crate rpassword;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
use librespot::core::config::SessionConfig;
|
use librespot::core::config::SessionConfig;
|
||||||
use librespot::core::session::Session;
|
use librespot::core::session::Session;
|
||||||
|
@ -69,6 +70,17 @@ async fn create_session(credentials: Credentials) -> Session {
|
||||||
session
|
session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_filename_compatible(filename: &str) -> String {
|
||||||
|
let invalid_chars = ['<', '>', ':', '\'', '"', '/', '\\', '|', '?', '*'];
|
||||||
|
let mut clean = String::new();
|
||||||
|
for c in filename.chars() {
|
||||||
|
if !invalid_chars.contains(&c) && c.is_ascii() && !c.is_control() && c.len_utf8() == 1 {
|
||||||
|
clean.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clean
|
||||||
|
}
|
||||||
|
|
||||||
async fn download_tracks(
|
async fn download_tracks(
|
||||||
session: &Session,
|
session: &Session,
|
||||||
destination: PathBuf,
|
destination: PathBuf,
|
||||||
|
@ -110,23 +122,45 @@ async fn download_tracks(
|
||||||
metadata.artists.push(artist_name.clone());
|
metadata.artists.push(artist_name.clone());
|
||||||
}
|
}
|
||||||
let full_track_name = format!("{} - {}", artist_name, metadata.track_name);
|
let full_track_name = format!("{} - {}", artist_name, metadata.track_name);
|
||||||
|
let full_track_name_clean = make_filename_compatible(full_track_name.as_str());
|
||||||
|
//let filename = format!("{}.flac", full_track_name_clean);
|
||||||
let filename: String;
|
let filename: String;
|
||||||
if ordered {
|
if ordered {
|
||||||
filename = format!("{:03} - {}.flac", i + 1, full_track_name);
|
filename = format!("{:03} - {}.flac", i + 1, full_track_name_clean);
|
||||||
} else {
|
} else {
|
||||||
filename = format!("{}.flac", full_track_name);
|
filename = format!("{}.flac", full_track_name_clean);
|
||||||
}
|
}
|
||||||
let joined_path = destination.join(&filename);
|
let joined_path = destination.join(&filename);
|
||||||
let path = joined_path.to_str().unwrap();
|
let path = joined_path.to_str().unwrap();
|
||||||
bar.set_message(full_track_name.as_str());
|
bar.set_message(full_track_name_clean.as_str());
|
||||||
|
|
||||||
|
|
||||||
|
let file_name = Path::new(path)
|
||||||
|
.file_stem()
|
||||||
|
.unwrap()
|
||||||
|
.to_str()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let path_parent = Path::new(path).parent().unwrap();
|
||||||
|
let entries = path_parent.read_dir().unwrap();
|
||||||
|
|
||||||
|
let mut file_exists = false;
|
||||||
|
for entry in entries {
|
||||||
|
let entry = entry.unwrap();
|
||||||
|
let entry_path = entry.path();
|
||||||
|
let entry_file_name = entry_path.file_stem().unwrap().to_str().unwrap();
|
||||||
|
if entry_file_name == file_name {
|
||||||
|
file_exists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !file_exists {
|
||||||
let mut file_sink = file_sink::FileSink::open(
|
let mut file_sink = file_sink::FileSink::open(
|
||||||
Some(path.to_owned()),
|
Some(path.to_owned()),
|
||||||
librespot::playback::config::AudioFormat::S16,
|
librespot::playback::config::AudioFormat::S16
|
||||||
);
|
);
|
||||||
file_sink.add_metadata(metadata);
|
file_sink.add_metadata(metadata);
|
||||||
if let Some(compression) = compression {
|
|
||||||
file_sink.set_compression(compression);
|
|
||||||
}
|
|
||||||
let (mut player, _) =
|
let (mut player, _) =
|
||||||
Player::new(player_config.clone(), session.clone(), None, move || {
|
Player::new(player_config.clone(), session.clone(), None, move || {
|
||||||
Box::new(file_sink)
|
Box::new(file_sink)
|
||||||
|
@ -135,6 +169,10 @@ async fn download_tracks(
|
||||||
player.await_end_of_track().await;
|
player.await_end_of_track().await;
|
||||||
player.stop();
|
player.stop();
|
||||||
bar.inc(1);
|
bar.inc(1);
|
||||||
|
} else {
|
||||||
|
// println!("File with the same name already exists, skipping: {}", path);
|
||||||
|
bar.inc(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bar.finish();
|
bar.finish();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue