1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
//! `freesound-credits` is a simple program to generate Freesound credits in a usable markdown file.
//!
//! # Usage
//!
//! ```text
//! Simple program to generate Freesound credits in a usable markdown file
//!
//! Usage: freesound-credits [OPTIONS] --path <PATH> --title <TITLE> --date <DATE> --artist <ARTIST>
//!
//! Options:
//! -p, --path <PATH> Path to the samples directory
//! -t, --title <TITLE> Song title (quote multiple words)
//! -d, --date <DATE> Song release date (quote multiple words)
//! -a, --artist <ARTIST> Song artist (quote multiple words)
//! -z, --zola Optionally include Zola frontmatter atop the markdown file
//! -h, --help Print help
//! -V, --version Print version
//! ```
//! `
//! # Example
//!
//! Run against an Ableton samples directory (also generating the Zola frontmatter)
//!
//! ```text
//! freesound-credits -p Samples/Imported/ -t "Field Notes" -a "Aner Andros" -d "2017-10-28" -z
//! ```
use clap::Parser;
use std::iter::FromIterator;
use std::path::Path;
use std::process;
/// A simple program to generate Freesound credits in a usable markdown file.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Path to the samples directory
#[arg(short, long)]
pub path: String,
/// Song title (quote multiple words)
#[arg(short, long)]
pub title: String,
/// Song release date (quote multiple words)
#[arg(short, long)]
pub date: String,
/// Song artist (quote multiple words)
#[arg(short, long)]
pub artist: String,
/// Optionally include Zola frontmatter atop the markdown file
#[arg(short, long)]
pub zola: bool,
}
/// Derives the markdown filename from the song title.
///
/// # Example
///
/// For a song titled “Field Notes” the resulting markdown file is `field-notes-credits.md`
///
pub fn set_filename(song_title: &str) -> String {
let credits_file: String = format!(
"{}-credits.md",
song_title.replace(&[' ', '\''][..], "-").to_lowercase()
);
credits_file
}
/// Derives a [Zola](https://www.getzola.org) page
/// [frontmatter](https://www.getzola.org/documentation/content/page/#front-matter)
/// header from given song details.
///
/// The frontmatter is a header, and it is placed atop the generated markdown file.
///
/// # Example
///
/// For a song titled “Field Notes” by “Aner Andros” with date "2017-10-28"
///
/// ```toml
/// +++
/// title="Field Notes Freesound Credits"
/// date=2017-10-28
///
/// [taxonomies]
/// tags=["Freesound", "Aner Andros", "Credits"]
/// +++
/// ```
///
pub fn set_frontmatter(song_title: &str, song_date: &str, song_artist: &str) -> String {
format!(
"+++
title=\"{song_title} Freesound Credits\"
date={song_date}
[taxonomies]
tags=[\"Freesound\", \"{song_artist}\", \"Credits\"]
+++
"
)
}
/// Paragraph notifying the song uses [Creative
/// Commons](https://creativecommons.org) licensed samples, with links.
///
/// The given song title is included in the paragraph, unchanged.
///
/// # Example
///
/// For a song titled “Field Notes”
///
/// ```markdown
/// ## Credits
///
/// *Field Notes* includes the following samples from
/// [Freesound](https://freesound.org). Used under a [Creative
/// Commons](https://creativecommons.org) license:
/// ````
///
pub fn set_header(song_title: &str) -> String {
format!(
"## Credits
*{song_title}* includes the following samples from
[Freesound](https://freesound.org). Used under a [Creative
Commons](https://creativecommons.org) license:
",
)
}
/// Scans the given directory for Freesound samples to credit.
///
/// # Notes
///
/// - the user must have permissions on the directory.
///
pub fn get_list_of_samples(samples_path: &str) -> Vec<String> {
let path: &Path = Path::new(&samples_path);
let mut all_samples: Vec<String> = vec![];
for entry in path
.read_dir()
.unwrap_or_else(|error| {
eprintln!("Problem listing samples from the provided path: {error}");
process::exit(2);
})
.flatten()
{
if entry.path().is_file() || entry.path().is_dir() {
let mut sample: String = format!(
"{:?}",
entry.path().file_stem().unwrap_or_else(|| {
eprintln!("Problem reading the sample file name.");
process::exit(2);
})
)
.replace(&['(', ')', '\'', '"'][..], "");
// Files specific: checks against DAWs metadata file extensions
if let Some(extension) = entry.path().extension() {
if is_not_metadata(extension.to_str().unwrap()) && is_freesound_sample(&sample) {
all_samples.push(sample);
}
// Renoise projects specific
} else if sample.contains("Instrument") {
sample = sample
.split_whitespace()
.last()
.unwrap_or_else(|| {
eprintln!("Problem splitting Instrument into sample string");
process::exit(2);
})
.to_string();
if is_freesound_sample(&sample) {
all_samples.push(sample);
}
}
}
}
all_samples
}
fn is_not_metadata(extension: &str) -> bool {
let metadata_extensions = ["asd", "reapeaks"];
!metadata_extensions.contains(&extension)
}
/// Private helper function to validate Freesound samples we care about.
fn is_freesound_sample(sample: &str) -> bool {
sample.chars().next().unwrap().is_numeric() && sample.contains('_')
}
/// Extrapolate the sample to credit based on [Freesound](https://freesound.org) naming standards.
///
/// # Notes
///
/// This programs only matches for Freesound samples that maintain their original sample names.
///
/// # Examples
///
/// - new standard with double underscore: `69604__timkahn__subverse_whisper.wav`
/// - old standard with single underscore: `2166_suburban_grilla_bowl_struck.flac`
///
pub fn set_credit(sample: &str) -> String {
let mut sample_line_vec: Vec<&str> = vec![];
if sample.contains("__") {
sample_line_vec = sample.split("__").collect();
} else if sample.contains('_') {
sample_line_vec = sample.split('_').collect();
}
let credit_id: String = sample_line_vec
.first()
.unwrap_or_else(|| {
eprintln!("Problem reading credit ID");
process::exit(2);
})
.to_string();
let credit_artist: String = sample_line_vec
.get(1)
.unwrap_or_else(|| {
eprintln!("Problem reading credit artist");
process::exit(2);
})
.to_string();
let credit_parts_to_end: Vec<&str> = Vec::from_iter(sample_line_vec[2..].iter().cloned());
let credit_sound: String = credit_parts_to_end.join("_");
let credit_line: String = format!(
"- [{credit_sound}](https://freesound.org/people/{credit_artist}/sounds/{credit_id}/)\n",
);
credit_line
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_filename() {
let song_title = "Field Notes";
assert_eq!("field-notes-credits.md", set_filename(song_title));
}
#[test]
fn check_frontmatter() {
let song_title = "Field Notes";
let song_artist = "Aner Andros";
let song_date = "2017-10-28";
let frontmatter = "+++
title=\"Field Notes Freesound Credits\"
date=2017-10-28
[taxonomies]
tags=[\"Freesound\", \"Aner Andros\", \"Credits\"]
+++
";
assert_eq!(
frontmatter,
set_frontmatter(song_title, song_date, song_artist)
);
}
#[test]
fn check_header() {
let song_title = "Field Notes";
let header = "## Credits
*Field Notes* includes the following samples from
[Freesound](https://freesound.org). Used under a [Creative
Commons](https://creativecommons.org) license:
";
assert_eq!(header, set_header(song_title));
}
#[test]
fn check_credit_new() {
let credit = "275012__alienxxx__squadron_leader_form_up";
assert_eq!(
"- [squadron_leader_form_up](https://freesound.org/people/alienxxx/sounds/275012/)\n",
set_credit(credit)
);
}
#[test]
fn check_credit_old() {
let credit = "275012_alienxxx_squadron_leader_form_up";
assert_eq!(
"- [squadron_leader_form_up](https://freesound.org/people/alienxxx/sounds/275012/)\n",
set_credit(credit)
);
}
}