diff options
| author | Nathan Jaremko | 2017-12-05 15:26:07 -0500 | 
|---|---|---|
| committer | Nathan Jaremko | 2017-12-05 15:26:07 -0500 | 
| commit | 978dbf71f22e3123b2f25154cfbe20a6e207dfe3 (patch) | |
| tree | c4cc1155775f88e1d829549060f9ebf3fb39989f | |
| parent | 2e8a59ebe8f076811d66072ec410320d7df1fd2e (diff) | |
| download | podcast-978dbf71f22e3123b2f25154cfbe20a6e207dfe3.tar.bz2 | |
Improve subscribing default behaviour
| -rw-r--r-- | README.md | 32 | ||||
| -rw-r--r-- | src/actions.rs | 43 | ||||
| -rw-r--r-- | src/main.rs | 24 | ||||
| -rw-r--r-- | src/structs.rs | 3 | ||||
| -rw-r--r-- | src/utils.rs | 23 | 
5 files changed, 59 insertions, 66 deletions
| diff --git a/README.md b/README.md deleted file mode 100644 index 1161056..0000000 --- a/README.md +++ /dev/null @@ -1,32 +0,0 @@ - # podcast - --- - `podcast` is a command line podcast player. -  - NOTE: Playback requires either mpv or vlc to be installed -  - It currently supports: -- [x] Subscribing to RSS feeds -- [x] Unsubscribing from RSS feeds -- [x] Streaming podcasts -- [x] Parallel downloading of multiple podcasts  -- [x] Playing podcasts -- [x] Auto-download new episodes -- [x] Automatically check for updates -- [ ] Auto-delete old episodes -- [ ] Shell Completions -    - [x] zsh -    - [ ] bash -    - [ ] sh -- [ ] Searching for podcasts...(WIP) - -By default, podcasts are downloaded to $HOME/Podcasts, but this folder can be set with the $PODCASTS environmental variable. - -How many latest episodes to download when subscibing to new podcasts can be set in the $PODCASTS/.config YAML file - -Downloads can be done a variety of ways: - -Individually: `podcast download $podcast_name 4` - -Multiple: `podcast download $podcast_name 1,5,9-12,14` - -All: `podcast download $podcast_name` diff --git a/src/actions.rs b/src/actions.rs index 76841bb..93ddf07 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -32,30 +32,29 @@ pub fn list_episodes(search: &str) {      }  } -pub fn download_rss(url: &str, config: &Config) { +pub fn subscribe_rss(url: &str) {      println!("Downloading RSS feed..."); -    let mut path = get_podcast_dir(); -    path.push(".rss"); -    DirBuilder::new().recursive(true).create(&path).unwrap(); -    let mut resp = reqwest::get(url).unwrap(); -    let mut content: Vec<u8> = Vec::new(); -    resp.read_to_end(&mut content).unwrap(); -    let channel = Channel::read_from(BufReader::new(&content[..])).unwrap(); -    let mut filename = String::from(channel.title()); -    filename.push_str(".xml"); -    path.push(filename); -    let mut file = File::create(&path).unwrap(); -    file.write_all(&content).unwrap(); - -    let download_limit = config.auto_download_limit as usize; -    if download_limit > 0 { -        let podcast = Podcast::from(channel); -        let episodes = podcast.episodes(); -        &episodes[..download_limit].par_iter().for_each(|ref ep| { -            if let Err(err) = ep.download(podcast.title()) { -                eprintln!("Error downloading {}: {}", podcast.title(), err); +    if let Err(err) = download_rss_feed(url) { +        eprintln!("Error: {}", err); +    } +} + +pub fn download_rss(config: &Config, url: &str) { +    println!("Downloading episode(s)..."); +    match download_rss_feed(url) { +        Ok(channel) => { +            let download_limit = config.auto_download_limit as usize; +            if download_limit > 0 { +                let podcast = Podcast::from(channel); +                let episodes = podcast.episodes(); +                &episodes[..download_limit].par_iter().for_each(|ref ep| { +                    if let Err(err) = ep.download(podcast.title()) { +                        eprintln!("Error downloading {}: {}", podcast.title(), err); +                    } +                });              } -        }); +        } +        Err(err) => eprintln!("Error: {}", err),      }  } diff --git a/src/main.rs b/src/main.rs index 5dab9b2..590a0ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,6 +92,12 @@ fn main() {                          .help("URL to RSS feed")                          .required(true)                          .index(1), +                ) +                .arg( +                    Arg::with_name("download") +                        .short("d") +                        .long("download") +                        .help("auto download based on config"),                  ),          )          .subcommand(SubCommand::with_name("refresh").about("refresh subscribed podcasts")) @@ -140,14 +146,16 @@ fn main() {                  None => play_latest(&state, podcast),              }          } -        Some("subscribe") => state.subscribe( -            matches -                .subcommand_matches("subscribe") -                .unwrap() -                .value_of("URL") -                .unwrap(), -            &config, -        ), +        Some("subscribe") => { +            let subscribe_matches = matches.subcommand_matches("subscribe").unwrap(); +            let url = subscribe_matches.value_of("URL").unwrap(); +            state.subscribe(url); +            if subscribe_matches.occurrences_of("download") > 0 { +                download_rss(&config, url); +            } else { +                subscribe_rss(url); +            } +        }          Some("search") => println!("This feature is coming soon..."),          Some("rm") => {              let rm_matches = matches.subcommand_matches("rm").unwrap(); diff --git a/src/structs.rs b/src/structs.rs index 97c30c2..817a0d1 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -114,7 +114,7 @@ impl State {          }      } -    pub fn subscribe(&mut self, url: &str, config: &Config) { +    pub fn subscribe(&mut self, url: &str) {          let mut set = BTreeSet::new();          for sub in self.subscriptions() {              set.insert(sub.title); @@ -130,7 +130,6 @@ impl State {          if let Err(err) = self.save() {              eprintln!("{}", err);          } -        download_rss(url, config);      }      pub fn subscriptions(&self) -> Vec<Subscription> { diff --git a/src/utils.rs b/src/utils.rs index a12a73f..789c7da 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,13 @@  use std::collections::HashSet;  use std::env; -use std::fs::{self, DirBuilder}; -use std::io; +use std::fs::{self, DirBuilder, File}; +use std::io::{self, BufReader, Read, Write};  use std::num::ParseIntError;  use std::path::PathBuf; +use reqwest; +use rss::Channel; +  pub fn trim_extension(filename: &str) -> Option<String> {      let name = String::from(filename);      match name.rfind('.') { @@ -90,6 +93,22 @@ pub fn get_xml_dir() -> PathBuf {      path  } +pub fn download_rss_feed(url: &str) -> Result<Channel, String> { +    let mut path = get_podcast_dir(); +    path.push(".rss"); +    DirBuilder::new().recursive(true).create(&path).unwrap(); +    let mut resp = reqwest::get(url).unwrap(); +    let mut content: Vec<u8> = Vec::new(); +    resp.read_to_end(&mut content).unwrap(); +    let channel = Channel::read_from(BufReader::new(&content[..])).unwrap(); +    let mut filename = String::from(channel.title()); +    filename.push_str(".xml"); +    path.push(filename); +    let mut file = File::create(&path).unwrap(); +    file.write_all(&content).unwrap(); +    Ok(channel) +} +  pub fn parse_download_episodes(e_search: &str) -> Result<Vec<usize>, ParseIntError> {      let input = String::from(e_search);      let mut ranges = Vec::<(usize, usize)>::new(); | 
