diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/actions.rs | 1 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/structs.rs | 30 | 
3 files changed, 28 insertions, 5 deletions
| diff --git a/src/actions.rs b/src/actions.rs index 0d32d46..fb0d9e6 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -27,6 +27,7 @@ pub fn list_episodes(state: &State, search: &str) {  }  pub fn update_rss(state: &State) { +    println!("Updating RSS feeds...");      state.subscriptions().par_iter().for_each(|ref sub| {          let mut path = get_podcast_dir();          path.push(".rss"); diff --git a/src/main.rs b/src/main.rs index 92ae630..52ab321 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +extern crate chrono;  extern crate clap;  extern crate rayon;  extern crate regex; @@ -122,4 +123,5 @@ fn main() {          Some("update") => update_rss(&state),          _ => (),      } +    state.save();  } diff --git a/src/structs.rs b/src/structs.rs index a639bd3..0139394 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,4 +1,5 @@  use actions::*; +use chrono::prelude::*;  use rayon::prelude::*;  use reqwest;  use rss::{self, Channel, Item}; @@ -15,7 +16,10 @@ pub struct Subscription {  }  #[derive(Serialize, Deserialize, Clone)] -pub struct State(Vec<Subscription>); +pub struct State { +    last_run_time: DateTime<Utc>, +    subs: Vec<Subscription>, +}  impl State {      pub fn new() -> State { @@ -24,9 +28,22 @@ impl State {          if path.exists() {              let mut s = String::new();              File::open(&path).unwrap().read_to_string(&mut s).unwrap(); -            serde_json::from_str(&s).unwrap() +            let mut state: State = serde_json::from_str(&s).unwrap(); +            // Check if a day has passed (86400 seconds) +            if state +                .last_run_time +                .signed_duration_since(Utc::now()) +                .num_seconds() < -86400 +            { +                update_rss(&state.clone()); +            } +            state.last_run_time = Utc::now(); +            state          } else { -            State(Vec::new()) +            State { +                last_run_time: Utc::now(), +                subs: Vec::new(), +            }          }      } @@ -37,7 +54,7 @@ impl State {          }          if !set.contains(url) {              let channel = Channel::from_url(url).unwrap(); -            self.0.push(Subscription { +            self.subs.push(Subscription {                  name: String::from(channel.title()),                  url: String::from(url),              }); @@ -45,14 +62,17 @@ impl State {          if let Err(err) = self.save() {              println!("{}", err);          } +        // TODO only download new rss, don't refresh all          update_rss(&self.clone());      }      pub fn subscriptions(&self) -> Vec<Subscription> { -        self.0.clone() +        self.subs.clone()      }      pub fn save(&self) -> Result<(), io::Error> { +        // TODO write to a temp file and rename instead of overwriting +          let mut path = get_podcast_dir();          DirBuilder::new().recursive(true).create(&path).unwrap();          path.push(".subscriptions"); | 
