aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Jaremko2017-07-25 01:15:55 -0400
committerNathan Jaremko2017-07-25 01:15:55 -0400
commitcd7b8cb559199c609331b5851076f5a0ed9e1d54 (patch)
treee17a8b057bad192231f8325f46451e2990aee893
parent7c40d1d07eb32e7e7fe923241b38ed7057338dc1 (diff)
downloadpodcast-cd7b8cb559199c609331b5851076f5a0ed9e1d54.tar.bz2
Less cloning
-rw-r--r--src/actions.rs42
-rw-r--r--src/structs.rs53
2 files changed, 48 insertions, 47 deletions
diff --git a/src/actions.rs b/src/actions.rs
index 8f54359..5c67c40 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -11,10 +11,10 @@ use utils::*;
pub fn list_episodes(state: &State, search: &str) {
let re = Regex::new(search).unwrap();
- for podcast in state.subscriptions() {
- if re.is_match(&podcast.title()) {
- println!("Episodes for {}:", &podcast.title());
- match Podcast::from_url(&podcast.url()) {
+ for podcast in &state.subs {
+ if re.is_match(&podcast.title) {
+ println!("Episodes for {}:", &podcast.title);
+ match Podcast::from_url(&podcast.url) {
Ok(podcast) => {
let episodes = podcast.episodes();
for (index, episode) in episodes.iter().enumerate() {
@@ -56,9 +56,9 @@ pub fn download_rss(url: &str, config: &Config) {
pub fn update_rss(state: &mut State) {
println!("Checking for new episodes...");
- state.subs.par_iter_mut().for_each(|mut sub| {
+ &state.subs.par_iter_mut().for_each(|mut sub| {
let mut path = get_podcast_dir();
- path.push(sub.title());
+ path.push(&sub.title);
DirBuilder::new().recursive(true).create(&path).unwrap();
let mut titles = HashSet::new();
@@ -67,7 +67,7 @@ pub fn update_rss(state: &mut State) {
titles.insert(trim_extension(&entry.file_name().into_string().unwrap()));
}
- let mut resp = reqwest::get(&sub.url()).unwrap();
+ let mut resp = reqwest::get(&sub.url).unwrap();
let mut content: Vec<u8> = Vec::new();
resp.read_to_end(&mut content).unwrap();
let podcast = Podcast::from(Channel::read_from(BufReader::new(&content[..])).unwrap());
@@ -92,17 +92,17 @@ pub fn update_rss(state: &mut State) {
}
pub fn list_subscriptions(state: &State) {
- for podcast in state.subscriptions() {
- println!("{}", podcast.title());
+ for podcast in &state.subscriptions() {
+ println!("{}", &podcast.title);
}
}
pub fn download_range(state: &State, p_search: &str, e_search: &str) {
let re_pod = Regex::new(p_search).unwrap();
- for subscription in state.subscriptions() {
- if re_pod.is_match(&subscription.title()) {
- let podcast = Podcast::from_url(&subscription.url()).unwrap();
+ for subscription in &state.subs {
+ if re_pod.is_match(&subscription.title) {
+ let podcast = Podcast::from_url(&subscription.url).unwrap();
match parse_download_episodes(e_search) {
Ok(episodes_to_download) => podcast.download_specific(episodes_to_download),
Err(err) => eprintln!("{}", err),
@@ -116,9 +116,9 @@ 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.title()) {
- let podcast = Podcast::from_url(&subscription.url()).unwrap();
+ for subscription in &state.subs {
+ if re_pod.is_match(&subscription.title) {
+ let podcast = Podcast::from_url(&subscription.url).unwrap();
let episodes = podcast.episodes();
if let Err(err) = episodes[episodes.len() - ep_num].download(podcast.title()) {
eprintln!("{}", err);
@@ -130,9 +130,9 @@ pub fn download_episode(state: &State, p_search: &str, e_search: &str) {
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.title()) {
- let podcast = Podcast::from_url(&subscription.url()).unwrap();
+ for subscription in &state.subs {
+ if re_pod.is_match(&subscription.title) {
+ let podcast = Podcast::from_url(&subscription.url).unwrap();
podcast.download();
}
}
@@ -151,9 +151,9 @@ pub fn play_episode(state: &State, p_search: &str, ep_num_string: &str) {
);
return;
}
- for subscription in state.subscriptions() {
- if re_pod.is_match(&subscription.title()) {
- let mut filename = String::from(subscription.title());
+ for subscription in &state.subs {
+ if re_pod.is_match(&subscription.title) {
+ let mut filename = subscription.title.clone();
filename.push_str(".xml");
path.push(filename);
diff --git a/src/structs.rs b/src/structs.rs
index d620c43..45191ea 100644
--- a/src/structs.rs
+++ b/src/structs.rs
@@ -53,16 +53,6 @@ pub struct Subscription {
pub num_episodes: usize,
}
-impl Subscription {
- pub fn title(&self) -> String {
- self.title.clone()
- }
-
- pub fn url(&self) -> String {
- self.url.clone()
- }
-}
-
#[derive(Serialize, Deserialize, Clone)]
pub struct State {
pub last_run_time: DateTime<Utc>,
@@ -70,24 +60,35 @@ pub struct State {
}
impl State {
- pub fn new() -> Result<State, serde_json::Error> {
+ pub fn new() -> Result<State, String> {
let mut path = get_podcast_dir();
- DirBuilder::new().recursive(true).create(&path).unwrap();
+ if let Err(err) = DirBuilder::new().recursive(true).create(&path) {
+ return Err(format!(
+ "Couldn't create directory: {}\nReason: {}",
+ path.to_str().unwrap(),
+ err
+ ))
+ }
path.push(".subscriptions");
if path.exists() {
let mut s = String::new();
File::open(&path).unwrap().read_to_string(&mut s).unwrap();
- let mut state: State = serde_json::from_str(&s)?;
+ match serde_json::from_str(&s) {
+ Ok(val) => {
+ let mut state: State = val;
// Check if a day has passed (86400 seconds)
- if state
- .last_run_time
- .signed_duration_since(Utc::now())
- .num_seconds() < -86400
- {
- update_rss(&mut state);
+ if state
+ .last_run_time
+ .signed_duration_since(Utc::now())
+ .num_seconds() < -86400
+ {
+ update_rss(&mut state);
+ }
+ state.last_run_time = Utc::now();
+ Ok(state)
+ },
+ Err(_) => Err(format!("Failed to parse .subscriptions ... I probably changed the schema ... sorry"))
}
- state.last_run_time = Utc::now();
- Ok(state)
} else {
Ok(State {
last_run_time: Utc::now(),
@@ -99,7 +100,7 @@ impl State {
pub fn subscribe(&mut self, url: &str, config: &Config) {
let mut set = BTreeSet::new();
for sub in self.subscriptions() {
- set.insert(sub.title());
+ set.insert(sub.title);
}
let podcast = Podcast::from(Channel::from_url(url).unwrap());
if !set.contains(podcast.title()) {
@@ -184,7 +185,7 @@ impl Podcast {
self.episodes().par_iter().for_each(
|ref i| if let Some(ep_title) =
- i.title()
+ i.title()
{
if !downloaded.contains(ep_title) {
if let Err(err) = i.download(self.title()) {
@@ -192,7 +193,7 @@ impl Podcast {
}
}
},
- );
+ );
}
pub fn download_specific(&self, episode_numbers: Vec<usize>) {
@@ -204,7 +205,7 @@ impl Podcast {
episode_numbers.par_iter().for_each(
|ep_num| if let Some(ep_title) =
- episodes[episodes.len() - ep_num].title()
+ episodes[episodes.len() - ep_num].title()
{
if !downloaded.contains(ep_title) {
if let Err(err) = episodes[episodes.len() - ep_num].download(self.title()) {
@@ -212,7 +213,7 @@ impl Podcast {
}
}
},
- );
+ );
}
}