diff options
| author | njaremko | 2017-07-17 19:58:04 -0400 | 
|---|---|---|
| committer | njaremko | 2017-07-17 19:58:04 -0400 | 
| commit | c981cf824f5d75423f88437361d915cb1233eb3a (patch) | |
| tree | bea037bea03603b4333bb1f2ad6a273bb1334eac /src | |
| parent | 4e128165278d92b5efae7054355037eadbde1724 (diff) | |
| download | podcast-c981cf824f5d75423f88437361d915cb1233eb3a.tar.bz2 | |
Fix download support
Diffstat (limited to 'src')
| -rw-r--r-- | src/actions.rs | 27 | ||||
| -rw-r--r-- | src/main.rs | 23 | ||||
| -rw-r--r-- | src/structs.rs | 17 | 
3 files changed, 47 insertions, 20 deletions
| diff --git a/src/actions.rs b/src/actions.rs index f126d7f..a228b74 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -27,6 +27,33 @@ pub fn list_subscriptions(state: State) {      }  } +pub fn download_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(); +            match episodes[episodes.len() - ep_num].download(podcast.title()) { +                Err(err) => println!("{}", err), +                _ => (), +            } +        } +    } +} + +pub fn download_all(state: State, p_search: &str) { +    let re_pod = Regex::new(&p_search).unwrap(); + +    for subscription in state.subscriptions() { +        if re_pod.is_match(&subscription.name) { +            let podcast = Podcast::from_url(&subscription.url).unwrap(); +            podcast.download(); +        } +    } +} +  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(); diff --git a/src/main.rs b/src/main.rs index cff7c45..2ab284c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,17 @@ fn main() {          .author("Nathan J. <njaremko@gmail.com>")          .about("Does awesome things")          .subcommand( +            SubCommand::with_name("download") +                .about("download 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").index(2)), +        ) +        .subcommand(              SubCommand::with_name("list")                  .about("list episodes of podcast")                  .arg( @@ -75,6 +86,14 @@ fn main() {          .get_matches();      match matches.subcommand_name() { +        Some("download") => { +            let download_matches = matches.subcommand_matches("download").unwrap(); +            let podcast = download_matches.value_of("PODCAST").unwrap(); +            match download_matches.value_of("EPISODE") { +                Some(ep) => download_episode(state, podcast, ep), +                None => download_all(state, podcast), +            } +        }          Some("list") => {              let list_matches = matches.subcommand_matches("list").unwrap();              match list_matches.value_of("PODCAST") { @@ -87,10 +106,6 @@ fn main() {              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( diff --git a/src/structs.rs b/src/structs.rs index 5e50307..ac1de96 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -102,25 +102,10 @@ impl Podcast {          result      } -    pub fn list_episodes(&self) -> Vec<&str> { -        let mut result = Vec::new(); - -        let items = self.0.items(); -        for item in items { -            match item.title() { -                Some(val) => result.push(val), -                None => (), -            } -        } -        result -    } -      pub fn download(&self) {          let mut path = get_podcast_dir();          path.push(self.title()); -        DirBuilder::new().recursive(true).create(path).unwrap(); -          let downloaded = already_downloaded(self.title());          for ep in self.episodes() { @@ -165,6 +150,7 @@ impl Episode {      pub fn download(&self, podcast_name: &str) -> Result<(), io::Error> {          let mut path = get_podcast_dir();          path.push(podcast_name); +        DirBuilder::new().recursive(true).create(&path).unwrap();          if let Some(url) = self.download_url() {              if let Some(title) = self.title() { @@ -172,7 +158,6 @@ impl Episode {                  let mut filename = String::from(title);                  filename.push_str(self.download_extension().unwrap());                  path.push(filename); -                  let mut file = File::create(&path)?;                  let mut resp = reqwest::get(url).unwrap();                  let mut content: Vec<u8> = Vec::new(); | 
