Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Options:
By default files are saved in the current directory.

-S, --skip-albums <ALBUMS>
Skip downloading these albums, note that albums need to be delimited by ',' eg: -s 'one,two' or --skip-albums=one,two
Skip downloading these albums, note that albums need to be delimited by ',' eg: -S 'one,two' or --skip-albums=one,two

-l, --list-available
List albums/tracks available for download
Expand Down
6 changes: 4 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ note that `.mp3` is appended automatically.")]
pub(crate) track_format: Option<String>,

/// Skip downloading these albums, note that albums need to be delimited by ','
/// eg: -s 'one,two' or --skip-albums=one,two
/// eg: -S 'one,two' or --skip-albums=one,two
#[clap(short = 'S', long, value_name = "ALBUMS", value_delimiter = ',')]
pub(crate) skip_albums: Option<Vec<String>>,

Expand Down Expand Up @@ -95,7 +95,9 @@ impl Default for Config {
fn validate_format(f: &str) -> Result<String, String> {
let vars = format_container("", "", "", "");

strfmt(f, &vars).map_err(|err| err.to_string())
strfmt(f, &vars).map_err(|err| err.to_string())?;

Ok(f.to_string())
}

pub fn expand_tilde(p: &str) -> PathBuf {
Expand Down
6 changes: 2 additions & 4 deletions src/lib/spider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn scrape_by_application_ld_json(dom: &Html) -> Option<Album> {

vec![Track {
num: 1,
name: track_name.to_string().replace("/", ":"),
name: track_name.to_string(),
url,
lyrics: None,
album: album.clone(),
Expand All @@ -113,9 +113,7 @@ fn scrape_by_application_ld_json(dom: &Html) -> Option<Album> {

Some(Track {
num: track.get("position").i32(),
name: decode_html_entities(&track.get("item.name").to_string())
.replace("/", ":")
.into(),
name: decode_html_entities(&track.get("item.name").to_string()).into(),
url: decode_html_entities(&url).to_string(),
lyrics: Some(track.get("item.recordingOf.lyrics.text").to_string()),
album: album.clone(),
Expand Down
23 changes: 18 additions & 5 deletions src/lib/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ use std::{

use super::models::{Album, Track};

// Function to sanitize filenames by replacing invalid characters
fn sanitize_filename(filename: &str) -> String {
filename.replace(|c: char| {
c == '<' || c == '>' || c == ':' || c == '"' || c == '/' ||
c == '\\' || c == '|' || c == '?' || c == '*'
}, "_")
}

pub fn prepare_directory(path: Option<&PathBuf>, album: &Album) -> Result<PathBuf> {
let artist = sanitize_filename(&album.artist);
let album_name = sanitize_filename(&album.album);

let path = match path {
Some(path) => Path::new(path).join(&album.artist),
None => PathBuf::from(&album.artist),
Some(path) => Path::new(path).join(artist),
None => PathBuf::from(artist),
}
.join(&album.album);
.join(album_name);

if !path.exists() {
fs::create_dir_all(&path)?;
Expand All @@ -35,7 +46,8 @@ pub fn make_path(track: &Track, root: &Path, track_format: &String) -> PathBuf {
parse_track_template(track_format, track)
};

root.join(file_name).with_extension("mp3")
let sanitized_file_name = sanitize_filename(&file_name);
root.join(sanitized_file_name).with_extension("mp3")
}

pub fn track_path(track: &Track, root: &Path, track_format: &String) -> Result<PathBuf> {
Expand All @@ -45,7 +57,8 @@ pub fn track_path(track: &Track, root: &Path, track_format: &String) -> Result<P
parse_track_template(track_format, track)
};

let file = root.join(file_name).with_extension("mp3");
let sanitized_file_name = sanitize_filename(&file_name);
let file = root.join(sanitized_file_name).with_extension("mp3");

if file.exists() {
bail!("{} already exists", file.display())
Expand Down