diff options
| author | njaremko | 2017-07-17 19:27:45 -0400 |
|---|---|---|
| committer | njaremko | 2017-07-17 19:27:45 -0400 |
| commit | 4e128165278d92b5efae7054355037eadbde1724 (patch) | |
| tree | a19539fe67af54bd81c7db18da4134af36a9535e | |
| parent | 57b19c651f2193e4331085585e6385b153c4b0ec (diff) | |
| download | podcast-4e128165278d92b5efae7054355037eadbde1724.tar.bz2 | |
Add play support
| -rw-r--r-- | src/actions.rs | 28 | ||||
| -rw-r--r-- | src/main.rs | 30 |
2 files changed, 55 insertions, 3 deletions
diff --git a/src/actions.rs b/src/actions.rs index 0f8d337..f126d7f 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,5 +1,6 @@ use regex::Regex; use structs::*; +use std::process::{Command, Stdio}; pub fn list_episodes(state: State, search: &str) { let re = Regex::new(&search).unwrap(); @@ -8,8 +9,9 @@ pub fn list_episodes(state: State, search: &str) { println!("Episodes for {}:", &podcast.name); match Podcast::from_url(&podcast.url) { Ok(podcast) => { - for title in podcast.list_episodes() { - println!("{}", title) + let episodes = podcast.episodes(); + for (index, episode) in episodes.iter().enumerate() { + println!("({}) {}", episodes.len() - index, episode.title().unwrap()); } } Err(err) => println!("{}", err), @@ -24,3 +26,25 @@ pub fn list_subscriptions(state: State) { println!("{}", podcast.name); } } + +pub fn stream_episode(state: State, p_search: &str, e_search: &str) { + let re_pod = Regex::new(&p_search).unwrap(); + let ep_num = e_search.parse::<usize>().unwrap(); + + for subscription in state.subscriptions() { + if re_pod.is_match(&subscription.name) { + let podcast = Podcast::from_url(&subscription.url).unwrap(); + let episodes = podcast.episodes(); + launch_mpv(episodes[episodes.len() - ep_num].download_url().unwrap()); + } + } +} + +fn launch_mpv(url: &str) { + Command::new("mpv") + .args(&["--audio-display=no", url]) + .stdin(Stdio::inherit()) + .stdout(Stdio::inherit()) + .output() + .expect("failed to execute process"); +} diff --git a/src/main.rs b/src/main.rs index 05c8bf2..cff7c45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,9 @@ extern crate serde_json; #[macro_use] extern crate serde_derive; extern crate clap; +use std::fs::File; +use std::io::BufReader; +use rss::Channel; mod actions; mod structs; @@ -28,11 +31,26 @@ fn main() { .arg( Arg::with_name("PODCAST") .help("Regex for subscribed podcast") - //.required(true) .index(1), ), ) .subcommand( + SubCommand::with_name("play") + .about("list episodes of podcast") + .arg( + Arg::with_name("PODCAST") + .help("Regex for subscribed podcast") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("EPISODE") + .help("Episode index") + .required(true) + .index(2), + ), + ) + .subcommand( SubCommand::with_name("search") .about("searches for podcasts") .arg( @@ -64,6 +82,16 @@ fn main() { None => list_subscriptions(state), } } + Some("play") => { + let play_matches = matches.subcommand_matches("play").unwrap(); + let podcast = play_matches.value_of("PODCAST").unwrap(); + let episode = play_matches.value_of("EPISODE").unwrap(); + stream_episode(state, podcast, episode); + // let file = File::open("rss.xml").unwrap(); + // let channel = Channel::read_from(BufReader::new(file)).unwrap(); + // let ep = Episode::from(channel.items()[20].clone()); + // stream_episode(ep); + } Some("subscribe") => { state.subscribe( matches |
