diff options
| author | Nathan Jaremko | 2017-07-26 16:47:13 -0400 |
|---|---|---|
| committer | Nathan Jaremko | 2017-07-26 16:47:13 -0400 |
| commit | 4ff4a4e802595f2fac1175c6cb62bb49c6ffda64 (patch) | |
| tree | bd5f826e1f01c99c474c6e48ac667780e5e42f3d /src | |
| parent | cd7b8cb559199c609331b5851076f5a0ed9e1d54 (diff) | |
| download | podcast-4ff4a4e802595f2fac1175c6cb62bb49c6ffda64.tar.bz2 | |
Transfer all work from chromebook
Diffstat (limited to 'src')
| -rw-r--r-- | src/actions.rs | 26 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/structs.rs | 19 | ||||
| -rw-r--r-- | src/utils.rs | 18 |
4 files changed, 40 insertions, 25 deletions
diff --git a/src/actions.rs b/src/actions.rs index 5c67c40..553ac37 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -9,20 +9,22 @@ use std::process::Command; use structs::*; use utils::*; -pub fn list_episodes(state: &State, search: &str) { +pub fn list_episodes(search: &str) { let re = Regex::new(search).unwrap(); - 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() { - println!("({}) {}", episodes.len() - index, episode.title().unwrap()); - } - } - Err(err) => println!("{}", err), + let mut path = get_podcast_dir(); + path.push(".rss"); + DirBuilder::new().recursive(true).create(&path).unwrap(); + for entry in fs::read_dir(&path).unwrap() { + let entry = entry.unwrap(); + if re.is_match(&entry.file_name().into_string().unwrap()) { + let file = File::open(&entry.path()).unwrap(); + let channel = Channel::read_from(BufReader::new(file)).unwrap(); + let podcast = Podcast::from(channel); + let episodes = podcast.episodes(); + for (num, ep) in episodes.iter().enumerate() { + println!("({}) {}", episodes.len()-num, ep.title().unwrap()); } + return; } } } diff --git a/src/main.rs b/src/main.rs index 338c7b2..64f03ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,7 +103,7 @@ fn main() { Some("list") => { let list_matches = matches.subcommand_matches("list").unwrap(); match list_matches.value_of("PODCAST") { - Some(regex) => list_episodes(&state, regex), + Some(regex) => list_episodes(regex), None => list_subscriptions(&state), } } diff --git a/src/structs.rs b/src/structs.rs index 45191ea..2ea385e 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -61,14 +61,10 @@ pub struct State { impl State { pub fn new() -> Result<State, String> { - let mut path = get_podcast_dir(); - if let Err(err) = DirBuilder::new().recursive(true).create(&path) { - return Err(format!( - "Couldn't create directory: {}\nReason: {}", - path.to_str().unwrap(), - err - )) + if let Err(err) = create_directories() { + return Err(format!("{}", err)) } + let mut path = get_podcast_dir(); path.push(".subscriptions"); if path.exists() { let mut s = String::new(); @@ -76,7 +72,7 @@ impl State { match serde_json::from_str(&s) { Ok(val) => { let mut state: State = val; - // Check if a day has passed (86400 seconds) + // Check if a day has passed (86400 seconds) if state .last_run_time .signed_duration_since(Utc::now()) @@ -86,8 +82,10 @@ impl State { } state.last_run_time = Utc::now(); Ok(state) - }, - Err(_) => Err(format!("Failed to parse .subscriptions ... I probably changed the schema ... sorry")) + } + Err(_) => Err(format!( + "Failed to parse .subscriptions ... I probably changed the schema ... sorry" + )), } } else { Ok(State { @@ -122,7 +120,6 @@ impl State { pub fn save(&self) -> Result<(), io::Error> { let mut path = get_podcast_dir(); - DirBuilder::new().recursive(true).create(&path).unwrap(); path.push(".subscriptions.tmp"); let serialized = serde_json::to_string(self)?; let mut file = File::create(&path)?; diff --git a/src/utils.rs b/src/utils.rs index 7d355b3..5444cb1 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,6 +3,8 @@ use std::env; use std::fs; use std::num::ParseIntError; use std::path::PathBuf; +use std::fs::DirBuilder; +use std::io; pub fn trim_extension(filename: &str) -> String { @@ -26,6 +28,20 @@ pub fn find_extension(input: &str) -> Option<&str> { } } +pub fn create_directories() -> io::Result<()> { + let mut path = get_podcast_dir(); + path.push(".rss"); + if let Err(err) = DirBuilder::new().recursive(true).create(&path) { + eprintln!( + "Couldn't create directory: {}\nReason: {}", + path.to_str().unwrap(), + err + ); + return Err(err) + } + Ok(()) +} + pub fn already_downloaded(dir: &str) -> HashSet<String> { let mut result = HashSet::new(); @@ -45,7 +61,7 @@ pub fn already_downloaded(dir: &str) -> HashSet<String> { println!( "OsString: {:?} couldn't be converted to String", entry.file_name() - ); + ); } } } |
