You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.8 KiB
50 lines
1.8 KiB
use clap::{Arg, Error, ErrorKind};
|
|
use clap_nested::{Command};
|
|
|
|
use crate::lib::m3u::{Playlist, PlaylistTrack};
|
|
use std::collections::HashSet;
|
|
|
|
fn dedupe(playlist_tracks: Vec<PlaylistTrack>) -> Vec<PlaylistTrack> {
|
|
let mut seen_tracks: HashSet<String> = HashSet::new();
|
|
let mut dedupe_playlist_tracks = Vec::new();
|
|
for track in playlist_tracks {
|
|
if ! seen_tracks.contains(&track.track) {
|
|
seen_tracks.insert(String::from(&track.track));
|
|
dedupe_playlist_tracks.push(track);
|
|
}
|
|
}
|
|
dedupe_playlist_tracks
|
|
}
|
|
|
|
pub fn get_command<'a>() -> Command<'a, str>{
|
|
Command::new("dedupe")
|
|
.description("dedupe a m3u playlist")
|
|
.options(|app| {
|
|
app.arg(Arg::with_name("playlist")
|
|
.help("Path of the playlist(s) to de-duplicate")
|
|
.index(1)
|
|
.multiple(true)
|
|
.required(true)
|
|
)
|
|
})
|
|
.runner(| _args, matches| {
|
|
let dry_run = matches.is_present("dry-run");
|
|
match matches.values_of("playlist") {
|
|
Some(playlists) => {
|
|
for path in playlists {
|
|
let playlist = Playlist::read(path)?;
|
|
let dedupe_playlist_tracks: Vec<PlaylistTrack> = dedupe(*playlist.tracks);
|
|
let dedupe_playlist = Playlist::new(&path, dedupe_playlist_tracks);
|
|
if ! dry_run {
|
|
dedupe_playlist.write(path)?
|
|
} else {
|
|
println!("{:?}", dedupe_playlist);
|
|
}
|
|
}
|
|
Ok(())
|
|
},
|
|
None => Err(
|
|
Error::with_description("playlist must be provided", ErrorKind::EmptyValue))
|
|
}
|
|
})
|
|
}
|