diff --git a/migrations/2017-09-15-001128_init_schema/up.sql b/migrations/2017-09-15-001128_init_schema/up.sql index 77131a6..503404f 100644 --- a/migrations/2017-09-15-001128_init_schema/up.sql +++ b/migrations/2017-09-15-001128_init_schema/up.sql @@ -20,8 +20,8 @@ CREATE TABLE `episode` ( CREATE TABLE `podcast` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `title` TEXT NOT NULL, - `uri` TEXT UNIQUE NOT NULL, + `title` TEXT NOT NULL UNIQUE, + `uri` TEXT NOT NULL UNIQUE, `link` TEXT NOT NULL, `description` TEXT NOT NULL, `image_uri` TEXT, diff --git a/src/cli.rs b/src/cli.rs index 24aa948..3527ddf 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,7 +1,7 @@ use structopt::StructOpt; use loggerv; use errors::*; -use models::NewPodcast; +// use models::NewPodcast; #[derive(StructOpt, Debug)] #[structopt(name = "example", about = "An example of StructOpt usage.")] @@ -20,8 +20,7 @@ pub fn run() -> Result<()> { info!("{:?}", foo); ::init()?; - // ::parse_feeds::foo(); - // ::index_feed::foo(); + ::index_feed::foo(); Ok(()) } \ No newline at end of file diff --git a/src/dbqueries.rs b/src/dbqueries.rs index b998fde..e988609 100644 --- a/src/dbqueries.rs +++ b/src/dbqueries.rs @@ -1,10 +1,9 @@ use diesel::prelude::*; -use schema::podcast::dsl::*; -// use schema::episode::dsl::*; -use models::{Podcast, Episode}; +use schema::source::dsl::*; +use models::{Podcast, Episode, Source}; -pub fn get_podcasts(con: &SqliteConnection) -> QueryResult> { - let pds = podcast.load::(con); +pub fn get_podcasts(con: &SqliteConnection, parent: &Source) -> QueryResult> { + let pds = Podcast::belonging_to(parent).load::(con); // debug!("Returned Podcasts:\n{:?}", pds); pds } @@ -13,4 +12,9 @@ pub fn get_pd_episodes(con: &SqliteConnection, parent: &Podcast) -> QueryResult< let eps = Episode::belonging_to(parent).load::(con); eps } - \ No newline at end of file + + + pub fn get_sources(con: &SqliteConnection) -> QueryResult>{ + let s = source.load::(con); + s + } \ No newline at end of file diff --git a/src/index_feed.rs b/src/index_feed.rs index e44d904..2668cf9 100644 --- a/src/index_feed.rs +++ b/src/index_feed.rs @@ -1,10 +1,9 @@ use diesel::prelude::*; use diesel; -use rss::Channel; use schema; use dbqueries; use errors::*; -use models::NewPodcast; +use models::{NewPodcast, NewSource}; pub fn foo() { let inpt = vec![ @@ -15,7 +14,7 @@ pub fn foo() { let db = ::establish_connection(); for feed in inpt.iter() { - match insert_feed(&db, feed) { + match insert_source(&db, feed) { Ok(_) => {} Err(foo) => { debug!("Error: {}", foo); @@ -24,26 +23,17 @@ pub fn foo() { } } } - update_podcasts(&db); + + let f = dbqueries::get_sources(&db); + info!("{:?}", f); } -fn insert_feed(connection: &SqliteConnection, url: &str) -> Result<()> { - let foo = NewPodcast::from_url(url)?; +fn insert_source(connection: &SqliteConnection, url: &str) -> Result<()> { + let foo = NewSource::new_with_uri(url); - diesel::insert(&foo).into(schema::podcast::table).execute( + diesel::insert(&foo).into(schema::source::table).execute( connection, )?; Ok(()) } - - -fn update_podcasts(connection: &SqliteConnection) { - let pds = dbqueries::get_podcasts(connection).unwrap(); - info!("{:?}", pds); - - // for pd in pds { - // println!("{:?}" pd.uri); - // } - -} \ No newline at end of file diff --git a/src/models.rs b/src/models.rs index 7c08a38..44f19bd 100644 --- a/src/models.rs +++ b/src/models.rs @@ -37,7 +37,7 @@ pub struct Podcast { source_id: i32, } -#[derive(Queryable, Identifiable)] +#[derive(Queryable, Identifiable, AsChangeset)] #[table_name = "source"] #[derive(Debug, Clone)] pub struct Source { @@ -47,6 +47,45 @@ pub struct Source { http_etag: Option, } +impl<'a> Source { + // This is a mess + pub fn get_podcast(&mut self) -> Result { + use std::io::Read; + use reqwest::header::*; + use std::str::FromStr; + + let mut req = reqwest::get(&self.uri)?; + + let mut buf = String::new(); + req.read_to_string(&mut buf)?; + // info!("{}", buf); + + 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.http_etag = etag.map(|x| x.tag().to_string().to_owned()); + self.last_modified = lst_mod.map(|x| format!("{}", x)); + info!("Self etag: {:?}", self.http_etag); + info!("Self last_mod: {:?}", self.last_modified); + + let chan = Channel::from_str(&buf)?; + let foo = ::parse_feeds::parse_podcast(&chan, &self.uri)?; + + Ok(foo) + } +} + +// TODO: Remove pub fields and add setters. #[derive(Insertable)] #[table_name = "source"] #[derive(Debug, Clone)] @@ -56,6 +95,16 @@ pub struct NewSource<'a> { pub http_etag: Option<&'a str>, } +impl<'a> NewSource<'a> { + pub fn new_with_uri(uri: &'a str) -> NewSource { + NewSource { + uri, + last_modified: None, + http_etag: None, + } + } +} + #[derive(Insertable)] #[table_name = "episode"] #[derive(Debug, Clone)] @@ -89,39 +138,4 @@ impl<'a> NewPodcast { let foo = ::parse_feeds::parse_podcast(&chan, uri)?; Ok(foo) } - - // Ignore this atm - // pub fn from_url(uri: &str) -> Result<()> { - - // use std::io::Read; - // use reqwest::header::*; - // use std::str::FromStr; - // use parse_feeds; - - // let mut req = reqwest::get(uri)?; - - // let mut buf = String::new(); - // req.read_to_string(&mut buf)?; - // info!("{}", buf); - - // let headers = req.headers(); - // info!("{:#?}", headers); - - // // for h in headers.iter() { - // // info!("{}: {}", h.name(), h.value_string()); - // // } - - // // Sometimes dsnt work - // // let etag = headers.get::(); - // let etag = headers.get_raw("ETag").unwrap(); - // let lst_mod = headers.get::().unwrap(); - // info!("Etag: {:?}", etag); - // info!("Last mod: {}", lst_mod); - - // let pd_chan = Channel::from_str(buf.as_str())?; - // // let bar = parse_feeds::parse_podcast(&foo)?; - // // let baz = bar.clone(); - - // Ok(()) - // } } \ No newline at end of file diff --git a/src/parse_feeds.rs b/src/parse_feeds.rs index 74a60a3..142e80c 100644 --- a/src/parse_feeds.rs +++ b/src/parse_feeds.rs @@ -13,7 +13,7 @@ pub fn parse_podcast(chan: &Channel, uri: &str) -> Result { // Some(foo) => Some(foo.url().to_owned()), // None => None, // }; - + // Same as the above match expression. let image_uri = chan.image().map(|foo| foo.url().to_owned()); let foo = models::NewPodcast { @@ -26,6 +26,7 @@ pub fn parse_podcast(chan: &Channel, uri: &str) -> Result { Ok(foo) } +// This is also an initial prototype mess. pub fn parse_episode<'a>(item: &'a Item, parent_id: i32) -> Result> { let title = item.title().unwrap();