diff --git a/hammond-data/src/feed.rs b/hammond-data/src/feed.rs index 515130e..3c35770 100644 --- a/hammond-data/src/feed.rs +++ b/hammond-data/src/feed.rs @@ -8,7 +8,7 @@ use rss; use dbqueries; use errors::*; -use models::{IndexState, Update}; +use models::{Index, IndexState, Update}; use models::{NewEpisode, NewPodcast, Podcast}; use pipeline::*; diff --git a/hammond-data/src/models/episode.rs b/hammond-data/src/models/episode.rs index 10634c7..5bd7bb4 100644 --- a/hammond-data/src/models/episode.rs +++ b/hammond-data/src/models/episode.rs @@ -83,7 +83,7 @@ impl Episode { self.description = value.map(|x| x.to_string()); } - /// Get the value of the `description`. + /// Get the Episode's `guid`. pub fn guid(&self) -> Option<&str> { self.guid.as_ref().map(|s| s.as_str()) } @@ -475,6 +475,11 @@ impl EpisodeMinimal { self.uri.as_ref().map(|s| s.as_str()) } + /// Get the Episode's `guid`. + pub fn guid(&self) -> Option<&str> { + self.guid.as_ref().map(|s| s.as_str()) + } + /// Get the `epoch` value. /// /// Retrieved from the rss Item publish date. diff --git a/hammond-data/src/models/mod.rs b/hammond-data/src/models/mod.rs index b637ae0..736b734 100644 --- a/hammond-data/src/models/mod.rs +++ b/hammond-data/src/models/mod.rs @@ -35,3 +35,7 @@ pub trait Insert { pub trait Update { fn update(&self, i32) -> Result<()>; } + +pub trait Index: Insert + Update { + fn index(&self) -> Result<()>; +} diff --git a/hammond-data/src/models/new_episode.rs b/hammond-data/src/models/new_episode.rs index 33f9561..ca8e1a4 100644 --- a/hammond-data/src/models/new_episode.rs +++ b/hammond-data/src/models/new_episode.rs @@ -10,7 +10,7 @@ use rss; use database::connection; use dbqueries; use errors::*; -use models::{Episode, Insert, Update}; +use models::{Episode, Index, Insert, Update}; use parser; use utils::{replace_extra_spaces, url_cleaner}; @@ -75,6 +75,30 @@ impl Update for NewEpisode { } } +impl Index for NewEpisode { + fn index(&self) -> Result<()> { + let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?; + + match exists { + false => self.insert(), + true => { + let old = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?; + + // This is messy + if (self.title() != old.title()) || (self.uri() != old.uri()) + || (self.duration() != old.duration()) + || (self.epoch() != old.epoch()) + || (self.guid() != old.guid()) + { + self.update(old.rowid()) + } else { + Ok(()) + } + } + } + } +} + impl NewEpisode { /// Parses an `rss::Item` into a `NewEpisode` Struct. pub(crate) fn new(item: &rss::Item, podcast_id: i32) -> Result { @@ -86,29 +110,6 @@ impl NewEpisode { self.index()?; dbqueries::get_episode_from_pk(&self.title, self.podcast_id) } - - pub(crate) fn index(&self) -> Result<()> { - let ep = dbqueries::get_episode_from_pk(&self.title, self.podcast_id); - - match ep { - Ok(foo) => { - if foo.podcast_id() != self.podcast_id { - error!("NEP pid: {}\nEP pid: {}", self.podcast_id, foo.podcast_id()); - }; - - if foo.title() != self.title.as_str() || foo.epoch() != self.epoch - || foo.uri() != self.uri.as_ref().map(|s| s.as_str()) - || foo.duration() != self.duration - { - self.update(foo.rowid())?; - } - } - Err(_) => { - self.insert()?; - } - } - Ok(()) - } } #[allow(dead_code)] @@ -134,6 +135,10 @@ impl NewEpisode { self.epoch } + pub(crate) fn duration(&self) -> Option { + self.duration + } + pub(crate) fn length(&self) -> Option { self.length } diff --git a/hammond-data/src/models/new_podcast.rs b/hammond-data/src/models/new_podcast.rs index 7ba7068..7168aa3 100644 --- a/hammond-data/src/models/new_podcast.rs +++ b/hammond-data/src/models/new_podcast.rs @@ -4,7 +4,7 @@ use diesel::prelude::*; use ammonia; use rss; -use models::{Insert, Update}; +use models::{Index, Insert, Update}; use models::Podcast; use schema::podcast; @@ -57,6 +57,28 @@ impl Update for NewPodcast { } } +impl Index for NewPodcast { + fn index(&self) -> Result<()> { + let exists = dbqueries::podcast_exists(self.source_id)?; + + match exists { + false => self.insert(), + true => { + let old = dbqueries::get_podcast_from_source_id(self.source_id)?; + + // This is messy + if (self.link() != old.link()) || (self.title() != old.title()) + || (self.image_uri() != old.image_uri()) + { + self.update(old.id()) + } else { + Ok(()) + } + } + } + } +} + impl NewPodcast { /// Parses a `rss::Channel` into a `NewPodcast` Struct. pub(crate) fn new(chan: &rss::Channel, source_id: i32) -> NewPodcast { @@ -92,26 +114,7 @@ impl NewPodcast { // Look out for when tryinto lands into stable. pub(crate) fn into_podcast(self) -> Result { self.index()?; - Ok(dbqueries::get_podcast_from_source_id(self.source_id)?) - } - - pub(crate) fn index(&self) -> Result<()> { - let pd = dbqueries::get_podcast_from_source_id(self.source_id); - - match pd { - Ok(foo) => { - if (foo.link() != self.link) || (foo.title() != self.title) - || (foo.image_uri() != self.image_uri.as_ref().map(|x| x.as_str())) - { - info!("NewEpisode: {:?}\n OldEpisode: {:?}", self, foo); - self.update(foo.id())?; - } - } - Err(_) => { - self.insert()?; - } - } - Ok(()) + dbqueries::get_podcast_from_source_id(self.source_id).map_err(From::from) } }