diff options
| author | Nathan | 2017-07-23 12:01:23 -0400 | 
|---|---|---|
| committer | Nathan | 2017-07-23 12:01:23 -0400 | 
| commit | d1c2572a0cb2882b2661dafac0631e5888144dce (patch) | |
| tree | 8604681935932062cd97b8b1672d694dafc80cc1 /src/structs.rs | |
| parent | efb6ef328ca159bf4d3b5f1b02d13e1c0aef0764 (diff) | |
| download | podcast-d1c2572a0cb2882b2661dafac0631e5888144dce.tar.bz2 | |
Improve RSS downloading
Diffstat (limited to 'src/structs.rs')
| -rw-r--r-- | src/structs.rs | 32 | 
1 files changed, 20 insertions, 12 deletions
| diff --git a/src/structs.rs b/src/structs.rs index e0088be..07cb901 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -5,14 +5,24 @@ use reqwest;  use rss::{self, Channel, Item};  use serde_json;  use std::collections::BTreeSet; -use std::fs::{DirBuilder, File}; +use std::fs::{self, DirBuilder, File};  use std::io::{self, Read, Write};  use utils::*;  #[derive(Serialize, Deserialize, Clone)]  pub struct Subscription { -    pub name: String, -    pub url: String, +    title: String, +    url: String, +} + +impl Subscription { +    pub fn title(&self) -> String { +        self.title.clone() +    } + +    pub fn url(&self) -> String { +        self.url.clone() +    }  }  #[derive(Serialize, Deserialize, Clone)] @@ -50,20 +60,19 @@ impl State {      pub fn subscribe(&mut self, url: &str) {          let mut set = BTreeSet::new();          for sub in self.subscriptions() { -            set.insert(sub.url); +            set.insert(sub.title());          } -        if !set.contains(url) { -            let channel = Channel::from_url(url).unwrap(); +        let channel = Channel::from_url(url).unwrap(); +        if !set.contains(channel.title()) {              self.subs.push(Subscription { -                name: String::from(channel.title()), +                title: String::from(channel.title()),                  url: String::from(url),              });          }          if let Err(err) = self.save() {              eprintln!("{}", err);          } -        // TODO only download new rss, don't refresh all -        update_rss(&self.clone()); +        download_rss(url);      }      pub fn subscriptions(&self) -> Vec<Subscription> { @@ -71,14 +80,13 @@ impl State {      }      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"); +        path.push(".subscriptions.tmp");          let serialized = serde_json::to_string(self)?;          let mut file = File::create(&path)?;          file.write_all(serialized.as_bytes())?; +        fs::rename(&path, get_sub_file())?;          Ok(())      }  } | 
