From ffda7c6fc8b84c69b762fba17ec43d1b09e1fcdb Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 19 Sep 2017 08:15:54 +0300 Subject: [PATCH] Trying insertable stucts that might own their data. --- .../2017-09-15-001128_init_schema/up.sql | 2 +- src/index_feed.rs | 49 ++++++++++++++ src/models.rs | 32 +++++---- src/parse_feeds.rs | 67 ++++++++++++------- src/schema.rs | 2 +- 5 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 src/index_feed.rs diff --git a/migrations/2017-09-15-001128_init_schema/up.sql b/migrations/2017-09-15-001128_init_schema/up.sql index abe3a77..fd07042 100644 --- a/migrations/2017-09-15-001128_init_schema/up.sql +++ b/migrations/2017-09-15-001128_init_schema/up.sql @@ -1,6 +1,6 @@ CREATE TABLE `source` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - `url` TEXT NOT NULL UNIQUE, + `uri` TEXT NOT NULL UNIQUE, `last_modified` TEXT, `http_etag` TEXT ); diff --git a/src/index_feed.rs b/src/index_feed.rs new file mode 100644 index 0000000..e44d904 --- /dev/null +++ b/src/index_feed.rs @@ -0,0 +1,49 @@ +use diesel::prelude::*; +use diesel; +use rss::Channel; +use schema; +use dbqueries; +use errors::*; +use models::NewPodcast; + +pub fn foo() { + let inpt = vec![ + "https://feeds.feedburner.com/InterceptedWithJeremyScahill", + "http://feeds.feedburner.com/linuxunplugged", + "http://feeds.propublica.org/propublica/main", + ]; + + let db = ::establish_connection(); + for feed in inpt.iter() { + match insert_feed(&db, feed) { + Ok(_) => {} + Err(foo) => { + debug!("Error: {}", foo); + debug!("Skipping..."); + continue; + } + } + } + update_podcasts(&db); +} + +fn insert_feed(connection: &SqliteConnection, url: &str) -> Result<()> { + let foo = NewPodcast::from_url(url)?; + + diesel::insert(&foo).into(schema::podcast::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 28d5c27..238d33e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -47,6 +47,14 @@ pub struct Source { http_etag: Option, } +#[derive(Insertable)] +#[table_name = "source"] +#[derive(Debug, Clone)] +pub struct NewSource<'a> { + pub uri: &'a str, + pub last_modified: Option<&'a str>, + pub http_etag: Option<&'a str>, +} #[derive(Insertable)] #[table_name = "episode"] @@ -66,21 +74,21 @@ pub struct NewEpisode<'a> { #[derive(Insertable)] #[table_name = "podcast"] #[derive(Debug, Clone)] -pub struct NewPodcast<'a> { - pub title: &'a str, - pub uri: &'a str, - pub link: Option<&'a str>, - pub description: Option<&'a str>, - pub image_uri: Option<&'a str>, +pub struct NewPodcast { + pub title: String, + pub uri: String, + pub link: Option, + pub description: Option, + pub image_uri: Option, } -impl<'a> NewPodcast<'a> { - // fn from_url(uri: &'a str) -> Result { - // let chan = Channel::from_url(uri)?; - // let foo = ::parse_feeds::parse_podcast(&chan, uri)?; - // Ok(foo) - // } +impl<'a> NewPodcast { + pub fn from_url(uri: &'a str) -> Result { + let chan = Channel::from_url(uri)?; + let foo = ::parse_feeds::parse_podcast(&chan, uri)?; + Ok(foo) + } // Ignore this atm // pub fn from_url(uri: &str) -> Result<()> { diff --git a/src/parse_feeds.rs b/src/parse_feeds.rs index 6e16e23..0c8246f 100644 --- a/src/parse_feeds.rs +++ b/src/parse_feeds.rs @@ -2,20 +2,23 @@ use rss::{Channel, Item}; use models; use errors::*; -pub fn parse_podcast<'a>(pd_chan: &'a Channel, uri: &'a str) -> Result> { - let title = pd_chan.title(); +pub fn parse_podcast(chan: &Channel, uri: &str) -> Result { - let link = Some(pd_chan.link()); - let description = Some(pd_chan.description()); + let title = chan.title().to_owned(); - let image_uri = match pd_chan.image() { - Some(foo) => Some(foo.url()), - None => None, - }; + let link = Some(chan.link().to_owned()); + let description = Some(chan.description().to_owned()); + + // let image_uri = match chan.image() { + // Some(foo) => Some(foo.url().to_owned()), + // None => None, + // }; + + let image_uri = chan.image().map(|foo| foo.url().to_owned()); let foo = models::NewPodcast { + uri: uri.to_owned(), title, - uri, link, description, image_uri, @@ -78,13 +81,16 @@ mod tests { let descr = "The people behind The Intercept’s fearless reporting and incisive commentary—Jeremy Scahill, Glenn Greenwald, Betsy Reed and others—discuss the crucial issues of our time: national security, civil liberties, foreign policy, and criminal justice. Plus interviews with artists, thinkers, and newsmakers who challenge our preconceptions about the world we live in."; let pd = parse_podcast(&channel, uri).unwrap(); - assert_eq!(pd.title, "Intercepted with Jeremy Scahill"); - // assert_eq!( - // pd.uri, - // "https://feeds.feedburner.com/InterceptedWithJeremyScahill" - // ); - assert_eq!(pd.link, Some("https://theintercept.com/podcasts")); - assert_eq!(pd.description, Some(descr)); + assert_eq!(pd.title, "Intercepted with Jeremy Scahill".to_string()); + assert_eq!( + pd.uri, + "https://feeds.feedburner.com/InterceptedWithJeremyScahill".to_string() + ); + assert_eq!( + pd.link, + Some("https://theintercept.com/podcasts".to_string()) + ); + assert_eq!(pd.description, Some(descr.to_string())); assert_eq!(pd.image_uri, None); @@ -97,12 +103,17 @@ mod tests { let descr = "An open show powered by community LINUX Unplugged takes the best attributes of open collaboration and focuses them into a weekly lifestyle show about Linux."; let pd = parse_podcast(&channel, uri).unwrap(); - assert_eq!(pd.title, "LINUX Unplugged Podcast"); - assert_eq!(pd.link, Some("http://www.jupiterbroadcasting.com/")); - assert_eq!(pd.description, Some(descr)); + assert_eq!(pd.title, "LINUX Unplugged Podcast".to_string()); + assert_eq!( + pd.link, + Some("http://www.jupiterbroadcasting.com/".to_string()) + ); + assert_eq!(pd.description, Some(descr.to_string())); assert_eq!( pd.image_uri, - Some("http://michaeltunnell.com/images/linux-unplugged.jpg") + Some( + "http://michaeltunnell.com/images/linux-unplugged.jpg".to_string(), + ) ); @@ -115,12 +126,20 @@ mod tests { let descr = "Latest Articles and Investigations from ProPublica, an independent, non-profit newsroom that produces investigative journalism in the public interest."; let pd = parse_podcast(&channel, uri).unwrap(); - assert_eq!(pd.title, "Articles and Investigations - ProPublica"); - assert_eq!(pd.link, Some("https://www.propublica.org/feeds/54Ghome")); - assert_eq!(pd.description, Some(descr)); + assert_eq!( + pd.title, + "Articles and Investigations - ProPublica".to_string() + ); + assert_eq!( + pd.link, + Some("https://www.propublica.org/feeds/54Ghome".to_string()) + ); + assert_eq!(pd.description, Some(descr.to_string())); assert_eq!( pd.image_uri, - Some("https://assets.propublica.org/propublica-rss-logo.png") + Some( + "https://assets.propublica.org/propublica-rss-logo.png".to_string(), + ) ); } diff --git a/src/schema.rs b/src/schema.rs index a91027e..78d401e 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -28,7 +28,7 @@ table! { table! { source (id) { id -> Integer, - url -> Text, + uri -> Text, last_modified -> Nullable, http_etag -> Nullable, }