use device id & fix merge

This commit is contained in:
Niklas 2024-01-17 13:07:52 +01:00
parent 60bcf14403
commit de64ad6b6d
4 changed files with 333 additions and 74 deletions

View file

@ -32,12 +32,12 @@ impl FileSink {
impl Open for FileSink {
fn open(path: Option<String>, _audio_format: AudioFormat) -> Self {
let file_path = path.unwrap_or_else(|| panic!());
FileSink {
sink: file_path,
content: Vec::new(),
metadata: None
metadata: None,
compression: 4,
}
}
}

View file

@ -2,8 +2,8 @@ mod file_sink;
extern crate rpassword;
use std::path::PathBuf;
use std::path::Path;
use std::path::PathBuf;
use librespot::core::config::SessionConfig;
use librespot::core::session::Session;
@ -63,7 +63,8 @@ pub struct TrackMetadata {
}
async fn create_session(credentials: Credentials) -> Session {
let session_config = SessionConfig::default();
let mut session_config = SessionConfig::default();
session_config.device_id = machine_uid::get().unwrap();
let session = Session::connect(session_config, credentials, None)
.await
.unwrap();
@ -74,7 +75,7 @@ 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 {
if !invalid_chars.contains(&c) && c.is_ascii() && !c.is_control() && c.len_utf8() == 1 {
clean.push(c);
}
}
@ -121,6 +122,7 @@ async fn download_tracks(
.name;
metadata.artists.push(artist_name.clone());
}
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);
@ -134,44 +136,40 @@ async fn download_tracks(
let path = joined_path.to_str().unwrap();
bar.set_message(full_track_name_clean.as_str());
let file_name = Path::new(path).file_stem().unwrap().to_str().unwrap();
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 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;
}
}
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(
Some(path.to_owned()),
librespot::playback::config::AudioFormat::S16
);
file_sink.add_metadata(metadata);
let (mut player, _) =
let mut file_sink = file_sink::FileSink::open(
Some(path.to_owned()),
librespot::playback::config::AudioFormat::S16,
);
file_sink.add_metadata(metadata);
file_sink.set_compression(compression.unwrap_or(4));
let (mut player, _) =
Player::new(player_config.clone(), session.clone(), None, move || {
Box::new(file_sink)
});
player.load(*track, true, 0);
player.await_end_of_track().await;
player.stop();
bar.inc(1);
player.load(*track, true, 0);
player.await_end_of_track().await;
player.stop();
bar.inc(1);
} else {
// println!("File with the same name already exists, skipping: {}", path);
bar.inc(1);
// println!("File with the same name already exists, skipping: {}", path);
bar.inc(1);
}
}
bar.finish();