2021-11-03 18:57:16 +00:00
|
|
|
use anyhow::Result;
|
2023-01-12 00:15:53 +00:00
|
|
|
use bliss_audio_symphonia::playlist::{closest_to_first_song, dedup_playlist, euclidean_distance};
|
|
|
|
use bliss_audio_symphonia::{analyze_paths, Song};
|
2021-11-27 12:13:54 +00:00
|
|
|
use clap::{App, Arg};
|
2021-11-03 18:57:16 +00:00
|
|
|
use glob::glob;
|
|
|
|
use std::env;
|
|
|
|
use std::fs;
|
2021-11-15 20:07:00 +00:00
|
|
|
use std::io::BufReader;
|
|
|
|
use std::path::{Path, PathBuf};
|
2021-11-03 18:57:16 +00:00
|
|
|
|
|
|
|
/* Analyzes a folder recursively, and make a playlist out of the file
|
|
|
|
* provided by the user. */
|
2021-11-27 12:13:54 +00:00
|
|
|
// How to use: ./playlist [-o file.m3u] [-a analysis.json] <folder> <file to start the playlist from>
|
2021-11-03 18:57:16 +00:00
|
|
|
fn main() -> Result<()> {
|
2021-11-27 12:13:54 +00:00
|
|
|
let matches = App::new("playlist")
|
|
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
|
|
.author("Polochon_street")
|
|
|
|
.about("Analyze a folder and make a playlist from a target song")
|
|
|
|
.arg(Arg::with_name("output-playlist").short("o").long("output-playlist")
|
|
|
|
.value_name("PLAYLIST.M3U")
|
|
|
|
.help("Outputs the playlist to a file.")
|
|
|
|
.takes_value(true))
|
|
|
|
.arg(Arg::with_name("analysis-file").short("a").long("analysis-file")
|
|
|
|
.value_name("ANALYSIS.JSON")
|
|
|
|
.help("Use the songs that have been analyzed in <analysis-file>, and appends newly analyzed songs to it. Defaults to /tmp/analysis.json.")
|
|
|
|
.takes_value(true))
|
|
|
|
.arg(Arg::with_name("FOLDER").help("Folders containing some songs.").required(true))
|
|
|
|
.arg(Arg::with_name("FIRST-SONG").help("Song to start from (can be outside of FOLDER).").required(true))
|
|
|
|
.get_matches();
|
|
|
|
|
|
|
|
let folder = matches.value_of("FOLDER").unwrap();
|
|
|
|
let file = fs::canonicalize(matches.value_of("FIRST-SONG").unwrap())?;
|
2021-11-03 18:57:16 +00:00
|
|
|
let pattern = Path::new(folder).join("**").join("*");
|
2021-11-15 20:07:00 +00:00
|
|
|
|
|
|
|
let mut songs: Vec<Song> = Vec::new();
|
2021-11-27 12:13:54 +00:00
|
|
|
let analysis_path = matches
|
|
|
|
.value_of("analysis-file")
|
|
|
|
.unwrap_or("/tmp/analysis.json");
|
|
|
|
let analysis_file = fs::File::open(analysis_path);
|
2021-11-15 20:07:00 +00:00
|
|
|
if let Ok(f) = analysis_file {
|
|
|
|
let reader = BufReader::new(f);
|
|
|
|
songs = serde_json::from_reader(reader)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let analyzed_paths = songs
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.path.to_owned())
|
|
|
|
.collect::<Vec<PathBuf>>();
|
|
|
|
|
|
|
|
let paths = glob(&pattern.to_string_lossy())?
|
2021-11-03 18:57:16 +00:00
|
|
|
.map(|e| fs::canonicalize(e.unwrap()).unwrap())
|
|
|
|
.filter(|e| match mime_guess::from_path(e).first() {
|
|
|
|
Some(m) => m.type_() == "audio",
|
|
|
|
None => false,
|
|
|
|
})
|
|
|
|
.map(|x| x.to_string_lossy().to_string())
|
|
|
|
.collect::<Vec<String>>();
|
2021-11-15 20:07:00 +00:00
|
|
|
|
2022-04-02 18:30:43 +01:00
|
|
|
let song_iterator = analyze_paths(
|
2021-11-15 20:07:00 +00:00
|
|
|
paths
|
|
|
|
.iter()
|
|
|
|
.filter(|p| !analyzed_paths.contains(&PathBuf::from(p)))
|
|
|
|
.map(|p| p.to_owned())
|
2022-04-14 22:52:32 +01:00
|
|
|
.collect::<Vec<String>>(),
|
2022-04-02 18:30:43 +01:00
|
|
|
);
|
2022-04-02 18:47:06 +01:00
|
|
|
let first_song = Song::from_path(file)?;
|
2021-11-03 18:57:16 +00:00
|
|
|
let mut analyzed_songs = vec![first_song.to_owned()];
|
2022-04-02 18:30:43 +01:00
|
|
|
for (path, result) in song_iterator {
|
2021-11-03 18:57:16 +00:00
|
|
|
match result {
|
|
|
|
Ok(song) => analyzed_songs.push(song),
|
2022-09-22 18:13:27 +01:00
|
|
|
Err(e) => println!("error analyzing {}: {}", path.display(), e),
|
2021-11-03 18:57:16 +00:00
|
|
|
};
|
|
|
|
}
|
2021-11-15 20:07:00 +00:00
|
|
|
analyzed_songs.extend_from_slice(&songs);
|
|
|
|
let serialized = serde_json::to_string(&analyzed_songs).unwrap();
|
2022-04-11 16:26:05 +01:00
|
|
|
let mut songs_to_chose_from: Vec<_> = analyzed_songs
|
2021-11-15 20:07:00 +00:00
|
|
|
.into_iter()
|
|
|
|
.filter(|x| x == &first_song || paths.contains(&x.path.to_string_lossy().to_string()))
|
|
|
|
.collect();
|
|
|
|
closest_to_first_song(&first_song, &mut songs_to_chose_from, euclidean_distance);
|
|
|
|
dedup_playlist(&mut songs_to_chose_from, None);
|
2021-11-27 12:13:54 +00:00
|
|
|
|
|
|
|
fs::write(analysis_path, serialized)?;
|
2021-11-15 20:07:00 +00:00
|
|
|
let playlist = songs_to_chose_from
|
2021-11-03 18:57:16 +00:00
|
|
|
.iter()
|
|
|
|
.map(|s| s.path.to_string_lossy().to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n");
|
2021-11-27 12:13:54 +00:00
|
|
|
if let Some(m) = matches.value_of("output-playlist") {
|
|
|
|
fs::write(m, playlist)?;
|
|
|
|
} else {
|
|
|
|
println!("{}", playlist);
|
|
|
|
}
|
2021-11-03 18:57:16 +00:00
|
|
|
Ok(())
|
|
|
|
}
|