diff options
Diffstat (limited to 'src/arg_parser.rs')
| -rw-r--r-- | src/arg_parser.rs | 60 | 
1 files changed, 58 insertions, 2 deletions
| diff --git a/src/arg_parser.rs b/src/arg_parser.rs index 8f9650a..6438af3 100644 --- a/src/arg_parser.rs +++ b/src/arg_parser.rs @@ -1,12 +1,15 @@  use clap::{App, ArgMatches};  use std::env; +use std::io; +use std::io::Write;  use std::path::Path;  use crate::actions::*;  use crate::download;  use crate::errors::*;  use crate::playback; +use crate::search;  use crate::structs::*;  pub fn download(state: &mut State, matches: &ArgMatches) -> Result<()> { @@ -60,14 +63,19 @@ pub fn play(state: &mut State, matches: &ArgMatches) -> Result<()> {      Ok(())  } -pub fn subscribe(state: &mut State, config: &Config, matches: &ArgMatches) -> Result<()> { +pub fn subscribe(state: &mut State, config: Config, matches: &ArgMatches) -> Result<()> {      let subscribe_matches = matches          .subcommand_matches("sub")          .or_else(|| matches.subcommand_matches("subscribe"))          .unwrap();      let url = subscribe_matches.value_of("URL").unwrap(); +    sub(state, config, url)?; +    Ok(()) +} + +fn sub(state: &mut State, config: Config, url: &str) -> Result<()> {      state.subscribe(url)?; -    download::download_rss(&config, url)?; +    download::download_rss(config, url)?;      Ok(())  } @@ -94,3 +102,51 @@ pub fn complete(app: &mut App, matches: &ArgMatches) -> Result<()> {      }      Ok(())  } + +pub fn search(state: &mut State, config: Config, matches: &ArgMatches) -> Result<()> { +    let matches = matches.subcommand_matches("search").unwrap(); +    let podcast = matches.value_of("PODCAST").unwrap(); +    let resp = search::search_for_podcast(podcast)?; +    if resp.found().is_empty() { +        println!("No Results"); +        return Ok(()); +    } + +    { +        let stdout = io::stdout(); +        let mut lock = stdout.lock(); +        for (i, r) in resp.found().iter().enumerate() { +            writeln!(&mut lock, "({}) {}", i, r)?; +        } +    } + +    print!("Would you like to subscribe to any of these? (y/n): "); +    io::stdout().flush().ok(); +    let mut input = String::new(); +    io::stdin().read_line(&mut input)?; +    if input.to_lowercase().trim() != "y" { +        return Ok(()); +    } + +    print!("Which one? (#): "); +    io::stdout().flush().ok(); +    let mut num_input = String::new(); +    io::stdin().read_line(&mut num_input)?; +    let n: usize = num_input.trim().parse()?; +    if n > resp.found().len() { +        eprintln!("Invalid!"); +        return Ok(()); +    } + +    let rss_resp = search::retrieve_rss(&resp.found()[n])?; +    if let Some(err) = rss_resp.error() { +        eprintln!("{}", err); +        return Ok(()); +    } +    match rss_resp.url() { +        Some(r) => sub(state, config, r)?, +        None => eprintln!("Subscription failed. No url in API response."), +    } +     +    Ok(()) +} | 
