From 159ac4cd5d42ac6468b55707ab4487c584a22bad Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 19 Sep 2017 14:16:40 +0300 Subject: [PATCH] Finally getting somewhere. --- .../2017-09-15-001128_init_schema/up.sql | 4 +- src/dbqueries.rs | 1 - src/index_feed.rs | 25 ++++++++++-- src/models.rs | 40 +++++++++++-------- 4 files changed, 47 insertions(+), 23 deletions(-) diff --git a/migrations/2017-09-15-001128_init_schema/up.sql b/migrations/2017-09-15-001128_init_schema/up.sql index 53c656a..ca5928a 100644 --- a/migrations/2017-09-15-001128_init_schema/up.sql +++ b/migrations/2017-09-15-001128_init_schema/up.sql @@ -12,8 +12,8 @@ CREATE TABLE `episode` ( `local_uri` TEXT, `description` TEXT, `published_date` TEXT NOT NULL, - `epoch` INTEGER NOT NULL DEFAULT 0, - `length` INTEGER DEFAULT 0, + `epoch` INTEGER NOT NULL, + `length` INTEGER, `guid` TEXT, `podcast_id` INTEGER NOT NULL ); diff --git a/src/dbqueries.rs b/src/dbqueries.rs index e988609..d54a9b8 100644 --- a/src/dbqueries.rs +++ b/src/dbqueries.rs @@ -12,7 +12,6 @@ pub fn get_pd_episodes(con: &SqliteConnection, parent: &Podcast) -> QueryResult< let eps = Episode::belonging_to(parent).load::(con); eps } - pub fn get_sources(con: &SqliteConnection) -> QueryResult>{ let s = source.load::(con); diff --git a/src/index_feed.rs b/src/index_feed.rs index 2668cf9..db1de71 100644 --- a/src/index_feed.rs +++ b/src/index_feed.rs @@ -3,7 +3,7 @@ use diesel; use schema; use dbqueries; use errors::*; -use models::{NewPodcast, NewSource}; +use models::{NewPodcast, NewSource, Source}; pub fn foo() { let inpt = vec![ @@ -24,8 +24,7 @@ pub fn foo() { } } - let f = dbqueries::get_sources(&db); - info!("{:?}", f); + index_loop(db); } fn insert_source(connection: &SqliteConnection, url: &str) -> Result<()> { @@ -37,3 +36,23 @@ fn insert_source(connection: &SqliteConnection, url: &str) -> Result<()> { Ok(()) } + + +pub fn index_loop(db: SqliteConnection) -> Result<()> { + // let db = ::establish_connection(); + use parse_feeds; + + let f = dbqueries::get_sources(&db); + + for feed in f.unwrap().iter_mut() { + info!("{:?}", feed.id()); + // This method will defently get split and nuked + // but for now its poc + let chan = feed.get_podcast_chan(&db)?; + let pd = parse_feeds::parse_podcast(&chan, feed.id())?; + info!("{:#?}", pd); + // info!("{:?}", chan); + + } + Ok(()) +} \ No newline at end of file diff --git a/src/models.rs b/src/models.rs index 0d4c0d1..d2524a0 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,5 +1,8 @@ use reqwest; use rss::Channel; +use diesel::SaveChangesDsl; +use SqliteConnection; +use reqwest::header::{ETag, LastModified}; use schema::{episode, podcast, source}; use errors::*; @@ -63,10 +66,10 @@ impl<'a> Source { self.http_etag } - // This is a mess - pub fn get_podcast(&mut self) -> Result { + /// Fetch the xml feed from the source url, update the etag headers, + /// and parse the feed into an rss:Channel and return it. + pub fn get_podcast_chan(&mut self, con: &SqliteConnection) -> Result { use std::io::Read; - use reqwest::header::*; use std::str::FromStr; let mut req = reqwest::get(&self.uri)?; @@ -78,28 +81,31 @@ impl<'a> Source { let headers = req.headers(); debug!("{:#?}", headers); - // for h in headers.iter() { - // info!("{}: {}", h.name(), h.value_string()); - // } - // let etag = headers.get_raw("ETag").unwrap(); let etag = headers.get::(); let lst_mod = headers.get::(); - info!("Etag: {:?}", etag); - info!("Last mod: {:?}", lst_mod); + self.update(con, etag, lst_mod)?; + + let chan = Channel::from_str(&buf)?; + // let foo = ::parse_feeds::parse_podcast(&chan, self.id())?; + + Ok(chan) + } + + pub fn update( + &mut self, + con: &SqliteConnection, + etag: Option<&ETag>, + lmod: Option<&LastModified>, + ) -> Result<()> { - // This is useless atm since theres no db passed to save the change - // but I needed to have it somewhere implemented for later. self.http_etag = etag.map(|x| x.tag().to_string().to_owned()); - self.last_modified = lst_mod.map(|x| format!("{}", x)); + self.last_modified = lmod.map(|x| format!("{}", x)); info!("Self etag: {:?}", self.http_etag); info!("Self last_mod: {:?}", self.last_modified); - // Maybe it would be better to just return buf - let chan = Channel::from_str(&buf)?; - let foo = ::parse_feeds::parse_podcast(&chan, self.id())?; - - Ok(foo) + self.save_changes::(con)?; + Ok(()) } }