diff options
| author | Nathan Jaremko | 2017-07-24 23:52:15 -0400 |
|---|---|---|
| committer | Nathan Jaremko | 2017-07-24 23:52:15 -0400 |
| commit | 404c468a4c51b07c27e92ccd952f647db11a6369 (patch) | |
| tree | 35372d1fe61c608eadb45e0bef52e0d01e342c7e /src/actions.rs | |
| parent | 237a0be7f0ca4d7dceca1c9e43391381593064fb (diff) | |
| download | podcast-404c468a4c51b07c27e92ccd952f647db11a6369.tar.bz2 | |
Basic subscriptions are working now
Diffstat (limited to 'src/actions.rs')
| -rw-r--r-- | src/actions.rs | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/src/actions.rs b/src/actions.rs index 120d58b..8f54359 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,10 +1,11 @@ use regex::Regex; use reqwest; +use rayon::prelude::*; use rss::Channel; -use std::fs::{DirBuilder, File}; +use std::collections::HashSet; +use std::fs::{self, DirBuilder, File}; use std::io::{self, BufReader, Read, Write}; use std::process::Command; -use rayon::prelude::*; use structs::*; use utils::*; @@ -26,7 +27,7 @@ pub fn list_episodes(state: &State, search: &str) { } } -pub fn download_rss(url: &str) { +pub fn download_rss(url: &str, config: &Config) { println!("Downloading RSS feed..."); let mut path = get_podcast_dir(); path.push(".rss"); @@ -40,23 +41,53 @@ pub fn download_rss(url: &str) { 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); + } + }); + } } -pub fn update_rss(state: &State) { - println!("Updating RSS feeds..."); - state.subscriptions().par_iter().for_each(|ref sub| { +pub fn update_rss(state: &mut State) { + println!("Checking for new episodes..."); + state.subs.par_iter_mut().for_each(|mut sub| { let mut path = get_podcast_dir(); - path.push(".rss"); + path.push(sub.title()); DirBuilder::new().recursive(true).create(&path).unwrap(); + + let mut titles = HashSet::new(); + for entry in fs::read_dir(&path).unwrap() { + let entry = entry.unwrap(); + titles.insert(trim_extension(&entry.file_name().into_string().unwrap())); + } + let mut resp = reqwest::get(&sub.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()); + let podcast = Podcast::from(Channel::read_from(BufReader::new(&content[..])).unwrap()); + path = get_podcast_dir(); + path.push(".rss"); + + let mut filename = String::from(podcast.title()); filename.push_str(".xml"); - path.push(filename); + path.push(&filename); let mut file = File::create(&path).unwrap(); file.write_all(&content).unwrap(); + + if podcast.episodes().len() > sub.num_episodes { + &podcast.episodes()[..podcast.episodes().len() - sub.num_episodes] + .par_iter() + .for_each(|ref ep| if let Err(err) = ep.download(podcast.title()) { + eprintln!("Error downloading {}: {}", podcast.title(), err); + }); + } + sub.num_episodes = podcast.episodes().len(); }); } |
