Browse Source

Added tooling for reading m3u playlists

master
Drew Short 4 years ago
parent
commit
a60a57463d
  1. 7
      src/command/dedupe.rs
  2. 0
      src/lib.rs
  3. 33
      src/lib/m3u.rs
  4. 1
      src/lib/mod.rs
  5. 1
      src/main.rs
  6. 7
      test.m3u

7
src/command/dedupe.rs

@ -1,6 +1,8 @@
use clap::{Arg, Error, ErrorKind};
use clap_nested::{Command};
use crate::lib::m3u::{Playlist};
pub fn get_command<'a>() -> Command<'a, str>{
Command::new("dedupe")
.description("dedupe a m3u playlist")
@ -16,8 +18,9 @@ pub fn get_command<'a>() -> Command<'a, str>{
// println!("Running dedupe, logging = {}", args);
match matches.values_of("playlist") {
Some(playlists) => {
for playlist in playlists {
println!("{}", playlist)
for path in playlists {
let playlist = Playlist::read(path)?;
println!("{:?}", playlist);
}
Ok(())
},

0
src/lib.rs

33
src/lib/m3u.rs

@ -0,0 +1,33 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::string::String;
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
#[derive(Debug)]
pub struct Playlist {
path: String,
tracks: Box<Vec<String>>
}
impl Playlist {
pub fn read(path: &str) -> Result<Playlist, io::Error> {
let playlist_path: String = String::from(path);
let mut playlist_tracks: Vec<String> = Vec::new();
let lines = read_lines(path)?;
lines.for_each(|read_line| {
if read_line.is_ok() {
let line = read_line.unwrap();
if line.len() > 0 && !line.starts_with("#") {
playlist_tracks.push(line)
}
}
});
Ok(Playlist { path: playlist_path, tracks: Box::new(playlist_tracks) })
}
}

1
src/lib/mod.rs

@ -0,0 +1 @@
pub mod m3u;

1
src/main.rs

@ -2,6 +2,7 @@ use clap::{Arg};
use clap_nested::{Commander};
mod command;
mod lib;
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");

7
test.m3u

@ -0,0 +1,7 @@
#EXTM3U
#EXTINF:123, Sample artist - Sample title
C:\Documents and Settings\I\My Music\Sample.mp3
#EXTINF:321,Example Artist - Example title
C:\Documents and Settings\I\My Music\Greatest Hits\Example.ogg
Loading…
Cancel
Save