aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNathan Jaremko2017-12-05 15:26:07 -0500
committerNathan Jaremko2017-12-05 15:26:07 -0500
commit978dbf71f22e3123b2f25154cfbe20a6e207dfe3 (patch)
treec4cc1155775f88e1d829549060f9ebf3fb39989f /src
parent2e8a59ebe8f076811d66072ec410320d7df1fd2e (diff)
downloadpodcast-978dbf71f22e3123b2f25154cfbe20a6e207dfe3.tar.bz2
Improve subscribing default behaviour
Diffstat (limited to 'src')
-rw-r--r--src/actions.rs43
-rw-r--r--src/main.rs24
-rw-r--r--src/structs.rs3
-rw-r--r--src/utils.rs23
4 files changed, 59 insertions, 34 deletions
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();